added some basic transparency support to pdf2pdf
[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 "../log.h"
35 #include "../gfxdevice.h"
36 #include "../gfxtools.h"
37 #include "../gfximage.h"
38
39 typedef struct _internal {
40     PDF* p;
41     
42     const char*config_pdfx;
43     char config_addblankpages;
44     double config_xpad;
45     double config_ypad;
46     int config_maxdpi;
47
48     int width,height;
49     int num_pages;
50
51     char*tempfile;
52     char*page_opts;
53     double lastx,lasty;
54     gfxfontlist_t*fontlist;
55 } internal_t;
56
57 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
58 {
59     internal_t*i = (internal_t*)dev->internal;
60     return 0;
61 }
62
63 void pdf_startpage(gfxdevice_t*dev, int width, int height)
64 {
65     internal_t*i = (internal_t*)dev->internal;
66
67     if(!i->tempfile) {
68         i->tempfile = strdup(mktempname(0));
69         PDF_open_file(i->p, i->tempfile);
70         PDF_set_parameter(i->p, "usercoordinates", "true");
71         PDF_set_parameter(i->p, "topdown", "true");
72     }
73
74     PDF_begin_page(i->p, width, height);
75     PDF_set_parameter(i->p, "fillrule", "evenodd");
76 }
77
78 static int mkline(gfxline_t*line, PDF*p, char fill)
79 {
80     double x,y;
81     char first = 1;
82     int ret = 0;
83     char free_line = 0;
84     if(fill) {
85         line = gfxline_restitch(gfxline_clone(line));
86         free_line = 1;
87     }
88     while(line) {
89         if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
90             first = 0;
91             PDF_moveto(p, line->x, line->y);
92         } else if(line->type == gfx_lineTo) {
93             PDF_lineto(p, line->x, line->y);
94             ret = 1;
95         } else {
96             /* when converting a quadratic bezier to a cubic bezier, the
97                two new control points are both 2/3 the way from the
98                endpoints to the old control point */
99             double c1x = (x + line->sx*2)/3;
100             double c1y = (y + line->sy*2)/3;
101             double c2x = (line->x + line->sx*2)/3;
102             double c2y = (line->y + line->sy*2)/3;
103             PDF_curveto(p, c1x, c1y, c2x, c2y, line->x, line->y);
104             ret = 1;
105         }
106         x = line->x;
107         y = line->y;
108         line = line->next;
109     }
110     if(free_line)
111         gfxline_free(line);
112     return ret;
113 }
114
115 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
116 {
117     internal_t*i = (internal_t*)dev->internal;
118     PDF_save(i->p);
119     if(mkline(line, i->p, 1))
120         PDF_clip(i->p);
121     else   
122         ; // TODO: strictly speaking, an empty clip clears everything
123
124 }
125 void pdf_endclip(gfxdevice_t*dev)
126 {
127     internal_t*i = (internal_t*)dev->internal;
128     PDF_restore(i->p);
129 }
130 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)
131 {
132     internal_t*i = (internal_t*)dev->internal;
133     if(width<1e-6)
134         return;
135     PDF_setlinewidth(i->p, width);
136     PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
137     PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
138     PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
139     if(joint_style==gfx_joinMiter)
140         PDF_setmiterlimit(i->p, miterLimit);
141     if(mkline(line, i->p, 0))
142         PDF_stroke(i->p);
143 }
144
145 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
146 {
147     internal_t*i = (internal_t*)dev->internal;
148     PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
149     if(color->a!=255) {
150         char opacityfill[80];
151         sprintf(opacityfill, "opacityfill %f", color->a/256.0);
152         int gstate = PDF_create_gstate(i->p, opacityfill);
153         PDF_set_gstate(i->p, gstate);
154     }
155         
156     if(mkline(line, i->p, 1)) {
157         PDF_fill(i->p);
158     }
159 }
160
161 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
162 {
163     internal_t*i = (internal_t*)dev->internal;
164
165     int t,size=img->width*img->height;
166     int has_alpha=0;
167     for(t=0;t<size;t++) {
168         if(img->data[t].a!=255) {
169             has_alpha=1;
170             break;
171         }
172     }
173
174     double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01);
175     double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11);
176     double l1 = w*img->width;
177     double l2 = h*img->height;
178     double r = atan2(matrix->m01, matrix->m00);
179
180     /* fit_image needs the lower left corner of the image */
181     double x = matrix->tx + matrix->m10*img->height;
182     double y = matrix->ty + matrix->m11*img->height;
183
184     double dpi_x = 72.0 / w;
185     double dpi_y = 72.0 / h;
186     double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
187     gfximage_t*rescaled_image = 0;
188     if(i->config_maxdpi && dpi > i->config_maxdpi) {
189         int newwidth = img->width*i->config_maxdpi/dpi;
190         int newheight = img->height*i->config_maxdpi/dpi;
191         rescaled_image = gfximage_rescale(img, newwidth, newheight);
192         msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)", 
193                 img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
194         img = rescaled_image;
195     }
196
197     char tempfile[128];
198     mktempname(tempfile);
199
200     gfximage_save_jpeg(img, tempfile, 96);
201
202     int imgid=-1;
203     if(has_alpha) {
204         char tempfile2[128];
205         mktempname(tempfile2);
206         int t;
207         int size = img->width*img->height;
208         unsigned char*alpha = malloc(size);
209         for(t=0;t<size;t++) {
210             alpha[t] = img->data[t].a;
211         }
212         jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
213         free(alpha);
214         int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
215         unlink(tempfile2);
216         char masked[80];
217         if(maskid<0) {
218             msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
219             return;
220         }
221         sprintf(masked, "masked %d", maskid);
222         imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
223     } else {
224         imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
225     }
226
227     if(imgid<0) {
228         msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
229         return;
230     }
231     unlink(tempfile);
232     
233     char options[80];
234     sprintf(options, "boxsize {%f %f} fitmethod meet rotate %f", l1, l2, r*180/M_PI);
235     PDF_fit_image(i->p, imgid, x, y, options);
236     PDF_close_image(i->p, imgid);
237
238     if(rescaled_image)
239         gfximage_free(rescaled_image);
240 }
241
242 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
243 {
244     internal_t*i = (internal_t*)dev->internal;
245 }
246
247 static char type3 = 1;
248
249 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
250 {
251     internal_t*i = (internal_t*)dev->internal;
252
253     if(type3) {
254         int fontid = 0;
255         if(!gfxfontlist_hasfont(i->fontlist, font)) {
256
257             static int fontnr = 1;
258             char fontname[32];
259             sprintf(fontname, "font%d", fontnr++);
260             int l = strlen(fontname);
261             char fontname2[64];
262             int t;
263             for(t=0;t<l+1;t++) {
264                 fontname2[t*2+0] = fontname[t];
265                 fontname2[t*2+1] = 0;
266             }
267
268             PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, "");
269             int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
270             for(t=0;t<num;t++) {
271                 gfxglyph_t*g = &font->glyphs[t];
272                 gfxbbox_t bbox = gfxline_getbbox(g->line);
273                 char name[32];
274                 sprintf(name, "chr%d", t+32);
275                 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
276                 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax);
277                 if(mkline(g->line, i->p, 1))
278                     PDF_fill(i->p);
279                 PDF_end_glyph(i->p);
280             }
281             PDF_end_font(i->p);
282             fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
283             
284             i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
285         }
286     }
287 }
288
289 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
290 {
291     internal_t*i = (internal_t*)dev->internal;
292
293     if(!font)
294         return;
295
296     gfxglyph_t*glyph = &font->glyphs[glyphnr];
297     PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
298     char as_shape = 0;
299     if(!type3) as_shape=1;
300     if(glyphnr>256-32) as_shape=1;
301     if(fabs(matrix->m00 + matrix->m11) > 0.01) as_shape=1;
302     if(fabs(fabs(matrix->m01) + fabs(matrix->m10)) > 0.01) as_shape=1;
303     if(fabs(matrix->m00) < 1e-6) as_shape=1;
304
305     if(as_shape) {
306         gfxline_t*line2 = gfxline_clone(glyph->line);
307         gfxline_transform(line2, matrix);
308         if(mkline(line2, i->p, 1)) {
309             PDF_fill(i->p);
310         }
311         gfxline_free(line2);
312     } else {
313         assert(gfxfontlist_hasfont(i->fontlist, font));
314         int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
315         PDF_setfont(i->p, fontid, matrix->m00);
316         char name[32];
317         sprintf(name, "%c", glyphnr+32);
318
319         if(fabs(matrix->tx - i->lastx) > 0.001 || matrix->ty != i->lasty) {
320             PDF_show_xy2(i->p, name, strlen(name), matrix->tx, matrix->ty);
321         } else {
322             PDF_show2(i->p, name, strlen(name));
323         }
324
325         i->lastx = matrix->tx + glyph->advance*matrix->m00;
326         i->lasty = matrix->ty;
327     }
328     return;
329 }
330
331 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
332 {
333     internal_t*i = (internal_t*)dev->internal;
334 }
335
336 void pdf_endpage(gfxdevice_t*dev)
337 {
338     internal_t*i = (internal_t*)dev->internal;
339     PDF_end_page(i->p);
340 }
341
342 typedef struct pdfresult_internal {
343     char*tempfile;
344 } pdfresult_internal_t;
345
346 void pdfresult_destroy(gfxresult_t*gfx)
347 {
348     pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
349     unlink(i->tempfile);
350     free(i->tempfile);
351     free(gfx->internal);gfx->internal = 0;
352     free(gfx);
353 }
354
355 int pdfresult_save(gfxresult_t*gfx, const char*filename)
356 {
357     pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
358     FILE*fi = fopen(i->tempfile, "rb");
359     FILE*fo = fopen(filename, "wb");
360     if(!fo) {
361         perror(filename);
362         return -1;
363     }
364     char buffer[4096];
365     int size = 0;
366     while((size = fread(buffer, 1, 4096, fi))) {
367         fwrite(buffer, 1, size, fo);
368     }
369     fclose(fi);
370     fclose(fo);
371     return 0;
372 }
373
374 void* pdfresult_get(gfxresult_t*gfx, const char*name)
375 {
376     return 0; 
377 }
378
379 gfxresult_t* pdf_finish(gfxdevice_t*dev)
380 {
381     internal_t*i = (internal_t*)dev->internal;
382     
383     PDF_close(i->p);
384     PDF_delete(i->p);
385
386     gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
387     memset(result, 0, sizeof(gfxresult_t));
388     result->save = pdfresult_save;
389     result->get = pdfresult_get;
390     result->destroy = pdfresult_destroy;
391     result->internal = 0;
392     result->internal = malloc(sizeof(pdfresult_internal_t));
393     pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
394     ri->tempfile = i->tempfile;i->tempfile=0;
395     free(dev->internal);dev->internal = 0;i=0;
396     return result;
397 }
398
399 void gfxdevice_pdf_init(gfxdevice_t*dev)
400 {
401     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
402     memset(dev, 0, sizeof(gfxdevice_t));
403
404     dev->name = "pdf";
405
406     dev->internal = i;
407
408     dev->setparameter = pdf_setparameter;
409     dev->startpage = pdf_startpage;
410     dev->startclip = pdf_startclip;
411     dev->endclip = pdf_endclip;
412     dev->stroke = pdf_stroke;
413     dev->fill = pdf_fill;
414     dev->fillbitmap = pdf_fillbitmap;
415     dev->fillgradient = pdf_fillgradient;
416     dev->addfont = pdf_addfont;
417     dev->drawchar = pdf_drawchar;
418     dev->drawlink = pdf_drawlink;
419     dev->endpage = pdf_endpage;
420     dev->finish = pdf_finish;
421
422     i->lastx = -1e38;
423     i->lasty = -1e38;
424     i->p = PDF_new();
425 }
426