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)
73 static void set_matrix(internal_t*i, double m00, double m01, double m10, double m11)
83 PDF_setmatrix(i->p, m00, -m01, m10, -m11, 0, i->height+2*i->config_ypad);
86 static void reset_matrix(internal_t*i)
88 set_matrix(i, 1.0, 0.0, 0.0, 1.0);
90 static void transform_back(internal_t*i, double x, double y, double *ox, double *oy)
92 double det = i->m00*i->m11 - i->m10*i->m01;
94 msg("<warning> Codependent text matrix");
98 *ox = (x*i->m11 - i->m10*y)/det;
99 *oy = (i->m00*y - x*i->m01)/det;
102 void pdf_startpage(gfxdevice_t*dev, int width, int height)
104 internal_t*i = (internal_t*)dev->internal;
107 i->tempfile = strdup(mktempname(0, "pdf"));
109 PDF_begin_document(i->p, i->tempfile, 0, "");
110 //PDF_set_value(i->p, "compress", 0);
111 PDF_set_parameter(i->p, "usercoordinates", "true");
112 PDF_set_parameter(i->p, "topdown", "true");
115 int width_plus_pad = width+floor(i->config_xpad*2);
116 int height_plus_pad = height+floor(i->config_ypad*2);
117 PDF_begin_page_ext(i->p, width_plus_pad, height_plus_pad, i->page_opts);
118 PDF_set_value(i->p, "CropBox/llx", 0);
119 PDF_set_value(i->p, "CropBox/lly", 0);
120 PDF_set_value(i->p, "CropBox/urx", width_plus_pad);
121 PDF_set_value(i->p, "CropBox/ury", height_plus_pad);
122 if(i->config_xpad || i->config_ypad) {
123 PDF_set_value(i->p, "TrimBox/llx", i->config_xpad);
124 PDF_set_value(i->p, "TrimBox/lly", i->config_ypad);
125 PDF_set_value(i->p, "TrimBox/urx", i->config_xpad+width);
126 PDF_set_value(i->p, "TrimBox/ury", i->config_ypad+height);
129 PDF_set_parameter(i->p, "fillrule", "evenodd");
137 int pdf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
139 internal_t*i = (internal_t*)dev->internal;
140 if(!strcmp(key, "addblankpages")) {
141 i->config_addblankpages = atoi(value);
142 } else if(!strcmp(key, "maxdpi")) {
143 i->config_maxdpi = atoi(value);
144 } else if(!strcmp(key, "mindpi")) {
145 i->config_mindpi = atoi(value);
146 } else if(!strcmp(key, "xpad")) {
147 i->config_xpad = atof(value);
148 } else if(!strcmp(key, "ypad")) {
149 i->config_ypad = atof(value);
155 static int mkline(gfxline_t*line, PDF*p, double mx, double my, double scale, char fill)
160 gfxline_t*free_line = 0;
162 line = gfxline_restitch(gfxline_clone(line));
166 if(line->type == gfx_moveTo && (x!=line->x || y!=line->y || first)) {
168 PDF_moveto(p, line->x*scale+mx, line->y*scale+my);
169 } else if(line->type == gfx_lineTo) {
170 PDF_lineto(p, line->x*scale+mx, line->y*scale+my);
173 /* when converting a quadratic bezier to a cubic bezier, the
174 two new control points are both 2/3 the way from the
175 endpoints to the old control point */
176 double c1x = (x + line->sx*2)/3;
177 double c1y = (y + line->sy*2)/3;
178 double c2x = (line->x + line->sx*2)/3;
179 double c2y = (line->y + line->sy*2)/3;
180 PDF_curveto(p, c1x*scale+mx, c1y*scale+my,
181 c2x*scale+mx, c2y*scale+my,
182 line->x*scale+mx, line->y*scale+my);
190 gfxline_free(free_line);
194 void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
196 internal_t*i = (internal_t*)dev->internal;
201 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1))
204 ; // TODO: strictly speaking, an empty clip clears everything
208 void pdf_endclip(gfxdevice_t*dev)
210 internal_t*i = (internal_t*)dev->internal;
214 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)
216 internal_t*i = (internal_t*)dev->internal;
220 PDF_setlinewidth(i->p, width);
221 PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2));
222 PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2));
224 PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
226 if(joint_style==gfx_joinMiter)
227 PDF_setmiterlimit(i->p, miterLimit);
228 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 0))
232 void pdf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
234 internal_t*i = (internal_t*)dev->internal;
236 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
238 pdf-x (pdf 1.3) doesn't support opacityfill
240 char opacityfill[80];
241 sprintf(opacityfill, "opacityfill %f", color->a/256.0);
242 int gstate = PDF_create_gstate(i->p, opacityfill);
243 PDF_set_gstate(i->p, gstate);
246 if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
251 void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
253 internal_t*i = (internal_t*)dev->internal;
255 int t,size=img->width*img->height;
257 for(t=0;t<size;t++) {
258 if(img->data[t].a!=255) {
264 double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01);
265 double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11);
266 double l1 = w*img->width;
267 double l2 = h*img->height;
269 double dpi_x = 72.0 / w;
270 double dpi_y = 72.0 / h;
271 double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
272 gfximage_t*rescaled_image = 0;
273 if(i->config_maxdpi && dpi > i->config_maxdpi) {
274 int newwidth = img->width*i->config_maxdpi/dpi;
275 int newheight = img->height*i->config_maxdpi/dpi;
276 rescaled_image = gfximage_rescale(img, newwidth, newheight);
277 msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)",
278 img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
279 img = rescaled_image;
281 if(i->config_mindpi && dpi < i->config_mindpi && img->width>1 && img->height>1) {
282 msg("<error> Found image of size %dx%d with dpi %f, minimum allowed dpi is %d",
283 img->width, img->height, dpi, i->config_mindpi);
288 mktempname(tempfile, "jpg");
290 gfximage_save_jpeg(img, tempfile, 96);
295 mktempname(tempfile2, "jpg");
297 int size = img->width*img->height;
298 unsigned char*alpha = malloc(size);
299 for(t=0;t<size;t++) {
300 alpha[t] = img->data[t].a;
302 jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
304 int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
308 msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
311 sprintf(masked, "masked %d", maskid);
312 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
314 imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
318 msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
324 set_matrix(i, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
325 /* an image's (0,0) is at the lower left corner */
326 double x=matrix->tx + i->config_xpad + matrix->m10*img->height;
327 double y=matrix->ty + i->config_ypad + matrix->m11*img->height;
329 transform_back(i, x, y, &tx, &ty);
330 PDF_place_image(i->p, imgid, tx, ty, 1.0);
331 PDF_close_image(i->p, imgid);
334 gfximage_free(rescaled_image);
337 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
339 internal_t*i = (internal_t*)dev->internal;
342 static const char type3 = 0;
343 static const char ttf = 1;
345 void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font)
347 internal_t*i = (internal_t*)dev->internal;
349 int num = font->num_glyphs<256-32?font->num_glyphs:256-32;
352 if(!gfxfontlist_hasfont(i->fontlist, font)) {
354 static int fontnr = 1;
356 sprintf(fontname, "font%d", fontnr++);
357 int l = strlen(fontname);
361 fontname2[t*2+0] = fontname[t];
362 fontname2[t*2+1] = 0;
365 PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, "");
367 gfxglyph_t*g = &font->glyphs[t];
368 gfxbbox_t bbox = gfxline_getbbox(g->line);
370 sprintf(name, "chr%d", t+32);
371 PDF_encoding_set_char(i->p, fontname, t+32, name, 0);
372 PDF_begin_glyph(i->p, name, g->advance, bbox.xmin/64.0, bbox.ymin/64.0, bbox.xmax/64.0, bbox.ymax/64.0);
373 if(mkline(g->line, i->p, 0, 0, 1.0/64.0, 1))
378 fontid = PDF_load_font(i->p, fontname2, l*2, fontname, "");
380 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
384 if(!gfxfontlist_hasfont(i->fontlist, font)) {
385 char fontname[32],filename[32],fontname2[64];
386 static int fontnr = 1;
387 sprintf(fontname, "font%d", fontnr);
388 sprintf(filename, "font%d.ttf", fontnr);
390 const char*old_id = font->id;
394 font->glyphs[t].unicode = 32+t;
396 font->max_unicode = 0;
397 font->unicode2glyph = 0;
398 gfxfont_save(font, filename);
402 /* for testing the generated fonts: run everything through ttx (fonttools) */
404 sprintf(cmd, "mv %s.ttf test.ttf", fontname);system(cmd);
405 system("rm -f test.ttx");
406 if(system("ttx test.ttf")&0xff00) exit(1);
407 sprintf(cmd, "mv test.ttf %s.old.ttf", fontname, fontname);system(cmd);
408 sprintf(cmd, "ttx test.ttx;mv test.ttf %s.ttf", fontname);system(cmd);
409 sprintf(cmd, "rm -f test.ttx");system(cmd);
412 int l = strlen(fontname);
414 fontname2[t*2+0] = fontname[t];
415 fontname2[t*2+1] = 0;
418 fontid = PDF_load_font(i->p, fontname2, l*2, "host", "embedding=true");
419 i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid);
425 void pdf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
427 internal_t*i = (internal_t*)dev->internal;
432 gfxglyph_t*glyph = &font->glyphs[glyphnr];
434 if(!type3 && !ttf) {msg("<warning> No type3 enabled. Drawing char %d as shape", glyphnr);as_shape=1;}
435 if(glyphnr>256-32) {msg("<warning> Drawing char %d as shape (not < 224)", glyphnr);as_shape=1;}
439 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
440 gfxline_t*line2 = gfxline_clone(glyph->line);
441 gfxline_transform(line2, matrix);
442 if(mkline(line2, i->p, i->config_xpad, i->config_ypad, 1.0, 1)) {
447 assert(gfxfontlist_hasfont(i->fontlist, font));
448 int fontid = (int)(ptroff_t)gfxfontlist_getuserdata(i->fontlist, font->id);
450 gfxmatrix_t m = *matrix;
461 if(!(fabs(m.m00 - i->m00) < 1e-6 &&
462 fabs(m.m01 - i->m01) < 1e-6 &&
463 fabs(m.m10 - i->m10) < 1e-6 &&
464 fabs(m.m11 - i->m11) < 1e-6)) {
465 set_matrix(i, m.m00, m.m01, m.m10, m.m11);
468 transform_back(i, m.tx+i->config_xpad, m.ty+i->config_ypad, &tx, &ty);
470 PDF_setfont(i->p, fontid, ttf?16.0:1.0);
471 PDF_setrgbcolor_fill(i->p, color->r/255.0, color->g/255.0, color->b/255.0);
474 sprintf(name, "%c", glyphnr+32);
476 if(fabs(tx - i->lastx) > 0.001 || ty != i->lasty) {
477 PDF_show_xy2(i->p, name, strlen(name), tx, ty);
479 PDF_show2(i->p, name, strlen(name));
482 i->lastx = tx + glyph->advance;
487 void pdf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
489 internal_t*i = (internal_t*)dev->internal;
492 void pdf_endpage(gfxdevice_t*dev)
494 internal_t*i = (internal_t*)dev->internal;
499 typedef struct pdfresult_internal {
501 } pdfresult_internal_t;
503 void pdfresult_destroy(gfxresult_t*gfx)
505 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
508 free(gfx->internal);gfx->internal = 0;
512 int pdfresult_save(gfxresult_t*gfx, const char*filename)
514 pdfresult_internal_t*i = (pdfresult_internal_t*)gfx->internal;
515 FILE*fi = fopen(i->tempfile, "rb");
516 FILE*fo = fopen(filename, "wb");
523 while((size = fread(buffer, 1, 4096, fi))) {
524 fwrite(buffer, 1, size, fo);
531 void* pdfresult_get(gfxresult_t*gfx, const char*name)
536 gfxresult_t* pdf_finish(gfxdevice_t*dev)
538 internal_t*i = (internal_t*)dev->internal;
540 if(i->config_addblankpages) {
541 int mod = i->num_pages%i->config_addblankpages;
543 int count = i->config_addblankpages - mod;
545 for(t=0;t<count;t++) {
546 dev->startpage(dev, i->width, i->height);
552 PDF_end_document(i->p, "");
556 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
557 memset(result, 0, sizeof(gfxresult_t));
558 result->save = pdfresult_save;
559 result->get = pdfresult_get;
560 result->destroy = pdfresult_destroy;
561 result->internal = 0;
562 result->internal = malloc(sizeof(pdfresult_internal_t));
563 pdfresult_internal_t*ri = (pdfresult_internal_t*)result->internal;
564 ri->tempfile = i->tempfile;i->tempfile=0;
565 free(dev->internal);dev->internal = 0;i=0;
569 void gfxdevice_pdf_init(gfxdevice_t*dev)
571 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
572 memset(dev, 0, sizeof(gfxdevice_t));
578 dev->setparameter = pdf_setparameter;
579 dev->startpage = pdf_startpage;
580 dev->startclip = pdf_startclip;
581 dev->endclip = pdf_endclip;
582 dev->stroke = pdf_stroke;
583 dev->fill = pdf_fill;
584 dev->fillbitmap = pdf_fillbitmap;
585 dev->fillgradient = pdf_fillgradient;
586 dev->addfont = pdf_addfont;
587 dev->drawchar = pdf_drawchar;
588 dev->drawlink = pdf_drawlink;
589 dev->endpage = pdf_endpage;
590 dev->finish = pdf_finish;