5 #define PANGO_ENABLE_BACKEND /* to access PangoFcFont.font_pattern */
8 #include <libgnomecanvas/libgnomecanvas.h>
12 #include <pango/pango.h>
13 #include <pango/pangofc-font.h>
14 #include <pango/pangoft2.h>
15 #include <fontconfig/fontconfig.h>
17 #include FT_FREETYPE_H
22 #include "ttsubset/sft.h"
25 #include "xo-support.h"
31 #define RGBA_RED(rgba) (((rgba>>24)&0xff)/255.0)
32 #define RGBA_GREEN(rgba) (((rgba>>16)&0xff)/255.0)
33 #define RGBA_BLUE(rgba) (((rgba>>8)&0xff)/255.0)
34 #define RGBA_ALPHA(rgba) (((rgba>>0)&0xff)/255.0)
35 #define RGBA_RGB(rgba) RGBA_RED(rgba), RGBA_GREEN(rgba), RGBA_BLUE(rgba)
37 /*********** Printing to PDF ************/
39 gboolean ispdfspace(char c)
41 return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
44 gboolean ispdfdelim(char c)
46 return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
47 c=='{' || c=='}' || c=='/' || c=='%');
50 void skipspace(char **p, char *eof)
52 while (ispdfspace(**p) || **p=='%') {
53 if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
59 void free_pdfobj(struct PdfObj *obj)
63 if (obj==NULL) return;
64 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
65 obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
67 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
68 obj->type == PDFTYPE_STREAM) && obj->num>0) {
69 for (i=0; i<obj->num; i++)
70 free_pdfobj(obj->elts[i]);
73 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
74 for (i=0; i<obj->num; i++)
75 g_free(obj->names[i]);
81 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
86 if (obj==NULL) return NULL;
87 dup = g_memdup(obj, sizeof(struct PdfObj));
88 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
89 obj->type == PDFTYPE_STREAM) && obj->str!=NULL) {
90 if (obj->type == PDFTYPE_NAME) obj->len = strlen(obj->str);
91 dup->str = g_memdup(obj->str, obj->len+1);
93 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
94 obj->type == PDFTYPE_STREAM) && obj->num>0) {
95 dup->elts = g_malloc(obj->num*sizeof(struct PdfObj *));
96 for (i=0; i<obj->num; i++)
97 dup->elts[i] = dup_pdfobj(obj->elts[i]);
99 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
100 dup->names = g_malloc(obj->num*sizeof(char *));
101 for (i=0; i<obj->num; i++)
102 dup->names[i] = g_strdup(obj->names[i]);
107 void show_pdfobj(struct PdfObj *obj, GString *str)
110 if (obj==NULL) return;
113 if (obj->intval==1) g_string_append(str, "true");
114 if (obj->intval==0) g_string_append(str, "false");
115 if (obj->intval==-1) g_string_append(str, "null");
118 g_string_append_printf(str, "%d", obj->intval);
121 g_string_append_printf(str, "%f", obj->realval);
124 g_string_append_len(str, obj->str, obj->len);
127 g_string_append(str, obj->str);
130 g_string_append_c(str, '[');
131 for (i=0;i<obj->num;i++) {
132 if (i) g_string_append_c(str, ' ');
133 show_pdfobj(obj->elts[i], str);
135 g_string_append_c(str, ']');
138 g_string_append(str, "<<");
139 for (i=0;i<obj->num;i++) {
140 g_string_append_printf(str, " %s ", obj->names[i]);
141 show_pdfobj(obj->elts[i], str);
143 g_string_append(str, " >>");
146 g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
151 void DEBUG_PRINTOBJ(struct PdfObj *obj)
153 GString *s = g_string_new("");
156 g_string_free(s, TRUE);
159 // parse a PDF object; returns NULL if fails
160 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
162 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
164 struct PdfObj *obj, *elt;
165 char *p, *q, *r, *eltname;
168 obj = g_malloc(sizeof(struct PdfObj));
171 if (p==eof) { g_free(obj); return NULL; }
174 if (!strncmp(p, "true", 4)) {
175 obj->type = PDFTYPE_CST;
180 if (!strncmp(p, "false", 5)) {
181 obj->type = PDFTYPE_CST;
186 if (!strncmp(p, "null", 4)) {
187 obj->type = PDFTYPE_CST;
194 obj->intval = strtol(p, &q, 10);
198 obj->type = PDFTYPE_REAL;
199 obj->realval = g_ascii_strtod(p, ptr);
202 if (ispdfspace(*q)) {
203 // check for indirect reference
205 obj->num = strtol(q, &r, 10);
210 obj->type = PDFTYPE_REF;
215 obj->type = PDFTYPE_INT;
222 while (stack>0 && q!=eof) {
223 if (*q=='(') stack++;
224 if (*q==')') stack--;
228 if (q==eof) { g_free(obj); return NULL; }
229 obj->type = PDFTYPE_STRING;
231 obj->str = g_malloc(obj->len+1);
232 obj->str[obj->len] = 0;
233 g_memmove(obj->str, p, obj->len);
237 if (*p=='<' && p[1]!='<') {
239 while (*q!='>' && q!=eof) q++;
240 if (q==eof) { g_free(obj); return NULL; }
242 obj->type = PDFTYPE_STRING;
244 obj->str = g_malloc(obj->len+1);
245 obj->str[obj->len] = 0;
246 g_memmove(obj->str, p, obj->len);
254 while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
255 obj->type = PDFTYPE_NAME;
256 obj->str = g_strndup(p, q-p);
263 obj->type = PDFTYPE_ARRAY;
266 q=p+1; skipspace(&q, eof);
268 elt = parse_pdf_object(&q, eof);
269 if (elt==NULL) { free_pdfobj(obj); return NULL; }
271 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
272 obj->elts[obj->num-1] = elt;
280 if (*p=='<' && p[1]=='<') {
281 obj->type = PDFTYPE_DICT;
285 q=p+2; skipspace(&q, eof);
286 while (*q!='>' || q[1]!='>') {
287 if (*q!='/') { free_pdfobj(obj); return NULL; }
289 while (!ispdfspace(*r) && !ispdfdelim(*r)) r++;
290 eltname = g_strndup(q, r-q);
291 q=r; skipspace(&q, eof);
292 elt = parse_pdf_object(&q, eof);
293 if (elt==NULL) { g_free(eltname); free_pdfobj(obj); return NULL; }
295 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
296 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
297 obj->elts[obj->num-1] = elt;
298 obj->names[obj->num-1] = eltname;
305 // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
311 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
315 if (dict==NULL) return NULL;
316 if (dict->type != PDFTYPE_DICT) return NULL;
317 for (i=0; i<dict->num; i++)
318 if (!strcmp(dict->names[i], name)) return dict->elts[i];
322 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
327 if (obj==NULL) return NULL;
328 if (obj->type!=PDFTYPE_REF) return dup_pdfobj(obj);
329 if (obj->intval>xref->last) return NULL;
330 offs = xref->data[obj->intval];
331 if (offs<=0 || offs >= pdfbuf->len) return NULL;
333 p = pdfbuf->str + offs;
334 eof = pdfbuf->str + pdfbuf->len;
335 n = strtol(p, &p, 10);
336 if (n!=obj->intval) return NULL;
338 n = strtol(p, &p, 10);
340 if (strncmp(p, "obj", 3)) return NULL;
342 return parse_pdf_object(&p, eof);
345 // read the xref table of a PDF file in memory, and return the trailerdict
347 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
350 struct PdfObj *trailerdict, *obj;
353 if (strncmp(pdfbuf->str+offs, "xref", 4)) return NULL;
354 p = strstr(pdfbuf->str+offs, "trailer");
355 eof = pdfbuf->str + pdfbuf->len;
356 if (p==NULL) return NULL;
358 trailerdict = parse_pdf_object(&p, eof);
359 obj = get_dict_entry(trailerdict, "/Size");
360 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval-1>xref->last)
361 make_xref(xref, obj->intval-1, 0);
362 obj = get_dict_entry(trailerdict, "/Prev");
363 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval>0 && obj->intval!=offs) {
364 // recurse into older xref table
365 obj = parse_xref_table(pdfbuf, xref, obj->intval);
368 p = pdfbuf->str+offs+4;
370 if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
371 while (*p>='0' && *p<='9') {
372 start = strtol(p, &p, 10);
374 len = strtol(p, &p, 10);
376 if (len <= 0 || 20*len > eof-p) break;
377 if (start+len-1 > xref->last) make_xref(xref, start+len-1, 0);
378 for (i=start; i<start+len; i++) {
379 xref->data[i] = strtol(p, NULL, 10);
384 if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
388 // parse the page tree
390 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref,
391 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
393 struct PdfObj *obj, *kid;
396 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
397 if (obj == NULL || obj->type != PDFTYPE_NAME)
399 if (!strcmp(obj->str, "/Page")) {
401 pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
402 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
404 free_pdfobj(pages->resources);
405 pages->resources = obj;
407 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
409 free_pdfobj(pages->mediabox);
410 pages->mediabox = obj;
412 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
413 if (obj!=NULL && obj->type == PDFTYPE_INT)
414 pages->rotate = obj->intval;
418 else if (!strcmp(obj->str, "/Pages")) {
420 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Count"));
421 if (obj!=NULL && obj->type == PDFTYPE_INT &&
422 obj->intval>0 && obj->intval<=nmax) count = obj->intval;
425 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
427 for (i=0; i<count; i++) {
428 free_pdfobj(pages[i].resources);
429 pages[i].resources = dup_pdfobj(obj);
432 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
434 for (i=0; i<count; i++) {
435 free_pdfobj(pages[i].mediabox);
436 pages[i].mediabox = dup_pdfobj(obj);
439 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
440 if (obj!=NULL && obj->type == PDFTYPE_INT)
441 for (i=0; i<count; i++)
442 pages[i].rotate = obj->intval;
444 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Kids"));
445 if (obj!=NULL && obj->type == PDFTYPE_ARRAY) {
446 for (i=0; i<obj->num; i++) {
447 kid = get_pdfobj(pdfbuf, xref, obj->elts[i]);
449 j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
462 // parse a PDF file in memory
464 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
468 struct PdfObj *obj, *pages;
470 xref->n_alloc = xref->last = 0;
472 p = pdfbuf->str + pdfbuf->len-1;
474 while (*p!='s' && p!=pdfbuf->str) p--;
475 if (strncmp(p, "startxref", 9)) return FALSE; // fail
477 while (ispdfspace(*p) && p!=pdfbuf->str+pdfbuf->len) p++;
478 offs = strtol(p, NULL, 10);
479 if (offs <= 0 || offs > pdfbuf->len) return FALSE; // fail
480 pdfinfo->startxref = offs;
482 pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
483 if (pdfinfo->trailerdict == NULL) return FALSE; // fail
485 obj = get_pdfobj(pdfbuf, xref,
486 get_dict_entry(pdfinfo->trailerdict, "/Root"));
488 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
489 pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
492 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
493 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pages, "/Count"));
494 if (obj == NULL || obj->type != PDFTYPE_INT || obj->intval<=0)
495 { free_pdfobj(pdfinfo->trailerdict); free_pdfobj(pages);
496 free_pdfobj(obj); return FALSE; }
497 pdfinfo->npages = obj->intval;
500 pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
501 pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
507 // add an entry to the xref table
509 void make_xref(struct XrefTable *xref, int nobj, int offset)
511 if (xref->n_alloc <= nobj) {
512 xref->n_alloc = nobj + 10;
513 xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
515 if (xref->last < nobj) xref->last = nobj;
516 xref->data[nobj] = offset;
519 // a wrapper for deflate
521 GString *do_deflate(char *in, int len)
528 deflateInit(&zs, Z_DEFAULT_COMPRESSION);
529 zs.next_in = (Bytef *)in;
531 zs.avail_out = deflateBound(&zs, len);
532 out = g_string_sized_new(zs.avail_out);
533 zs.next_out = (Bytef *)out->str;
534 deflate(&zs, Z_FINISH);
535 out->len = zs.total_out;
540 // prefix to scale the original page
542 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
545 double v[4], t, xscl, yscl;
548 // push 3 times in case code to be annotated has unbalanced q/Q (B of A., ...)
549 str = g_string_new("q q q ");
550 if (pgdesc->rotate == 90) {
551 g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
552 t = height; height = width; width = t;
554 if (pgdesc->rotate == 270) {
555 g_string_append_printf(str, "0 1 -1 0 %.2f 0 cm ", width);
556 t = height; height = width; width = t;
558 if (pgdesc->rotate == 180) {
559 g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
561 if (pgdesc->mediabox==NULL || pgdesc->mediabox->type != PDFTYPE_ARRAY ||
562 pgdesc->mediabox->num != 4) return str;
563 for (i=0; i<4; i++) {
564 if (pgdesc->mediabox->elts[i]->type == PDFTYPE_INT)
565 v[i] = pgdesc->mediabox->elts[i]->intval;
566 else if (pgdesc->mediabox->elts[i]->type == PDFTYPE_REAL)
567 v[i] = pgdesc->mediabox->elts[i]->realval;
570 if (v[0]>v[2]) { t = v[0]; v[0] = v[2]; v[2] = t; }
571 if (v[1]>v[3]) { t = v[1]; v[1] = v[3]; v[3] = t; }
572 if (v[2]-v[0] < 1. || v[3]-v[1] < 1.) return str;
573 xscl = width/(v[2]-v[0]);
574 yscl = height/(v[3]-v[1]);
575 g_string_append_printf(str, "%.4f 0 0 %.4f %.2f %.2f cm ",
576 xscl, yscl, -v[0]*xscl, -v[1]*yscl);
580 // add an entry to a subentry of a directory
582 struct PdfObj *mk_pdfname(char *name)
586 obj = g_malloc(sizeof(struct PdfObj));
587 obj->type = PDFTYPE_NAME;
588 obj->str = g_strdup(name);
592 struct PdfObj *mk_pdfref(int num)
596 obj = g_malloc(sizeof(struct PdfObj));
597 obj->type = PDFTYPE_REF;
603 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
605 if (a==NULL || b==NULL) return (a==b);
606 if (a->type!=b->type) return FALSE;
607 if (a->type == PDFTYPE_CST || a->type == PDFTYPE_INT)
608 return (a->intval == b->intval);
609 if (a->type == PDFTYPE_REAL)
610 return (a->realval == b->realval);
611 if (a->type == PDFTYPE_NAME)
612 return !strcmp(a->str, b->str);
613 if (a->type == PDFTYPE_REF)
614 return (a->intval == b->intval && a->num == b->num);
618 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
619 struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
625 for (i=0; i<obj->num; i++)
626 if (!strcmp(obj->names[i], section)) subpos = i;
630 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj*));
631 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
632 obj->names[subpos] = g_strdup(section);
633 obj->elts[subpos] = NULL;
635 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type==PDFTYPE_REF) {
636 sec = get_pdfobj(pdfbuf, xref, obj->elts[subpos]);
637 free_pdfobj(obj->elts[subpos]);
638 obj->elts[subpos] = sec;
640 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type!=type)
641 { free_pdfobj(obj->elts[subpos]); obj->elts[subpos] = NULL; }
642 if (obj->elts[subpos] == NULL) {
643 obj->elts[subpos] = sec = g_malloc(sizeof(struct PdfObj));
649 sec = obj->elts[subpos];
652 if (type==PDFTYPE_DICT) {
653 for (i=0; i<sec->num; i++)
654 if (!strcmp(sec->names[i], name)) subpos = i;
658 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
659 sec->names = g_realloc(sec->names, sec->num*sizeof(char *));
660 sec->names[subpos] = g_strdup(name);
661 sec->elts[subpos] = NULL;
663 free_pdfobj(sec->elts[subpos]);
664 sec->elts[subpos] = entry;
666 if (type==PDFTYPE_ARRAY) {
667 for (i=0; i<sec->num; i++)
668 if (iseq_obj(sec->elts[i], entry)) subpos = i;
672 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
673 sec->elts[subpos] = entry;
675 else free_pdfobj(entry);
679 // draw a page's background
681 void pdf_draw_solid_background(struct Page *pg, GString *str)
685 g_string_append_printf(str,
686 "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
687 RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
688 if (!ui.print_ruling) return;
689 if (pg->bg->ruling == RULING_NONE) return;
690 g_string_append_printf(str,
691 "%.2f %.2f %.2f RG %.2f w ",
692 RGBA_RGB(RULING_COLOR), RULING_THICKNESS);
693 if (pg->bg->ruling == RULING_GRAPH) {
694 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
695 g_string_append_printf(str, "%.2f 0 m %.2f %.2f l S ",
697 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
698 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
702 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
703 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
705 if (pg->bg->ruling == RULING_LINED)
706 g_string_append_printf(str,
707 "%.2f %.2f %.2f RG %.2f 0 m %.2f %.2f l S ",
708 RGBA_RGB(RULING_MARGIN_COLOR),
709 RULING_LEFTMARGIN, RULING_LEFTMARGIN, pg->height);
712 int pdf_draw_bitmap_background(struct Page *pg, GString *str,
713 struct XrefTable *xref, GString *pdfbuf)
718 PopplerPage *pdfpage;
720 int height, width, stride, x, y, chan;
721 double pgheight, pgwidth;
723 if (pg->bg->type == BG_PDF) {
724 if (!bgpdf.document) return -1;
725 pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
726 if (!pdfpage) return -1;
727 poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
728 width = (int) (PDFTOPPM_PRINTING_DPI * pgwidth/72.0);
729 height = (int) (PDFTOPPM_PRINTING_DPI * pgheight/72.0);
730 pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
731 poppler_page_render_to_pixbuf(
732 pdfpage, 0, 0, width, height, PDFTOPPM_PRINTING_DPI/72.0, 0, pix);
733 g_object_unref(pdfpage);
735 else pix = g_object_ref(pg->bg->pixbuf);
737 if (gdk_pixbuf_get_bits_per_sample(pix) != 8 ||
738 gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB)
739 { g_object_unref(pix); return -1; }
741 width = gdk_pixbuf_get_width(pix);
742 height = gdk_pixbuf_get_height(pix);
743 stride = gdk_pixbuf_get_rowstride(pix);
744 chan = gdk_pixbuf_get_n_channels(pix);
745 if (chan!=3 && chan!=4) { g_object_unref(pix); return -1; }
747 g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
748 pg->width, -pg->height, pg->height);
750 p2 = buf = (char *)g_malloc(3*width*height);
751 for (y=0; y<height; y++) {
752 p1 = (char *)gdk_pixbuf_get_pixels(pix)+stride*y;
753 for (x=0; x<width; x++) {
754 *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
758 zpix = do_deflate(buf, 3*width*height);
762 make_xref(xref, xref->last+1, pdfbuf->len);
763 g_string_append_printf(pdfbuf,
764 "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
765 "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
766 "/BitsPerComponent 8 >> stream\n",
767 xref->last, zpix->len, width, height);
768 g_string_append_len(pdfbuf, zpix->str, zpix->len);
769 g_string_free(zpix, TRUE);
770 g_string_append(pdfbuf, "endstream\nendobj\n");
775 // manipulate Pdf fonts
777 struct PdfFont *new_pdffont(struct XrefTable *xref, GList **fonts,
778 char *filename, int font_id, FT_Face face, int glyph_page)
781 struct PdfFont *font;
785 for (list = *fonts; list!=NULL; list = list->next) {
786 font = (struct PdfFont *)list->data;
787 if (!strcmp(font->filename, filename) && font->font_id == font_id
788 && font->glyph_page == glyph_page)
789 { font->used_in_this_page = TRUE; return font; }
791 font = g_malloc(sizeof(struct PdfFont));
792 *fonts = g_list_append(*fonts, font);
793 font->n_obj = xref->last+1;
794 make_xref(xref, xref->last+1, 0); // will give it a value later
795 font->filename = g_strdup(filename);
796 font->font_id = font_id;
797 font->glyph_page = glyph_page;
798 font->used_in_this_page = TRUE;
799 font->num_glyphs_used = 0;
800 for (i=0; i<256; i++) {
801 font->glyphmap[i] = -1;
802 font->advance[i] = 0;
803 font->glyphpsnames[i] = NULL;
805 font->glyphmap[0] = 0;
806 // fill in info from the FT_Face
807 font->is_truetype = FT_IS_SFNT(face);
808 font->nglyphs = face->num_glyphs;
809 font->ft2ps = 1000.0 / face->units_per_EM;
810 font->ascender = (int)(font->ft2ps*face->ascender);
811 font->descender = (int)(font->ft2ps*face->descender);
812 if (face->bbox.xMin < -100000 || face->bbox.xMin > 100000) font->xmin = 0;
813 else font->xmin = (int)(font->ft2ps*face->bbox.xMin);
814 if (face->bbox.xMax < -100000 || face->bbox.xMax > 100000) font->xmax = 0;
815 else font->xmax = (int)(font->ft2ps*face->bbox.xMax);
816 if (face->bbox.yMin < -100000 || face->bbox.yMin > 100000) font->ymin = 0;
817 else font->ymin = (int)(font->ft2ps*face->bbox.yMin);
818 if (face->bbox.yMax < -100000 || face->bbox.yMax > 100000) font->ymax = 0;
819 else font->ymax = (int)(font->ft2ps*face->bbox.yMax);
820 if (font->is_truetype) font->flags = 4; // symbolic
822 font->flags = 4; // symbolic
823 if (FT_IS_FIXED_WIDTH(face)) font->flags |= 1;
824 if (face->style_flags & FT_STYLE_FLAG_ITALIC) font->flags |= 64;
826 s = FT_Get_Postscript_Name(face);
827 if (s==NULL) s = "Noname";
828 if (glyph_page) font->fontname = g_strdup_printf("%s_%03d", s, glyph_page);
829 else font->fontname = g_strdup(s);
833 #define pfb_get_length(x) (((x)[3]<<24) + ((x)[2]<<16) + ((x)[1]<<8) + (x)[0])
834 #define T1_SEGMENT_1_END "currentfile eexec"
835 #define T1_SEGMENT_3_END "cleartomark"
837 void embed_pdffont(GString *pdfbuf, struct XrefTable *xref, struct PdfFont *font)
839 // this code inspired by libgnomeprint
840 gboolean fallback, is_binary;
841 guchar encoding[256];
843 int i, j, num, len1, len2;
849 int nobj_fontprog, nobj_descr, lastchar;
852 // embed the font file: TrueType case
853 if (font->is_truetype) {
854 glyphs[0] = encoding[0] = 0;
856 for (i=1; i<=255; i++)
857 if (font->glyphmap[i]>=0) {
858 font->glyphmap[i] = num;
859 glyphs[num] = 255*font->glyph_page+i-1;
863 font->num_glyphs_used = num-1;
864 if (OpenTTFont(font->filename, 0, &ttfnt) == SF_OK) {
865 if (CreateTTFromTTGlyphs_tomemory(ttfnt, (guint8**)&fontdata, &len, glyphs, encoding, num,
866 0, NULL, TTCF_AutoName | TTCF_IncludeOS2) == SF_OK) {
867 make_xref(xref, xref->last+1, pdfbuf->len);
868 nobj_fontprog = xref->last;
869 g_string_append_printf(pdfbuf,
870 "%d 0 obj\n<< /Length %d /Length1 %d >> stream\n",
871 nobj_fontprog, (int)len, (int)len);
872 g_string_append_len(pdfbuf, fontdata, len);
873 g_string_append(pdfbuf, "endstream\nendobj\n");
876 else fallback = TRUE;
879 else fallback = TRUE;
881 // embed the font file: Type1 case
882 if (g_file_get_contents(font->filename, &fontdata, &len, NULL) && len>=8) {
883 if (fontdata[0]==(char)0x80 && fontdata[1]==(char)0x01) {
885 len1 = pfb_get_length((unsigned char *)fontdata+2);
886 if (fontdata[len1+6]!=(char)0x80 || fontdata[len1+7]!=(char)0x02) fallback = TRUE;
888 len2 = pfb_get_length((unsigned char *)fontdata+len1+8);
889 if (fontdata[len1+len2+12]!=(char)0x80 || fontdata[len1+len2+13]!=(char)0x01)
893 else if (!strncmp(fontdata, "%!PS", 4)) {
895 p = strstr(fontdata, T1_SEGMENT_1_END) + strlen(T1_SEGMENT_1_END);
896 if (p==NULL) fallback = TRUE;
898 if (*p=='\n' || *p=='\r') p++;
899 if (*p=='\n' || *p=='\r') p++;
901 p = g_strrstr_len(fontdata, len, T1_SEGMENT_3_END);
902 if (p==NULL) fallback = TRUE;
906 while (i>0 && p!=fontdata && (*p=='0' || *p=='\r' || *p=='\n')) {
910 while (p!=fontdata && (*p=='\r' || *p=='\n')) p--;
912 if (i>0) fallback = TRUE;
913 else len2 = p-fontdata-len1;
917 else fallback = TRUE;
921 seg2 = fontdata + len1 + 12;
924 seg2 = g_malloc(len2/2);
927 while (p+1 < fontdata+len1+len2) {
928 if (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') { p++; continue; }
929 if (p[0]>'9') { p[0]|=0x20; p[0]-=39; }
930 if (p[1]>'9') { p[1]|=0x20; p[1]-=39; }
931 seg2[j++] = ((p[0]-'0')<<4) + (p[1]-'0');
936 make_xref(xref, xref->last+1, pdfbuf->len);
937 nobj_fontprog = xref->last;
938 g_string_append_printf(pdfbuf,
939 "%d 0 obj\n<< /Length %d /Length1 %d /Length2 %d /Length3 0 >> stream\n",
940 nobj_fontprog, len1+len2, len1, len2);
941 g_string_append_len(pdfbuf, seg1, len1);
942 g_string_append_len(pdfbuf, seg2, len2);
943 g_string_append(pdfbuf, "endstream\nendobj\n");
944 if (!is_binary) g_free(seg2);
948 else fallback = TRUE;
951 // next, the font descriptor
953 make_xref(xref, xref->last+1, pdfbuf->len);
954 nobj_descr = xref->last;
955 g_string_append_printf(pdfbuf,
956 "%d 0 obj\n<< /Type /FontDescriptor /FontName /%s /Flags %d "
957 "/FontBBox [%d %d %d %d] /ItalicAngle 0 /Ascent %d "
958 "/Descent %d /CapHeight %d /StemV 100 /%s %d 0 R >> endobj\n",
959 nobj_descr, font->fontname, font->flags,
960 font->xmin, font->ymin, font->xmax, font->ymax,
961 font->ascender, -font->descender, font->ascender,
962 font->is_truetype ? "FontFile2":"FontFile",
966 // finally, the font itself
967 /* Note: in Type1 case, font->glyphmap maps charcodes to glyph no's
968 in TrueType case, encoding lists the used charcodes by index,
969 glyphs list the used glyph no's by index
970 font->glyphmap maps charcodes to indices */
971 xref->data[font->n_obj] = pdfbuf->len;
972 if (font->is_truetype) lastchar = encoding[font->num_glyphs_used];
973 else lastchar = font->num_glyphs_used;
975 font->is_truetype = FALSE;
976 g_free(font->fontname);
977 font->fontname = g_strdup("Helvetica");
980 if (font->is_truetype) {
981 num = font->glyph_page;
982 for (i=0; i<6; i++) { prefix[i] = 'A'+(num%26); num/=26; }
983 prefix[6]='+'; prefix[7]=0;
985 g_string_append_printf(pdfbuf,
986 "%d 0 obj\n<< /Type /Font /Subtype /%s /BaseFont /%s%s /Name /F%d ",
987 font->n_obj, font->is_truetype?"TrueType":"Type1",
988 prefix, font->fontname, font->n_obj);
990 g_string_append_printf(pdfbuf,
991 "/FontDescriptor %d 0 R /FirstChar 0 /LastChar %d /Widths [",
992 nobj_descr, lastchar);
993 for (i=0; i<=lastchar; i++)
994 g_string_append_printf(pdfbuf, "%d ", font->advance[i]);
995 g_string_append(pdfbuf, "] ");
997 if (!font->is_truetype) { /* encoding */
998 g_string_append(pdfbuf, "/Encoding << /Type /Encoding "
999 "/BaseEncoding /MacRomanEncoding /Differences [1 ");
1000 for (i=1; i<=lastchar; i++) {
1001 g_string_append_printf(pdfbuf, "/%s ", font->glyphpsnames[i]);
1002 g_free(font->glyphpsnames[i]);
1004 g_string_append(pdfbuf, "] >> ");
1006 g_string_append(pdfbuf, ">> endobj\n");
1009 // draw a page's graphics
1011 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter,
1012 struct XrefTable *xref, GList **pdffonts)
1014 GList *layerlist, *itemlist, *tmplist;
1017 guint old_rgba, old_text_rgba;
1018 double old_thickness;
1021 PangoFontDescription *font_desc;
1022 PangoContext *context;
1023 PangoLayout *layout;
1024 PangoLayoutIter *iter;
1025 PangoRectangle logical_rect;
1026 PangoLayoutRun *run;
1027 PangoFcFont *fcfont;
1028 PangoFontMap *fontmap;
1030 int baseline, advance;
1031 int glyph_no, glyph_page, current_page;
1036 struct PdfFont *cur_font;
1039 old_rgba = old_text_rgba = 0x12345678; // not any values we use, so we'll reset them
1040 old_thickness = 0.0;
1041 for (tmplist = *pdffonts; tmplist!=NULL; tmplist = tmplist->next) {
1042 cur_font = (struct PdfFont *)tmplist->data;
1043 cur_font->used_in_this_page = FALSE;
1046 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1047 l = (struct Layer *)layerlist->data;
1048 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1049 item = (struct Item *)itemlist->data;
1050 if (item->type == ITEM_STROKE) {
1051 if ((item->brush.color_rgba & ~0xff) != old_rgba)
1052 g_string_append_printf(str, "%.2f %.2f %.2f RG ",
1053 RGBA_RGB(item->brush.color_rgba));
1054 if (item->brush.thickness != old_thickness)
1055 g_string_append_printf(str, "%.2f w ", item->brush.thickness);
1056 if ((item->brush.color_rgba & 0xf0) != 0xf0) { // transparent
1057 g_string_append(str, "q /XoHi gs ");
1058 *use_hiliter = TRUE;
1060 old_rgba = item->brush.color_rgba & ~0xff;
1061 old_thickness = item->brush.thickness;
1062 pt = item->path->coords;
1063 if (!item->brush.variable_width) {
1064 g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
1065 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1066 g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
1067 g_string_append_printf(str,"S\n");
1068 old_thickness = item->brush.thickness;
1070 for (i=0; i<item->path->num_points-1; i++, pt+=2)
1071 g_string_append_printf(str, "%.2f w %.2f %.2f m %.2f %.2f l S\n",
1072 item->widths[i], pt[0], pt[1], pt[2], pt[3]);
1073 old_thickness = 0.0;
1075 if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
1076 g_string_append(str, "Q ");
1078 else if (item->type == ITEM_TEXT) {
1079 if ((item->brush.color_rgba & ~0xff) != old_text_rgba)
1080 g_string_append_printf(str, "%.2f %.2f %.2f rg ",
1081 RGBA_RGB(item->brush.color_rgba));
1082 old_text_rgba = item->brush.color_rgba & ~0xff;
1083 fontmap = pango_ft2_font_map_new();
1084 pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP (fontmap), 72, 72);
1085 context = pango_font_map_create_context(fontmap);
1086 layout = pango_layout_new(context);
1087 g_object_unref(fontmap);
1088 g_object_unref(context);
1089 font_desc = pango_font_description_from_string(item->font_name);
1090 pango_font_description_set_absolute_size(font_desc,
1091 item->font_size*PANGO_SCALE);
1092 pango_layout_set_font_description(layout, font_desc);
1093 pango_font_description_free(font_desc);
1094 pango_layout_set_text(layout, item->text, -1);
1095 // this code inspired by the code in libgnomeprint
1096 iter = pango_layout_get_iter(layout);
1098 run = pango_layout_iter_get_run(iter);
1099 if (run==NULL) continue;
1100 pango_layout_iter_get_run_extents (iter, NULL, &logical_rect);
1101 baseline = pango_layout_iter_get_baseline (iter);
1102 if (!PANGO_IS_FC_FONT(run->item->analysis.font)) continue;
1103 fcfont = PANGO_FC_FONT(run->item->analysis.font);
1104 pattern = fcfont->font_pattern;
1105 if (FcPatternGetString(pattern, FC_FILE, 0, (unsigned char **)&filename) != FcResultMatch ||
1106 FcPatternGetInteger(pattern, FC_INDEX, 0, &font_id) != FcResultMatch)
1108 ftface = pango_fc_font_lock_face(fcfont);
1111 g_string_append_printf(str, "BT %.2f 0 0 %.2f %.2f %.2f Tm ",
1112 item->font_size, -item->font_size,
1113 item->bbox.left + (gdouble) logical_rect.x/PANGO_SCALE,
1114 item->bbox.top + (gdouble) baseline/PANGO_SCALE);
1116 for (i=0; i<run->glyphs->num_glyphs; i++) {
1117 glyph_no = run->glyphs->glyphs[i].glyph;
1118 if (FT_IS_SFNT(ftface)) glyph_page = glyph_no/255;
1119 else glyph_page = 0;
1120 if (glyph_page != current_page) {
1121 cur_font = new_pdffont(xref, pdffonts, filename, font_id,
1122 ftface, glyph_page);
1123 if (in_string) g_string_append(str, ") Tj ");
1125 g_string_append_printf(str, "/F%d 1 Tf ", cur_font->n_obj);
1127 current_page = glyph_page;
1128 FT_Load_Glyph(ftface, glyph_no, FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
1129 advance = (int)(ftface->glyph->metrics.horiAdvance * cur_font->ft2ps + 0.5);
1130 if (!in_string) g_string_append_c(str, '(');
1132 if (cur_font->is_truetype) {
1133 if (glyph_no) glyph_no = (glyph_no%255)+1;
1134 cur_font->glyphmap[glyph_no] = glyph_no;
1137 for (j=1; j<=cur_font->num_glyphs_used; j++)
1138 if (cur_font->glyphmap[j] == glyph_no) break;
1139 if (j==256) j=0; // font is full, what do we do?
1140 if (j>cur_font->num_glyphs_used) {
1141 cur_font->glyphmap[j] = glyph_no;
1142 cur_font->num_glyphs_used++;
1143 if (FT_Get_Glyph_Name(ftface, glyph_no, tmpstr, 200) == FT_Err_Ok)
1144 cur_font->glyphpsnames[j] = g_strdup(tmpstr);
1145 else cur_font->glyphpsnames[j] = g_strdup(".notdef");
1149 cur_font->advance[glyph_no] = advance;
1150 if (glyph_no=='\\' || glyph_no == '(' || glyph_no == ')' || glyph_no == 10 || glyph_no == 13)
1151 g_string_append_c(str, '\\');
1152 if (glyph_no==10) g_string_append_c(str,'n');
1153 else if (glyph_no==13) g_string_append_c(str,'r');
1154 else g_string_append_c(str, glyph_no);
1156 if (in_string) g_string_append(str, ") Tj ");
1157 g_string_append(str, "ET ");
1158 pango_fc_font_unlock_face(fcfont);
1159 } while (pango_layout_iter_next_run(iter));
1160 pango_layout_iter_free(iter);
1161 g_object_unref(layout);
1167 // main printing function
1169 /* we use the following object numbers, starting with n_obj_catalog:
1170 0 the document catalog
1172 2 the GS for the hiliters
1173 3 ... the page objects
1176 gboolean print_to_pdf(char *filename)
1179 GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
1180 int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
1182 struct XrefTable xref;
1185 gboolean annot, uses_pdf;
1186 gboolean use_hiliter;
1187 struct PdfInfo pdfinfo;
1189 GList *pdffonts, *list;
1190 struct PdfFont *font;
1193 f = fopen(filename, "w");
1194 if (f == NULL) return FALSE;
1195 setlocale(LC_NUMERIC, "C");
1200 for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
1201 pg = (struct Page *)pglist->data;
1202 if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
1205 if (uses_pdf && bgpdf.status != STATUS_NOT_INIT &&
1206 bgpdf.file_contents!=NULL && !strncmp(bgpdf.file_contents, "%PDF-1.", 7)) {
1207 // parse the existing PDF file
1208 pdfbuf = g_string_new_len(bgpdf.file_contents, bgpdf.file_length);
1209 if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
1210 annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
1212 g_string_free(pdfbuf, TRUE);
1213 if (xref.data != NULL) g_free(xref.data);
1218 pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
1219 xref.n_alloc = xref.last = 0;
1223 // catalog and page tree
1224 n_obj_catalog = xref.last+1;
1225 n_obj_pages_offs = xref.last+4;
1226 make_xref(&xref, n_obj_catalog, pdfbuf->len);
1227 g_string_append_printf(pdfbuf,
1228 "%d 0 obj\n<< /Type /Catalog /Pages %d 0 R >> endobj\n",
1229 n_obj_catalog, n_obj_catalog+1);
1230 make_xref(&xref, n_obj_catalog+1, pdfbuf->len);
1231 g_string_append_printf(pdfbuf,
1232 "%d 0 obj\n<< /Type /Pages /Kids [", n_obj_catalog+1);
1233 for (i=0;i<journal.npages;i++)
1234 g_string_append_printf(pdfbuf, "%d 0 R ", n_obj_pages_offs+i);
1235 g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
1236 make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
1237 g_string_append_printf(pdfbuf,
1238 "%d 0 obj\n<< /Type /ExtGState /CA %.2f >> endobj\n",
1239 n_obj_catalog+2, ui.hiliter_opacity);
1240 xref.last = n_obj_pages_offs + journal.npages-1;
1242 for (pglist = journal.pages, n_page = 0; pglist!=NULL;
1243 pglist = pglist->next, n_page++) {
1244 pg = (struct Page *)pglist->data;
1246 // draw the background and page into pgstrm
1247 pgstrm = g_string_new("");
1248 g_string_printf(pgstrm, "q 1 0 0 -1 0 %.2f cm 1 J 1 j ", pg->height);
1251 if (pg->bg->type == BG_SOLID)
1252 pdf_draw_solid_background(pg, pgstrm);
1253 else if (pg->bg->type == BG_PDF && annot &&
1254 pdfinfo.pages[pg->bg->file_page_seq-1].contents!=NULL) {
1255 make_xref(&xref, xref.last+1, pdfbuf->len);
1256 n_obj_prefix = xref.last;
1257 tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
1258 pg->width, pg->height);
1259 g_string_append_printf(pdfbuf,
1260 "%d 0 obj\n<< /Length %d >> stream\n%s\nendstream\nendobj\n",
1261 n_obj_prefix, tmpstr->len, tmpstr->str);
1262 g_string_free(tmpstr, TRUE);
1263 g_string_prepend(pgstrm, "Q Q Q ");
1265 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
1266 n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
1267 // draw the page contents
1268 use_hiliter = FALSE;
1269 pdf_draw_page(pg, pgstrm, &use_hiliter, &xref, &pdffonts);
1270 g_string_append_printf(pgstrm, "Q\n");
1272 // deflate pgstrm and write it
1273 zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
1274 g_string_free(pgstrm, TRUE);
1276 make_xref(&xref, xref.last+1, pdfbuf->len);
1277 g_string_append_printf(pdfbuf,
1278 "%d 0 obj\n<< /Length %d /Filter /FlateDecode>> stream\n",
1279 xref.last, zpgstrm->len);
1280 g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
1281 g_string_free(zpgstrm, TRUE);
1282 g_string_append(pdfbuf, "endstream\nendobj\n");
1284 // write the page object
1286 make_xref(&xref, n_obj_pages_offs+n_page, pdfbuf->len);
1287 g_string_append_printf(pdfbuf,
1288 "%d 0 obj\n<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %.2f %.2f] ",
1289 n_obj_pages_offs+n_page, n_obj_catalog+1, pg->width, pg->height);
1290 if (n_obj_prefix>0) {
1291 obj = get_pdfobj(pdfbuf, &xref, pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1292 if (obj->type != PDFTYPE_ARRAY) {
1294 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1296 g_string_append_printf(pdfbuf, "/Contents [%d 0 R ", n_obj_prefix);
1297 if (obj->type == PDFTYPE_REF)
1298 g_string_append_printf(pdfbuf, "%d %d R ", obj->intval, obj->num);
1299 if (obj->type == PDFTYPE_ARRAY) {
1300 for (i=0; i<obj->num; i++) {
1301 show_pdfobj(obj->elts[i], pdfbuf);
1302 g_string_append_c(pdfbuf, ' ');
1306 g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
1308 else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
1309 g_string_append(pdfbuf, "/Resources ");
1312 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
1314 if (obj!=NULL && obj->type!=PDFTYPE_DICT)
1315 { free_pdfobj(obj); obj=NULL; }
1317 obj = g_malloc(sizeof(struct PdfObj));
1318 obj->type = PDFTYPE_DICT;
1323 add_dict_subentry(pdfbuf, &xref,
1324 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
1326 add_dict_subentry(pdfbuf, &xref,
1327 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
1329 add_dict_subentry(pdfbuf, &xref,
1330 obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
1332 add_dict_subentry(pdfbuf, &xref,
1333 obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
1334 for (list=pdffonts; list!=NULL; list = list->next) {
1335 font = (struct PdfFont *)list->data;
1336 if (font->used_in_this_page) {
1337 add_dict_subentry(pdfbuf, &xref,
1338 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/Text"));
1339 tmpbuf = g_strdup_printf("/F%d", font->n_obj);
1340 add_dict_subentry(pdfbuf, &xref,
1341 obj, "/Font", PDFTYPE_DICT, tmpbuf, mk_pdfref(font->n_obj));
1345 show_pdfobj(obj, pdfbuf);
1347 g_string_append(pdfbuf, " >> endobj\n");
1350 // after the pages, we insert fonts
1351 for (list = pdffonts; list!=NULL; list = list->next) {
1352 font = (struct PdfFont *)list->data;
1353 embed_pdffont(pdfbuf, &xref, font);
1354 g_free(font->filename);
1355 g_free(font->fontname);
1358 g_list_free(pdffonts);
1361 startxref = pdfbuf->len;
1362 if (annot) g_string_append_printf(pdfbuf,
1363 "xref\n%d %d\n", n_obj_catalog, xref.last-n_obj_catalog+1);
1364 else g_string_append_printf(pdfbuf,
1365 "xref\n0 %d\n0000000000 65535 f \n", xref.last+1);
1366 for (i=n_obj_catalog; i<=xref.last; i++)
1367 g_string_append_printf(pdfbuf, "%010d 00000 n \n", xref.data[i]);
1368 g_string_append_printf(pdfbuf,
1369 "trailer\n<< /Size %d /Root %d 0 R ", xref.last+1, n_obj_catalog);
1371 g_string_append_printf(pdfbuf, "/Prev %d ", pdfinfo.startxref);
1372 // keeping encryption info somehow doesn't work.
1373 // xournal can't annotate encrypted PDFs anyway...
1375 obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
1377 g_string_append_printf(pdfbuf, "/Encrypt ");
1378 show_pdfobj(obj, pdfbuf);
1382 g_string_append_printf(pdfbuf,
1383 ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
1387 free_pdfobj(pdfinfo.trailerdict);
1388 if (pdfinfo.pages!=NULL)
1389 for (i=0; i<pdfinfo.npages; i++) {
1390 free_pdfobj(pdfinfo.pages[i].resources);
1391 free_pdfobj(pdfinfo.pages[i].mediabox);
1392 free_pdfobj(pdfinfo.pages[i].contents);
1396 setlocale(LC_NUMERIC, "");
1397 if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
1399 g_string_free(pdfbuf, TRUE);
1403 g_string_free(pdfbuf, TRUE);
1407 /*********** Printing via gtk-print **********/
1409 #if GTK_CHECK_VERSION(2, 10, 0)
1411 // does the same job as update_canvas_bg(), but to a print context
1413 void print_background(cairo_t *cr, struct Page *pg)
1418 PopplerPage *pdfpage;
1419 cairo_surface_t *cr_pixbuf;
1420 double pgwidth, pgheight;
1422 if (pg->bg->type == BG_SOLID) {
1423 cairo_set_source_rgb(cr, RGBA_RGB(pg->bg->color_rgba));
1424 cairo_rectangle(cr, 0, 0, pg->width, pg->height);
1426 if (!ui.print_ruling) return;
1427 if (pg->bg->ruling == RULING_NONE) return;
1428 cairo_set_source_rgb(cr, RGBA_RGB(RULING_COLOR));
1429 cairo_set_line_width(cr, RULING_THICKNESS);
1431 if (pg->bg->ruling == RULING_GRAPH) {
1432 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
1433 { cairo_move_to(cr, x, 0); cairo_line_to(cr, x, pg->height); }
1434 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
1435 { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
1440 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
1441 { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
1443 if (pg->bg->ruling == RULING_LINED) {
1444 cairo_set_source_rgb(cr, RGBA_RGB(RULING_MARGIN_COLOR));
1445 cairo_move_to(cr, RULING_LEFTMARGIN, 0);
1446 cairo_line_to(cr, RULING_LEFTMARGIN, pg->height);
1452 if (pg->bg->type == BG_PDF) {
1453 if (!bgpdf.document) return;
1454 pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
1455 if (!pdfpage) return;
1456 poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
1458 cairo_scale(cr, pg->width/pgwidth, pg->height/pgheight);
1459 poppler_page_render(pdfpage, cr);
1461 g_object_unref(pdfpage);
1464 if (pg->bg->type == BG_PIXMAP) {
1466 cairo_scale(cr, pg->width/gdk_pixbuf_get_width(pg->bg->pixbuf),
1467 pg->height/gdk_pixbuf_get_height(pg->bg->pixbuf));
1468 gdk_cairo_set_source_pixbuf(cr, pg->bg->pixbuf, 0, 0);
1469 cairo_rectangle(cr, 0, 0, gdk_pixbuf_get_width(pg->bg->pixbuf), gdk_pixbuf_get_height(pg->bg->pixbuf));
1475 void print_job_render_page(GtkPrintOperation *print, GtkPrintContext *context, gint pageno, gpointer user_data)
1478 gdouble width, height, scale;
1481 double old_thickness;
1482 GList *layerlist, *itemlist;
1487 PangoFontDescription *font_desc;
1488 PangoLayout *layout;
1490 pg = (struct Page *)g_list_nth_data(journal.pages, pageno);
1491 cr = gtk_print_context_get_cairo_context(context);
1492 width = gtk_print_context_get_width(context);
1493 height = gtk_print_context_get_height(context);
1494 scale = MIN(width/pg->width, height/pg->height);
1496 cairo_translate(cr, (width-scale*pg->width)/2, (height-scale*pg->height)/2);
1497 cairo_scale(cr, scale, scale);
1498 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
1499 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
1501 print_background(cr, pg);
1503 old_rgba = predef_colors_rgba[COLOR_BLACK];
1504 cairo_set_source_rgb(cr, 0, 0, 0);
1505 old_thickness = 0.0;
1507 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1508 l = (struct Layer *)layerlist->data;
1509 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1510 item = (struct Item *)itemlist->data;
1511 if (item->type == ITEM_STROKE || item->type == ITEM_TEXT) {
1512 if (item->brush.color_rgba != old_rgba)
1513 cairo_set_source_rgba(cr, RGBA_RGB(item->brush.color_rgba),
1514 RGBA_ALPHA(item->brush.color_rgba));
1515 old_rgba = item->brush.color_rgba;
1517 if (item->type == ITEM_STROKE) {
1518 if (item->brush.thickness != old_thickness)
1519 cairo_set_line_width(cr, item->brush.thickness);
1520 pt = item->path->coords;
1521 if (!item->brush.variable_width) {
1522 cairo_move_to(cr, pt[0], pt[1]);
1523 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1524 cairo_line_to(cr, pt[0], pt[1]);
1526 old_thickness = item->brush.thickness;
1528 for (i=0; i<item->path->num_points-1; i++, pt+=2) {
1529 cairo_move_to(cr, pt[0], pt[1]);
1530 cairo_set_line_width(cr, item->widths[i]);
1531 cairo_line_to(cr, pt[2], pt[3]);
1534 old_thickness = 0.0;
1537 if (item->type == ITEM_TEXT) {
1538 layout = gtk_print_context_create_pango_layout(context);
1539 font_desc = pango_font_description_from_string(item->font_name);
1540 if (item->font_size)
1541 pango_font_description_set_absolute_size(font_desc,
1542 item->font_size*PANGO_SCALE);
1543 pango_layout_set_font_description(layout, font_desc);
1544 pango_font_description_free(font_desc);
1545 pango_layout_set_text(layout, item->text, -1);
1546 cairo_move_to(cr, item->bbox.left, item->bbox.top);
1547 pango_cairo_show_layout(cr, layout);
1548 g_object_unref(layout);