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