added more gfxdevice-like prototypes
[swftools.git] / pdf2swf / SWFOutputDev.cc
index 182fc28..43f106f 100644 (file)
@@ -29,7 +29,7 @@
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
-#ifdef HAVE_FONTCONFIG_H
+#ifdef HAVE_FONTCONFIG
 #include <fontconfig.h>
 #endif
 //xpdf header files
@@ -65,7 +65,8 @@
 #include "swfoutput.h"
 #include "../lib/log.h"
 #include "../lib/gfxdevice.h"
-#include "gfxtools.h"
+#include "../lib/gfxtools.h"
+#include "../lib/gfxfont.h"
 
 #include <math.h>
 
@@ -90,7 +91,7 @@ static int pagepos = 0;
 /* config */
 static double caplinewidth = 3.0;
 static int zoom = 72; /* xpdf: 86 */
-static int forceType0Fonts = 0;
+static int forceType0Fonts = 1;
 
 static void printInfoString(Dict *infoDict, char *key, char *fmt);
 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
@@ -124,6 +125,14 @@ class SWFOutputState {
     }
 };
 
+typedef struct _fontlist
+{
+    char*id;
+    char*filename;
+    gfxfont_t*font;
+    _fontlist*next;
+} fontlist_t;
+
 class SWFOutputDev:  public OutputDev {
   int outputstarted;
   struct swfoutput output;
@@ -227,6 +236,8 @@ public:
   void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
                                   int width, int height, GfxImageColorMap*colorMap, GBool invert,
                                   GBool inlineImg, int mask, int *maskColors);
+  int SWFOutputDev::setGfxFont(char*id, char*filename);
+
   SWFOutputState states[64];
   int statepos;
 
@@ -257,6 +268,11 @@ public:
 
   int user_movex,user_movey;
   int user_clipx1,user_clipx2,user_clipy1,user_clipy2;
+
+  gfxline_t* current_text_stroke;
+  char* current_font_id;
+  gfxfont_t* current_gfxfont;
+  fontlist_t* fontlist;
 };
 
 static char*getFontID(GfxFont*font);
@@ -303,7 +319,7 @@ class InfoOutputDev:  public OutputDev
       GfxFont*font = state->getFont();
       if(!font)
           return;
-      char*id = getFontID(font);
+      /*char*id = getFontID(font);*/
       /* FIXME*/
       num_fonts++;
   }
@@ -339,6 +355,8 @@ SWFOutputDev::SWFOutputDev()
     user_clipy1 = 0;
     user_clipx2 = 0;
     user_clipy2 = 0;
+    current_text_stroke = 0;
+    fontlist = 0;
     memset(&output, 0, sizeof(output));
 //    printf("SWFOutputDev::SWFOutputDev() \n");
 };
@@ -839,6 +857,17 @@ SWFOutputDev::~SWFOutputDev()
 {
     swfoutput_destroy(&output);
     outputstarted = 0;
+
+    fontlist_t*l = this->fontlist;
+    while(l) {
+       fontlist_t*next = l->next;
+       l->next = 0;
+       gfxfont_free(l->font);
+       free(l->id);
+       free(l->filename);
+       free(l);
+       l = next;
+    }
 };
 GBool SWFOutputDev::upsideDown() 
 {
@@ -862,6 +891,32 @@ GBool SWFOutputDev::useGradients()
 char*renderModeDesc[]= {"fill", "stroke", "fill+stroke", "invisible",
                       "clip+fill", "stroke+clip", "fill+stroke+clip", "clip"};
 
+static char tmp_printstr[4096];
+char* makeStringPrintable(char*str)
+{
+    int len = strlen(str);
+    int dots = 0;
+    if(len>=80) {
+       len = 80;
+       dots = 1;
+    }
+    int t;
+    for(t=0;t<len;t++) {
+       char c = str[t];
+       if(c<32 || c>124) {
+           c = '.';
+       }
+       tmp_printstr[t] = c;
+    }
+    if(dots) {
+       tmp_printstr[len++] = '.';
+       tmp_printstr[len++] = '.';
+       tmp_printstr[len++] = '.';
+    }
+    tmp_printstr[len] = 0;
+    return tmp_printstr;
+}
+
 void SWFOutputDev::beginString(GfxState *state, GString *s) 
 { 
     int render = state->getRender();
@@ -873,7 +928,7 @@ void SWFOutputDev::beginString(GfxState *state, GString *s)
     m21 *= state->getHorizScaling();
     swfoutput_setfontmatrix(&output, m11, -m21, m12, -m22);
     if(render != 3 && render != 0)
-       msg("<warning> Text rendering mode %d (%s) not fully supported yet (for text \"%s\")", render, renderModeDesc[render&7], s->getCString());
+       msg("<warning> Text rendering mode %d (%s) not fully supported yet (for text \"%s\")", render, renderModeDesc[render&7], makeStringPrintable(s->getCString()));
     states[statepos].textRender = render;
 }
 
@@ -947,14 +1002,14 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
        if(enc && enc[c])
           name = enc[c];
     }
     if (CIDToGIDMap) {
        msg("<debug> drawChar(%f, %f, c='%c' (%d), GID=%d, u=%d <%d>) CID=%d name=\"%s\" render=%d\n", x, y, (c&127)>=32?c:'?', c, CIDToGIDMap[c], u, uLen, font->isCIDFont(), FIXNULL(name), render);
-       swfoutput_drawchar(&output, x1, y1, name, CIDToGIDMap[c], u, &col);
+       c = CIDToGIDMap[c];
     } else {
        msg("<debug> drawChar(%f,%f,c='%c' (%d), u=%d <%d>) CID=%d name=\"%s\" render=%d\n",x,y,(c&127)>=32?c:'?',c,u, uLen, font->isCIDFont(), FIXNULL(name), render);
-       swfoutput_drawchar(&output, x1, y1, name, c, u, &col);
     }
+
+    swfoutput_drawchar(&output, x1, y1, name, c, u, &col);
 }
 
 void SWFOutputDev::endString(GfxState *state) 
@@ -964,6 +1019,8 @@ void SWFOutputDev::endString(GfxState *state)
 
 void SWFOutputDev::endTextObject(GfxState *state)
 {
+    if(states[statepos].textRender != state->getRender())
+       msg("<error> Internal error: drawChar.render!=beginString.render");
     msg("<trace> endTextObject()");
 }
 
@@ -1572,6 +1629,38 @@ void SWFOutputDev::setXRef(PDFDoc*doc, XRef *xref)
     this->xref = xref;
 }
 
+int SWFOutputDev::setGfxFont(char*id, char*filename)
+{
+    gfxfont_t*font = 0;
+    fontlist_t*last=0,*l = this->fontlist;
+
+    /* TODO: should this be part of the state? */
+    while(l) {
+       last = l;
+       if(!strcmp(l->id, id)) {
+           current_font_id = l->id;
+           current_gfxfont = l->font;
+           font = l->font;
+           return 1;
+       }
+       l = l->next;
+    }
+    if(!filename) return 0;
+    font = gfxfont_load(filename);
+    l = new fontlist_t;
+    l->font = font;
+    l->filename = strdup(filename);
+    l->id = strdup(id);
+    l->next = 0;
+    current_font_id = l->id;
+    current_gfxfont = l->font;
+    if(last) {
+       last->next = l;
+    } else {
+       this->fontlist = l;
+    }
+    return 1;
+}
 
 void SWFOutputDev::updateFont(GfxState *state) 
 {
@@ -1596,6 +1685,7 @@ void SWFOutputDev::updateFont(GfxState *state)
 
     /* second, see if swfoutput already has this font
        cached- if so, we are done */
+    setGfxFont(fontid, 0);
     if(swfoutput_queryfont(&output, fontid))
     {
        swfoutput_setfont(&output, fontid, 0);
@@ -1657,9 +1747,13 @@ void SWFOutputDev::updateFont(GfxState *state)
     dumpFontInfo("<verbose>", gfxFont);
 
     swfoutput_setfont(&output, fontid, fileName);
+    
+    if(!setGfxFont(fontid, 0)) {
+       setGfxFont(fontid, fileName);
+    }
    
-    /*if(fileName && del)
-       unlinkfont(fileName);*/
+    if(fileName && del)
+       unlinkfont(fileName);
     if(fileName)
         free(fileName);
     free(fontid);