SWFFont now also stores char widths. (a.k.a. advance values)
[swftools.git] / pdf2swf / swfoutput.cc
index 6cef2e6..f84ebee 100644 (file)
@@ -27,10 +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;
@@ -515,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);
@@ -549,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++;
             }
@@ -561,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++;
@@ -577,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);
@@ -600,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);
@@ -607,6 +648,7 @@ SWFFont::~SWFFont()
     for(t=0;t<charnum;t++)
         free(charname[t]);
     free(charname);
+    free(width);
     free(used);
     free(swfcharid2char);
     free(char2swfcharid);
@@ -1020,6 +1062,130 @@ void swfoutput_endclip(swfoutput*obj)
     swf_ObjectPlaceClip(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
 }
 
+void drawlink(struct swfoutput*obj, ActionTAG*, swfcoord*points);
+
+void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
+{
+    ActionTAG* actions;
+    
+    if(shapeid>=0)
+     endshape();
+    if(textid>=0)
+     endtext();
+    
+    actions = swf_ActionStart();
+    if(opennewwindow)
+      action_GetUrl(url, "_parent");
+    else
+      action_GetUrl(url, "_this");
+      action_End();
+    swf_ActionEnd();
+    
+    drawlink(obj, actions, points);
+}
+void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
+{
+    ActionTAG* actions;
+
+    if(shapeid>=0)
+     endshape();
+    if(textid>=0)
+     endtext();
+   
+    actions = swf_ActionStart();
+      action_GotoFrame(page);
+      action_End();
+    swf_ActionEnd();
+
+    drawlink(obj, actions, points);
+}
+
+void drawlink(struct swfoutput*obj, ActionTAG*actions, swfcoord*points)
+{
+    RGBA rgb;
+    SRECT r;
+    int lsid=0;
+    int fsid;
+    struct plotxy p1,p2,p3,p4;
+    int myshapeid;
+    int myshapeid2;
+    double xmin,ymin;
+    double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
+    int t;
+    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;
+    }
+    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;
+    
+    /* shape */
+    myshapeid = ++currentswfid;
+    tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
+    swf_ShapeNew(&shape);
+    rgb.r = rgb.b = rgb.a = rgb.g = 0; 
+    fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
+    swf_SetU16(tag, myshapeid);
+    r.xmin = (int)(xmin*20);
+    r.ymin = (int)(ymin*20);
+    r.xmax = (int)(xmax*20);
+    r.ymax = (int)(ymax*20);
+    swf_SetRect(tag,&r);
+    swf_SetShapeStyles(tag,shape);
+    swf_ShapeCountBits(shape,NULL,NULL);
+    swf_SetShapeBits(tag,shape);
+    swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
+    swflastx = swflasty = 0;
+    moveto(tag, p1);
+    lineto(tag, p2);
+    lineto(tag, p3);
+    lineto(tag, p4);
+    lineto(tag, p1);
+    swf_ShapeSetEnd(tag);
+
+    /* shape2 */
+    myshapeid2 = ++currentswfid;
+    tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
+    swf_ShapeNew(&shape);
+    rgb.r = rgb.b = rgb.a = rgb.g = 255;
+    rgb.a = 40;
+    fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
+    swf_SetU16(tag, myshapeid2);
+    r.xmin = (int)(xmin*20);
+    r.ymin = (int)(ymin*20);
+    r.xmax = (int)(xmax*20);
+    r.ymax = (int)(ymax*20);
+    swf_SetRect(tag,&r);
+    swf_SetShapeStyles(tag,shape);
+    swf_ShapeCountBits(shape,NULL,NULL);
+    swf_SetShapeBits(tag,shape);
+    swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
+    swflastx = swflasty = 0;
+    moveto(tag, p1);
+    lineto(tag, p2);
+    lineto(tag, p3);
+    lineto(tag, p4);
+    lineto(tag, p1);
+    swf_ShapeSetEnd(tag);
+
+    tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
+    swf_SetU16(tag,buttonid); //id
+    swf_ButtonSetFlags(tag, 0); //menu=no
+    swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
+    swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
+    swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
+    swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
+    swf_SetU8(tag,0);
+    swf_ActionSet(tag,actions);
+    swf_SetU8(tag,0);
+    
+    tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
+    swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
+}
 
 void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey, 
         double x1,double y1,