3 Part of the swftools package.
5 Copyright (c) 2007 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
34 #include "../gfxdevice.h"
35 #include "../gfxtools.h"
36 #include "../gfximage.h"
38 typedef struct _internal {
42 gfxfontlist_t*fontlist;
45 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
47 internal_t*i = (internal_t*)dev->internal;
51 void pdf_startpage(gfxdevice_t*dev, int width, int height)
53 internal_t*i = (internal_t*)dev->internal;
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");
62 PDF_begin_page(i->p, width, height);
63 PDF_set_parameter(i->p, "fillrule", "evenodd");
66 static int mkline(gfxline_t*line, PDF*p, char fill)
73 line = gfxline_restitch(gfxline_clone(line));
77 if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
79 PDF_moveto(p, line->x, line->y);
80 } else if(line->type == gfx_lineTo) {
81 PDF_lineto(p, line->x, line->y);
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);
103 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
105 internal_t*i = (internal_t*)dev->internal;
107 if(mkline(line, i->p, 1))
110 ; // TODO: strictly speaking, an empty clip clears everything
113 void pdf_endclip(gfxdevice_t*dev)
115 internal_t*i = (internal_t*)dev->internal;
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)
120 internal_t*i = (internal_t*)dev->internal;
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))
133 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
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);
138 if(mkline(line, i->p, 1)) {
143 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
145 internal_t*i = (internal_t*)dev->internal;
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);
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;
155 char*tempfile = mktempname(0);
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);
163 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
165 internal_t*i = (internal_t*)dev->internal;
168 static char type3 = 1;
170 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
172 internal_t*i = (internal_t*)dev->internal;
176 if(!gfxfontlist_hasfont(i->fontlist, font)) {
178 static int fontnr = 1;
180 sprintf(fontname, "font%d", fontnr++);
181 int l = strlen(fontname);
185 fontname2[t*2+0] = fontname[t];
186 fontname2[t*2+1] = 0;
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;
192 gfxglyph_t*g = &font->glyphs[t];
193 gfxbbox_t bbox = gfxline_getbbox(g->line);
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))
203 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
205 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
210 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
212 internal_t*i = (internal_t*)dev->internal;
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);
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;
227 gfxline_t*line2 = gfxline_clone(glyph->line);
228 gfxline_transform(line2, matrix);
229 if(mkline(line2, i->p, 1)) {
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);
238 sprintf(name, "%c", glyphnr+32);
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);
243 PDF_show2(i->p, name, strlen(name));
246 i->lastx = matrix->tx + glyph->advance*matrix->m00;
247 i->lasty = matrix->ty;
252 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
254 internal_t*i = (internal_t*)dev->internal;
257 void pdf_endpage(gfxdevice_t*dev)
259 internal_t*i = (internal_t*)dev->internal;
263 typedef struct pdfresult_internal {
265 } pdfresult_internal_t;
267 void pdfresult_destroy(gfxresult_t*gfx)
269 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
271 free(gfx->internal);gfx->internal = 0;
275 int pdfresult_save(gfxresult_t*gfx, const char*filename)
277 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
278 FILE*fi = fopen(i->tempfile, "rb");
279 FILE*fo = fopen(filename, "wb");
286 while((size = fread(buffer, 1, 4096, fi))) {
287 fwrite(buffer, 1, size, fo);
294 void* pdfresult_get(gfxresult_t*gfx, const char*name)
299 gfxresult_t* pdf_finish(gfxdevice_t*dev)
301 internal_t*i = (internal_t*)dev->internal;
306 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
307 memset(result, 0, sizeof(gfxresult_t));
308 result->save = pdfresult_save;
309 result->get = pdfresult_get;
310 result->destroy = pdfresult_destroy;
311 result->internal = 0;
312 result->internal = malloc(sizeof(pdfresult_internal_t));
313 pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
314 ri->tempfile = i->tempfile;i->tempfile=0;
315 free(dev->internal);dev->internal = 0;i=0;
319 void gfxdevice_pdf_init(gfxdevice_t*dev)
321 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
322 memset(dev, 0, sizeof(gfxdevice_t));
328 dev->setparameter = pdf_setparameter;
329 dev->startpage = pdf_startpage;
330 dev->startclip = pdf_startclip;
331 dev->endclip = pdf_endclip;
332 dev->stroke = pdf_stroke;
333 dev->fill = pdf_fill;
334 dev->fillbitmap = pdf_fillbitmap;
335 dev->fillgradient = pdf_fillgradient;
336 dev->addfont = pdf_addfont;
337 dev->drawchar = pdf_drawchar;
338 dev->drawlink = pdf_drawlink;
339 dev->endpage = pdf_endpage;
340 dev->finish = pdf_finish;