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+2*i->config_ypad);
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, "pdf"));
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;
232 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
234 pdf-x (pdf 1.3) doesn't support opacityfill
236 char opacityfill[80];
237 sprintf(opacityfill, "opacityfill %f", color->a/256.0);
238 int gstate = PDF_create_gstate(i->p, opacityfill);
239 PDF_set_gstate(i->p, gstate);
242 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
247 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
249 internal_t*i = (internal_t*)dev->internal;
251 int t,size=img->width*img->height;
253 for(t=0;t<size;t++) {
254 if(img->data[t].a!=255) {
260 double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01);
261 double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11);
262 double l1 = w*img->width;
263 double l2 = h*img->height;
265 double dpi_x = 72.0 / w;
266 double dpi_y = 72.0 / h;
267 double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
268 gfximage_t*rescaled_image = 0;
269 if(i->config_maxdpi && dpi > i->config_maxdpi) {
270 int newwidth = img->width*i->config_maxdpi/dpi;
271 int newheight = img->height*i->config_maxdpi/dpi;
272 rescaled_image = gfximage_rescale(img, newwidth, newheight);
273 msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)",
274 img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
275 img = rescaled_image;
277 if(i->config_mindpi && dpi < i->config_mindpi && img->width>1 && img->height>1) {
278 msg("<error> Found image of size %dx%d with dpi %f, minimum allowed dpi is %d",
279 img->width, img->height, dpi, i->config_mindpi);
284 mktempname(tempfile, "jpg");
286 gfximage_save_jpeg(img, tempfile, 96);
291 mktempname(tempfile2, "jpg");
293 int size = img->width*img->height;
294 unsigned char*alpha = malloc(size);
295 for(t=0;t<size;t++) {
296 alpha[t] = img->data[t].a;
298 jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
300 int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
304 msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
307 sprintf(masked, "masked %d", maskid);
308 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
310 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
314 msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
320 set_matrix(i, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
321 /* an image's (0,0) is at the lower left corner */
322 double x=matrix->tx + i->config_xpad + matrix->m10*img->height;
323 double y=matrix->ty + i->config_ypad + matrix->m11*img->height;
325 transform_back(i, x, y, &tx, &ty);
326 PDF_place_image(i->p, imgid, tx, ty, 1.0);
327 PDF_close_image(i->p, imgid);
330 gfximage_free(rescaled_image);
333 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
335 internal_t*i = (internal_t*)dev->internal;
338 static const char type3 = 0;
339 static const char ttf = 1;
341 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
343 internal_t*i = (internal_t*)dev->internal;
345 int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
348 if(!gfxfontlist_hasfont(i->fontlist, font)) {
350 static int fontnr = 1;
352 sprintf(fontname, "font%d", fontnr++);
353 int l = strlen(fontname);
357 fontname2[t*2+0] = fontname[t];
358 fontname2[t*2+1] = 0;
361 PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, "");
363 gfxglyph_t*g = &font->glyphs[t];
364 gfxbbox_t bbox = gfxline_getbbox(g->line);
366 sprintf(name, "chr%d", t+32);
367 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
368 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin/64.0, bbox.ymin/64.0, bbox.xmax/64.0, bbox.ymax/64.0);
369 if(mkline(g->line, i->p, 0, 0, 1.0/64.0, 1))
374 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
376 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
380 if(!gfxfontlist_hasfont(i->fontlist, font)) {
381 char fontname[32],filename[32],fontname2[64];
382 static int fontnr = 1;
383 sprintf(fontname, "font%d", fontnr);
384 sprintf(filename, "font%d.ttf", fontnr);
386 const char*old_id = font->id;
390 font->glyphs[t].unicode = 32+t;
392 font->max_unicode = 0;
393 font->unicode2glyph = 0;
394 gfxfont_save(font, filename);
398 /* for testing the generated fonts: run everything through ttx (fonttools) */
400 sprintf(cmd, "mv %s.ttf test.ttf", fontname);system(cmd);
401 system("rm -f test.ttx");
402 if(system("ttx test.ttf")&0xff00) exit(1);
403 sprintf(cmd, "mv test.ttf %s.old.ttf", fontname, fontname);system(cmd);
404 sprintf(cmd, "ttx test.ttx;mv test.ttf %s.ttf", fontname);system(cmd);
405 sprintf(cmd, "rm -f test.ttx");system(cmd);
408 int l = strlen(fontname);
410 fontname2[t*2+0] = fontname[t];
411 fontname2[t*2+1] = 0;
414 fontid = PDF_load_font(i->p, fontname2, l*2, "host", "embedding=true");
415 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
421 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
423 internal_t*i = (internal_t*)dev->internal;
428 gfxglyph_t*glyph = &font->glyphs[glyphnr];
430 if(!type3 && !ttf) {msg("<warning> No type3 enabled. Drawing char %d as shape", glyphnr);as_shape=1;}
431 if(glyphnr>256-32) {msg("<warning> Drawing char %d as shape (not < 224)", glyphnr);as_shape=1;}
435 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
436 gfxline_t*line2 = gfxline_clone(glyph->line);
437 gfxline_transform(line2, matrix);
438 if(mkline(line2, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
443 assert(gfxfontlist_hasfont(i->fontlist, font));
444 int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
446 gfxmatrix_t m = *matrix;
457 if(!(fabs(m.m00 - i->m00) < 1e-6 &&
458 fabs(m.m01 - i->m01) < 1e-6 &&
459 fabs(m.m10 - i->m10) < 1e-6 &&
460 fabs(m.m11 - i->m11) < 1e-6)) {
461 set_matrix(i, m.m00, m.m01, m.m10, m.m11);
464 transform_back(i, m.tx+i->config_xpad, m.ty+i->config_ypad, &tx, &ty);
466 PDF_setfont(i->p, fontid, ttf?16.0:1.0);
467 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
470 sprintf(name, "%c", glyphnr+32);
472 if(fabs(tx - i->lastx) > 0.001 || ty != i->lasty) {
473 PDF_show_xy2(i->p, name, strlen(name), tx, ty);
475 PDF_show2(i->p, name, strlen(name));
478 i->lastx = tx + glyph->advance;
483 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
485 internal_t*i = (internal_t*)dev->internal;
488 void pdf_endpage(gfxdevice_t*dev)
490 internal_t*i = (internal_t*)dev->internal;
495 typedef struct pdfresult_internal {
497 } pdfresult_internal_t;
499 void pdfresult_destroy(gfxresult_t*gfx)
501 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
504 free(gfx->internal);gfx->internal = 0;
508 int pdfresult_save(gfxresult_t*gfx, const char*filename)
510 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
511 FILE*fi = fopen(i->tempfile, "rb");
512 FILE*fo = fopen(filename, "wb");
519 while((size = fread(buffer, 1, 4096, fi))) {
520 fwrite(buffer, 1, size, fo);
527 void* pdfresult_get(gfxresult_t*gfx, const char*name)
532 gfxresult_t* pdf_finish(gfxdevice_t*dev)
534 internal_t*i = (internal_t*)dev->internal;
536 if(i->config_addblankpages) {
537 int mod = i->num_pages%i->config_addblankpages;
539 int count = i->config_addblankpages - mod;
541 for(t=0;t<count;t++) {
542 dev->startpage(dev, i->width, i->height);
548 PDF_end_document(i->p, "");
552 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
553 memset(result, 0, sizeof(gfxresult_t));
554 result->save = pdfresult_save;
555 result->get = pdfresult_get;
556 result->destroy = pdfresult_destroy;
557 result->internal = 0;
558 result->internal = malloc(sizeof(pdfresult_internal_t));
559 pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
560 ri->tempfile = i->tempfile;i->tempfile=0;
561 free(dev->internal);dev->internal = 0;i=0;
565 void gfxdevice_pdf_init(gfxdevice_t*dev)
567 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
568 memset(dev, 0, sizeof(gfxdevice_t));
574 dev->setparameter = pdf_setparameter;
575 dev->startpage = pdf_startpage;
576 dev->startclip = pdf_startclip;
577 dev->endclip = pdf_endclip;
578 dev->stroke = pdf_stroke;
579 dev->fill = pdf_fill;
580 dev->fillbitmap = pdf_fillbitmap;
581 dev->fillgradient = pdf_fillgradient;
582 dev->addfont = pdf_addfont;
583 dev->drawchar = pdf_drawchar;
584 dev->drawlink = pdf_drawlink;
585 dev->endpage = pdf_endpage;
586 dev->finish = pdf_finish;