*** empty log message ***
[swftools.git] / pdf2swf / SWFOutputDev.cc
index b0472ba..defd24e 100644 (file)
@@ -144,7 +144,7 @@ public:
   void startDoc(XRef *xref);
 
   // Start a page.
-  virtual void startPage(int pageNum, GfxState *state) ;
+  virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
 
   //----- link borders
   virtual void drawLink(Link *link, Catalog *catalog) ;
@@ -224,6 +224,22 @@ public:
   GfxState *laststate;
 };
 
+char*getFontName(GfxFont*font)
+{
+    GString*gstr = font->getName();
+    char* fontname = gstr==0?0:gstr->getCString();
+    if(fontname==0) {
+       char buf[32];
+       Ref*r=font->getID();
+       sprintf(buf, "UFONT%d", r->num);
+       return strdup(buf);
+    }
+    char* plus = strchr(fontname, '+');
+    if(plus && plus < &fontname[strlen(fontname)-1])
+       fontname = plus+1;
+    return fontname;
+}
+
 char mybuf[1024];
 char* gfxstate2str(GfxState *state)
 {
@@ -303,8 +319,8 @@ char* gfxstate2str(GfxState *state)
   if(state->getLineJoin()!=0)
   bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
 
-  if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
-  bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
+  if(state->getFont() && getFontName(state->getFont()))
+  bufpos+=sprintf(bufpos,"F\"%s\" ",getFontName(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]);
@@ -331,6 +347,8 @@ char* gfxstate2str(GfxState *state)
   return mybuf;
 }
 
+
+
 void dumpFontInfo(char*loglevel, GfxFont*font);
 int lastdumps[1024];
 int lastdumppos = 0;
@@ -360,15 +378,13 @@ void showFontError(GfxFont*font, int nr)
 
 void dumpFontInfo(char*loglevel, GfxFont*font)
 {
-  GString *gstr;
-  char*name = 0;
-  gstr = font->getName();
+  char* name = getFontName(font);
   Ref* r=font->getID();
-  msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?FIXNULL(gstr->getCString()):"(unknown font)", r->num,r->gen);
+  msg("%s=========== %s (ID:%d,%d) ==========\n", loglevel, name, r->num,r->gen);
 
-  gstr  = font->getTag();
-  if(gstr) 
-   msg("%sTag: %s\n", loglevel, FIXNULL(gstr->getCString()));
+  GString*gstr  = font->getTag();
+   
+  msg("%sTag: %s\n", loglevel, name);
   
   if(font->isCIDFont()) msg("%sis CID font\n", loglevel);
 
@@ -624,8 +640,6 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
                        double originX, double originY,
                        CharCode c, Unicode *_u, int uLen)
 {
-    msg("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
-    
     // check for invisible text -- this is used by Acrobat Capture
     if ((state->getRender() & 3) == 3)
        return;
@@ -640,13 +654,16 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
     x1 = x;
     y1 = y;
     state->transform(x, y, &x1, &y1);
+       
+    Unicode u=0;
+    if(_u) 
+       u = *_u;
+    
+    msg("<debug> drawChar(%f,%f,%f,%f,'%c',%d) CID=%d\n",x,y,dx,dy,c,u, font->isCIDFont());
 
     if(font->isCIDFont()) {
        GfxCIDFont*cfont = (GfxCIDFont*)font;
-       Unicode u=0;
        char*name=0;
-       if(_u) 
-           u = *_u;
        if(u) {
            int t;
            for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++)
@@ -656,22 +673,23 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
                    break;
                }
        }
-/*        printf("%02x %04x/%04x-%d \"%s\" %s %d\n", c,u, *_u, uLen, name, cfont->getName()->getCString(),
-               cfont->getType());*/
 
        if(name)
-          swfoutput_drawchar(&output, x1, y1, name, c);
-       else
+          swfoutput_drawchar(&output, x1, y1, name, c, u);
+       else {
           msg("<warning> couldn't get name for CID character %02x from Encoding", c);
+          swfoutput_drawchar(&output, x1, y1, "<unknown>", c, u);
+       }
     } else {
        Gfx8BitFont*font8;
        font8 = (Gfx8BitFont*)font;
        char**enc=font8->getEncoding();
 
        if(enc && enc[c])
-          swfoutput_drawchar(&output, x1, y1, enc[c], c);
+          swfoutput_drawchar(&output, x1, y1, enc[c], c, u);
        else {
           msg("<warning> couldn't get name for character %02x from Encoding", c);
+          swfoutput_drawchar(&output, x1, y1, "<unknown>", c, u);
        }
     }
 }
@@ -698,17 +716,28 @@ void SWFOutputDev::endType3Char(GfxState *state)
     msg("<debug> endType3Char");
 }
 
-void SWFOutputDev::startPage(int pageNum, GfxState *state) 
+void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2) 
 {
   double x1,y1,x2,y2;
   laststate = state;
-  msg("<debug> startPage %d\n", pageNum);
+  msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
   msg("<notice> processing page %d", pageNum);
 
-  state->transform(state->getX1(),state->getY1(),&x1,&y1);
+  /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
   state->transform(state->getX2(),state->getY2(),&x2,&y2);
+  Use CropBox, not MediaBox, as page size
+  */
+  x1 = crop_x1;
+  y1 = crop_y1;
+  x2 = crop_x2;
+  y2 = crop_y2;
+
+  if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
+  if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
+
   if(!outputstarted) {
-    swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
+    msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
+    swfoutput_init(&output, swffilename,(int)x1,(int)y1,(int)x2,(int)y2);
     outputstarted = 1;
   }
   else
@@ -888,6 +917,8 @@ int SWFOutputDev::searchT1Font(char*name)
     int i;
     int mapid=-1;
     char*filename=0;
+       
+    msg("<verbose> SearchT1Font(%s)", name);
 
     for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++) 
     {
@@ -978,10 +1009,10 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
       int c;
       char *fontBuf;
       int fontLen;
-      Type1CFontFile *cvt;
       Ref embRef;
       Object refObj, strObj;
-      tmpFileName = "/tmp/tmpfont";
+      char namebuf[512];
+      tmpFileName = mktmpname(namebuf);
       int ret;
 
       ret = font->getEmbeddedFontID(&embRef);
@@ -997,16 +1028,28 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
        msg("<error> Couldn't create temporary Type 1 font file");
          return 0;
       }
-      if (font->getType() == fontType1C) {
+      if (font->getType() == fontType1C ||
+         font->getType() == fontCIDType0C) {
        if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
          fclose(f);
          msg("<error> Couldn't read embedded font file");
          return 0;
        }
-       cvt = new Type1CFontFile(fontBuf, fontLen);
+       Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
        cvt->convertToType1(f);
        delete cvt;
        gfree(fontBuf);
+      } else if(font->getType() == fontTrueType) {
+       msg("<verbose> writing font using TrueTypeFontFile::writeTTF");
+       if (!(fontBuf = font->readEmbFontFile(xref, &fontLen))) {
+         fclose(f);
+         msg("<error> Couldn't read embedded font file");
+         return 0;
+       }
+       TrueTypeFontFile *cvt = new TrueTypeFontFile(fontBuf, fontLen);
+       cvt->writeTTF(f);
+       delete cvt;
+       gfree(fontBuf);
       } else {
        font->getEmbeddedFontID(&embRef);
        refObj.initRef(embRef.num, embRef.gen);
@@ -1050,41 +1093,27 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
              msg("<notice> File contains TrueType fonts");
              ttfinfo = 1;
          }
-         char name2[80];
+         char name2[512];
          char*tmp;
          tmp = strdup(mktmpname((char*)name2));
          sprintf(name2, "%s", tmp);
-         char*a[] = {"./ttf2pt1", "-W0",
+         char*a[] = {"./ttf2pt1", "-W", "0",
 #ifndef USE_FREETYPE
-             "-pttf",
+             "-p", "ttf",
 #else
-             "-pft",
+             "-p", "ft",
 #endif
              "-b", tmpFileName, name2};
-         msg("<verbose> Invoking ttf2pt1...");
-         ttf2pt1_main(6,a);
+         msg("<verbose> Invoking %s %s %s %s %s %s %s %s",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
+         ttf2pt1_main(8,a);
          unlink(tmpFileName);
          sprintf(name2,"%s.pfb",tmp);
          tmpFileName = strdup(name2);
       }
 
-    return tmpFileName;
+    return strdup(tmpFileName);
 }
 
-char* gfxFontName(GfxFont* gfxFont)
-{
-      GString *gstr;
-      gstr = gfxFont->getName();
-      if(gstr) {
-         return gstr->getCString();
-      }
-      else {
-         char buf[32];
-         Ref*r=gfxFont->getID();
-         sprintf(buf, "UFONT%d", r->num);
-         return strdup(buf);
-      }
-}
 
 char* substitutetarget[256];
 char* substitutesource[256];
@@ -1104,7 +1133,7 @@ char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
     if(oldname) {
        substitutesource[substitutepos] = oldname;
        substitutetarget[substitutepos] = fontname;
-       msg("<verbose> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
+       msg("<notice> substituting %s -> %s", FIXNULL(oldname), FIXNULL(fontname));
        substitutepos ++;
     }
     return fontname;
@@ -1241,7 +1270,7 @@ void SWFOutputDev::updateFont(GfxState *state)
   if (!gfxFont) {
     return;
   }  
-  char * fontname = gfxFontName(gfxFont);
+  char * fontname = getFontName(gfxFont);
  
   int t;
   /* first, look if we substituted this font before-
@@ -1275,9 +1304,11 @@ void SWFOutputDev::updateFont(GfxState *state)
   GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
   if(embedded) {
     if (gfxFont->getType() == fontType1 ||
+       gfxFont->getType() == fontCIDType0C ||
        gfxFont->getType() == fontType1C ||
        gfxFont->getType() == fontTrueType ||
-       gfxFont->getType() == fontCIDType2) 
+       gfxFont->getType() == fontCIDType2
+       )
     {
        fileName = writeEmbeddedFontToFile(xref, gfxFont);
        if(!fileName) {
@@ -1287,7 +1318,7 @@ void SWFOutputDev::updateFont(GfxState *state)
        }
        this->t1id = T1_AddFont(fileName);
        if(this->t1id<0) {
-         msg("<error> Couldn't load font from file");
+         msg("<error> Couldn't load font from file %s", fileName);
          showFontError(gfxFont,0);
          unlinkfont(fileName);
          return ;
@@ -1299,6 +1330,7 @@ void SWFOutputDev::updateFont(GfxState *state)
           font directories */
        int newt1id = searchT1Font(fontname);
        if(newt1id<0) {
+           msg("<error> Couldn't find any suitable replacement for %s",fontname);
            showFontError(gfxFont,0);
            fontname = substituteFont(gfxFont, fontname);
        } else
@@ -1320,7 +1352,8 @@ void SWFOutputDev::updateFont(GfxState *state)
   }
 
   if(t1id<0) {
-      showFontError(gfxFont,0);
+      msg("<error> Current font's t1id is %d", t1id);
+      //showFontError(gfxFont,0);
       return;
   }