fixed page52.pdf segfault (added null pointer handling to
[swftools.git] / pdf2swf / SWFOutputDev.cc
index 23cb0c3..2728dcb 100644 (file)
 #include <string.h>
 #include <unistd.h>
 #include "../config.h"
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #ifdef HAVE_FONTCONFIG_H
 #include <fontconfig.h>
 #endif
@@ -71,6 +77,8 @@ typedef struct _fontfile
 static fontfile_t fonts[2048];
 static int fontnum = 0;
 
+static int config_use_fontconfig = 1;
+
 // swf <-> pdf pages
 // TODO: move into pdf_doc_t
 static int*pages = 0;
@@ -80,6 +88,7 @@ static int pagepos = 0;
 /* config */
 static double caplinewidth = 3.0;
 static int zoom = 72; /* xpdf: 86 */
+static int forceType0Fonts = 0;
 
 static void printInfoString(Dict *infoDict, char *key, char *fmt);
 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
@@ -104,8 +113,8 @@ struct mapping {
 {"ZapfDingbats",          "d050000l"}};
 
 class SWFOutputDev:  public OutputDev {
-  struct swfoutput output;
   int outputstarted;
+  struct swfoutput output;
 public:
 
   // Constructor.
@@ -117,7 +126,11 @@ public:
   void setMove(int x,int y);
   void setClip(int x1,int y1,int x2,int y2);
   
-  void save(char*filename);
+  int save(char*filename);
+  void  pagefeed();
+  void* getSWF();
+
+  void getDimensions(int*x1,int*y1,int*x2,int*y2);
 
   //----- get info about output device
 
@@ -341,6 +354,10 @@ void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
     this->user_clipx2 = x2;
     this->user_clipy2 = y2;
 }
+void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
+{
+    return swfoutput_getdimensions(&output, x1,y1,x2,y2);
+}
 
 static char*getFontID(GfxFont*font)
 {
@@ -685,7 +702,7 @@ void SWFOutputDev::stroke(GfxState *state)
     m.m12 = 0; m.m13 = 0; m.m23 = 0;
     SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
     
-    if(screenloglevel >= LOGLEVEL_TRACE)  {
+    if(getLogLevel() >= LOGLEVEL_TRACE)  {
         msg("<trace> stroke\n");
         dump_outline(outline);
     }
@@ -721,7 +738,7 @@ void SWFOutputDev::fill(GfxState *state)
     m.m12 = 0; m.m13 = 0; m.m23 = 0;
     SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
 
-    if(screenloglevel >= LOGLEVEL_TRACE)  {
+    if(getLogLevel() >= LOGLEVEL_TRACE)  {
         msg("<trace> fill\n");
         dump_outline(outline);
     }
@@ -738,7 +755,7 @@ void SWFOutputDev::eoFill(GfxState *state)
     m.m12 = 0; m.m13 = 0; m.m23 = 0;
     SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
 
-    if(screenloglevel >= LOGLEVEL_TRACE)  {
+    if(getLogLevel() >= LOGLEVEL_TRACE)  {
         msg("<trace> eofill\n");
         dump_outline(outline);
     }
@@ -756,7 +773,7 @@ void SWFOutputDev::clip(GfxState *state)
     m.m13 = 0; m.m23 = 0;
     SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
 
-    if(screenloglevel >= LOGLEVEL_TRACE)  {
+    if(getLogLevel() >= LOGLEVEL_TRACE)  {
         msg("<trace> clip\n");
         dump_outline(outline);
     }
@@ -773,7 +790,7 @@ void SWFOutputDev::eoClip(GfxState *state)
     m.m12 = 0; m.m13 = 0; m.m23 = 0;
     SWF_OUTLINE*outline = gfxPath_to_SWF_OUTLINE(state, path);
 
-    if(screenloglevel >= LOGLEVEL_TRACE)  {
+    if(getLogLevel() >= LOGLEVEL_TRACE)  {
         msg("<trace> eoclip\n");
         dump_outline(outline);
     }
@@ -782,9 +799,19 @@ void SWFOutputDev::eoClip(GfxState *state)
     clipping[clippos] ++;
     free_outline(outline);
 }
-void SWFOutputDev::save(char*filename)
+
+/* pass through functions for swf_output */
+int SWFOutputDev::save(char*filename)
 {
-    swfoutput_save(&output, filename);
+    return swfoutput_save(&output, filename);
+}
+void SWFOutputDev::pagefeed()
+{
+    swfoutput_pagefeed(&output);
+}
+void* SWFOutputDev::getSWF()
+{
+    return (void*)swfoutput_get(&output);
 }
 
 SWFOutputDev::~SWFOutputDev() 
@@ -829,7 +856,7 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
     // check for invisible text -- this is used by Acrobat Capture
     if ((state->getRender() & 3) == 3)
        return;
-
+    Gushort *CIDToGIDMap = 0;
     GfxFont*font = state->getFont();
 
     if(font->getType() == fontType3) {
@@ -842,20 +869,29 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
     state->transform(x, y, &x1, &y1);
     
     Unicode u=0;
-    if(_u && uLen) 
+    char*name=0;
+
+    if(_u && uLen) {
        u = *_u;
+       if (u) {
+           int t;
+           /* find out char name from unicode index 
+              TODO: should be precomputed
+            */
+           for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
+               if(nameToUnicodeTab[t].u == u) {
+                   name = nameToUnicodeTab[t].name;
+                   break;
+               }
+           }
+       }
+    }
 
-    /* find out the character name */
-    char*name=0;
-    if(font->isCIDFont() && u) {
+    if(font->isCIDFont()) {
        GfxCIDFont*cfont = (GfxCIDFont*)font;
-       int t;
-       for(t=0;t<sizeof(nameToUnicodeTab)/sizeof(nameToUnicodeTab[0]);t++) {
-           /* todo: should be precomputed */
-           if(nameToUnicodeTab[t].u == u) {
-               name = nameToUnicodeTab[t].name;
-               break;
-           }
+
+       if(font->getType() == fontCIDType2) {
+           CIDToGIDMap = cfont->getCIDToGID();
        }
     } else {
        Gfx8BitFont*font8;
@@ -864,13 +900,14 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
        if(enc && enc[c])
           name = enc[c];
     }
-    
-    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));
-
-    /*x1 = (int)(x1+0.5);
-    y1 = (int)(y1+0.5);*/
-    
-    int ret = swfoutput_drawchar(&output, x1, y1, name, c, u);
+    if (CIDToGIDMap) {
+       msg("<debug> drawChar(%f, %f, c='%c' (%d), GID=%d, u=%d <%d>) CID=%d name=\"%s\"\n", x, y, (c&127)>=32?c:'?', c, CIDToGIDMap[c], u, uLen, font->isCIDFont(), FIXNULL(name));
+       swfoutput_drawchar(&output, x1, y1, name, CIDToGIDMap[c], u);
+    } else {
+       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));
+       swfoutput_drawchar(&output, x1, y1, name, c, u);
+    }
 }
 
 void SWFOutputDev::endString(GfxState *state) { 
@@ -920,10 +957,10 @@ void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, doubl
 
     /* apply user clip box */
     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
-        if(user_clipx1 > x1) x1 = user_clipx1;
-        if(user_clipx2 < x2) x2 = user_clipx2;
-        if(user_clipy1 > y1) y1 = user_clipy1;
-        if(user_clipy2 < y2) y2 = user_clipy2;
+        /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
+        /*if(user_clipx2 < x2)*/ x2 = user_clipx2;
+        /*if(user_clipy1 > y1)*/ y1 = user_clipy1;
+        /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
     }
 
     if(!outputstarted) {
@@ -1224,9 +1261,11 @@ char*SWFOutputDev::writeEmbeddedFontToFile(XRef*ref, GfxFont*font)
       }
 #ifdef XPDF_101
       Type1CFontFile *cvt = new Type1CFontFile(fontBuf, fontLen);
+      if(!cvt) return 0;
       cvt->convertToType1(f);
 #else
       FoFiType1C *cvt = FoFiType1C::make(fontBuf, fontLen);
+      if(!cvt) return 0;
       cvt->convertToType1(NULL, gTrue, FoFiWrite, f);
 #endif
       //cvt->convertToCIDType0("test", f);
@@ -1293,6 +1332,9 @@ char* searchForSuitableFont(GfxFont*gfxFont)
     char*name = getFontName(gfxFont);
     char*fontname = 0;
     char*filename = 0;
+
+    if(!config_use_fontconfig)
+        return 0;
     
 #ifdef HAVE_FONTCONFIG
     FcPattern *pattern, *match;
@@ -1300,25 +1342,38 @@ char* searchForSuitableFont(GfxFont*gfxFont)
     FcChar8 *v;
 
     static int fcinitcalled = false; 
+        
+    msg("<debug> searchForSuitableFont(%s)", name);
     
     // call init ony once
     if (!fcinitcalled) {
+        msg("<debug> Initializing FontConfig...");
         fcinitcalled = true;
-       FcInit(); //leaks
+       if(FcInit()) {
+            msg("<debug> FontConfig Initialization failed. Disabling.");
+            config_use_fontconfig = 0;
+            return 0;
+        }
+        msg("<debug> ...initialized FontConfig");
     }
    
+    msg("<debug> FontConfig: Create \"%s\" Family Pattern", name);
     pattern = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, name, NULL);
     if (gfxFont->isItalic()) // check for italic
+        msg("<debug> FontConfig: Adding Italic Slant");
        FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
     if (gfxFont->isBold()) // check for bold
+        msg("<debug> FontConfig: Adding Bold Weight");
         FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
 
+    msg("<debug> FontConfig: Try to match...");
     // configure and match using the original font name 
     FcConfigSubstitute(0, pattern, FcMatchPattern); 
     FcDefaultSubstitute(pattern);
     match = FcFontMatch(0, pattern, &result);
     
     if (FcPatternGetString(match, "family", 0, &v) == FcResultMatch) {
+        msg("<debug> FontConfig: family=%s", (char*)v);
         // if we get an exact match
         if (strcmp((char *)v, name) == 0) {
            if (FcPatternGetString(match, "file", 0, &v) == FcResultMatch) {
@@ -1327,6 +1382,7 @@ char* searchForSuitableFont(GfxFont*gfxFont)
                if(nfn) fontname = strdup(nfn+1);
                else    fontname = filename;
             }
+            msg("<debug> FontConfig: Returning \"%s\"", fontname);
         } else {
             // initialize patterns
             FcPatternDestroy(pattern);
@@ -1334,22 +1390,28 @@ char* searchForSuitableFont(GfxFont*gfxFont)
 
             // now match against serif etc.
            if (gfxFont->isSerif()) {
+                msg("<debug> FontConfig: Create Serif Family Pattern");
                 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "serif", NULL);
             } else if (gfxFont->isFixedWidth()) {
+                msg("<debug> FontConfig: Create Monospace Family Pattern");
                 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "monospace", NULL);
             } else {
+                msg("<debug> FontConfig: Create Sans Family Pattern");
                 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString, "sans", NULL);
             }
 
             // check for italic
             if (gfxFont->isItalic()) {
+                msg("<debug> FontConfig: Adding Italic Slant");
                 int bb = FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
             }
             // check for bold
             if (gfxFont->isBold()) {
+                msg("<debug> FontConfig: Adding Bold Weight");
                 int bb = FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
             }
 
+            msg("<debug> FontConfig: Try to match... (2)");
             // configure and match using serif etc
            FcConfigSubstitute (0, pattern, FcMatchPattern);
             FcDefaultSubstitute (pattern);
@@ -1361,6 +1423,7 @@ char* searchForSuitableFont(GfxFont*gfxFont)
                if(nfn) fontname = strdup(nfn+1);
                else    fontname = filename;
            }
+            msg("<debug> FontConfig: Returning \"%s\"", fontname);
         }        
     }
 
@@ -1388,6 +1451,10 @@ char* SWFOutputDev::substituteFont(GfxFont*gfxFont, char* oldname)
        fontname = "Times-Roman";
     }
     filename = searchFont(fontname);
+    if(!filename) {
+       msg("<error> Couldn't find font %s- did you install the default fonts?");
+       return 0;
+    }
 
     if(substitutepos>=sizeof(substitutesource)/sizeof(char*)) {
        msg("<fatal> Too many fonts in file.");
@@ -1487,7 +1554,7 @@ void SWFOutputDev::updateFont(GfxState *state)
     if(embedded &&
        (gfxFont->getType() == fontType1 ||
        gfxFont->getType() == fontType1C ||
-       //gfxFont->getType() == fontCIDType0C ||
+       (gfxFont->getType() == fontCIDType0C && forceType0Fonts) ||
        gfxFont->getType() == fontTrueType ||
        gfxFont->getType() == fontCIDType2
        ))
@@ -1894,10 +1961,19 @@ static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
 
 void pdfswf_setparameter(char*name, char*value)
 {
+    msg("<verbose> setting parameter %s to \"%s\"", name, value);
     if(!strcmp(name, "caplinewidth")) {
        caplinewidth = atof(value);
     } else if(!strcmp(name, "zoom")) {
        zoom = atoi(value);
+    } else if(!strcmp(name, "forceType0Fonts")) {
+       forceType0Fonts = atoi(value);
+    } else if(!strcmp(name, "fontdir")) {
+        pdfswf_addfontdir(value);
+    } else if(!strcmp(name, "languagedir")) {
+        pdfswf_addlanguagedir(value);
+    } else if(!strcmp(name, "fontconfig")) {
+        config_use_fontconfig = atoi(value);
     } else {
        swfoutput_setparameter(name, value);
     }
@@ -1914,6 +1990,82 @@ void pdfswf_addfont(char*filename)
     }
 }
 
+static char* dirseparator()
+{
+#ifdef WIN32
+    return "\\";
+#else
+    return "/";
+#endif
+}
+
+void pdfswf_addlanguagedir(char*dir)
+{
+    if(!globalParams)
+        globalParams = new GlobalParams("");
+    
+    msg("<notice> Adding %s to language pack directories", dir);
+
+    int l;
+    FILE*fi = 0;
+    char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc"));
+    strcpy(config_file, dir);
+    strcat(config_file, dirseparator());
+    strcat(config_file, "add-to-xpdfrc");
+
+    fi = fopen(config_file, "rb");
+    if(!fi) {
+        msg("<error> Could not open %s", config_file);
+        return;
+    }
+    globalParams->parseFile(new GString(config_file), fi);
+    fclose(fi);
+}
+
+void pdfswf_addfontdir(char*dirname)
+{
+#ifdef HAVE_DIRENT_H
+    msg("<notice> Adding %s to font directories", dirname);
+    DIR*dir = opendir(dirname);
+    if(!dir) {
+       msg("<warning> Couldn't open directory %s\n", dirname);
+       return;
+    }
+    struct dirent*ent;
+    while(1) {
+       ent = readdir (dir);
+       if (!ent) 
+           break;
+       int l;
+       char*name = ent->d_name;
+       char type = 0;
+       if(!name) continue;
+       l=strlen(name);
+       if(l<4)
+           continue;
+       if(!strncasecmp(&name[l-4], ".pfa", 4)) 
+           type=1;
+       if(!strncasecmp(&name[l-4], ".pfb", 4)) 
+           type=3;
+       if(!strncasecmp(&name[l-4], ".ttf", 4)) 
+           type=2;
+       if(type)
+       {
+           char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
+           strcpy(fontname, dirname);
+            strcat(fontname, dirseparator());
+           strcat(fontname, name);
+           msg("<verbose> Adding %s to fonts", fontname);
+           pdfswf_addfont(fontname);
+       }
+    }
+    closedir(dir);
+#else
+    msg("<warning> No dirent.h- unable to add font dir %s", dir);
+#endif
+}
+
+
 typedef struct _pdf_doc_internal
 {
     int protect;
@@ -1940,7 +2092,8 @@ pdf_doc_t* pdf_init(char*filename, char*userPassword)
     Object info;
 
     // read config file
-    globalParams = new GlobalParams("");
+    if(!globalParams)
+        globalParams = new GlobalParams("");
 
     // open PDF file
     if (userPassword && userPassword[0]) {
@@ -1959,7 +2112,7 @@ pdf_doc_t* pdf_init(char*filename, char*userPassword)
     // print doc info
     i->doc->getDocInfo(&info);
     if (info.isDict() &&
-      (screenloglevel>=LOGLEVEL_NOTICE)) {
+      (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
       printInfoString(info.getDict(), "Title",        "Title:        %s\n");
       printInfoString(info.getDict(), "Subject",      "Subject:      %s\n");
       printInfoString(info.getDict(), "Keywords",     "Keywords:     %s\n");
@@ -1987,8 +2140,8 @@ pdf_doc_t* pdf_init(char*filename, char*userPassword)
     i->protect = 0;
     if (i->doc->isEncrypted()) {
           if(!i->doc->okToCopy()) {
-              printf("PDF disallows copying. Terminating.\n");
-              exit(1); //bail out
+              printf("PDF disallows copying.\n");
+              return 0;
           }
           if(!i->doc->okToChange() || !i->doc->okToAddNotes())
               i->protect = 1;
@@ -2017,7 +2170,7 @@ class MemCheck
 {
     public: ~MemCheck()
     {
-        delete globalParams;
+        delete globalParams;globalParams=0;
         Object::memCheck(stderr);
         gMemReport(stderr);
     }
@@ -2078,10 +2231,27 @@ void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
     pdfswf_setparameter(name, value);
 }
 
-void swf_output_save(swf_output_t*swf, char*filename)
+void swf_output_pagefeed(swf_output_t*swf)
+{
+    swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+    i->outputDev->pagefeed();
+    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
+}
+
+int swf_output_save(swf_output_t*swf, char*filename)
+{
+    swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+    int ret = i->outputDev->save(filename);
+    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
+    return ret;
+}
+
+void* swf_output_get(swf_output_t*swf)
 {
     swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
-    i->outputDev->save(filename);
+    void* ret = i->outputDev->getSWF();
+    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
+    return ret;
 }
 
 void swf_output_destroy(swf_output_t*output)
@@ -2092,10 +2262,10 @@ void swf_output_destroy(swf_output_t*output)
     free(output);
 }
 
-void pdf_page_render2(pdf_page_t*page, swf_output_t*output)
+void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
 {
     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
-    swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
+    swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
 
     if(pi->protect) {
         swfoutput_setparameter("protect", "1");
@@ -2106,6 +2276,7 @@ void pdf_page_render2(pdf_page_t*page, swf_output_t*output)
 #else
     pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
 #endif
+    si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
 }
 
 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)