moved most of the parameter parsing to swfoutput.cc.
[swftools.git] / pdf2swf / SWFOutputDev.cc
index 8d564a7..58fb1a2 100644 (file)
@@ -55,7 +55,13 @@ static char* swffilename = 0;
 static int numpages;
 static int currentpage;
 
-static char*fonts[2048];
+typedef struct _fontfile
+{
+    char*filename;
+    int used;
+} fontfile_t;
+
+static fontfile_t fonts[2048];
 static int fontnum = 0;
 
 // swf <-> pdf pages
@@ -226,7 +232,7 @@ public:
   GfxState *laststate;
 };
 
-char*getFontName(GfxFont*font)
+static char*getFontID(GfxFont*font)
 {
     GString*gstr = font->getName();
     char* fontname = gstr==0?0:gstr->getCString();
@@ -236,6 +242,12 @@ char*getFontName(GfxFont*font)
        sprintf(buf, "UFONT%d", r->num);
        return strdup(buf);
     }
+    return fontname;
+}
+
+static char*getFontName(GfxFont*font)
+{
+    char*fontname = getFontID(font);
     char* plus = strchr(fontname, '+');
     if(plus && plus < &fontname[strlen(fontname)-1])
        fontname = plus+1;
@@ -321,8 +333,8 @@ char* gfxstate2str(GfxState *state)
   if(state->getLineJoin()!=0)
   bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
 
-  if(state->getFont() && getFontName(state->getFont()))
-  bufpos+=sprintf(bufpos,"F\"%s\" ",getFontName(state->getFont()));
+  if(state->getFont() && getFontID(state->getFont()))
+  bufpos+=sprintf(bufpos,"F\"%s\" ",getFontID(state->getFont()));
   bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
   bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
                                   state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
@@ -380,9 +392,9 @@ void showFontError(GfxFont*font, int nr)
 
 void dumpFontInfo(char*loglevel, GfxFont*font)
 {
-  char* name = getFontName(font);
+  char* name = getFontID(font);
   Ref* r=font->getID();
-  msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, name, r->num,r->gen);
+  msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, getFontName(font), r->num,r->gen);
 
   GString*gstr  = font->getTag();
    
@@ -679,7 +691,7 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
           name = enc[c];
     }
     
-    msg("<debug> drawChar(%f,%f,c='%c' (%d),u=%d <%d>) CID=%d name=\"%s\"\n",x1,y1,(c&127)>=32?c:'?',c,u, uLen, font->isCIDFont(), FIXNULL(name));
+    msg("<debug> drawChar(%f,%f,c='%c' (%d),u=%d <%d>) CID=%d name=\"%s\"\n",x,y,(c&127)>=32?c:'?',c,u, uLen, font->isCIDFont(), FIXNULL(name));
 
     int ret = swfoutput_drawchar(&output, x1, y1, name, c, u);
 }
@@ -911,8 +923,9 @@ char* SWFOutputDev::searchFont(char*name)
 {      
     int i;
     char*filename=0;
+    int is_standard_font = 0;
        
-    msg("<verbose> SearchT1Font(%s)", name);
+    msg("<verbose> SearchFont(%s)", name);
 
     /* see if it is a pdf standard font */
     for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++) 
@@ -920,15 +933,22 @@ char* SWFOutputDev::searchFont(char*name)
        if(!strcmp(name, pdf2t1map[i].pdffont))
        {
            name = pdf2t1map[i].filename;
+           is_standard_font = 1;
            break;
        }
     }
     /* look in all font files */
     for(i=0;i<fontnum;i++) 
     {
-       if(strstr(fonts[i], name))
+       if(strstr(fonts[i].filename, name))
        {
-           return fonts[i];
+           if(!fonts[i].used) {
+
+               fonts[i].used = 1;
+               if(!is_standard_font)
+                   msg("<notice> Using %s for %s", fonts[i].filename, name);
+           }
+           return fonts[i].filename;
        }
     }
     return 0;
@@ -996,6 +1016,13 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
       msg("<error> Couldn't create temporary Type 1 font file");
        return 0;
     }
+
+    /*if(font->isCIDFont()) {
+       GfxCIDFont* cidFont = (GfxCIDFont *)font;
+       GString c = cidFont->getCollection();
+       msg("<notice> Collection: %s", c.getCString());
+    }*/
+
     if (font->getType() == fontType1C ||
        font->getType() == fontCIDType0C) {
       if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
@@ -1005,6 +1032,8 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
       }
       Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
       cvt->convertToType1(f);
+      //cvt->convertToCIDType0("test", f);
+      //cvt->convertToType0("test", f);
       delete cvt;
       gfree(fontBuf);
     } else if(font->getType() == fontTrueType) {
@@ -1122,26 +1151,26 @@ void SWFOutputDev::updateFont(GfxState *state)
     if (!gfxFont) {
        return;
     }  
-    char * fontname = getFontName(gfxFont);
+    char * fontid = getFontID(gfxFont);
     
     int t;
     /* first, look if we substituted this font before-
        this way, we don't initialize the T1 Fonts
        too often */
     for(t=0;t<substitutepos;t++) {
-       if(!strcmp(fontname, substitutesource[t])) {
-           fontname = substitutetarget[t];
+       if(!strcmp(fontid, substitutesource[t])) {
+           fontid = substitutetarget[t];
            break;
        }
     }
 
     /* second, see if swfoutput already has this font
        cached- if so, we are done */
-    if(swfoutput_queryfont(&output, fontname))
+    if(swfoutput_queryfont(&output, fontid))
     {
-       swfoutput_setfont(&output, fontname, 0);
+       swfoutput_setfont(&output, fontid, 0);
        
-       msg("<debug> updateFont(%s) [cached]", fontname);
+       msg("<debug> updateFont(%s) [cached]", fontid);
        return;
     }
 
@@ -1160,8 +1189,8 @@ void SWFOutputDev::updateFont(GfxState *state)
     int del = 0;
     if(embedded &&
        (gfxFont->getType() == fontType1 ||
-       //gfxFont->getType() == fontCIDType0C ||
        gfxFont->getType() == fontType1C ||
+       //gfxFont->getType() == fontCIDType0C ||
        gfxFont->getType() == fontTrueType ||
        gfxFont->getType() == fontCIDType2
        ))
@@ -1170,24 +1199,26 @@ void SWFOutputDev::updateFont(GfxState *state)
       if(!fileName) showFontError(gfxFont,0);
       else del = 1;
     } else {
+      char * fontname = getFontName(gfxFont);
       fileName = searchFont(fontname);
       if(!fileName) showFontError(gfxFont,0);
     }
     if(!fileName) {
+       char * fontname = getFontName(gfxFont);
        msg("<warning> Font %s could not be loaded.", fontname);
        msg("<warning> Try putting a TTF version of that font (named \"%s.ttf\") into /swftools/fonts", fontname);
-       fileName = substituteFont(gfxFont, fontname);
+       fileName = substituteFont(gfxFont, fontid);
     }
 
     if(!fileName) {
-       msg("<error> Couldn't set font %s\n", fontname);
+       msg("<error> Couldn't set font %s\n", fontid);
        return;
     }
        
-    msg("<verbose> updateFont(%s) -> %s", fontname, fileName);
+    msg("<verbose> updateFont(%s) -> %s", fontid, fileName);
     dumpFontInfo("<verbose>", gfxFont);
 
-    swfoutput_setfont(&output, fontname, fileName);
+    swfoutput_setfont(&output, fontid, fileName);
    
     if(fileName && del)
        unlinkfont(fileName);
@@ -1634,95 +1665,42 @@ void pdfswf_init(char*filename, char*userPassword)
 
 void pdfswf_setparameter(char*name, char*value)
 {
-    if(!strcmp(name, "drawonlyshapes")) {
-       drawonlyshapes = atoi(value);
-    } else if(!strcmp(name, "ignoredraworder")) {
-       ignoredraworder = atoi(value);
-    } else if(!strcmp(name, "linksopennewwindow")) {
-       opennewwindow = atoi(value);
-    } else if(!strcmp(name, "storeallcharacters")) {
-       storeallcharacters = atoi(value);
-    } else if(!strcmp(name, "enablezlib")) {
-       enablezlib = atoi(value);
-    } else if(!strcmp(name, "insertstop")) {
-       insertstoptag = atoi(value);
-    } else if(!strcmp(name, "flashversion")) {
-       flashversion = atoi(value);
-    } else if(!strcmp(name, "jpegquality")) {
-       int val = atoi(value);
-       if(val<0) val=0;
-       if(val>100) val=100;
-       jpegquality = val;
-    } else if(!strcmp(name, "outputfilename")) {
+    if(!strcmp(name, "outputfilename")) {
        swffilename = value;
     } else if(!strcmp(name, "caplinewidth")) {
        caplinewidth = atof(value);
-    } else if(!strcmp(name, "splinequality")) {
-       int v = atoi(value);
-       v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
-       if(v<1) v = 1;
-       splinemaxerror = v;
-    } else if(!strcmp(name, "fontquality")) {
-       int v = atoi(value);
-       v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
-       if(v<1) v = 1;
-       fontsplinemaxerror = v;
     } else {
-       fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
+       swfoutput_setparameter(name, value);
     }
 }
 
 void pdfswf_addfont(char*filename)
 {
-    fonts[fontnum++] = filename;
+    fontfile_t f;
+    memset(&f, 0, sizeof(fontfile_t));
+    f.filename = filename;
+    fonts[fontnum++] = f;
 }
 
-void pdfswf_drawonlyshapes()
-{
-    drawonlyshapes = 1;
+/* TODO: get rid of this */
+void pdfswf_drawonlyshapes() { pdfswf_setparameter("drawonlyshapes", "1"); }
+void pdfswf_ignoredraworder() { pdfswf_setparameter("ignoredraworder", "1"); }
+void pdfswf_linksopennewwindow() { pdfswf_setparameter("opennewwindow", "1"); }
+void pdfswf_storeallcharacters() { pdfswf_setparameter("storeallcharacters", "1"); }
+void pdfswf_enablezlib() { pdfswf_setparameter("enablezlib", "1"); }
+void pdfswf_setoutputfilename(char*_filename) { swffilename = _filename; }
+void pdfswf_insertstop() { pdfswf_setparameter("insertstoptag", "1"); }
+void pdfswf_jpegquality(int val) {
+    char tmp[32];
+    sprintf(tmp, "%d", val);
+    pdfswf_setparameter("jpegquality", tmp);
 }
-
-void pdfswf_ignoredraworder()
-{
-    ignoredraworder = 1;
+void pdfswf_setversion(int n) {
+    char tmp[32];
+    sprintf(tmp, "%d", n);
+    pdfswf_setparameter("flashversion", tmp);
 }
 
-void pdfswf_linksopennewwindow()
-{
-    opennewwindow = 1;
-}
-
-void pdfswf_storeallcharacters()
-{
-    storeallcharacters = 1;
-}
-
-void pdfswf_enablezlib()
-{
-    enablezlib = 1;
-}
-
-void pdfswf_jpegquality(int val)
-{
-    if(val<0) val=0;
-    if(val>100) val=100;
-    jpegquality = val;
-}
-
-void pdfswf_setoutputfilename(char*_filename)
-{
-    swffilename = _filename;
-}
-
-void pdfswf_insertstop()
-{
-    insertstoptag = 1;
-}
-
-void pdfswf_setversion(int n)
-{
-    flashversion = n;
-}
 
 
 void pdfswf_convertpage(int page)
@@ -1750,7 +1728,6 @@ void pdfswf_performconversion()
        doc->displayPage((OutputDev*)output, currentpage, /*dpi*/72, /*rotate*/0, /*doLinks*/(int)1);
     }
 }
-
 int pdfswf_numpages()
 {
   return doc->getNumPages();