SWFFont now also stores char widths. (a.k.a. advance values)
[swftools.git] / pdf2swf / swfoutput.cc
index 44db4bd..f84ebee 100644 (file)
@@ -27,11 +27,14 @@ extern "C" {
 #include "../lib/log.h"
 #include "../lib/rfxswf.h"
 }
+#define standardEncodingSize 335
+extern char *standardEncodingNames[standardEncodingSize];
 
 int opennewwindow=0;
 int ignoredraworder=0;
 int drawonlyshapes=0;
 int jpegquality=85;
+int storeallcharacters=0;
 static int flag_protected = 0;
 
 typedef unsigned char u8;
@@ -516,6 +519,9 @@ SWFFont::SWFFont(char*name, int id, char*filename)
     
     outline = (T1_OUTLINE**)malloc(t*sizeof(T1_OUTLINE*));
     charname = (char**)malloc(t*sizeof(char*));
+    width = (int*)malloc(t*sizeof(int));
+    memset(width, 0, t*sizeof(int));
+    memset(charname, 0, t*sizeof(char*));
     used = (char*)malloc(t*sizeof(char));
     char2swfcharid = (U16*)malloc(t*2);
     swfcharid2char = (U16*)malloc(t*2);
@@ -550,6 +556,7 @@ SWFFont::SWFFont(char*name, int id, char*filename)
             for(s=0;s<t;s++)
             {
                 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
+               this->width[outlinepos] = T1_GetCharWidth(id, s);
                 this->charname[outlinepos] = strdup(T1_GetCharName(id, s));
                 outlinepos++;
             }
@@ -562,7 +569,19 @@ SWFFont::SWFFont(char*name, int id, char*filename)
 SWFFont::~SWFFont()
 {
     int t,usednum=0;
-    int*ptr = (int*)malloc(swfcharpos*sizeof(int));
+    int*ptr; 
+
+    if(storeallcharacters)
+    {
+       int t;
+       for(t=0;t<this->charnum;t++) 
+       {
+           if(this->charname[t])
+             getSWFCharID(this->charname[t]);
+       }
+    }
+    
+    ptr = (int*)malloc(swfcharpos*sizeof(int));
 
     for(t=0;t<charnum;t++)
         if(used[t]) usednum++;
@@ -578,6 +597,7 @@ SWFFont::~SWFFont()
         m.m21 = m.m12 = 0;
         m.m13 = CHARMIDX;
         m.m23 = CHARMIDY;
+
         for(t=0;t<swfcharpos;t++) 
         {
             ptr[t] = swf_GetDataSize(ftag);
@@ -601,6 +621,26 @@ SWFFont::~SWFFont()
             fill = lastfill;
             swf_ShapeSetEnd(ftag);
         }
+        ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
+       swf_SetU16(ftag, this->swfid);
+       if(this->fontid) {
+           swf_SetU8(ftag, strlen(this->fontid));
+           swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
+       } else {
+           swf_SetU8(ftag, 0);
+       }
+       swf_SetU8(ftag, 0); //flags
+       for(t=0;t<swfcharpos;t++)
+       {
+           int s;
+           char * name = this->charname[this->swfcharid2char[t]];
+           for(s=0;s<256;s++) {
+               if(standardEncodingNames[s] && 
+                       !strcmp(name,standardEncodingNames[s]))
+                   break;
+           }
+           swf_SetU8(ftag, (U8)s);
+       }
     }
 
     free(ptr);
@@ -608,6 +648,7 @@ SWFFont::~SWFFont()
     for(t=0;t<charnum;t++)
         free(charname[t]);
     free(charname);
+    free(width);
     free(used);
     free(swfcharid2char);
     free(char2swfcharid);
@@ -1032,7 +1073,7 @@ void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
     if(textid>=0)
      endtext();
     
-    actions = swf_ActionStart(tag);
+    actions = swf_ActionStart();
     if(opennewwindow)
       action_GetUrl(url, "_parent");
     else
@@ -1051,7 +1092,7 @@ void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
     if(textid>=0)
      endtext();
    
-    actions = swf_ActionStart(tag);
+    actions = swf_ActionStart();
       action_GotoFrame(page);
       action_End();
     swf_ActionEnd();
@@ -1074,10 +1115,10 @@ void drawlink(struct swfoutput*obj, ActionTAG*actions, swfcoord*points)
     int buttonid = ++currentswfid;
     for(t=1;t<4;t++)
     {
-       if(points[t].x>xmax) xmax=points[t].x;
-       if(points[t].y>ymax) ymax=points[t].y;
-       if(points[t].x<xmin) xmin=points[t].x;
-       if(points[t].y<ymin) ymin=points[t].y;
+        if(points[t].x>xmax) xmax=points[t].x;
+        if(points[t].y>ymax) ymax=points[t].y;
+        if(points[t].x<xmin) xmin=points[t].x;
+        if(points[t].y<ymin) ymin=points[t].y;
     }
     p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y; 
     p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
@@ -1131,9 +1172,6 @@ void drawlink(struct swfoutput*obj, ActionTAG*actions, swfcoord*points)
     lineto(tag, p1);
     swf_ShapeSetEnd(tag);
 
-    tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
-    swf_ObjectPlace(tag, buttonid, depth ++, 0,0,0);
-   
     tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
     swf_SetU16(tag,buttonid); //id
     swf_ButtonSetFlags(tag, 0); //menu=no
@@ -1142,9 +1180,9 @@ void drawlink(struct swfoutput*obj, ActionTAG*actions, swfcoord*points)
     swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
     swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
     swf_SetU8(tag,0);
-    swf_SetActions(tag,actions);
+    swf_ActionSet(tag,actions);
     swf_SetU8(tag,0);
-
+    
     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
     swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
 }