converted external fonts into a linked list
[swftools.git] / lib / pdf / GFXOutputDev.cc
index 2c41499..4258a37 100644 (file)
 typedef struct _fontfile
 {
     const char*filename;
+    int len; // basename length
     int used;
+    struct _fontfile*next;
 } fontfile_t;
 
 // for pdfswf_addfont
-static fontfile_t fonts[2048];
+
+static fontfile_t* global_fonts = 0;
+static fontfile_t* global_fonts_next = 0;
+
 static int fontnum = 0;
 
 /* config */
@@ -327,12 +332,24 @@ DisplayFontParam *GFXGlobalParams::getDisplayFont(GString *fontName)
            return dfp;
        }
     }
-    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;
-       }
+    
+    int bestlen = 0x7fffffff;
+    const char*bestfilename = 0;
+    
+    fontfile_t*f = 0;
+    while(f) {
+       if(strstr(f->filename, name)) {
+            if(f->len < bestlen) {
+                bestlen = f->len;
+                bestfilename = f->filename;
+            }
+        }
+        f = f->next;
+    }
+    if(bestfilename) {
+        DisplayFontParam *dfp = new DisplayFontParam(new GString(fontName), displayFontT1);
+        dfp->t1.fileName = new GString(bestfilename);
+        return dfp;
     }
     return GlobalParams::getDisplayFont(fontName);
 }
@@ -939,7 +956,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 +1087,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 +1537,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);
@@ -2004,14 +2021,25 @@ static const char* dirseparator()
 
 void addGlobalFont(const char*filename)
 {
-    fontfile_t f;
-    memset(&f, 0, sizeof(fontfile_t));
-    f.filename = filename;
-    if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
-       msg("<notice> Adding font \"%s\".", filename);
-       fonts[fontnum++] = f;
+    fontfile_t* f = (fontfile_t*)malloc(sizeof(fontfile_t));
+    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;
+
+    msg("<notice> Adding font \"%s\".", filename);
+    if(global_fonts_next) {
+        global_fonts_next->next = f;
+        global_fonts_next = global_fonts_next->next;
     } else {
-       msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
+        global_fonts_next = global_fonts = f;
     }
 }