choose external fonts based on the shortest filename
[swftools.git] / lib / pdf / GFXOutputDev.cc
index 2c41499..416bf0b 100644 (file)
@@ -80,6 +80,7 @@
 typedef struct _fontfile
 {
     const char*filename;
+    int len; // basename length
     int used;
 } fontfile_t;
 
@@ -327,12 +328,21 @@ DisplayFontParam *GFXGlobalParams::getDisplayFont(GString *fontName)
            return dfp;
        }
     }
+    
+    int bestlen = 0x7fffffff;
+    const char*bestfilename = 0;
     for(t=0;t<fontnum;t++) {
        if(strstr(fonts[t].filename, name)) {
-           DisplayFontParam *dfp = new DisplayFontParam(new GString(fontName), displayFontT1);
-           dfp->t1.fileName = new GString(fonts[t].filename);
-           return dfp;
-       }
+            if(fonts[t].len < bestlen) {
+                bestlen = fonts[t].len;
+                bestfilename = fonts[t].filename;
+            }
+        }
+    }
+    if(bestfilename) {
+        DisplayFontParam *dfp = new DisplayFontParam(new GString(fontName), displayFontT1);
+        dfp->t1.fileName = new GString(bestfilename);
+        return dfp;
     }
     return GlobalParams::getDisplayFont(fontName);
 }
@@ -939,7 +949,7 @@ void GFXOutputDev::drawChar(GfxState *state, double x, double y,
                        CharCode charid, int nBytes, Unicode *_u, int uLen)
 {
     if(!current_fontinfo || (unsigned)charid >= current_fontinfo->num_glyphs || !current_fontinfo->glyphs[charid]) {
-       msg("<error> Invalid charid %d for font %s", charid, current_font_id);
+       msg("<error> Invalid charid %d for font (%d characters)", charid, current_fontinfo?current_fontinfo->num_glyphs:0);
        return;
     }
   
@@ -1070,7 +1080,7 @@ GBool GFXOutputDev::beginType3Char(GfxState *state, double x, double y, double d
        m.ty += user_movey + clipmovey;
 
        if(!current_fontinfo || (unsigned)charid >= current_fontinfo->num_glyphs || !current_fontinfo->glyphs[charid]) {
-           msg("<error> Invalid charid %d for font %s", charid, current_font_id);
+           msg("<error> Invalid charid %d for font", charid);
            return gFalse;
        }
        gfxcolor_t col={0,0,0,0};
@@ -1520,7 +1530,7 @@ void GFXOutputDev::updateFont(GfxState *state)
     this->current_fontinfo = this->info->getFont(id);
     if(!this->current_fontinfo) {
        msg("<error> Internal Error: no fontinfo for font %s\n", id);
-    return;
+       return;
     }
     if(!this->current_fontinfo->seen) {
        dumpFontInfo("<verbose>", gfxFont);
@@ -2007,6 +2017,15 @@ void addGlobalFont(const char*filename)
     fontfile_t f;
     memset(&f, 0, sizeof(fontfile_t));
     f.filename = filename;
+    int len = strlen(filename);
+    char*r1 = strrchr(filename, '/');
+    char*r2 = strrchr(filename, '\\');
+    if(r2>r1)
+        r1 = r2;
+    if(r1) {
+        len = strlen(r1+1);
+    }
+    f.len = len;
     if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
        msg("<notice> Adding font \"%s\".", filename);
        fonts[fontnum++] = f;