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