removed the embedded cache, as the swfoutput font cache now does the job,
[swftools.git] / pdf2swf / swfoutput.cc
index cb8e8f8..074f28b 100644 (file)
@@ -195,39 +195,25 @@ void drawpath(T1_OUTLINE*outline, struct swfmatrix*m, char*namehint)
        outline = outline->link;
     }
 }
+    //logf("<debug> Font name is %s", T1_GetFontFileName(t1fontindex));
+    //logf("<debug> char 0x%02x is named %s\n",character,charname);
+    //logf("<debug> bbox: %d %d %d %d\n",bbox.llx,bbox.lly,bbox.urx,bbox.ury);
+    //char*charname = T1_GetCharName(t1fontindex, character);
 
 /* process a character. */
-void drawchar(struct swfoutput*obj, int t1fontindex, char character, swfmatrix*m)
-{
-
-    /* <T1 stuff> */
-    T1_OUTLINE*outline;
-    int width = T1_GetCharWidth(t1fontindex, character);
-    BBox bbox = T1_GetCharBBox(t1fontindex, character);
-    char*charname= T1_GetCharName(t1fontindex, character);
-    logf("<debug> Font name is %s", T1_GetFontFileName(t1fontindex));
-    logf("<debug> char 0x%02x is named %s\n",character,charname);
-    logf("<debug> bbox: %d %d %d %d\n",bbox.llx,bbox.lly,bbox.urx,bbox.ury);
-    if(!charname || charname[0] == '.') 
-    {
-     /* for newline, we don't print an error. FIXME: We shouldn't get newlines here
-       in the first place
-      */
-     if(character != '\n') {
-        logf("<error> Char to set is not defined!");
-        logf("<error> -  font file is %s\n", T1_GetFontFileName(t1fontindex));
-        logf("<error> -  char 0x%02x is named %s\n",character,charname);
-     }
+void drawchar(struct swfoutput*obj, SWFFont*font, char*character, swfmatrix*m)
+{
+    T1_OUTLINE*outline = font->getOutline(character);
+    char* charname = character;
+
+    if(!outline)
      return;
-    }
+    
     swfmatrix m2=*m;    
     m2.m11/=100;
     m2.m21/=100;
     m2.m12/=100;
     m2.m22/=100;
-    outline =  T1_GetCharOutline( t1fontindex, character, 100.0, 0);
-
-    /** </T1 stuff> **/
 
     if(shapeid<0)
        startshape(obj);
@@ -259,11 +245,127 @@ void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, struct swfmatrix*m
     drawpath(outline,m, 0); 
 }
 
+SWFFont::SWFFont(char*name, int id, char*filename)
+{
+    if(!T1_GetFontName(id))
+       T1_LoadFont(id);
+
+    this->name = strdup(T1_GetFontFileName(id));
+    this->fontid = name;
+    this->t1id = id;
+
+    char**a= T1_GetAllCharNames(id);
+    int t=0, outlinepos=0;
+    char*map[256];
+    while(a[t])
+       t++;
+    this->charnum = t;
+    if(!t) 
+       return;
+    logf("<notice> Font %s(%d): Storing %d outlines.\n", name, id, t);
+    
+    outline = (T1_OUTLINE**)malloc(t*sizeof(T1_OUTLINE*));
+    charname = (char**)malloc(t*sizeof(char*));
+    
+    t=0;
+    while(*a)
+    {
+       map[t] = *a;
+       a++;
+       t++;
+       if(t==256 || !*a) {
+           int s;
+           for(s=t;s<256;s++)
+               map[s] = ".notdef";
+
+           int ret = T1_ReencodeFont(id, map);
+           if(ret) {
+            T1_DeleteFont(id);
+            T1_LoadFont(id);
+            int ret = T1_ReencodeFont(id, map);
+            if(ret)
+              fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
+           }
+
+           // parsecharacters
+           for(s=0;s<t;s++)
+           {
+               this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
+               this->charname[outlinepos] = strdup(T1_GetCharName(id, s));
+               outlinepos++;
+           }
+           t=0;
+       }
+    }
+}
+
+T1_OUTLINE*SWFFont::getOutline(char*name)
+{
+    int t;
+    for(t=0;t<this->charnum;t++) {
+       if(!strcmp(this->charname[t],name))
+           return outline[t];
+    }
+    return 0;
+}
+
+char*SWFFont::getName()
+{
+    return this->name;
+}
+
+struct fontlist_t 
+{
+    SWFFont * font;
+    fontlist_t*next;
+} *fontlist = 0;
 
 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
-int swfoutput_setfont(struct swfoutput*obj, int fontid, int t1id)
+void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
+{
+    fontlist_t*last=0,*iterator;
+    if(obj->font && !strcmp(obj->font->fontid,fontid))
+       return;
+
+    iterator = fontlist;
+    while(iterator) {
+       if(!strcmp(iterator->font->fontid,fontid))
+           break;
+       last = iterator;
+       iterator = iterator->next;
+    }
+    if(iterator) 
+    {
+       obj->font = iterator->font;
+       return ;
+    }
+
+    if(t1id<0) {
+       logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,fontid);
+    }
+    
+    SWFFont*font = new SWFFont(fontid, t1id, filename);
+    iterator = new fontlist_t;
+    iterator->font = font;
+    iterator->next = 0;
+
+    if(last) 
+       last->next = iterator;
+    else 
+       fontlist = iterator;
+    obj->font = font;
+}
+
+int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
 {
-    obj->t1font = t1id;
+    fontlist_t *iterator = fontlist;
+    while(iterator) {
+       if(!strcmp(iterator->font->fontid,fontid))
+           return 1;
+       iterator = iterator->next;
+    }
+    return 0;
 }
 
 /* set's the matrix which is to be applied to characters drawn by
@@ -278,7 +380,7 @@ void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
 }
 
 /* draws a character at x,y. */
-void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char character) 
+void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character) 
 {
     swfmatrix m;
     m.m11 = obj->fontm11;
@@ -287,7 +389,7 @@ void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char character)
     m.m22 = obj->fontm22;
     m.m13 = x;
     m.m23 = y;
-    drawchar(obj, obj->t1font, character, &m);
+    drawchar(obj, obj->font, character, &m);
 }
 
 /* initialize the swf writer */
@@ -303,7 +405,7 @@ void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _size
 
   logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
 
-  obj->t1font = 0;
+  obj->font = 0;
   
   memset(&swf,0x00,sizeof(SWF));