5 #define PANGO_ENABLE_BACKEND /* to access PangoFcFont.font_pattern */
8 #include <libgnomecanvas/libgnomecanvas.h>
9 #include <libgnomeprint/gnome-print-job.h>
10 #include <libgnomeprint/gnome-print-pango.h>
14 #include <pango/pango.h>
15 #include <pango/pangofc-font.h>
16 #include <fontconfig/fontconfig.h>
18 #include FT_FREETYPE_H
19 #include "sft.h" /* Sun Font Tools, embedded in libgnomeprint */
27 #define RGBA_RED(rgba) (((rgba>>24)&0xff)/255.0)
28 #define RGBA_GREEN(rgba) (((rgba>>16)&0xff)/255.0)
29 #define RGBA_BLUE(rgba) (((rgba>>8)&0xff)/255.0)
30 #define RGBA_ALPHA(rgba) (((rgba>>0)&0xff)/255.0)
31 #define RGBA_RGB(rgba) RGBA_RED(rgba), RGBA_GREEN(rgba), RGBA_BLUE(rgba)
33 /*********** Printing to PDF ************/
35 gboolean ispdfspace(char c)
37 return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
40 gboolean ispdfdelim(char c)
42 return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
43 c=='{' || c=='}' || c=='/' || c=='%');
46 void skipspace(char **p, char *eof)
48 while (ispdfspace(**p) || **p=='%') {
49 if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
55 void free_pdfobj(struct PdfObj *obj)
59 if (obj==NULL) return;
60 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
61 obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
63 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
64 obj->type == PDFTYPE_STREAM) && obj->num>0) {
65 for (i=0; i<obj->num; i++)
66 free_pdfobj(obj->elts[i]);
69 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
70 for (i=0; i<obj->num; i++)
71 g_free(obj->names[i]);
77 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
82 if (obj==NULL) return NULL;
83 dup = g_memdup(obj, sizeof(struct PdfObj));
84 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
85 obj->type == PDFTYPE_STREAM) && obj->str!=NULL) {
86 if (obj->type == PDFTYPE_NAME) obj->len = strlen(obj->str);
87 dup->str = g_memdup(obj->str, obj->len+1);
89 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
90 obj->type == PDFTYPE_STREAM) && obj->num>0) {
91 dup->elts = g_malloc(obj->num*sizeof(struct PdfObj *));
92 for (i=0; i<obj->num; i++)
93 dup->elts[i] = dup_pdfobj(obj->elts[i]);
95 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
96 dup->names = g_malloc(obj->num*sizeof(char *));
97 for (i=0; i<obj->num; i++)
98 dup->names[i] = g_strdup(obj->names[i]);
103 void show_pdfobj(struct PdfObj *obj, GString *str)
106 if (obj==NULL) return;
109 if (obj->intval==1) g_string_append(str, "true");
110 if (obj->intval==0) g_string_append(str, "false");
111 if (obj->intval==-1) g_string_append(str, "null");
114 g_string_append_printf(str, "%d", obj->intval);
117 g_string_append_printf(str, "%f", obj->realval);
120 g_string_append_len(str, obj->str, obj->len);
123 g_string_append(str, obj->str);
126 g_string_append_c(str, '[');
127 for (i=0;i<obj->num;i++) {
128 if (i) g_string_append_c(str, ' ');
129 show_pdfobj(obj->elts[i], str);
131 g_string_append_c(str, ']');
134 g_string_append(str, "<<");
135 for (i=0;i<obj->num;i++) {
136 g_string_append_printf(str, " %s ", obj->names[i]);
137 show_pdfobj(obj->elts[i], str);
139 g_string_append(str, " >>");
142 g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
147 void DEBUG_PRINTOBJ(struct PdfObj *obj)
149 GString *s = g_string_new("");
152 g_string_free(s, TRUE);
155 // parse a PDF object; returns NULL if fails
156 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
158 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
160 struct PdfObj *obj, *elt;
161 char *p, *q, *r, *eltname;
164 obj = g_malloc(sizeof(struct PdfObj));
167 if (p==eof) { g_free(obj); return NULL; }
170 if (!strncmp(p, "true", 4)) {
171 obj->type = PDFTYPE_CST;
176 if (!strncmp(p, "false", 5)) {
177 obj->type = PDFTYPE_CST;
182 if (!strncmp(p, "null", 4)) {
183 obj->type = PDFTYPE_CST;
190 obj->intval = strtol(p, &q, 10);
194 obj->type = PDFTYPE_REAL;
195 obj->realval = g_ascii_strtod(p, ptr);
198 if (ispdfspace(*q)) {
199 // check for indirect reference
201 obj->num = strtol(q, &r, 10);
206 obj->type = PDFTYPE_REF;
211 obj->type = PDFTYPE_INT;
218 while (stack>0 && q!=eof) {
219 if (*q=='(') stack++;
220 if (*q==')') stack--;
224 if (q==eof) { g_free(obj); return NULL; }
225 obj->type = PDFTYPE_STRING;
227 obj->str = g_malloc(obj->len+1);
228 obj->str[obj->len] = 0;
229 g_memmove(obj->str, p, obj->len);
233 if (*p=='<' && p[1]!='<') {
235 while (*q!='>' && q!=eof) q++;
236 if (q==eof) { g_free(obj); return NULL; }
238 obj->type = PDFTYPE_STRING;
240 obj->str = g_malloc(obj->len+1);
241 obj->str[obj->len] = 0;
242 g_memmove(obj->str, p, obj->len);
250 while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
251 obj->type = PDFTYPE_NAME;
252 obj->str = g_strndup(p, q-p);
259 obj->type = PDFTYPE_ARRAY;
262 q=p+1; skipspace(&q, eof);
264 elt = parse_pdf_object(&q, eof);
265 if (elt==NULL) { free_pdfobj(obj); return NULL; }
267 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
268 obj->elts[obj->num-1] = elt;
276 if (*p=='<' && p[1]=='<') {
277 obj->type = PDFTYPE_DICT;
281 q=p+2; skipspace(&q, eof);
282 while (*q!='>' || q[1]!='>') {
283 if (*q!='/') { free_pdfobj(obj); return NULL; }
285 while (!ispdfspace(*r) && !ispdfdelim(*r)) r++;
286 eltname = g_strndup(q, r-q);
287 q=r; skipspace(&q, eof);
288 elt = parse_pdf_object(&q, eof);
289 if (elt==NULL) { g_free(eltname); free_pdfobj(obj); return NULL; }
291 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
292 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
293 obj->elts[obj->num-1] = elt;
294 obj->names[obj->num-1] = eltname;
301 // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
307 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
311 if (dict==NULL) return NULL;
312 if (dict->type != PDFTYPE_DICT) return NULL;
313 for (i=0; i<dict->num; i++)
314 if (!strcmp(dict->names[i], name)) return dict->elts[i];
318 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
323 if (obj==NULL) return NULL;
324 if (obj->type!=PDFTYPE_REF) return dup_pdfobj(obj);
325 if (obj->intval>xref->last) return NULL;
326 offs = xref->data[obj->intval];
327 if (offs<=0 || offs >= pdfbuf->len) return NULL;
329 p = pdfbuf->str + offs;
330 eof = pdfbuf->str + pdfbuf->len;
331 n = strtol(p, &p, 10);
332 if (n!=obj->intval) return NULL;
334 n = strtol(p, &p, 10);
336 if (strncmp(p, "obj", 3)) return NULL;
338 return parse_pdf_object(&p, eof);
341 // read the xref table of a PDF file in memory, and return the trailerdict
343 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
346 struct PdfObj *trailerdict, *obj;
349 if (strncmp(pdfbuf->str+offs, "xref", 4)) return NULL;
350 p = strstr(pdfbuf->str+offs, "trailer");
351 eof = pdfbuf->str + pdfbuf->len;
352 if (p==NULL) return NULL;
354 trailerdict = parse_pdf_object(&p, eof);
355 obj = get_dict_entry(trailerdict, "/Size");
356 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval-1>xref->last)
357 make_xref(xref, obj->intval-1, 0);
358 obj = get_dict_entry(trailerdict, "/Prev");
359 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval>0 && obj->intval!=offs) {
360 // recurse into older xref table
361 obj = parse_xref_table(pdfbuf, xref, obj->intval);
364 p = pdfbuf->str+offs+4;
366 if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
367 while (*p>='0' && *p<='9') {
368 start = strtol(p, &p, 10);
370 len = strtol(p, &p, 10);
372 if (len <= 0 || 20*len > eof-p) break;
373 if (start+len-1 > xref->last) make_xref(xref, start+len-1, 0);
374 for (i=start; i<start+len; i++) {
375 xref->data[i] = strtol(p, NULL, 10);
380 if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
384 // parse the page tree
386 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref,
387 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
389 struct PdfObj *obj, *kid;
392 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
393 if (obj == NULL || obj->type != PDFTYPE_NAME)
395 if (!strcmp(obj->str, "/Page")) {
397 pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
398 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
400 free_pdfobj(pages->resources);
401 pages->resources = obj;
403 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
405 free_pdfobj(pages->mediabox);
406 pages->mediabox = obj;
408 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
409 if (obj!=NULL && obj->type == PDFTYPE_INT)
410 pages->rotate = obj->intval;
414 else if (!strcmp(obj->str, "/Pages")) {
416 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Count"));
417 if (obj!=NULL && obj->type == PDFTYPE_INT &&
418 obj->intval>0 && obj->intval<=nmax) count = obj->intval;
421 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
423 for (i=0; i<count; i++) {
424 free_pdfobj(pages[i].resources);
425 pages[i].resources = dup_pdfobj(obj);
428 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
430 for (i=0; i<count; i++) {
431 free_pdfobj(pages[i].mediabox);
432 pages[i].mediabox = dup_pdfobj(obj);
435 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
436 if (obj!=NULL && obj->type == PDFTYPE_INT)
437 for (i=0; i<count; i++)
438 pages[i].rotate = obj->intval;
440 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Kids"));
441 if (obj!=NULL && obj->type == PDFTYPE_ARRAY) {
442 for (i=0; i<obj->num; i++) {
443 kid = get_pdfobj(pdfbuf, xref, obj->elts[i]);
445 j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
458 // parse a PDF file in memory
460 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
464 struct PdfObj *obj, *pages;
466 xref->n_alloc = xref->last = 0;
468 p = pdfbuf->str + pdfbuf->len-1;
470 while (*p!='s' && p!=pdfbuf->str) p--;
471 if (strncmp(p, "startxref", 9)) return FALSE; // fail
473 while (ispdfspace(*p) && p!=pdfbuf->str+pdfbuf->len) p++;
474 offs = strtol(p, NULL, 10);
475 if (offs <= 0 || offs > pdfbuf->len) return FALSE; // fail
476 pdfinfo->startxref = offs;
478 pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
479 if (pdfinfo->trailerdict == NULL) return FALSE; // fail
481 obj = get_pdfobj(pdfbuf, xref,
482 get_dict_entry(pdfinfo->trailerdict, "/Root"));
484 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
485 pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
488 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
489 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pages, "/Count"));
490 if (obj == NULL || obj->type != PDFTYPE_INT || obj->intval<=0)
491 { free_pdfobj(pdfinfo->trailerdict); free_pdfobj(pages);
492 free_pdfobj(obj); return FALSE; }
493 pdfinfo->npages = obj->intval;
496 pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
497 pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
503 // add an entry to the xref table
505 void make_xref(struct XrefTable *xref, int nobj, int offset)
507 if (xref->n_alloc <= nobj) {
508 xref->n_alloc = nobj + 10;
509 xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
511 if (xref->last < nobj) xref->last = nobj;
512 xref->data[nobj] = offset;
515 // a wrapper for deflate
517 GString *do_deflate(char *in, int len)
524 deflateInit(&zs, Z_DEFAULT_COMPRESSION);
525 zs.next_in = (Bytef *)in;
527 zs.avail_out = deflateBound(&zs, len);
528 out = g_string_sized_new(zs.avail_out);
529 zs.next_out = (Bytef *)out->str;
530 deflate(&zs, Z_FINISH);
531 out->len = zs.total_out;
536 // prefix to scale the original page
538 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
541 double v[4], t, xscl, yscl;
544 // push 3 times in case code to be annotated has unbalanced q/Q (B of A., ...)
545 str = g_string_new("q q q ");
546 if (pgdesc->rotate == 90) {
547 g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
548 t = height; height = width; width = t;
550 if (pgdesc->rotate == 270) {
551 g_string_append_printf(str, "0 1 -1 0 %.2f 0 cm ", width);
552 t = height; height = width; width = t;
554 if (pgdesc->rotate == 180) {
555 g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
557 if (pgdesc->mediabox==NULL || pgdesc->mediabox->type != PDFTYPE_ARRAY ||
558 pgdesc->mediabox->num != 4) return str;
559 for (i=0; i<4; i++) {
560 if (pgdesc->mediabox->elts[i]->type == PDFTYPE_INT)
561 v[i] = pgdesc->mediabox->elts[i]->intval;
562 else if (pgdesc->mediabox->elts[i]->type == PDFTYPE_REAL)
563 v[i] = pgdesc->mediabox->elts[i]->realval;
566 if (v[0]>v[2]) { t = v[0]; v[0] = v[2]; v[2] = t; }
567 if (v[1]>v[3]) { t = v[1]; v[1] = v[3]; v[3] = t; }
568 if (v[2]-v[0] < 1. || v[3]-v[1] < 1.) return str;
569 xscl = width/(v[2]-v[0]);
570 yscl = height/(v[3]-v[1]);
571 g_string_append_printf(str, "%.4f 0 0 %.4f %.2f %.2f cm ",
572 xscl, yscl, -v[0]*xscl, -v[1]*yscl);
576 // add an entry to a subentry of a directory
578 struct PdfObj *mk_pdfname(char *name)
582 obj = g_malloc(sizeof(struct PdfObj));
583 obj->type = PDFTYPE_NAME;
584 obj->str = g_strdup(name);
588 struct PdfObj *mk_pdfref(int num)
592 obj = g_malloc(sizeof(struct PdfObj));
593 obj->type = PDFTYPE_REF;
599 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
601 if (a==NULL || b==NULL) return (a==b);
602 if (a->type!=b->type) return FALSE;
603 if (a->type == PDFTYPE_CST || a->type == PDFTYPE_INT)
604 return (a->intval == b->intval);
605 if (a->type == PDFTYPE_REAL)
606 return (a->realval == b->realval);
607 if (a->type == PDFTYPE_NAME)
608 return !strcmp(a->str, b->str);
609 if (a->type == PDFTYPE_REF)
610 return (a->intval == b->intval && a->num == b->num);
614 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
615 struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
621 for (i=0; i<obj->num; i++)
622 if (!strcmp(obj->names[i], section)) subpos = i;
626 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj*));
627 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
628 obj->names[subpos] = g_strdup(section);
629 obj->elts[subpos] = NULL;
631 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type==PDFTYPE_REF) {
632 sec = get_pdfobj(pdfbuf, xref, obj->elts[subpos]);
633 free_pdfobj(obj->elts[subpos]);
634 obj->elts[subpos] = sec;
636 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type!=type)
637 { free_pdfobj(obj->elts[subpos]); obj->elts[subpos] = NULL; }
638 if (obj->elts[subpos] == NULL) {
639 obj->elts[subpos] = sec = g_malloc(sizeof(struct PdfObj));
645 sec = obj->elts[subpos];
648 if (type==PDFTYPE_DICT) {
649 for (i=0; i<sec->num; i++)
650 if (!strcmp(sec->names[i], name)) subpos = i;
654 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
655 sec->names = g_realloc(sec->names, sec->num*sizeof(char *));
656 sec->names[subpos] = g_strdup(name);
657 sec->elts[subpos] = NULL;
659 free_pdfobj(sec->elts[subpos]);
660 sec->elts[subpos] = entry;
662 if (type==PDFTYPE_ARRAY) {
663 for (i=0; i<sec->num; i++)
664 if (iseq_obj(sec->elts[i], entry)) subpos = i;
668 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
669 sec->elts[subpos] = entry;
671 else free_pdfobj(entry);
675 // draw a page's background
677 void pdf_draw_solid_background(struct Page *pg, GString *str)
681 g_string_append_printf(str,
682 "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
683 RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
684 if (!ui.print_ruling) return;
685 if (pg->bg->ruling == RULING_NONE) return;
686 g_string_append_printf(str,
687 "%.2f %.2f %.2f RG %.2f w ",
688 RGBA_RGB(RULING_COLOR), RULING_THICKNESS);
689 if (pg->bg->ruling == RULING_GRAPH) {
690 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
691 g_string_append_printf(str, "%.2f 0 m %.2f %.2f l S ",
693 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
694 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
698 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
699 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
701 if (pg->bg->ruling == RULING_LINED)
702 g_string_append_printf(str,
703 "%.2f %.2f %.2f RG %.2f 0 m %.2f %.2f l S ",
704 RGBA_RGB(RULING_MARGIN_COLOR),
705 RULING_LEFTMARGIN, RULING_LEFTMARGIN, pg->height);
708 int pdf_draw_bitmap_background(struct Page *pg, GString *str,
709 struct XrefTable *xref, GString *pdfbuf)
715 int height, width, stride, x, y, chan;
717 if (pg->bg->type == BG_PDF) {
718 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
719 if (pgpdf == NULL) return -1;
720 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
721 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
722 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING)
723 gtk_main_iteration();
727 else pix = pg->bg->pixbuf;
729 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return -1;
730 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return -1;
732 width = gdk_pixbuf_get_width(pix);
733 height = gdk_pixbuf_get_height(pix);
734 stride = gdk_pixbuf_get_rowstride(pix);
735 chan = gdk_pixbuf_get_n_channels(pix);
736 if (chan!=3 && chan!=4) return -1;
738 g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
739 pg->width, -pg->height, pg->height);
741 p2 = buf = (char *)g_malloc(3*width*height);
742 for (y=0; y<height; y++) {
743 p1 = (char *)gdk_pixbuf_get_pixels(pix)+stride*y;
744 for (x=0; x<width; x++) {
745 *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
749 zpix = do_deflate(buf, 3*width*height);
752 make_xref(xref, xref->last+1, pdfbuf->len);
753 g_string_append_printf(pdfbuf,
754 "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
755 "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
756 "/BitsPerComponent 8 >> stream\n",
757 xref->last, zpix->len, width, height);
758 g_string_append_len(pdfbuf, zpix->str, zpix->len);
759 g_string_free(zpix, TRUE);
760 g_string_append(pdfbuf, "endstream\nendobj\n");
765 // manipulate Pdf fonts
767 struct PdfFont *new_pdffont(struct XrefTable *xref, GList **fonts,
768 char *filename, int font_id, FT_Face face, int glyph_page)
771 struct PdfFont *font;
775 for (list = *fonts; list!=NULL; list = list->next) {
776 font = (struct PdfFont *)list->data;
777 if (!strcmp(font->filename, filename) && font->font_id == font_id
778 && font->glyph_page == glyph_page)
779 { font->used_in_this_page = TRUE; return font; }
781 font = g_malloc(sizeof(struct PdfFont));
782 *fonts = g_list_append(*fonts, font);
783 font->n_obj = xref->last+1;
784 make_xref(xref, xref->last+1, 0); // will give it a value later
785 font->filename = g_strdup(filename);
786 font->font_id = font_id;
787 font->glyph_page = glyph_page;
788 font->used_in_this_page = TRUE;
789 font->num_glyphs_used = 0;
790 for (i=0; i<256; i++) {
791 font->glyphmap[i] = -1;
792 font->advance[i] = 0;
793 font->glyphpsnames[i] = NULL;
795 font->glyphmap[0] = 0;
796 // fill in info from the FT_Face
797 font->is_truetype = FT_IS_SFNT(face);
798 font->nglyphs = face->num_glyphs;
799 font->ft2ps = 1000.0 / face->units_per_EM;
800 font->ascender = (int)(font->ft2ps*face->ascender);
801 font->descender = (int)(font->ft2ps*face->descender);
802 if (face->bbox.xMin < -100000 || face->bbox.xMin > 100000) font->xmin = 0;
803 else font->xmin = (int)(font->ft2ps*face->bbox.xMin);
804 if (face->bbox.xMax < -100000 || face->bbox.xMax > 100000) font->xmax = 0;
805 else font->xmax = (int)(font->ft2ps*face->bbox.xMax);
806 if (face->bbox.yMin < -100000 || face->bbox.yMin > 100000) font->ymin = 0;
807 else font->ymin = (int)(font->ft2ps*face->bbox.yMin);
808 if (face->bbox.yMax < -100000 || face->bbox.yMax > 100000) font->ymax = 0;
809 else font->ymax = (int)(font->ft2ps*face->bbox.yMax);
810 if (font->is_truetype) font->flags = 4; // symbolic
812 font->flags = 4; // symbolic
813 if (FT_IS_FIXED_WIDTH(face)) font->flags |= 1;
814 if (face->style_flags & FT_STYLE_FLAG_ITALIC) font->flags |= 64;
816 s = FT_Get_Postscript_Name(face);
817 if (s==NULL) s = "Noname";
818 if (glyph_page) font->fontname = g_strdup_printf("%s_%03d", s, glyph_page);
819 else font->fontname = g_strdup(s);
823 #define pfb_get_length(x) (((x)[3]<<24) + ((x)[2]<<16) + ((x)[1]<<8) + (x)[0])
824 #define T1_SEGMENT_1_END "currentfile eexec"
825 #define T1_SEGMENT_3_END "cleartomark"
827 void embed_pdffont(GString *pdfbuf, struct XrefTable *xref, struct PdfFont *font)
829 // this code inspired by libgnomeprint
830 gboolean fallback, is_binary;
831 guchar encoding[256];
833 int i, j, num, len1, len2;
836 char *tmpfile, *seg1, *seg2;
839 int nobj_fontprog, nobj_descr, lastchar;
842 // embed the font file: TrueType case
843 if (font->is_truetype) {
844 glyphs[0] = encoding[0] = 0;
846 for (i=1; i<=255; i++)
847 if (font->glyphmap[i]>=0) {
848 font->glyphmap[i] = num;
849 glyphs[num] = 255*font->glyph_page+i-1;
853 font->num_glyphs_used = num-1;
854 if (OpenTTFont(font->filename, 0, &ttfnt) == SF_OK) {
855 tmpfile = mktemp(g_strdup(TMPDIR_TEMPLATE));
856 CreateTTFromTTGlyphs(ttfnt, tmpfile, glyphs, encoding, num,
857 0, NULL, TTCF_AutoName | TTCF_IncludeOS2);
859 if (g_file_get_contents(tmpfile, &fontdata, &len, NULL) && len>=8) {
860 make_xref(xref, xref->last+1, pdfbuf->len);
861 nobj_fontprog = xref->last;
862 g_string_append_printf(pdfbuf,
863 "%d 0 obj\n<< /Length %d /Length1 %d >> stream\n",
864 nobj_fontprog, (int)len, (int)len);
865 g_string_append_len(pdfbuf, fontdata, len);
866 g_string_append(pdfbuf, "endstream\nendobj\n");
869 else fallback = TRUE;
873 else fallback = TRUE;
875 // embed the font file: Type1 case
876 if (g_file_get_contents(font->filename, &fontdata, &len, NULL) && len>=8) {
877 if (fontdata[0]==(char)0x80 && fontdata[1]==(char)0x01) {
879 len1 = pfb_get_length((unsigned char *)fontdata+2);
880 if (fontdata[len1+6]!=(char)0x80 || fontdata[len1+7]!=(char)0x02) fallback = TRUE;
882 len2 = pfb_get_length((unsigned char *)fontdata+len1+8);
883 if (fontdata[len1+len2+12]!=(char)0x80 || fontdata[len1+len2+13]!=(char)0x01)
887 else if (!strncmp(fontdata, "%!PS", 4)) {
889 p = strstr(fontdata, T1_SEGMENT_1_END) + strlen(T1_SEGMENT_1_END);
890 if (p==NULL) fallback = TRUE;
892 if (*p=='\n' || *p=='\r') p++;
893 if (*p=='\n' || *p=='\r') p++;
895 p = g_strrstr_len(fontdata, len, T1_SEGMENT_3_END);
896 if (p==NULL) fallback = TRUE;
900 while (i>0 && p!=fontdata && (*p=='0' || *p=='\r' || *p=='\n')) {
904 while (p!=fontdata && (*p=='\r' || *p=='\n')) p--;
906 if (i>0) fallback = TRUE;
907 else len2 = p-fontdata-len1;
911 else fallback = TRUE;
915 seg2 = fontdata + len1 + 12;
918 seg2 = g_malloc(len2/2);
921 while (p+1 < fontdata+len1+len2) {
922 if (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') { p++; continue; }
923 if (p[0]>'9') { p[0]|=0x20; p[0]-=39; }
924 if (p[1]>'9') { p[1]|=0x20; p[1]-=39; }
925 seg2[j++] = ((p[0]-'0')<<4) + (p[1]-'0');
930 make_xref(xref, xref->last+1, pdfbuf->len);
931 nobj_fontprog = xref->last;
932 g_string_append_printf(pdfbuf,
933 "%d 0 obj\n<< /Length %d /Length1 %d /Length2 %d /Length3 0 >> stream\n",
934 nobj_fontprog, len1+len2, len1, len2);
935 g_string_append_len(pdfbuf, seg1, len1);
936 g_string_append_len(pdfbuf, seg2, len2);
937 g_string_append(pdfbuf, "endstream\nendobj\n");
938 if (!is_binary) g_free(seg2);
942 else fallback = TRUE;
945 // next, the font descriptor
947 make_xref(xref, xref->last+1, pdfbuf->len);
948 nobj_descr = xref->last;
949 g_string_append_printf(pdfbuf,
950 "%d 0 obj\n<< /Type /FontDescriptor /FontName /%s /Flags %d "
951 "/FontBBox [%d %d %d %d] /ItalicAngle 0 /Ascent %d "
952 "/Descent %d /CapHeight %d /StemV 100 /%s %d 0 R >> endobj\n",
953 nobj_descr, font->fontname, font->flags,
954 font->xmin, font->ymin, font->xmax, font->ymax,
955 font->ascender, -font->descender, font->ascender,
956 font->is_truetype ? "FontFile2":"FontFile",
960 // finally, the font itself
961 /* Note: in Type1 case, font->glyphmap maps charcodes to glyph no's
962 in TrueType case, encoding lists the used charcodes by index,
963 glyphs list the used glyph no's by index
964 font->glyphmap maps charcodes to indices */
965 xref->data[font->n_obj] = pdfbuf->len;
966 if (font->is_truetype) lastchar = encoding[font->num_glyphs_used];
967 else lastchar = font->num_glyphs_used;
969 font->is_truetype = FALSE;
970 g_free(font->fontname);
971 font->fontname = g_strdup("Helvetica");
974 if (font->is_truetype) {
975 num = font->glyph_page;
976 for (i=0; i<6; i++) { prefix[i] = 'A'+(num%26); num/=26; }
977 prefix[6]='+'; prefix[7]=0;
979 g_string_append_printf(pdfbuf,
980 "%d 0 obj\n<< /Type /Font /Subtype /%s /BaseFont /%s%s /Name /F%d ",
981 font->n_obj, font->is_truetype?"TrueType":"Type1",
982 prefix, font->fontname, font->n_obj);
984 g_string_append_printf(pdfbuf,
985 "/FontDescriptor %d 0 R /FirstChar 0 /LastChar %d /Widths [",
986 nobj_descr, lastchar);
987 for (i=0; i<=lastchar; i++)
988 g_string_append_printf(pdfbuf, "%d ", font->advance[i]);
989 g_string_append(pdfbuf, "] ");
991 if (!font->is_truetype) { /* encoding */
992 g_string_append(pdfbuf, "/Encoding << /Type /Encoding "
993 "/BaseEncoding /MacRomanEncoding /Differences [1 ");
994 for (i=1; i<=lastchar; i++) {
995 g_string_append_printf(pdfbuf, "/%s ", font->glyphpsnames[i]);
996 g_free(font->glyphpsnames[i]);
998 g_string_append(pdfbuf, "] >> ");
1000 g_string_append(pdfbuf, ">> endobj\n");
1003 // draw a page's graphics
1005 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter,
1006 struct XrefTable *xref, GList **pdffonts)
1008 GList *layerlist, *itemlist, *tmplist;
1011 guint old_rgba, old_text_rgba;
1012 double old_thickness;
1015 PangoFontDescription *font_desc;
1016 PangoContext *context;
1017 PangoLayout *layout;
1018 PangoLayoutIter *iter;
1019 PangoRectangle logical_rect;
1020 PangoLayoutRun *run;
1021 PangoFcFont *fcfont;
1023 int baseline, advance;
1024 int glyph_no, glyph_page, current_page;
1029 struct PdfFont *cur_font;
1032 old_rgba = old_text_rgba = 0x12345678; // not any values we use, so we'll reset them
1033 old_thickness = 0.0;
1034 for (tmplist = *pdffonts; tmplist!=NULL; tmplist = tmplist->next) {
1035 cur_font = (struct PdfFont *)tmplist->data;
1036 cur_font->used_in_this_page = FALSE;
1039 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1040 l = (struct Layer *)layerlist->data;
1041 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1042 item = (struct Item *)itemlist->data;
1043 if (item->type == ITEM_STROKE) {
1044 if ((item->brush.color_rgba & ~0xff) != old_rgba)
1045 g_string_append_printf(str, "%.2f %.2f %.2f RG ",
1046 RGBA_RGB(item->brush.color_rgba));
1047 if (item->brush.thickness != old_thickness)
1048 g_string_append_printf(str, "%.2f w ", item->brush.thickness);
1049 if ((item->brush.color_rgba & 0xf0) != 0xf0) { // transparent
1050 g_string_append(str, "q /XoHi gs ");
1051 *use_hiliter = TRUE;
1053 old_rgba = item->brush.color_rgba & ~0xff;
1054 old_thickness = item->brush.thickness;
1055 pt = item->path->coords;
1056 if (!item->brush.variable_width) {
1057 g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
1058 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1059 g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
1060 g_string_append_printf(str,"S\n");
1061 old_thickness = item->brush.thickness;
1063 for (i=0; i<item->path->num_points-1; i++, pt+=2)
1064 g_string_append_printf(str, "%.2f w %.2f %.2f m %.2f %.2f l S\n",
1065 item->widths[i], pt[0], pt[1], pt[2], pt[3]);
1066 old_thickness = 0.0;
1068 if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
1069 g_string_append(str, "Q ");
1071 else if (item->type == ITEM_TEXT) {
1072 if ((item->brush.color_rgba & ~0xff) != old_text_rgba)
1073 g_string_append_printf(str, "%.2f %.2f %.2f rg ",
1074 RGBA_RGB(item->brush.color_rgba));
1075 old_text_rgba = item->brush.color_rgba & ~0xff;
1076 context = gnome_print_pango_create_context(gnome_print_pango_get_default_font_map());
1077 layout = pango_layout_new(context);
1078 g_object_unref(context);
1079 font_desc = pango_font_description_from_string(item->font_name);
1080 pango_font_description_set_absolute_size(font_desc,
1081 item->font_size*PANGO_SCALE);
1082 pango_layout_set_font_description(layout, font_desc);
1083 pango_font_description_free(font_desc);
1084 pango_layout_set_text(layout, item->text, -1);
1085 // this code inspired by the code in libgnomeprint
1086 iter = pango_layout_get_iter(layout);
1088 run = pango_layout_iter_get_run(iter);
1089 if (run==NULL) continue;
1090 pango_layout_iter_get_run_extents (iter, NULL, &logical_rect);
1091 baseline = pango_layout_iter_get_baseline (iter);
1092 if (!PANGO_IS_FC_FONT(run->item->analysis.font)) continue;
1093 fcfont = PANGO_FC_FONT(run->item->analysis.font);
1094 pattern = fcfont->font_pattern;
1095 if (FcPatternGetString(pattern, FC_FILE, 0, (unsigned char **)&filename) != FcResultMatch ||
1096 FcPatternGetInteger(pattern, FC_INDEX, 0, &font_id) != FcResultMatch)
1098 ftface = pango_fc_font_lock_face(fcfont);
1101 g_string_append_printf(str, "BT %.2f 0 0 %.2f %.2f %.2f Tm ",
1102 item->font_size, -item->font_size,
1103 item->bbox.left + (gdouble) logical_rect.x/PANGO_SCALE,
1104 item->bbox.top + (gdouble) baseline/PANGO_SCALE);
1106 for (i=0; i<run->glyphs->num_glyphs; i++) {
1107 glyph_no = run->glyphs->glyphs[i].glyph;
1108 if (FT_IS_SFNT(ftface)) glyph_page = glyph_no/255;
1109 else glyph_page = 0;
1110 if (glyph_page != current_page) {
1111 cur_font = new_pdffont(xref, pdffonts, filename, font_id,
1112 ftface, glyph_page);
1113 if (in_string) g_string_append(str, ") Tj ");
1115 g_string_append_printf(str, "/F%d 1 Tf ", cur_font->n_obj);
1117 current_page = glyph_page;
1118 FT_Load_Glyph(ftface, glyph_no, FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
1119 advance = (int)(ftface->glyph->metrics.horiAdvance * cur_font->ft2ps + 0.5);
1120 if (!in_string) g_string_append_c(str, '(');
1122 if (cur_font->is_truetype) {
1123 if (glyph_no) glyph_no = (glyph_no%255)+1;
1124 cur_font->glyphmap[glyph_no] = glyph_no;
1127 for (j=1; j<=cur_font->num_glyphs_used; j++)
1128 if (cur_font->glyphmap[j] == glyph_no) break;
1129 if (j==256) j=0; // font is full, what do we do?
1130 if (j>cur_font->num_glyphs_used) {
1131 cur_font->glyphmap[j] = glyph_no;
1132 cur_font->num_glyphs_used++;
1133 if (FT_Get_Glyph_Name(ftface, glyph_no, tmpstr, 200) == FT_Err_Ok)
1134 cur_font->glyphpsnames[j] = g_strdup(tmpstr);
1135 else cur_font->glyphpsnames[j] = g_strdup(".notdef");
1139 cur_font->advance[glyph_no] = advance;
1140 if (glyph_no=='\\' || glyph_no == '(' || glyph_no == ')' || glyph_no == 10 || glyph_no == 13)
1141 g_string_append_c(str, '\\');
1142 if (glyph_no==10) g_string_append_c(str,'n');
1143 else if (glyph_no==13) g_string_append_c(str,'r');
1144 else g_string_append_c(str, glyph_no);
1146 if (in_string) g_string_append(str, ") Tj ");
1147 g_string_append(str, "ET ");
1148 pango_fc_font_unlock_face(fcfont);
1149 } while (pango_layout_iter_next_run(iter));
1150 pango_layout_iter_free(iter);
1151 g_object_unref(layout);
1157 // main printing function
1159 /* we use the following object numbers, starting with n_obj_catalog:
1160 0 the document catalog
1162 2 the GS for the hiliters
1163 3 ... the page objects
1166 gboolean print_to_pdf(char *filename)
1169 GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
1170 int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
1172 struct XrefTable xref;
1177 gboolean annot, uses_pdf;
1178 gboolean use_hiliter;
1179 struct PdfInfo pdfinfo;
1181 GList *pdffonts, *list;
1182 struct PdfFont *font;
1185 f = fopen(filename, "w");
1186 if (f == NULL) return FALSE;
1187 setlocale(LC_NUMERIC, "C");
1192 for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
1193 pg = (struct Page *)pglist->data;
1194 if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
1197 if (uses_pdf && bgpdf.status != STATUS_NOT_INIT &&
1198 g_file_get_contents(bgpdf.tmpfile_copy, &buf, &len, NULL) &&
1199 !strncmp(buf, "%PDF-1.", 7)) {
1200 // parse the existing PDF file
1201 pdfbuf = g_string_new_len(buf, len);
1203 if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
1204 annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
1206 g_string_free(pdfbuf, TRUE);
1207 if (xref.data != NULL) g_free(xref.data);
1212 pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
1213 xref.n_alloc = xref.last = 0;
1217 // catalog and page tree
1218 n_obj_catalog = xref.last+1;
1219 n_obj_pages_offs = xref.last+4;
1220 make_xref(&xref, n_obj_catalog, pdfbuf->len);
1221 g_string_append_printf(pdfbuf,
1222 "%d 0 obj\n<< /Type /Catalog /Pages %d 0 R >> endobj\n",
1223 n_obj_catalog, n_obj_catalog+1);
1224 make_xref(&xref, n_obj_catalog+1, pdfbuf->len);
1225 g_string_append_printf(pdfbuf,
1226 "%d 0 obj\n<< /Type /Pages /Kids [", n_obj_catalog+1);
1227 for (i=0;i<journal.npages;i++)
1228 g_string_append_printf(pdfbuf, "%d 0 R ", n_obj_pages_offs+i);
1229 g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
1230 make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
1231 g_string_append_printf(pdfbuf,
1232 "%d 0 obj\n<< /Type /ExtGState /CA %.2f >> endobj\n",
1233 n_obj_catalog+2, ui.hiliter_opacity);
1234 xref.last = n_obj_pages_offs + journal.npages-1;
1236 for (pglist = journal.pages, n_page = 0; pglist!=NULL;
1237 pglist = pglist->next, n_page++) {
1238 pg = (struct Page *)pglist->data;
1240 // draw the background and page into pgstrm
1241 pgstrm = g_string_new("");
1242 g_string_printf(pgstrm, "q 1 0 0 -1 0 %.2f cm 1 J 1 j ", pg->height);
1245 if (pg->bg->type == BG_SOLID)
1246 pdf_draw_solid_background(pg, pgstrm);
1247 else if (pg->bg->type == BG_PDF && annot &&
1248 pdfinfo.pages[pg->bg->file_page_seq-1].contents!=NULL) {
1249 make_xref(&xref, xref.last+1, pdfbuf->len);
1250 n_obj_prefix = xref.last;
1251 tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
1252 pg->width, pg->height);
1253 g_string_append_printf(pdfbuf,
1254 "%d 0 obj\n<< /Length %d >> stream\n%s\nendstream\nendobj\n",
1255 n_obj_prefix, tmpstr->len, tmpstr->str);
1256 g_string_free(tmpstr, TRUE);
1257 g_string_prepend(pgstrm, "Q Q Q ");
1259 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
1260 n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
1261 // draw the page contents
1262 use_hiliter = FALSE;
1263 pdf_draw_page(pg, pgstrm, &use_hiliter, &xref, &pdffonts);
1264 g_string_append_printf(pgstrm, "Q\n");
1266 // deflate pgstrm and write it
1267 zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
1268 g_string_free(pgstrm, TRUE);
1270 make_xref(&xref, xref.last+1, pdfbuf->len);
1271 g_string_append_printf(pdfbuf,
1272 "%d 0 obj\n<< /Length %d /Filter /FlateDecode>> stream\n",
1273 xref.last, zpgstrm->len);
1274 g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
1275 g_string_free(zpgstrm, TRUE);
1276 g_string_append(pdfbuf, "endstream\nendobj\n");
1278 // write the page object
1280 make_xref(&xref, n_obj_pages_offs+n_page, pdfbuf->len);
1281 g_string_append_printf(pdfbuf,
1282 "%d 0 obj\n<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %.2f %.2f] ",
1283 n_obj_pages_offs+n_page, n_obj_catalog+1, pg->width, pg->height);
1284 if (n_obj_prefix>0) {
1285 obj = get_pdfobj(pdfbuf, &xref, pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1286 if (obj->type != PDFTYPE_ARRAY) {
1288 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1290 g_string_append_printf(pdfbuf, "/Contents [%d 0 R ", n_obj_prefix);
1291 if (obj->type == PDFTYPE_REF)
1292 g_string_append_printf(pdfbuf, "%d %d R ", obj->intval, obj->num);
1293 if (obj->type == PDFTYPE_ARRAY) {
1294 for (i=0; i<obj->num; i++) {
1295 show_pdfobj(obj->elts[i], pdfbuf);
1296 g_string_append_c(pdfbuf, ' ');
1300 g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
1302 else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
1303 g_string_append(pdfbuf, "/Resources ");
1306 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
1308 if (obj!=NULL && obj->type!=PDFTYPE_DICT)
1309 { free_pdfobj(obj); obj=NULL; }
1311 obj = g_malloc(sizeof(struct PdfObj));
1312 obj->type = PDFTYPE_DICT;
1317 add_dict_subentry(pdfbuf, &xref,
1318 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
1320 add_dict_subentry(pdfbuf, &xref,
1321 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
1323 add_dict_subentry(pdfbuf, &xref,
1324 obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
1326 add_dict_subentry(pdfbuf, &xref,
1327 obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
1328 for (list=pdffonts; list!=NULL; list = list->next) {
1329 font = (struct PdfFont *)list->data;
1330 if (font->used_in_this_page) {
1331 add_dict_subentry(pdfbuf, &xref,
1332 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/Text"));
1333 tmpbuf = g_strdup_printf("/F%d", font->n_obj);
1334 add_dict_subentry(pdfbuf, &xref,
1335 obj, "/Font", PDFTYPE_DICT, tmpbuf, mk_pdfref(font->n_obj));
1339 show_pdfobj(obj, pdfbuf);
1341 g_string_append(pdfbuf, " >> endobj\n");
1344 // after the pages, we insert fonts
1345 for (list = pdffonts; list!=NULL; list = list->next) {
1346 font = (struct PdfFont *)list->data;
1347 embed_pdffont(pdfbuf, &xref, font);
1348 g_free(font->filename);
1349 g_free(font->fontname);
1352 g_list_free(pdffonts);
1355 startxref = pdfbuf->len;
1356 if (annot) g_string_append_printf(pdfbuf,
1357 "xref\n%d %d\n", n_obj_catalog, xref.last-n_obj_catalog+1);
1358 else g_string_append_printf(pdfbuf,
1359 "xref\n0 %d\n0000000000 65535 f \n", xref.last+1);
1360 for (i=n_obj_catalog; i<=xref.last; i++)
1361 g_string_append_printf(pdfbuf, "%010d 00000 n \n", xref.data[i]);
1362 g_string_append_printf(pdfbuf,
1363 "trailer\n<< /Size %d /Root %d 0 R ", xref.last+1, n_obj_catalog);
1365 g_string_append_printf(pdfbuf, "/Prev %d ", pdfinfo.startxref);
1366 // keeping encryption info somehow doesn't work.
1367 // xournal can't annotate encrypted PDFs anyway...
1369 obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
1371 g_string_append_printf(pdfbuf, "/Encrypt ");
1372 show_pdfobj(obj, pdfbuf);
1376 g_string_append_printf(pdfbuf,
1377 ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
1381 free_pdfobj(pdfinfo.trailerdict);
1382 if (pdfinfo.pages!=NULL)
1383 for (i=0; i<pdfinfo.npages; i++) {
1384 free_pdfobj(pdfinfo.pages[i].resources);
1385 free_pdfobj(pdfinfo.pages[i].mediabox);
1386 free_pdfobj(pdfinfo.pages[i].contents);
1390 setlocale(LC_NUMERIC, "");
1391 if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
1393 g_string_free(pdfbuf, TRUE);
1397 g_string_free(pdfbuf, TRUE);
1401 /*********** Printing via libgnomeprint **********/
1403 // does the same job as update_canvas_bg(), but to a print context
1405 void print_background(GnomePrintContext *gpc, struct Page *pg, gboolean *abort)
1411 if (pg->bg->type == BG_SOLID) {
1412 gnome_print_setopacity(gpc, 1.0);
1413 gnome_print_setrgbcolor(gpc, RGBA_RGB(pg->bg->color_rgba));
1414 gnome_print_rect_filled(gpc, 0, 0, pg->width, -pg->height);
1416 if (!ui.print_ruling) return;
1417 if (pg->bg->ruling == RULING_NONE) return;
1418 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_COLOR));
1419 gnome_print_setlinewidth(gpc, RULING_THICKNESS);
1421 if (pg->bg->ruling == RULING_GRAPH) {
1422 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
1423 gnome_print_line_stroked(gpc, x, 0, x, -pg->height);
1424 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
1425 gnome_print_line_stroked(gpc, 0, -y, pg->width, -y);
1429 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
1430 gnome_print_line_stroked(gpc, 0, -y, pg->width, -y);
1431 if (pg->bg->ruling == RULING_LINED) {
1432 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_MARGIN_COLOR));
1433 gnome_print_line_stroked(gpc, RULING_LEFTMARGIN, 0, RULING_LEFTMARGIN, -pg->height);
1437 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF) {
1438 if (pg->bg->type == BG_PDF) {
1439 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
1440 if (pgpdf == NULL) return;
1441 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
1442 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
1443 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING) {
1444 gtk_main_iteration();
1448 pix = pgpdf->pixbuf;
1450 else pix = pg->bg->pixbuf;
1451 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return;
1452 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return;
1453 gnome_print_gsave(gpc);
1454 gnome_print_scale(gpc, pg->width, pg->height);
1455 gnome_print_translate(gpc, 0., -1.);
1456 if (gdk_pixbuf_get_n_channels(pix) == 3)
1457 gnome_print_rgbimage(gpc, gdk_pixbuf_get_pixels(pix),
1458 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1459 else if (gdk_pixbuf_get_n_channels(pix) == 4)
1460 gnome_print_rgbaimage(gpc, gdk_pixbuf_get_pixels(pix),
1461 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1462 gnome_print_grestore(gpc);
1467 void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
1468 double pgwidth, double pgheight, gboolean *abort)
1473 double old_thickness;
1474 GList *layerlist, *itemlist;
1479 PangoFontDescription *font_desc;
1480 PangoLayout *layout;
1482 if (pg==NULL) return;
1484 g_snprintf(tmp, 10, "Page %d", pageno);
1485 gnome_print_beginpage(gpc, (guchar *)tmp);
1486 gnome_print_gsave(gpc);
1488 scale = MIN(pgwidth/pg->width, pgheight/pg->height)*0.95;
1489 gnome_print_translate(gpc,
1490 (pgwidth - scale*pg->width)/2, (pgheight + scale*pg->height)/2);
1491 gnome_print_scale(gpc, scale, scale);
1492 gnome_print_setlinejoin(gpc, 1); // round
1493 gnome_print_setlinecap(gpc, 1); // round
1495 print_background(gpc, pg, abort);
1497 old_rgba = 0x12345678; // not any values we use, so we'll reset them
1498 old_thickness = 0.0;
1500 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1502 l = (struct Layer *)layerlist->data;
1503 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1505 item = (struct Item *)itemlist->data;
1506 if (item->type == ITEM_STROKE || item->type == ITEM_TEXT) {
1507 if ((item->brush.color_rgba & ~0xff) != (old_rgba & ~0xff))
1508 gnome_print_setrgbcolor(gpc, RGBA_RGB(item->brush.color_rgba));
1509 if ((item->brush.color_rgba & 0xff) != (old_rgba & 0xff))
1510 gnome_print_setopacity(gpc, RGBA_ALPHA(item->brush.color_rgba));
1511 old_rgba = item->brush.color_rgba;
1513 if (item->type == ITEM_STROKE) {
1514 if (item->brush.thickness != old_thickness)
1515 gnome_print_setlinewidth(gpc, item->brush.thickness);
1516 gnome_print_newpath(gpc);
1517 pt = item->path->coords;
1518 if (!item->brush.variable_width) {
1519 gnome_print_moveto(gpc, pt[0], -pt[1]);
1520 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1521 gnome_print_lineto(gpc, pt[0], -pt[1]);
1522 gnome_print_stroke(gpc);
1523 old_thickness = item->brush.thickness;
1525 for (i=0; i<item->path->num_points-1; i++, pt+=2) {
1526 gnome_print_moveto(gpc, pt[0], -pt[1]);
1527 gnome_print_setlinewidth(gpc, item->widths[i]);
1528 gnome_print_lineto(gpc, pt[2], -pt[3]);
1529 gnome_print_stroke(gpc);
1531 old_thickness = 0.0;
1534 if (item->type == ITEM_TEXT) {
1535 layout = gnome_print_pango_create_layout(gpc);
1536 font_desc = pango_font_description_from_string(item->font_name);
1537 pango_font_description_set_absolute_size(font_desc,
1538 item->font_size*PANGO_SCALE);
1539 pango_layout_set_font_description(layout, font_desc);
1540 pango_font_description_free(font_desc);
1541 pango_layout_set_text(layout, item->text, -1);
1542 gnome_print_moveto(gpc, item->bbox.left, -item->bbox.top);
1543 gnome_print_pango_layout(gpc, layout);
1544 g_object_unref(layout);
1549 gnome_print_grestore(gpc);
1550 gnome_print_showpage(gpc);
1553 void cb_print_abort(GtkDialog *dialog, gint response, gboolean *abort)
1558 void print_job_render(GnomePrintJob *gpj, int fromPage, int toPage)
1560 GnomePrintConfig *config;
1561 GnomePrintContext *gpc;
1562 GtkWidget *wait_dialog;
1563 double pgwidth, pgheight;
1567 config = gnome_print_job_get_config(gpj);
1568 gnome_print_config_get_page_size(config, &pgwidth, &pgheight);
1569 g_object_unref(G_OBJECT(config));
1571 gpc = gnome_print_job_get_context(gpj);
1574 wait_dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
1575 GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL, "Preparing print job");
1576 gtk_widget_show(wait_dialog);
1577 g_signal_connect(wait_dialog, "response", G_CALLBACK (cb_print_abort), &abort);
1579 for (i = fromPage; i <= toPage; i++) {
1580 #if GTK_CHECK_VERSION(2,6,0)
1581 if (!gtk_check_version(2, 6, 0))
1582 gtk_message_dialog_format_secondary_text(
1583 GTK_MESSAGE_DIALOG(wait_dialog), "Page %d", i+1);
1585 while (gtk_events_pending()) gtk_main_iteration();
1586 print_page(gpc, (struct Page *)g_list_nth_data(journal.pages, i), i+1,
1587 pgwidth, pgheight, &abort);
1590 #if GTK_CHECK_VERSION(2,6,0)
1591 if (!gtk_check_version(2, 6, 0))
1592 gtk_message_dialog_format_secondary_text(
1593 GTK_MESSAGE_DIALOG(wait_dialog), "Finalizing...");
1595 while (gtk_events_pending()) gtk_main_iteration();
1597 gnome_print_context_close(gpc);
1598 g_object_unref(G_OBJECT(gpc));
1600 gnome_print_job_close(gpj);
1601 if (!abort) gnome_print_job_print(gpj);
1602 g_object_unref(G_OBJECT(gpj));
1604 gtk_widget_destroy(wait_dialog);