added swf_ExpandRect3() method.
[swftools.git] / pdf2swf / swfoutput.cc
index a14592b..dc84be2 100644 (file)
@@ -745,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;
@@ -905,6 +963,9 @@ 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)
 {
@@ -933,13 +994,13 @@ static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int
        /* x direction equals y direction- the text is invisible */
        return 1;
     }
-    det = 20 / det;
+    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,1);
+    putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE);
     swf_FontUseGlyph(swffont, charid);
     return 1;
 
@@ -978,6 +1039,19 @@ 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);
@@ -1076,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)) {
@@ -1091,7 +1169,7 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
        return;
     }
 
-    swf_SetLoadFontParameters(0,/*skip unused*/0,/*full unicode*/1);
+    swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
     SWFFONT*swffont = swf_LoadFont(filename);
 
     if(swffont == 0) {
@@ -1159,15 +1237,15 @@ void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
        obj->fontm22 == m22)
         return;
    if(textid>=0)
-      endtext(obj);
+       endtext(obj);
     obj->fontm11 = m11;
     obj->fontm12 = m12;
     obj->fontm21 = m21;
     obj->fontm22 = m22;
     
     MATRIX m;
-    m.sx = (U32)((obj->fontm11)*65536); m.r1 = (U32)((obj->fontm12)*65536);
-    m.r0 = (U32)((obj->fontm21)*65536); m.sy = (U32)((obj->fontm22)*65536); 
+    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;
@@ -1297,25 +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);
-
-  /* TODO: patch back */
-  r.xmin = 0;
-  r.ymin = 0;
-  r.xmax = 20*sizex;
-  r.ymax = 20*sizey;
-  
-  swf_SetRect(tag,&r);
 
-  swf_GetMatrix(0, &m);
-  swf_SetMatrix(tag,&m);
   swflastx=swflasty=0;
 }