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 set_matrix(internal_t*i, double m00, double m01, double m10, double m11)
74 PDF_setmatrix(i->p, m00, -m01, m10, -m11, 0, i->height);
76 static void reset_matrix(internal_t*i)
78 set_matrix(i, 1.0, 0.0, 0.0, 1.0);
80 static void transform_back(internal_t*i, double x, double y, double *ox, double *oy)
82 double det = i->m00*i->m11 - i->m10*i->m01;
84 msg("<warning> Codependent text matrix");
88 *ox = (x*i->m11 - i->m10*y)/det;
89 *oy = (i->m00*y - x*i->m01)/det;
92 void pdf_startpage(gfxdevice_t*dev, int width, int height)
94 internal_t*i = (internal_t*)dev->internal;
97 i->tempfile = strdup(mktempname(0));
99 PDF_begin_document(i->p, i->tempfile, 0, "");
100 //PDF_set_value(i->p, "compress", 0);
101 PDF_set_parameter(i->p, "usercoordinates", "true");
102 PDF_set_parameter(i->p, "topdown", "true");
105 int width_plus_pad = width+floor(i->config_xpad*2);
106 int height_plus_pad = height+floor(i->config_ypad*2);
107 PDF_begin_page_ext(i->p, width_plus_pad, height_plus_pad, i->page_opts);
108 PDF_set_value(i->p, "CropBox/llx", 0);
109 PDF_set_value(i->p, "CropBox/lly", 0);
110 PDF_set_value(i->p, "CropBox/urx", width_plus_pad);
111 PDF_set_value(i->p, "CropBox/ury", height_plus_pad);
112 if(i->config_xpad || i->config_ypad) {
113 PDF_set_value(i->p, "TrimBox/llx", i->config_xpad);
114 PDF_set_value(i->p, "TrimBox/lly", i->config_ypad);
115 PDF_set_value(i->p, "TrimBox/urx", i->config_xpad+width);
116 PDF_set_value(i->p, "TrimBox/ury", i->config_ypad+height);
119 PDF_set_parameter(i->p, "fillrule", "evenodd");
127 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
129 internal_t*i = (internal_t*)dev->internal;
130 if(!strcmp(key, "addblankpages")) {
131 i->config_addblankpages = atoi(value);
132 } else if(!strcmp(key, "maxdpi")) {
133 i->config_maxdpi = atoi(value);
134 } else if(!strcmp(key, "mindpi")) {
135 i->config_mindpi = atoi(value);
136 } else if(!strcmp(key, "xpad")) {
137 i->config_xpad = atof(value);
138 } else if(!strcmp(key, "ypad")) {
139 i->config_ypad = atof(value);
145 static int mkline(gfxline_t*line, PDF*p, double mx, double my, double scale, char fill)
150 gfxline_t*free_line = 0;
152 line = gfxline_restitch(gfxline_clone(line));
156 if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
158 PDF_moveto(p, line->x*scale+mx, line->y*scale+my);
159 } else if(line->type == gfx_lineTo) {
160 PDF_lineto(p, line->x*scale+mx, line->y*scale+my);
163 /* when converting a quadratic bezier to a cubic bezier, the
164 two new control points are both 2/3 the way from the
165 endpoints to the old control point */
166 double c1x = (x + line->sx*2)/3;
167 double c1y = (y + line->sy*2)/3;
168 double c2x = (line->x + line->sx*2)/3;
169 double c2y = (line->y + line->sy*2)/3;
170 PDF_curveto(p, c1x*scale+mx, c1y*scale+my,
171 c2x*scale+mx, c2y*scale+my,
172 line->x*scale+mx, line->y*scale+my);
180 gfxline_free(free_line);
184 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
186 internal_t*i = (internal_t*)dev->internal;
189 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1))
192 ; // TODO: strictly speaking, an empty clip clears everything
195 void pdf_endclip(gfxdevice_t*dev)
197 internal_t*i = (internal_t*)dev->internal;
200 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)
202 internal_t*i = (internal_t*)dev->internal;
206 PDF_setlinewidth(i->p, width);
207 PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
208 PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
210 PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
212 if(joint_style==gfx_joinMiter)
213 PDF_setmiterlimit(i->p, miterLimit);
214 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 0))
218 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
220 internal_t*i = (internal_t*)dev->internal;
222 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
224 pdf-x (pdf 1.3) doesn't support opacityfill
226 char opacityfill[80];
227 sprintf(opacityfill, "opacityfill %f", color->a/256.0);
228 int gstate = PDF_create_gstate(i->p, opacityfill);
229 PDF_set_gstate(i->p, gstate);
232 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
237 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
239 internal_t*i = (internal_t*)dev->internal;
241 int t,size=img->width*img->height;
243 for(t=0;t<size;t++) {
244 if(img->data[t].a!=255) {
250 double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01);
251 double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11);
252 double l1 = w*img->width;
253 double l2 = h*img->height;
255 double dpi_x = 72.0 / w;
256 double dpi_y = 72.0 / h;
257 double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
258 gfximage_t*rescaled_image = 0;
259 if(i->config_maxdpi && dpi > i->config_maxdpi) {
260 int newwidth = img->width*i->config_maxdpi/dpi;
261 int newheight = img->height*i->config_maxdpi/dpi;
262 rescaled_image = gfximage_rescale(img, newwidth, newheight);
263 msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)",
264 img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
265 img = rescaled_image;
267 if(i->config_mindpi && dpi < i->config_mindpi && img->width>1 && img->height>1) {
268 msg("<error> Found image of size %dx%d with dpi %f, minimum allowed dpi is %d",
269 img->width, img->height, dpi, i->config_mindpi);
274 mktempname(tempfile);
276 gfximage_save_jpeg(img, tempfile, 96);
281 mktempname(tempfile2);
283 int size = img->width*img->height;
284 unsigned char*alpha = malloc(size);
285 for(t=0;t<size;t++) {
286 alpha[t] = img->data[t].a;
288 jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
290 int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
294 msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
297 sprintf(masked, "masked %d", maskid);
298 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
300 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
304 msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
310 set_matrix(i, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
311 /* an image's (0,0) is at the lower left corner */
312 double x=matrix->tx + i->config_xpad + matrix->m10*img->height;
313 double y=matrix->ty + i->config_ypad + matrix->m11*img->height;
315 transform_back(i, x, y, &tx, &ty);
316 PDF_place_image(i->p, imgid, tx, ty, 1.0);
317 PDF_close_image(i->p, imgid);
320 gfximage_free(rescaled_image);
323 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
325 internal_t*i = (internal_t*)dev->internal;
328 static const char type3 = 1;
329 static const char ttf = 0;
331 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
333 internal_t*i = (internal_t*)dev->internal;
337 if(!gfxfontlist_hasfont(i->fontlist, font)) {
339 static int fontnr = 1;
341 sprintf(fontname, "font%d", fontnr++);
342 int l = strlen(fontname);
346 fontname2[t*2+0] = fontname[t];
347 fontname2[t*2+1] = 0;
350 PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, "");
351 int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
353 gfxglyph_t*g = &font->glyphs[t];
354 gfxbbox_t bbox = gfxline_getbbox(g->line);
356 sprintf(name, "chr%d", t+32);
357 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
358 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin/64.0, bbox.ymin/64.0, bbox.xmax/64.0, bbox.ymax/64.0);
359 if(mkline(g->line, i->p, 0, 0, 1.0/64.0, 1))
364 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
366 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
370 if(!gfxfontlist_hasfont(i->fontlist, font)) {
371 char fontname[32],filename[32],fontname2[64];
372 static int fontnr = 1;
373 sprintf(fontname, "font%d", fontnr);
374 sprintf(filename, "font%d.ttf", fontnr);
376 const char*old_id = font->id;
378 gfxfont_save(font, filename);
381 int l = strlen(font->id);
384 fontname2[t*2+0] = fontname[t];
385 fontname2[t*2+1] = 0;
387 fontid = PDF_load_font(i->p, fontname2, l*2, "host", "");
388 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
394 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
396 internal_t*i = (internal_t*)dev->internal;
401 gfxglyph_t*glyph = &font->glyphs[glyphnr];
403 if(!type3 && !ttf) {msg("<warning> No type3 enabled. Drawing char %d as shape", glyphnr);as_shape=1;}
404 if(glyphnr>256-32) {msg("<warning> Drawing char %d as shape (not < 224)", glyphnr);as_shape=1;}
408 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
409 gfxline_t*line2 = gfxline_clone(glyph->line);
410 gfxline_transform(line2, matrix);
411 if(mkline(line2, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
416 assert(gfxfontlist_hasfont(i->fontlist, font));
417 int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
419 gfxmatrix_t m = *matrix;
425 if(!(fabs(m.m00 - i->m00) < 1e-6 &&
426 fabs(m.m01 - i->m01) < 1e-6 &&
427 fabs(m.m10 - i->m10) < 1e-6 &&
428 fabs(m.m11 - i->m11) < 1e-6)) {
429 set_matrix(i, m.m00, m.m01, m.m10, m.m11);
432 transform_back(i, m.tx+i->config_xpad, m.ty+i->config_ypad, &tx, &ty);
434 //PDF_setfont(i->p, fontid, 1.0/64.0); // downscaling is done in glyph itself
435 PDF_setfont(i->p, fontid, 1.0);
436 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
439 sprintf(name, "%c", glyphnr+32);
441 if(fabs(tx - i->lastx) > 0.001 || ty != i->lasty) {
442 PDF_show_xy2(i->p, name, strlen(name), tx, ty);
444 PDF_show2(i->p, name, strlen(name));
447 i->lastx = tx + glyph->advance;
452 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
454 internal_t*i = (internal_t*)dev->internal;
457 void pdf_endpage(gfxdevice_t*dev)
459 internal_t*i = (internal_t*)dev->internal;
461 PDF_restore(i->p); i->has_matrix = 0;
466 typedef struct pdfresult_internal {
468 } pdfresult_internal_t;
470 void pdfresult_destroy(gfxresult_t*gfx)
472 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
475 free(gfx->internal);gfx->internal = 0;
479 int pdfresult_save(gfxresult_t*gfx, const char*filename)
481 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
482 FILE*fi = fopen(i->tempfile, "rb");
483 FILE*fo = fopen(filename, "wb");
490 while((size = fread(buffer, 1, 4096, fi))) {
491 fwrite(buffer, 1, size, fo);
498 void* pdfresult_get(gfxresult_t*gfx, const char*name)
503 gfxresult_t* pdf_finish(gfxdevice_t*dev)
505 internal_t*i = (internal_t*)dev->internal;
507 if(i->config_addblankpages) {
508 int mod = i->num_pages%i->config_addblankpages;
510 int count = i->config_addblankpages - mod;
512 for(t=0;t<count;t++) {
513 dev->startpage(dev, i->width, i->height);
519 PDF_end_document(i->p, "");
523 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
524 memset(result, 0, sizeof(gfxresult_t));
525 result->save = pdfresult_save;
526 result->get = pdfresult_get;
527 result->destroy = pdfresult_destroy;
528 result->internal = 0;
529 result->internal = malloc(sizeof(pdfresult_internal_t));
530 pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
531 ri->tempfile = i->tempfile;i->tempfile=0;
532 free(dev->internal);dev->internal = 0;i=0;
536 void gfxdevice_pdf_init(gfxdevice_t*dev)
538 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
539 memset(dev, 0, sizeof(gfxdevice_t));
545 dev->setparameter = pdf_setparameter;
546 dev->startpage = pdf_startpage;
547 dev->startclip = pdf_startclip;
548 dev->endclip = pdf_endclip;
549 dev->stroke = pdf_stroke;
550 dev->fill = pdf_fill;
551 dev->fillbitmap = pdf_fillbitmap;
552 dev->fillgradient = pdf_fillgradient;
553 dev->addfont = pdf_addfont;
554 dev->drawchar = pdf_drawchar;
555 dev->drawlink = pdf_drawlink;
556 dev->endpage = pdf_endpage;
557 dev->finish = pdf_finish;