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 */
36 #include "../gfxdevice.h"
37 #include "../gfxtools.h"
38 #include "../gfximage.h"
39 #include "../gfxfont.h"
41 typedef struct _internal {
44 char config_addblankpages;
56 gfxfontlist_t*fontlist;
59 double m00, m01, m10, m11;
62 static void restore_matrix(internal_t*i)
69 static void set_matrix(internal_t*i, double m00, double m01, double m10, double m11)
79 PDF_setmatrix(i->p, m00, -m01, m10, -m11, 0, i->height);
82 static void reset_matrix(internal_t*i)
84 set_matrix(i, 1.0, 0.0, 0.0, 1.0);
86 static void transform_back(internal_t*i, double x, double y, double *ox, double *oy)
88 double det = i->m00*i->m11 - i->m10*i->m01;
90 msg("<warning> Codependent text matrix");
94 *ox = (x*i->m11 - i->m10*y)/det;
95 *oy = (i->m00*y - x*i->m01)/det;
98 void pdf_startpage(gfxdevice_t*dev, int width, int height)
100 internal_t*i = (internal_t*)dev->internal;
103 i->tempfile = strdup(mktempname(0));
105 PDF_begin_document(i->p, i->tempfile, 0, "");
106 //PDF_set_value(i->p, "compress", 0);
107 PDF_set_parameter(i->p, "usercoordinates", "true");
108 PDF_set_parameter(i->p, "topdown", "true");
111 int width_plus_pad = width+floor(i->config_xpad*2);
112 int height_plus_pad = height+floor(i->config_ypad*2);
113 PDF_begin_page_ext(i->p, width_plus_pad, height_plus_pad, i->page_opts);
114 PDF_set_value(i->p, "CropBox/llx", 0);
115 PDF_set_value(i->p, "CropBox/lly", 0);
116 PDF_set_value(i->p, "CropBox/urx", width_plus_pad);
117 PDF_set_value(i->p, "CropBox/ury", height_plus_pad);
118 if(i->config_xpad || i->config_ypad) {
119 PDF_set_value(i->p, "TrimBox/llx", i->config_xpad);
120 PDF_set_value(i->p, "TrimBox/lly", i->config_ypad);
121 PDF_set_value(i->p, "TrimBox/urx", i->config_xpad+width);
122 PDF_set_value(i->p, "TrimBox/ury", i->config_ypad+height);
125 PDF_set_parameter(i->p, "fillrule", "evenodd");
133 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
135 internal_t*i = (internal_t*)dev->internal;
136 if(!strcmp(key, "addblankpages")) {
137 i->config_addblankpages = atoi(value);
138 } else if(!strcmp(key, "maxdpi")) {
139 i->config_maxdpi = atoi(value);
140 } else if(!strcmp(key, "mindpi")) {
141 i->config_mindpi = atoi(value);
142 } else if(!strcmp(key, "xpad")) {
143 i->config_xpad = atof(value);
144 } else if(!strcmp(key, "ypad")) {
145 i->config_ypad = atof(value);
151 static int mkline(gfxline_t*line, PDF*p, double mx, double my, double scale, char fill)
156 gfxline_t*free_line = 0;
158 line = gfxline_restitch(gfxline_clone(line));
162 if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
164 PDF_moveto(p, line->x*scale+mx, line->y*scale+my);
165 } else if(line->type == gfx_lineTo) {
166 PDF_lineto(p, line->x*scale+mx, line->y*scale+my);
169 /* when converting a quadratic bezier to a cubic bezier, the
170 two new control points are both 2/3 the way from the
171 endpoints to the old control point */
172 double c1x = (x + line->sx*2)/3;
173 double c1y = (y + line->sy*2)/3;
174 double c2x = (line->x + line->sx*2)/3;
175 double c2y = (line->y + line->sy*2)/3;
176 PDF_curveto(p, c1x*scale+mx, c1y*scale+my,
177 c2x*scale+mx, c2y*scale+my,
178 line->x*scale+mx, line->y*scale+my);
186 gfxline_free(free_line);
190 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
192 internal_t*i = (internal_t*)dev->internal;
197 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1))
200 ; // TODO: strictly speaking, an empty clip clears everything
204 void pdf_endclip(gfxdevice_t*dev)
206 internal_t*i = (internal_t*)dev->internal;
210 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)
212 internal_t*i = (internal_t*)dev->internal;
216 PDF_setlinewidth(i->p, width);
217 PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
218 PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
220 PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
222 if(joint_style==gfx_joinMiter)
223 PDF_setmiterlimit(i->p, miterLimit);
224 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 0))
228 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
230 internal_t*i = (internal_t*)dev->internal;
231 PDF_setrgbcolor_stroke(i->p, 0.1,0.2,0.3);
233 PDF_setrgbcolor_stroke(i->p, 0.2,0.3,0.4);
234 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
236 pdf-x (pdf 1.3) doesn't support opacityfill
238 char opacityfill[80];
239 sprintf(opacityfill, "opacityfill %f", color->a/256.0);
240 int gstate = PDF_create_gstate(i->p, opacityfill);
241 PDF_set_gstate(i->p, gstate);
244 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
249 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
251 internal_t*i = (internal_t*)dev->internal;
253 int t,size=img->width*img->height;
255 for(t=0;t<size;t++) {
256 if(img->data[t].a!=255) {
262 double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01);
263 double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11);
264 double l1 = w*img->width;
265 double l2 = h*img->height;
267 double dpi_x = 72.0 / w;
268 double dpi_y = 72.0 / h;
269 double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
270 gfximage_t*rescaled_image = 0;
271 if(i->config_maxdpi && dpi > i->config_maxdpi) {
272 int newwidth = img->width*i->config_maxdpi/dpi;
273 int newheight = img->height*i->config_maxdpi/dpi;
274 rescaled_image = gfximage_rescale(img, newwidth, newheight);
275 msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)",
276 img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
277 img = rescaled_image;
279 if(i->config_mindpi && dpi < i->config_mindpi && img->width>1 && img->height>1) {
280 msg("<error> Found image of size %dx%d with dpi %f, minimum allowed dpi is %d",
281 img->width, img->height, dpi, i->config_mindpi);
286 mktempname(tempfile);
288 gfximage_save_jpeg(img, tempfile, 96);
293 mktempname(tempfile2);
295 int size = img->width*img->height;
296 unsigned char*alpha = malloc(size);
297 for(t=0;t<size;t++) {
298 alpha[t] = img->data[t].a;
300 jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
302 int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
306 msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
309 sprintf(masked, "masked %d", maskid);
310 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
312 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
316 msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
322 set_matrix(i, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
323 /* an image's (0,0) is at the lower left corner */
324 double x=matrix->tx + i->config_xpad + matrix->m10*img->height;
325 double y=matrix->ty + i->config_ypad + matrix->m11*img->height;
327 transform_back(i, x, y, &tx, &ty);
328 PDF_place_image(i->p, imgid, tx, ty, 1.0);
329 PDF_close_image(i->p, imgid);
332 gfximage_free(rescaled_image);
335 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
337 internal_t*i = (internal_t*)dev->internal;
340 static const char type3 = 1;
341 static const char ttf = 0;
343 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
345 internal_t*i = (internal_t*)dev->internal;
349 if(!gfxfontlist_hasfont(i->fontlist, font)) {
351 static int fontnr = 1;
353 sprintf(fontname, "font%d", fontnr++);
354 int l = strlen(fontname);
358 fontname2[t*2+0] = fontname[t];
359 fontname2[t*2+1] = 0;
362 PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, "");
363 int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
365 gfxglyph_t*g = &font->glyphs[t];
366 gfxbbox_t bbox = gfxline_getbbox(g->line);
368 sprintf(name, "chr%d", t+32);
369 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
370 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin/64.0, bbox.ymin/64.0, bbox.xmax/64.0, bbox.ymax/64.0);
371 if(mkline(g->line, i->p, 0, 0, 1.0/64.0, 1))
376 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
378 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
382 if(!gfxfontlist_hasfont(i->fontlist, font)) {
383 char fontname[32],filename[32],fontname2[64];
384 static int fontnr = 1;
385 sprintf(fontname, "font%d", fontnr);
386 sprintf(filename, "font%d.ttf", fontnr);
388 const char*old_id = font->id;
390 gfxfont_save(font, filename);
393 int l = strlen(font->id);
396 fontname2[t*2+0] = fontname[t];
397 fontname2[t*2+1] = 0;
399 fontid = PDF_load_font(i->p, fontname2, l*2, "host", "");
400 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
406 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
408 internal_t*i = (internal_t*)dev->internal;
413 gfxglyph_t*glyph = &font->glyphs[glyphnr];
415 if(!type3 && !ttf) {msg("<warning> No type3 enabled. Drawing char %d as shape", glyphnr);as_shape=1;}
416 if(glyphnr>256-32) {msg("<warning> Drawing char %d as shape (not < 224)", glyphnr);as_shape=1;}
420 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
421 gfxline_t*line2 = gfxline_clone(glyph->line);
422 gfxline_transform(line2, matrix);
423 if(mkline(line2, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
428 assert(gfxfontlist_hasfont(i->fontlist, font));
429 int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
431 gfxmatrix_t m = *matrix;
437 if(!(fabs(m.m00 - i->m00) < 1e-6 &&
438 fabs(m.m01 - i->m01) < 1e-6 &&
439 fabs(m.m10 - i->m10) < 1e-6 &&
440 fabs(m.m11 - i->m11) < 1e-6)) {
441 set_matrix(i, m.m00, m.m01, m.m10, m.m11);
444 transform_back(i, m.tx+i->config_xpad, m.ty+i->config_ypad, &tx, &ty);
446 //PDF_setfont(i->p, fontid, 1.0/64.0); // downscaling is done in glyph itself
447 PDF_setfont(i->p, fontid, 1.0);
448 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
451 sprintf(name, "%c", glyphnr+32);
453 if(fabs(tx - i->lastx) > 0.001 || ty != i->lasty) {
454 PDF_show_xy2(i->p, name, strlen(name), tx, ty);
456 PDF_show2(i->p, name, strlen(name));
459 i->lastx = tx + glyph->advance;
464 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
466 internal_t*i = (internal_t*)dev->internal;
469 void pdf_endpage(gfxdevice_t*dev)
471 internal_t*i = (internal_t*)dev->internal;
476 typedef struct pdfresult_internal {
478 } pdfresult_internal_t;
480 void pdfresult_destroy(gfxresult_t*gfx)
482 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
485 free(gfx->internal);gfx->internal = 0;
489 int pdfresult_save(gfxresult_t*gfx, const char*filename)
491 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
492 FILE*fi = fopen(i->tempfile, "rb");
493 FILE*fo = fopen(filename, "wb");
500 while((size = fread(buffer, 1, 4096, fi))) {
501 fwrite(buffer, 1, size, fo);
508 void* pdfresult_get(gfxresult_t*gfx, const char*name)
513 gfxresult_t* pdf_finish(gfxdevice_t*dev)
515 internal_t*i = (internal_t*)dev->internal;
517 if(i->config_addblankpages) {
518 int mod = i->num_pages%i->config_addblankpages;
520 int count = i->config_addblankpages - mod;
522 for(t=0;t<count;t++) {
523 dev->startpage(dev, i->width, i->height);
529 PDF_end_document(i->p, "");
533 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
534 memset(result, 0, sizeof(gfxresult_t));
535 result->save = pdfresult_save;
536 result->get = pdfresult_get;
537 result->destroy = pdfresult_destroy;
538 result->internal = 0;
539 result->internal = malloc(sizeof(pdfresult_internal_t));
540 pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
541 ri->tempfile = i->tempfile;i->tempfile=0;
542 free(dev->internal);dev->internal = 0;i=0;
546 void gfxdevice_pdf_init(gfxdevice_t*dev)
548 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
549 memset(dev, 0, sizeof(gfxdevice_t));
555 dev->setparameter = pdf_setparameter;
556 dev->startpage = pdf_startpage;
557 dev->startclip = pdf_startclip;
558 dev->endclip = pdf_endclip;
559 dev->stroke = pdf_stroke;
560 dev->fill = pdf_fill;
561 dev->fillbitmap = pdf_fillbitmap;
562 dev->fillgradient = pdf_fillgradient;
563 dev->addfont = pdf_addfont;
564 dev->drawchar = pdf_drawchar;
565 dev->drawlink = pdf_drawlink;
566 dev->endpage = pdf_endpage;
567 dev->finish = pdf_finish;