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"
37 typedef struct _internal {
41 gfxfontlist_t*fontlist;
44 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
46 internal_t*i = (internal_t*)dev->internal;
50 void pdf_startpage(gfxdevice_t*dev, int width, int height)
52 internal_t*i = (internal_t*)dev->internal;
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");
61 PDF_begin_page(i->p, width, height);
62 PDF_set_parameter(i->p, "fillrule", "evenodd");
65 static int mkline(gfxline_t*line, PDF*p, char fill)
72 line = gfxline_restitch(gfxline_clone(line));
76 if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
78 PDF_moveto(p, line->x, line->y);
79 } else if(line->type == gfx_lineTo) {
80 PDF_lineto(p, line->x, line->y);
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);
102 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
104 internal_t*i = (internal_t*)dev->internal;
106 if(mkline(line, i->p, 1))
109 ; // TODO: strictly speaking, an empty clip clears everything
112 void pdf_endclip(gfxdevice_t*dev)
114 internal_t*i = (internal_t*)dev->internal;
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)
119 internal_t*i = (internal_t*)dev->internal;
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))
132 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
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);
137 if(mkline(line, i->p, 1)) {
142 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
144 internal_t*i = (internal_t*)dev->internal;
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);
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;
154 char*tempfile = mktempname(0);
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);
162 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
164 internal_t*i = (internal_t*)dev->internal;
167 static char type3 = 1;
169 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
171 internal_t*i = (internal_t*)dev->internal;
175 if(!gfxfontlist_hasfont(i->fontlist, font)) {
177 static int fontnr = 1;
179 sprintf(fontname, "font%d", fontnr++);
180 int l = strlen(fontname);
184 fontname2[t*2+0] = fontname[t];
185 fontname2[t*2+1] = 0;
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;
191 gfxglyph_t*g = &font->glyphs[t];
192 gfxbbox_t bbox = gfxline_getbbox(g->line);
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))
202 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
204 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
209 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
211 internal_t*i = (internal_t*)dev->internal;
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);
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;
226 gfxline_t*line2 = gfxline_clone(glyph->line);
227 gfxline_transform(line2, matrix);
228 if(mkline(line2, i->p, 1)) {
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);
237 sprintf(name, "%c\0", glyphnr+32);
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);
242 PDF_show2(i->p, name, strlen(name));
245 i->lastx = matrix->tx + glyph->advance*matrix->m00;
246 i->lasty = matrix->ty;
251 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
253 internal_t*i = (internal_t*)dev->internal;
256 void pdf_endpage(gfxdevice_t*dev)
258 internal_t*i = (internal_t*)dev->internal;
262 typedef struct pdfresult_internal {
264 } pdfresult_internal_t;
266 void pdfresult_destroy(gfxresult_t*gfx)
268 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
270 free(gfx->internal);gfx->internal = 0;
274 int pdfresult_save(gfxresult_t*gfx, const char*filename)
276 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
277 FILE*fi = fopen(i->tempfile, "rb");
278 FILE*fo = fopen(filename, "wb");
285 while((size = fread(buffer, 1, 4096, fi))) {
286 fwrite(buffer, 1, size, fo);
293 void* pdfresult_get(gfxresult_t*gfx, const char*name)
298 gfxresult_t* pdf_finish(gfxdevice_t*dev)
300 internal_t*i = (internal_t*)dev->internal;
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;
318 void gfxdevice_pdf_init(gfxdevice_t*dev)
320 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
321 memset(dev, 0, sizeof(gfxdevice_t));
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;