added pagefeed() function.
[swftools.git] / pdf2swf / swfoutput.cc
index 2a6d4a6..1c518a2 100644 (file)
@@ -72,13 +72,14 @@ float config_minlinewidth=0.05;
 
 typedef struct _swfoutput_internal
 {
+    SWF swf;
+
     fontlist_t* fontlist;
 
     char storefont;
 
     MATRIX page_matrix;
 
-    SWF swf;
     TAG *tag;
     int currentswfid;
     int depth;
@@ -112,6 +113,7 @@ typedef struct _swfoutput_internal
     chardata_t chardata[CHARDATAMAX];
     int chardatapos;
     int firstpage;
+    char pagefinished;
 } swfoutput_internal;
 
 static swfoutput_internal* init_internal_struct()
@@ -142,6 +144,7 @@ static swfoutput_internal* init_internal_struct()
     i->bboxrectpos = -1;
     i->chardatapos = 0;
     i->firstpage = 1;
+    i->pagefinished = 1;
 
     return i;
 };
@@ -1227,7 +1230,7 @@ int getCharID(SWFFONT *font, int charnr, char *charname, int u)
        }
     }
 
-    if(u>0) {
+    if(u>0 && font->encoding != 255) {
        /* try to use the unicode id */
        if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
            msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
@@ -1293,10 +1296,31 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
        msg("<warning> Couldn't load font %s (%s)", fontid, filename);
        swffont = swf_LoadFont(0);
     }
+    
+    if(swffont->glyph2ascii) {
+        int t;
+        int bad = 0;
+        /* check whether the Unicode indices look o.k.
+           If they don't, disable the unicode lookup by setting
+           the encoding to 255 */
+        for(t=0;t<swffont->numchars;t++) {
+            int c = swffont->glyph2ascii[t];
+            if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
+                // the character maps into the unicode control character range
+                // between 0001-001f. Yet it is not empty. Treat the one
+                // mapping as broken, and look how many of those we find.
+                bad ++;
+            }
+        }
+        if(bad>5) {
+           msg("<warning> Font %s has bad unicode mapping", swffont->name);
+            swffont->encoding = 255;
+        }
+    }
 
     swf_FontSetID(swffont, ++i->currentswfid);
     
-    if(screenloglevel >= LOGLEVEL_DEBUG)  {
+    if(getScreenLogLevel() >= LOGLEVEL_DEBUG)  {
        // print font information
        msg("<debug> Font %s (%s)",swffont->name, filename);
        msg("<debug> |   ID: %d", swffont->id);
@@ -1398,6 +1422,15 @@ static void endpage(struct swfoutput*obj)
       endtext(obj);
     while(i->clippos)
         swfoutput_endclip(obj);
+    i->pagefinished = 1;
+}
+
+void swfoutput_pagefeed(struct swfoutput*obj)
+{
+    swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+    
+    if(!i->pagefinished)
+        endpage(obj);
 
     if(config_insertstoptag) {
        ActionTAG*atag=0;
@@ -1412,17 +1445,19 @@ static void endpage(struct swfoutput*obj)
 void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey, int x1, int y1, int x2, int y2)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
-    if(!i->firstpage)
+    if(!i->firstpage && !i->pagefinished)
         endpage(obj);
 
     swf_GetMatrix(0, &i->page_matrix);
-    i->page_matrix.tx = movex;
-    i->page_matrix.ty = movey;
+    i->page_matrix.tx = movex*20;
+    i->page_matrix.ty = movey*20;
 
     for(i->depth--;i->depth>=i->startdepth;i->depth--) {
         i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
         swf_SetU16(i->tag,i->depth);
     }
+    /* TODO: this should all be done in SWFOutputDev */
+
     i->depth = i->startdepth = 3; /* leave room for clip and background rectangle */
 
     i->sizex = x2;
@@ -1482,6 +1517,7 @@ void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey,
     swf_ExpandRect2(&i->swf.movieSize, &i->lastpagesize);
 
     i->firstpage = 0;
+    i->pagefinished = 0;
 }
 
 /* initialize the swf writer */
@@ -1697,9 +1733,13 @@ static void endshape(swfoutput*obj, int clipdepth)
     i->bboxrectpos = -1;
 }
 
-void swfoutput_save(struct swfoutput* obj, char*filename) 
+void swfoutput_finalize(struct swfoutput*obj)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+
+    if(i->tag && i->tag->id == ST_END)
+        return; //already done
+
     endpage(obj);
     fontlist_t *tmp,*iterator = i->fontlist;
     while(iterator) {
@@ -1713,8 +1753,33 @@ void swfoutput_save(struct swfoutput* obj, char*filename)
 
         iterator = iterator->next;
     }
-    int fi;
+    i->tag = swf_InsertTag(i->tag,ST_END);
+}
 
+SWF* swfoutput_get(struct swfoutput*obj)
+{
+    swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+
+    swfoutput_finalize(obj);
+
+    return swf_CopySWF(&i->swf);
+}
+
+void swfoutput_getdimensions(struct swfoutput*obj, int*x1, int*y1, int*x2, int*y2)
+{
+    swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+    if(x1) *x1 = i->swf.movieSize.xmin/20;
+    if(y1) *y1 = i->swf.movieSize.ymin/20;
+    if(x2) *x2 = i->swf.movieSize.xmax/20;
+    if(y2) *y2 = i->swf.movieSize.ymax/20;
+}
+
+int swfoutput_save(struct swfoutput* obj, char*filename) 
+{
+    swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+    swfoutput_finalize(obj);
+
+    int fi;
     if(filename)
      fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
     else
@@ -1722,11 +1787,9 @@ void swfoutput_save(struct swfoutput* obj, char*filename)
     
     if(fi<=0) {
      msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
-     exit(1);
+     return 0;
     }
-    i->tag = swf_InsertTag(i->tag,ST_END);
-
+    
     if(config_enablezlib || config_flashversion>=6) {
       if FAILED(swf_WriteSWC(fi,&i->swf)) 
        msg("<error> WriteSWC() failed.\n");
@@ -1738,6 +1801,7 @@ void swfoutput_save(struct swfoutput* obj, char*filename)
     if(filename)
      close(fi);
     msg("<notice> SWF written\n");
+    return 1;
 }
 
 /* Perform cleaning up, complete the swf, and write it out. */