improved pdf output (pdf2pdf, gfx2gfx)
[swftools.git] / lib / devices / pdf.c
1 /* pdf.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2007 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <memory.h>
27 #include <pdflib.h>
28 #include <math.h>
29 #include "../os.h"
30 #include "../q.h"
31 #include "../jpeg.h"
32 #include "../types.h"
33 #include "../mem.h"
34 #include "../gfxdevice.h"
35 #include "../gfxtools.h"
36
37 typedef struct _internal {
38     PDF* p;
39     char*tempfile;
40     double lastx,lasty;
41     gfxfontlist_t*fontlist;
42 } internal_t;
43
44 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
45 {
46     internal_t*i = (internal_t*)dev->internal;
47     return 0;
48 }
49
50 void pdf_startpage(gfxdevice_t*dev, int width, int height)
51 {
52     internal_t*i = (internal_t*)dev->internal;
53
54     if(!i->tempfile) {
55         i->tempfile = strdup(mktempname(0));
56         PDF_open_file(i->p, i->tempfile);
57         PDF_set_parameter(i->p, "usercoordinates", "true");
58         PDF_set_parameter(i->p, "topdown", "true");
59     }
60
61     PDF_begin_page(i->p, width, height);
62     PDF_set_parameter(i->p, "fillrule", "evenodd");
63 }
64
65 static int mkline(gfxline_t*line, PDF*p, char fill)
66 {
67     double x,y;
68     char first = 1;
69     int ret = 0;
70     char free_line = 0;
71     if(fill) {
72         line = gfxline_restitch(gfxline_clone(line));
73         free_line = 1;
74     }
75     while(line) {
76         if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
77             first = 0;
78             PDF_moveto(p, line->x, line->y);
79         } else if(line->type == gfx_lineTo) {
80             PDF_lineto(p, line->x, line->y);
81             ret = 1;
82         } else {
83             /* when converting a quadratic bezier to a cubic bezier, the
84                two new control points are both 2/3 the way from the
85                endpoints to the old control point */
86             double c1x = (x + line->sx*2)/3;
87             double c1y = (y + line->sy*2)/3;
88             double c2x = (line->x + line->sx*2)/3;
89             double c2y = (line->y + line->sy*2)/3;
90             PDF_curveto(p, c1x, c1y, c2x, c2y, line->x, line->y);
91             ret = 1;
92         }
93         x = line->x;
94         y = line->y;
95         line = line->next;
96     }
97     if(free_line)
98         gfxline_free(line);
99     return ret;
100 }
101
102 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
103 {
104     internal_t*i = (internal_t*)dev->internal;
105     PDF_save(i->p);
106     if(mkline(line, i->p, 1))
107         PDF_clip(i->p);
108     else   
109         ; // TODO: strictly speaking, an empty clip clears everything
110
111 }
112 void pdf_endclip(gfxdevice_t*dev)
113 {
114     internal_t*i = (internal_t*)dev->internal;
115     PDF_restore(i->p);
116 }
117 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)
118 {
119     internal_t*i = (internal_t*)dev->internal;
120     if(width<1e-6)
121         return;
122     PDF_setlinewidth(i->p, width);
123     PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
124     PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
125     PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
126     if(joint_style==gfx_joinMiter)
127         PDF_setmiterlimit(i->p, miterLimit);
128     if(mkline(line, i->p, 0))
129         PDF_stroke(i->p);
130 }
131
132 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
133 {
134     internal_t*i = (internal_t*)dev->internal;
135     PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
136         
137     if(mkline(line, i->p, 1)) {
138         PDF_fill(i->p);
139     }
140 }
141
142 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
143 {
144     internal_t*i = (internal_t*)dev->internal;
145
146     double l1 = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01)*img->width;
147     double l2 = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11)*img->height;
148     double r = atan2(matrix->m01, matrix->m00);
149
150     /* fit_image needs the lower left corner of the image */
151     double x = matrix->tx + matrix->m10*img->height;
152     double y = matrix->ty + matrix->m11*img->height;
153
154     char*tempfile = mktempname(0);
155     char options[80];
156     sprintf(options, "boxsize {%f %f} fitmethod meet rotate %f", l1, l2, r*180/M_PI);
157     gfximage_save_jpeg(img, tempfile, 99);
158     int imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
159     PDF_fit_image(i->p, imgid, x, y, options);
160 }
161
162 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
163 {
164     internal_t*i = (internal_t*)dev->internal;
165 }
166
167 static char type3 = 1;
168
169 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
170 {
171     internal_t*i = (internal_t*)dev->internal;
172
173     if(type3) {
174         int fontid = 0;
175         if(!gfxfontlist_hasfont(i->fontlist, font)) {
176
177             static int fontnr = 1;
178             char fontname[32];
179             sprintf(fontname, "font%d", fontnr++);
180             int l = strlen(fontname);
181             char fontname2[64];
182             int t;
183             for(t=0;t<l+1;t++) {
184                 fontname2[t*2+0] = fontname[t];
185                 fontname2[t*2+1] = 0;
186             }
187
188             PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, "");
189             int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
190             for(t=0;t<num;t++) {
191                 gfxglyph_t*g = &font->glyphs[t];
192                 gfxbbox_t bbox = gfxline_getbbox(g->line);
193                 char name[32];
194                 sprintf(name, "chr%d", t+32);
195                 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
196                 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax);
197                 if(mkline(g->line, i->p, 1))
198                     PDF_fill(i->p);
199                 PDF_end_glyph(i->p);
200             }
201             PDF_end_font(i->p);
202             fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
203             
204             i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
205         }
206     }
207 }
208
209 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
210 {
211     internal_t*i = (internal_t*)dev->internal;
212
213     if(!font)
214         return;
215
216     gfxglyph_t*glyph = &font->glyphs[glyphnr];
217     PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
218     char as_shape = 0;
219     if(!type3) as_shape=1;
220     if(glyphnr>256-32) as_shape=1;
221     if(fabs(matrix->m00 + matrix->m11) > 0.01) as_shape=1;
222     if(fabs(fabs(matrix->m01) + fabs(matrix->m10)) > 0.01) as_shape=1;
223     if(fabs(matrix->m00) < 1e-6) as_shape=1;
224
225     if(as_shape) {
226         gfxline_t*line2 = gfxline_clone(glyph->line);
227         gfxline_transform(line2, matrix);
228         if(mkline(line2, i->p, 1)) {
229             PDF_fill(i->p);
230         }
231         gfxline_free(line2);
232     } else {
233         assert(gfxfontlist_hasfont(i->fontlist, font));
234         int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
235         PDF_setfont(i->p, fontid, matrix->m00);
236         char name[32];
237         sprintf(name, "%c\0", glyphnr+32);
238
239         if(fabs(matrix->tx - i->lastx) > 0.001 || matrix->ty != i->lasty) {
240             PDF_show_xy2(i->p, name, strlen(name), matrix->tx, matrix->ty);
241         } else {
242             PDF_show2(i->p, name, strlen(name));
243         }
244
245         i->lastx = matrix->tx + glyph->advance*matrix->m00;
246         i->lasty = matrix->ty;
247     }
248     return;
249 }
250
251 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
252 {
253     internal_t*i = (internal_t*)dev->internal;
254 }
255
256 void pdf_endpage(gfxdevice_t*dev)
257 {
258     internal_t*i = (internal_t*)dev->internal;
259     PDF_end_page(i->p);
260 }
261
262 typedef struct pdfresult_internal {
263     char*tempfile;
264 } pdfresult_internal_t;
265
266 void pdfresult_destroy(gfxresult_t*gfx)
267 {
268     pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
269     free(i->tempfile);
270     free(gfx->internal);gfx->internal = 0;
271     free(gfx);
272 }
273
274 int pdfresult_save(gfxresult_t*gfx, const char*filename)
275 {
276     pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
277     FILE*fi = fopen(i->tempfile, "rb");
278     FILE*fo = fopen(filename, "wb");
279     if(!fo) {
280         perror(filename);
281         return -1;
282     }
283     char buffer[4096];
284     int size = 0;
285     while((size = fread(buffer, 1, 4096, fi))) {
286         fwrite(buffer, 1, size, fo);
287     }
288     fclose(fi);
289     fclose(fo);
290     return 0;
291 }
292
293 void* pdfresult_get(gfxresult_t*gfx, const char*name)
294 {
295     return 0; 
296 }
297
298 gfxresult_t* pdf_finish(gfxdevice_t*dev)
299 {
300     internal_t*i = (internal_t*)dev->internal;
301     
302     PDF_close(i->p);
303     PDF_delete(i->p);
304
305     gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
306     memset(result, 0, sizeof(gfxresult_t));
307     result->save = pdfresult_save;
308     result->get = pdfresult_get;
309     result->destroy = pdfresult_destroy;
310     result->internal = 0;
311     result->internal = malloc(sizeof(pdfresult_internal_t));
312     pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
313     ri->tempfile = i->tempfile;i->tempfile=0;
314     free(dev->internal);dev->internal = 0;i=0;
315     return result;
316 }
317
318 void gfxdevice_pdf_init(gfxdevice_t*dev)
319 {
320     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
321     memset(dev, 0, sizeof(gfxdevice_t));
322
323     dev->name = "pdf";
324
325     dev->internal = i;
326
327     dev->setparameter = pdf_setparameter;
328     dev->startpage = pdf_startpage;
329     dev->startclip = pdf_startclip;
330     dev->endclip = pdf_endclip;
331     dev->stroke = pdf_stroke;
332     dev->fill = pdf_fill;
333     dev->fillbitmap = pdf_fillbitmap;
334     dev->fillgradient = pdf_fillgradient;
335     dev->addfont = pdf_addfont;
336     dev->drawchar = pdf_drawchar;
337     dev->drawlink = pdf_drawlink;
338     dev->endpage = pdf_endpage;
339     dev->finish = pdf_finish;
340
341     i->lastx = -1e38;
342     i->lasty = -1e38;
343     i->p = PDF_new();
344 }
345