fixed format warnings
[swftools.git] / lib / devices / pdf.c
index 4d223c8..9e6365b 100644 (file)
 #include <pdflib.h>
 #include <math.h>
 #include "../os.h"
+#include "../q.h"
 #include "../jpeg.h"
 #include "../types.h"
 #include "../mem.h"
 #include "../gfxdevice.h"
 #include "../gfxtools.h"
+#include "../gfximage.h"
 
 typedef struct _internal {
     PDF* p;
     char*tempfile;
+    double lastx,lasty;
     gfxfontlist_t*fontlist;
 } internal_t;
 
@@ -60,12 +63,19 @@ void pdf_startpage(gfxdevice_t*dev, int width, int height)
     PDF_set_parameter(i->p, "fillrule", "evenodd");
 }
 
-static int mkline(gfxline_t*line, PDF*p)
+static int mkline(gfxline_t*line, PDF*p, char fill)
 {
+    double x,y;
+    char first = 1;
     int ret = 0;
-    double x=0,y=0;
+    char free_line = 0;
+    if(fill) {
+       line = gfxline_restitch(gfxline_clone(line));
+       free_line = 1;
+    }
     while(line) {
-       if(line->type == gfx_moveTo) {
+       if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
+           first = 0;
            PDF_moveto(p, line->x, line->y);
        } else if(line->type == gfx_lineTo) {
            PDF_lineto(p, line->x, line->y);
@@ -85,6 +95,8 @@ static int mkline(gfxline_t*line, PDF*p)
        y = line->y;
        line = line->next;
     }
+    if(free_line)
+       gfxline_free(line);
     return ret;
 }
 
@@ -92,7 +104,7 @@ void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
 {
     internal_t*i = (internal_t*)dev->internal;
     PDF_save(i->p);
-    if(mkline(line, i->p))
+    if(mkline(line, i->p, 1))
        PDF_clip(i->p);
     else   
        ; // TODO: strictly speaking, an empty clip clears everything
@@ -106,13 +118,15 @@ void pdf_endclip(gfxdevice_t*dev)
 void pdf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
 {
     internal_t*i = (internal_t*)dev->internal;
+    if(width<1e-6)
+       return;
     PDF_setlinewidth(i->p, width);
     PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
     PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
     PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
     if(joint_style==gfx_joinMiter)
        PDF_setmiterlimit(i->p, miterLimit);
-    if(mkline(line, i->p))
+    if(mkline(line, i->p, 0))
        PDF_stroke(i->p);
 }
 
@@ -121,7 +135,7 @@ void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
     internal_t*i = (internal_t*)dev->internal;
     PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
        
-    if(mkline(line, i->p)) {
+    if(mkline(line, i->p, 1)) {
        PDF_fill(i->p);
     }
 }
@@ -181,7 +195,7 @@ void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
                sprintf(name, "chr%d", t+32);
                PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
                PDF_begin_glyph(i->p, name, g->advance, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax);
-               if(mkline(g->line, i->p))
+               if(mkline(g->line, i->p, 1))
                    PDF_fill(i->p);
                PDF_end_glyph(i->p);
            }
@@ -205,14 +219,14 @@ void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color
     char as_shape = 0;
     if(!type3) as_shape=1;
     if(glyphnr>256-32) as_shape=1;
-    gfxmatrix_dump(matrix, stdout, "");
     if(fabs(matrix->m00 + matrix->m11) > 0.01) as_shape=1;
     if(fabs(fabs(matrix->m01) + fabs(matrix->m10)) > 0.01) as_shape=1;
+    if(fabs(matrix->m00) < 1e-6) as_shape=1;
 
     if(as_shape) {
        gfxline_t*line2 = gfxline_clone(glyph->line);
        gfxline_transform(line2, matrix);
-       if(mkline(line2, i->p)) {
+       if(mkline(line2, i->p, 1)) {
            PDF_fill(i->p);
        }
        gfxline_free(line2);
@@ -221,8 +235,16 @@ void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color
        int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
        PDF_setfont(i->p, fontid, matrix->m00);
        char name[32];
-       sprintf(name, "%c\0", glyphnr+32);
-       PDF_show_xy2(i->p, name, strlen(name), matrix->tx, matrix->ty);
+       sprintf(name, "%c", glyphnr+32);
+
+       if(fabs(matrix->tx - i->lastx) > 0.001 || matrix->ty != i->lasty) {
+           PDF_show_xy2(i->p, name, strlen(name), matrix->tx, matrix->ty);
+       } else {
+           PDF_show2(i->p, name, strlen(name));
+       }
+
+       i->lastx = matrix->tx + glyph->advance*matrix->m00;
+       i->lasty = matrix->ty;
     }
     return;
 }
@@ -317,6 +339,8 @@ void gfxdevice_pdf_init(gfxdevice_t*dev)
     dev->endpage = pdf_endpage;
     dev->finish = pdf_finish;
 
+    i->lastx = -1e38;
+    i->lasty = -1e38;
     i->p = PDF_new();
 }