fixed space->m bug, removed x1,y1,x2,y2 fields in swf_output
[swftools.git] / pdf2swf / SWFOutputDev.cc
index 86af8d1..5ce505d 100644 (file)
@@ -157,10 +157,14 @@ public:
   void setClip(int x1,int y1,int x2,int y2);
   
   int save(char*filename);
-  void  pagefeed();
-  void* getSWF();
+    
+  // Start a page.
+  void startFrame(int width, int height);
+
+  virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
 
-  void getDimensions(int*x1,int*y1,int*x2,int*y2);
+  void endframe();
+  void* getSWF();
 
   //----- get info about output device
 
@@ -180,9 +184,6 @@ public:
 
   void setXRef(PDFDoc*doc, XRef *xref);
 
-  // Start a page.
-  virtual void startPage(int pageNum, GfxState *state, double x1, double y1, double x2, double y2) ;
-
   //----- link borders
   virtual void drawLink(Link *link, Catalog *catalog) ;
 
@@ -268,6 +269,7 @@ public:
   char* substituteFont(GfxFont*gfxFont, char*oldname);
   char* writeEmbeddedFontToFile(XRef*ref, GfxFont*font);
   int t1id;
+  int textmodeinfo; // did we write "Text will be rendered as polygon" yet?
   int jpeginfo; // did we write "File contains jpegs" yet?
   int pbminfo; // did we write "File contains jpegs" yet?
   int linkinfo; // did we write "File contains links" yet?
@@ -361,6 +363,7 @@ class InfoOutputDev:  public OutputDev
 SWFOutputDev::SWFOutputDev()
 {
     jpeginfo = 0;
+    textmodeinfo = 0;
     ttfinfo = 0;
     linkinfo = 0;
     pbminfo = 0;
@@ -407,18 +410,6 @@ void SWFOutputDev::setClip(int x1,int y1,int x2,int y2)
     this->user_clipy2 = y2;
 }
 
-void SWFOutputDev::getDimensions(int*x1,int*y1,int*x2,int*y2)
-{
-    if(result) {
-       *x1 = (int)result->get(result, "xmin");
-       *y1 = (int)result->get(result, "ymin");
-       *x2 = (int)result->get(result, "xmax");
-       *y2 = (int)result->get(result, "ymax");
-    } else {
-       *x1 = *y1 = *x2 = *y2 = 0;
-    }
-}
-
 static char*getFontID(GfxFont*font)
 {
     GString*gstr = font->getName();
@@ -897,14 +888,14 @@ void SWFOutputDev::eoClip(GfxState *state)
     gfxline_free(line);
 }
 
-void SWFOutputDev::pagefeed()
+void SWFOutputDev::endframe()
 {
     if(outer_clip_box) {
        output->endclip(output);
        outer_clip_box = 0;
     }
 
-    swfoutput_pagefeed(output);
+    output->endpage(output);
 }
 
 void SWFOutputDev::finish()
@@ -1033,6 +1024,11 @@ int getGfxCharID(gfxfont_t*font, int charnr, char *charname, int u)
        return font->unicode2glyph[u];
     }
 
+    /* we don't need to "draw" space characters, so don't overdo the search
+       for a matching glyph */
+    if(charname && !strcasecmp(charname, "space"))
+       return -1;
+
     if(charnr>=0 && charnr<font->num_glyphs) {
        msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
        return charnr;
@@ -1134,8 +1130,10 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
 
     int charid = getGfxCharID(current_gfxfont, c, name, u);
     if(charid<0) {
-       msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)", 
-               FIXNULL(name),c, u, FIXNULL((char*)current_font_id), current_gfxfont->num_glyphs);
+       if(strcasecmp(name, "space")) {
+           msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)", 
+                   FIXNULL(name),c, u, FIXNULL((char*)current_font_id), current_gfxfont->num_glyphs);
+       }
        return;
     }
 
@@ -1148,6 +1146,10 @@ void SWFOutputDev::drawChar(GfxState *state, double x, double y,
        output->drawchar(output, current_font_id, charid, &col, &m);
     } else {
        msg("<debug> Drawing glyph %d as shape", charid);
+       if(!textmodeinfo) {
+           msg("<notice> Some texts will be rendered as shape");
+           textmodeinfo = 1;
+       }
        gfxline_t*glyph = current_gfxfont->glyphs[charid].line;
        gfxline_t*tglyph = gfxline_clone(glyph);
        gfxline_transform(tglyph, &m);
@@ -1239,6 +1241,11 @@ void SWFOutputDev::endType3Char(GfxState *state)
     msg("<debug> endType3Char");
 }
 
+void SWFOutputDev::startFrame(int width, int height) 
+{
+    output->startpage(output, width, height);
+}
+
 void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2) 
 {
     this->currentpage = pageNum;
@@ -1250,10 +1257,6 @@ void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, doubl
 
     white.r = white.g = white.b = white.a = 255;
 
-    msg("<verbose> startPage %d (%f,%f,%f,%f)\n", pageNum, crop_x1, crop_y1, crop_x2, crop_y2);
-    if(rot!=0)
-        msg("<verbose> page is rotated %d degrees\n", rot);
-
     /* state->transform(state->getX1(),state->getY1(),&x1,&y1);
     state->transform(state->getX2(),state->getY2(),&x2,&y2);
     Use CropBox, not MediaBox, as page size
@@ -1263,12 +1266,13 @@ void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, doubl
     y1 = crop_y1;
     x2 = crop_x2;
     y2 = crop_y2;*/
-    state->transform(crop_x1,crop_y1,&x1,&y1);
-    state->transform(crop_x2,crop_y2,&x2,&y2);
+    state->transform(crop_x1,crop_y1,&x1,&y1); //x1 += user_movex; y1 += user_movey;
+    state->transform(crop_x2,crop_y2,&x2,&y2); //x2 += user_movex; y2 += user_movey;
 
     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
 
+
     /* apply user clip box */
     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
         /*if(user_clipx1 > x1)*/ x1 = user_clipx1;
@@ -1277,16 +1281,16 @@ void SWFOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, doubl
         /*if(user_clipy2 < y2)*/ y2 = user_clipy2;
     }
 
-    msg("<verbose> Bounding box is (%f,%f)-(%f,%f)", x1,y1,x2,y2);
+    //msg("<verbose> Bounding box is (%f,%f)-(%f,%f) [shifted by %d/%d]", x1,y1,x2,y2, user_movex, user_movey);
     
     if(outer_clip_box) {
        output->endclip(output);
        outer_clip_box = 0;
     }
 
-    msg("<notice> processing page %d (%dx%d:%d:%d)", pageNum, (int)x2-(int)x1,(int)y2-(int)y1, (int)x1, (int)y1);
-
-    swfoutput_newpage(output, (int)x1, (int)y1, (int)x2, (int)y2);
+    msg("<notice> processing PDF page %d (%dx%d:%d:%d) (move:%d:%d)", pageNum, (int)x2-(int)x1,(int)y2-(int)y1, (int)x1, (int)y1, user_movex, user_movey);
+    if(rot!=0)
+        msg("<verbose> page is rotated %d degrees\n", rot);
 
     clippath[0].type = gfx_moveTo;clippath[0].x = x1; clippath[0].y = y1; clippath[0].next = &clippath[1];
     clippath[1].type = gfx_lineTo;clippath[1].x = x2; clippath[1].y = y1; clippath[1].next = &clippath[2];
@@ -2585,7 +2589,7 @@ pdf_doc_t* pdf_init(char*filename, char*userPassword)
     return pdf_doc;
 }
 
-void pdfswf_preparepage(int page)
+static void pdfswf_preparepage(int page)
 {
     /*FIXME*/
     if(!pages) {
@@ -2615,7 +2619,6 @@ void pdf_destroy(pdf_doc_t*pdf_doc)
 {
     pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
 
-    msg("<debug> pdfswf.cc: pdfswf_close()");
     delete i->doc; i->doc=0;
     
     free(pages); pages = 0; //FIXME
@@ -2660,24 +2663,27 @@ swf_output_t* swf_output_init()
     return swf_output;
 }
 
-void swf_output_setparameter(swf_output_t*swf_output, char*name, char*value)
+void swf_output_setparameter(swf_output_t*swf, char*name, char*value)
 {
-    /* FIXME */
     pdfswf_setparameter(name, value);
 }
 
-void swf_output_pagefeed(swf_output_t*swf)
+void swf_output_startframe(swf_output_t*swf, int width, int height)
+{
+    swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+    i->outputDev->startFrame(width, height);
+}
+
+void swf_output_endframe(swf_output_t*swf)
 {
     swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
-    i->outputDev->pagefeed();
-    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
+    i->outputDev->endframe();
 }
 
 int swf_output_save(swf_output_t*swf, char*filename)
 {
     swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
     int ret = i->outputDev->save(filename);
-    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
     return ret;
 }
 
@@ -2685,7 +2691,6 @@ void* swf_output_get(swf_output_t*swf)
 {
     swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
     void* ret = i->outputDev->getSWF();
-    i->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
     return ret;
 }
 
@@ -2712,7 +2717,6 @@ void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
 #else
     pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, /*doLinks*/(int)1);
 #endif
-    si->outputDev->getDimensions(&swf->x1, &swf->y1, &swf->x2, &swf->y2);
 }
 
 void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)