* added some comments
[swftools.git] / pdf2swf / swfoutput.cc
index 6ed1c31..dc84be2 100644 (file)
@@ -44,7 +44,7 @@ int jpegquality=85;
 int storeallcharacters=0;
 int enablezlib=0;
 int insertstoptag=0;
-int flashversion=4;
+int flashversion=5;
 int splinemaxerror=1;
 int fontsplinemaxerror=1;
 
@@ -89,7 +89,7 @@ char fillstylechanged = 0;
 static void startshape(struct swfoutput* obj);
 static void starttext(struct swfoutput* obj);
 static void endshape();
-static void endtext();
+static void endtext(struct swfoutput* obj);
 
 // matrix multiplication. changes p0
 static void transform (plotxy*p0,struct swfmatrix*m)
@@ -119,11 +119,17 @@ static int moveto(TAG*tag, plotxy p0)
 // write a line-to command into the swf
 static void lineto(TAG*tag, plotxy p0)
 {
-    int rx = ((int)(p0.x*20)-swflastx);
-    int ry = ((int)(p0.y*20)-swflasty);
+    int px = (int)(p0.x*20);
+    int py = (int)(p0.y*20);
+    int rx = (px-swflastx);
+    int ry = (py-swflasty);
     /* we can't skip this for rx=0,ry=0, those
        are plots */
     swf_ShapeSetLine (tag, shape, rx,ry);
+
+    //swf_ExpandRect3(boundingBox, px, py, linewidth);
+    //swf_ExpandRect3(boundingBox, swflastx, swflasty, linewidth);
+
     shapeisempty = 0;
     swflastx+=rx;
     swflasty+=ry;
@@ -739,6 +745,64 @@ struct chardata {
 } chardata[CHARDATAMAX];
 int chardatapos = 0;
 
+static SRECT getcharacterbbox(SWFFONT*font)
+{
+    SRECT r;
+    char debug = 0;
+    memset(&r, 0, sizeof(r));
+
+    int t;
+    if(debug) printf("\n");
+    for(t=0;t<chardatapos;t++)
+    {
+       if(chardata[t].fontid != font->id) {
+           msg("<error> Internal error: fontid %d != fontid %d", chardata[t].fontid, font->id);
+           exit(1);
+       }
+       SRECT b = font->layout->bounds[chardata[t].charid];
+       b.xmin *= chardata[t].size;
+       b.ymin *= chardata[t].size;
+       b.xmax *= chardata[t].size;
+       b.ymax *= chardata[t].size;
+       b.xmin /= 1024;
+       b.ymin /= 1024;
+       b.xmax /= 1024;
+       b.ymax /= 1024;
+       b.xmin += chardata[t].x;
+       b.ymin += chardata[t].y;
+       b.xmax += chardata[t].x;
+       b.ymax += chardata[t].y;
+
+       /* until we solve the INTERNAL_SCALING problem (see below)
+          make sure the bounding box is big enough */
+       b.xmin -= 20;
+       b.ymin -= 20;
+       b.xmax += 20;
+       b.ymax += 20;
+
+       if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
+               font->layout->bounds[chardata[t].charid].xmin/20.0,
+               font->layout->bounds[chardata[t].charid].ymin/20.0,
+               font->layout->bounds[chardata[t].charid].xmax/20.0,
+               font->layout->bounds[chardata[t].charid].ymax/20.0,
+               b.xmin/20.0,
+               b.ymin/20.0,
+               b.xmax/20.0,
+               b.ymax/20.0,
+               chardata[t].fontid,
+               font->id,
+               chardata[t].charid
+               );
+       swf_ExpandRect2(&r, &b);
+    }
+    if(debug) printf("-----> (%f,%f,%f,%f)\n",
+           r.xmin/20.0,
+           r.ymin/20.0,
+           r.xmax/20.0,
+           r.ymax/20.0);
+    return r;
+}
+
 static void putcharacters(TAG*tag)
 {
     int t;
@@ -881,7 +945,7 @@ static void putcharacter(struct swfoutput*obj, int fontid, int charid,
     if(chardatapos == CHARDATAMAX)
     {
        msg("<warning> Character buffer too small. SWF will be slightly bigger");
-        endtext();
+        endtext(obj);
         starttext(obj);
     }
     chardata[chardatapos].fontid = fontid;
@@ -899,50 +963,47 @@ struct fontlist_t
     fontlist_t*next;
 } *fontlist = 0;
 
+/* todo: why don't higher values (64, 1024) work here? */
+#define FONT_INTERNAL_SIZE 1
+
 /* process a character. */
 static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m)
 {
-    int usefonts=1;
-    if(m->m12!=0 || m->m21!=0)
-        usefonts=0;
-    if(m->m11 != m->m22)
-        usefonts=0;
-
     if(!swffont) {
        msg("<warning> Font is NULL");
        return 0;
     }
 
-    if(!usefonts) {
-       msg("<verbose> Non diagonal font matrix: %f %f", m->m11, m->m21);
-       msg("<verbose> |                         %f %f", m->m12, m->m22);
+    int charid = getCharID(swffont, charnr, character, u); 
+    
+    if(charid<0) {
+       msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)", 
+               FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
+       return 0;
     }
 
-    //if(usefonts && ! drawonlyshapes)
-    if (1)
-    {
-        int charid = getCharID(swffont, charnr, character, u); 
-        
-       if(charid<0) {
-           msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)", 
-                   FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
-           /*fontlist_t*it = fontlist;
-           while(it) {
-               msg("<warning> Font history: %s [%d]", it->swffont->name, getCharID(it->swffont, charnr, character, u));
-               it = it->next;
-           }*/
-           return 0;
-       }
-
-        if(shapeid>=0)
-            endshape();
-        if(textid<0)
-            starttext(obj);
+    if(shapeid>=0)
+       endshape();
+    if(textid<0)
+       starttext(obj);
 
-        putcharacter(obj, swffont->id, charid,(int)(m->m13*20),(int)(m->m23*20),
-                (int)(m->m11+0.5));
+    float x = m->m13;
+    float y = m->m23;
+    float det = ((m->m11*m->m22)-(m->m21*m->m12));
+    if(fabs(det) < 0.0005) { 
+       /* x direction equals y direction- the text is invisible */
        return 1;
     }
+    det = 20*FONT_INTERNAL_SIZE / det;
+
+    SPOINT p;
+    p.x = (SCOORD)((  x * m->m22 - y * m->m12)*det);
+    p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
+
+    putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE);
+    swf_FontUseGlyph(swffont, charid);
+    return 1;
+
     /*else
     {
         SWF_OUTLINE*outline = font->getOutline(character, charnr);
@@ -961,7 +1022,7 @@ static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int
         m2.m22/=100;
 
         if(textid>=0)
-            endtext();
+            endtext(obj);
         if(shapeid<0)
             startshape(obj);
 
@@ -974,12 +1035,37 @@ static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int
     }*/
 }
 
+static void endtext(swfoutput*obj)
+{
+    if(textid<0)
+        return;
+
+    tag = swf_InsertTag(tag,ST_DEFINETEXT);
+    swf_SetU16(tag, textid);
+
+    SRECT r;
+    r = getcharacterbbox(obj->swffont);
+    
+    swf_SetRect(tag,&r);
+
+    MATRIX m;
+    swf_GetMatrix(0, &m);
+    swf_SetMatrix(tag,&m);
+
+    putcharacters(tag);
+    swf_SetU8(tag,0);
+    tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
+    swf_ObjectPlace(tag,textid,/*depth*/depth++,&obj->fontmatrix,NULL,NULL);
+    textid = -1;
+}
+
+
 /* draw a curved polygon. */
 void swfoutput_drawpath(swfoutput*output, SWF_OUTLINE*outline, 
                             struct swfmatrix*m)
 {
     if(textid>=0)
-        endtext();
+        endtext(output);
 
     /* Multiple polygons in one shape don't overlap correctly, 
        so we better start a new shape here if the polygon is filled
@@ -1002,7 +1088,7 @@ void swfoutput_drawpath(swfoutput*output, SWF_OUTLINE*outline,
 void swfoutput_drawpath2poly(struct swfoutput*output, SWF_OUTLINE*outline, struct swfmatrix*m, int line_join, int line_cap, double line_width, double miter_limit)
 {
     if(textid>=0)
-        endtext();
+        endtext(output);
     if(shapeid>=0)
        endshape();
     assert(shapeid<0);
@@ -1031,11 +1117,6 @@ int getCharID(SWFFONT *font, int charnr, char *charname, int u)
     }
 
     if(u>0) {
-       if(u>=font->maxascii)
-           msg("<debug> u=%d, font->maxascii=%d",u,font->maxascii);
-       else
-           msg("<debug> u=%d, font->maxascii=%d ascci2glyph[%d]=%d",u,font->maxascii,u,font->ascii2glyph[u]);
-
        /* try to use the unicode id */
        if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
            return font->ascii2glyph[u];
@@ -1046,6 +1127,13 @@ int getCharID(SWFFONT *font, int charnr, char *charname, int u)
        return charnr;
     }
 
+    /* the following is technically wrong, and only works if the font encoding
+       is US-ASCII based. It's needed for fonts which return broken unicode
+       indices */
+/*    if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
+       return font->ascii2glyph[charnr];
+    }*/
+
     return -1;
 }
 
@@ -1062,6 +1150,10 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
     if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
         return;
 
+    /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
+             with multiple fonts */
+    endtext(obj);
+
     iterator = fontlist;
     while(iterator) {
         if(!strcmp((char*)iterator->swffont->name,fontid)) {
@@ -1077,6 +1169,7 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
        return;
     }
 
+    swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
     SWFFONT*swffont = swf_LoadFont(filename);
 
     if(swffont == 0) {
@@ -1097,7 +1190,12 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
        msg("<debug> |   Style: %d", swffont->style);
        msg("<debug> |   Encoding: %d", swffont->encoding);
        for(int iii=0; iii<swffont->numchars;iii++) {
-           msg("<debug> |   Glyph %d) name=%s, unicode=%d\n", iii, swffont->glyphnames[iii], swffont->glyph2ascii[iii]);
+           msg("<debug> |   Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, swffont->glyphnames[iii], swffont->glyph2ascii[iii], swffont->glyph[iii].shape->bitlen, 
+                   swffont->layout->bounds[iii].xmin/20.0,
+                   swffont->layout->bounds[iii].ymin/20.0,
+                   swffont->layout->bounds[iii].xmax/20.0,
+                   swffont->layout->bounds[iii].ymax/20.0
+                   );
        }
     }
 
@@ -1138,12 +1236,19 @@ void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
        obj->fontm21 == m21 &&
        obj->fontm22 == m22)
         return;
-//    if(textid>=0)
-//      endtext();
+   if(textid>=0)
+       endtext(obj);
     obj->fontm11 = m11;
     obj->fontm12 = m12;
     obj->fontm21 = m21;
     obj->fontm22 = m22;
+    
+    MATRIX m;
+    m.sx = (U32)(((obj->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((obj->fontm12)*65536)/FONT_INTERNAL_SIZE);
+    m.r0 = (U32)(((obj->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((obj->fontm22)*65536)/FONT_INTERNAL_SIZE); 
+    m.tx = 0;
+    m.ty = 0;
+    obj->fontmatrix = m;
 }
 
 /* draws a character at x,y. */
@@ -1236,7 +1341,7 @@ static void startshape(struct swfoutput*obj)
   SRECT r;
 
   if(textid>=0)
-      endtext();
+      endtext(obj);
 
   tag = swf_InsertTag(tag,ST_DEFINESHAPE);
 
@@ -1250,6 +1355,7 @@ static void startshape(struct swfoutput*obj)
   shapeid = ++currentswfid;
   swf_SetU16(tag,shapeid);  // ID
 
+  /* TODO: patch back */
   r.xmin = 0;
   r.ymin = 0;
   r.xmax = 20*sizex;
@@ -1269,29 +1375,11 @@ static void startshape(struct swfoutput*obj)
 
 static void starttext(struct swfoutput*obj)
 {
-  SRECT r;
-  MATRIX m;
   if(shapeid>=0)
       endshape();
-  tag = swf_InsertTag(tag,ST_DEFINETEXT);
+    
   textid = ++currentswfid;
-  swf_SetU16(tag, textid);
 
-  r.xmin = 0;
-  r.ymin = 0;
-  r.xmax = 20*sizex;
-  r.ymax = 20*sizey;
-  
-  swf_SetRect(tag,&r);
-
-  m.sx = 65536;
-  m.sy = 65536;
-  m.r0 = 0;
-  m.r1 = 0;
-  m.tx = 0;
-  m.ty = 0;
-  swf_SetMatrix(tag,&m);
   swflastx=swflasty=0;
 }
 
@@ -1307,29 +1395,19 @@ static void endshape()
        tag = tag->prev;
        swf_DeleteTag(todel);
     } else {
+       /* TODO: fix bounding box */
        tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
        swf_ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
     }
     shapeid = -1;
 }
 
-static void endtext()
-{
-    if(textid<0)
-        return;
-    putcharacters(tag);
-    swf_SetU8(tag,0);
-    tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
-    swf_ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
-    textid = -1;
-}
-
 static void endpage(struct swfoutput*obj)
 {
     if(shapeid>=0)
       endshape();
     if(textid>=0)
-      endtext();
+      endtext(obj);
     while(clippos)
         swfoutput_endclip(obj);
 
@@ -1355,8 +1433,7 @@ void swfoutput_newpage(struct swfoutput*obj)
     depth = startdepth;
 }
 
-/* "destroy" like in (oo-terminology) "destructor". Perform cleaning
-   up, complete the swf, and write it out. */
+/* Perform cleaning up, complete the swf, and write it out. */
 void swfoutput_destroy(struct swfoutput* obj) 
 {
     endpage(obj);
@@ -1365,6 +1442,8 @@ void swfoutput_destroy(struct swfoutput* obj)
        TAG*mtag = swf.firstTag;
        if(iterator->swffont) {
            mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
+           /*if(!storeallcharacters)
+               swf_FontReduce(iterator->swffont);*/
            swf_FontSetDefine2(mtag, iterator->swffont);
            swf_FontFree(iterator->swffont);
        }
@@ -1388,7 +1467,7 @@ void swfoutput_destroy(struct swfoutput* obj)
  
     tag = swf_InsertTag(tag,ST_END);
 
-    if(enablezlib) {
+    if(enablezlib || flashversion>=6) {
       if FAILED(swf_WriteSWC(fi,&swf)) 
        msg("<error> WriteSWC() failed.\n");
     } else {
@@ -1460,7 +1539,7 @@ void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
 void swfoutput_startclip(swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m)
 {
     if(textid>=0)
-     endtext();
+     endtext(obj);
     if(shapeid>=0)
      endshape();
 
@@ -1488,7 +1567,7 @@ void swfoutput_startclip(swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m)
 void swfoutput_endclip(swfoutput*obj)
 {
     if(textid>=0)
-     endtext();
+     endtext(obj);
     if(shapeid>=0)
      endshape();
 
@@ -1518,7 +1597,7 @@ void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
     
     if(opennewwindow)
       actions = action_GetUrl(0, url, "_parent");
@@ -1535,7 +1614,7 @@ void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
    
       actions = action_GotoFrame(0, page);
       actions = action_End(actions);
@@ -1555,7 +1634,7 @@ void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     if(!strncmp(tmp, "call:", 5))
     {
@@ -1822,7 +1901,7 @@ int swfoutput_drawimagejpeg_old(struct swfoutput*obj, char*filename, int sizex,i
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     int bitid = ++currentswfid;
     oldtag = tag;
@@ -1850,7 +1929,7 @@ int swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     int bitid = ++currentswfid;
     oldtag = tag;
@@ -1871,7 +1950,7 @@ int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int si
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     int bitid = ++currentswfid;
     oldtag = tag;
@@ -1898,7 +1977,7 @@ int swfoutput_drawimagelosslessN(struct swfoutput*obj, U8*mem, RGBA*pal, int siz
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     if(sizex&3)
     { 
@@ -1942,7 +2021,7 @@ void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey,
     if(shapeid>=0)
      endshape();
     if(textid>=0)
-     endtext();
+     endtext(obj);
 
     drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
 }