6 #include <libgnomecanvas/libgnomecanvas.h>
7 #include <libgnomeprint/gnome-print-job.h>
18 #define RGBA_RED(rgba) (((rgba>>24)&0xff)/255.0)
19 #define RGBA_GREEN(rgba) (((rgba>>16)&0xff)/255.0)
20 #define RGBA_BLUE(rgba) (((rgba>>8)&0xff)/255.0)
21 #define RGBA_ALPHA(rgba) (((rgba>>0)&0xff)/255.0)
22 #define RGBA_RGB(rgba) RGBA_RED(rgba), RGBA_GREEN(rgba), RGBA_BLUE(rgba)
24 /*********** Printing to PDF ************/
26 gboolean ispdfspace(char c)
28 return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
31 gboolean ispdfdelim(char c)
33 return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
34 c=='{' || c=='}' || c=='/' || c=='%');
37 void skipspace(char **p, char *eof)
39 while (ispdfspace(**p) || **p=='%') {
40 if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
46 void free_pdfobj(struct PdfObj *obj)
50 if (obj==NULL) return;
51 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
52 obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
54 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
55 obj->type == PDFTYPE_STREAM) && obj->num>0) {
56 for (i=0; i<obj->num; i++)
57 free_pdfobj(obj->elts[i]);
60 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
61 for (i=0; i<obj->num; i++)
62 g_free(obj->names[i]);
68 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
73 if (obj==NULL) return NULL;
74 dup = g_memdup(obj, sizeof(struct PdfObj));
75 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
76 obj->type == PDFTYPE_STREAM) && obj->str!=NULL) {
77 if (obj->type == PDFTYPE_NAME) obj->len = strlen(obj->str);
78 dup->str = g_memdup(obj->str, obj->len+1);
80 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
81 obj->type == PDFTYPE_STREAM) && obj->num>0) {
82 dup->elts = g_malloc(obj->num*sizeof(struct PdfObj *));
83 for (i=0; i<obj->num; i++)
84 dup->elts[i] = dup_pdfobj(obj->elts[i]);
86 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
87 dup->names = g_malloc(obj->num*sizeof(char *));
88 for (i=0; i<obj->num; i++)
89 dup->names[i] = g_strdup(obj->names[i]);
94 void show_pdfobj(struct PdfObj *obj, GString *str)
97 if (obj==NULL) return;
100 if (obj->intval==1) g_string_append(str, "true");
101 if (obj->intval==0) g_string_append(str, "false");
102 if (obj->intval==-1) g_string_append(str, "null");
105 g_string_append_printf(str, "%d", obj->intval);
108 g_string_append_printf(str, "%f", obj->realval);
111 g_string_append_len(str, obj->str, obj->len);
114 g_string_append(str, obj->str);
117 g_string_append_c(str, '[');
118 for (i=0;i<obj->num;i++) {
119 if (i) g_string_append_c(str, ' ');
120 show_pdfobj(obj->elts[i], str);
122 g_string_append_c(str, ']');
125 g_string_append(str, "<<");
126 for (i=0;i<obj->num;i++) {
127 g_string_append_printf(str, " %s ", obj->names[i]);
128 show_pdfobj(obj->elts[i], str);
130 g_string_append(str, " >>");
133 g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
138 void DEBUG_PRINTOBJ(struct PdfObj *obj)
140 GString *s = g_string_new("");
143 g_string_free(s, TRUE);
146 // parse a PDF object; returns NULL if fails
147 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
149 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
151 struct PdfObj *obj, *elt;
152 char *p, *q, *r, *eltname;
155 obj = g_malloc(sizeof(struct PdfObj));
158 if (p==eof) { g_free(obj); return NULL; }
161 if (!strncmp(p, "true", 4)) {
162 obj->type = PDFTYPE_CST;
167 if (!strncmp(p, "false", 5)) {
168 obj->type = PDFTYPE_CST;
173 if (!strncmp(p, "null", 4)) {
174 obj->type = PDFTYPE_CST;
181 obj->intval = strtol(p, &q, 10);
185 obj->type = PDFTYPE_REAL;
186 obj->realval = g_ascii_strtod(p, ptr);
189 if (ispdfspace(*q)) {
190 // check for indirect reference
192 obj->num = strtol(q, &r, 10);
197 obj->type = PDFTYPE_REF;
202 obj->type = PDFTYPE_INT;
209 while (stack>0 && q!=eof) {
210 if (*q=='(') stack++;
211 if (*q==')') stack--;
215 if (q==eof) { g_free(obj); return NULL; }
216 obj->type = PDFTYPE_STRING;
218 obj->str = g_malloc(obj->len+1);
219 obj->str[obj->len] = 0;
220 g_memmove(obj->str, p, obj->len);
224 if (*p=='<' && p[1]!='<') {
226 while (*q!='>' && q!=eof) q++;
227 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);
241 while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
242 obj->type = PDFTYPE_NAME;
243 obj->str = g_strndup(p, q-p);
250 obj->type = PDFTYPE_ARRAY;
253 q=p+1; skipspace(&q, eof);
255 elt = parse_pdf_object(&q, eof);
256 if (elt==NULL) { free_pdfobj(obj); return NULL; }
258 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
259 obj->elts[obj->num-1] = elt;
267 if (*p=='<' && p[1]=='<') {
268 obj->type = PDFTYPE_DICT;
272 q=p+2; skipspace(&q, eof);
273 while (*q!='>' || q[1]!='>') {
274 if (*q!='/') { free_pdfobj(obj); return NULL; }
276 while (!ispdfspace(*r) && !ispdfdelim(*r)) r++;
277 eltname = g_strndup(q, r-q);
278 q=r; skipspace(&q, eof);
279 elt = parse_pdf_object(&q, eof);
280 if (elt==NULL) { g_free(eltname); free_pdfobj(obj); return NULL; }
282 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
283 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
284 obj->elts[obj->num-1] = elt;
285 obj->names[obj->num-1] = eltname;
292 // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
298 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
302 if (dict==NULL) return NULL;
303 if (dict->type != PDFTYPE_DICT) return NULL;
304 for (i=0; i<dict->num; i++)
305 if (!strcmp(dict->names[i], name)) return dict->elts[i];
309 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
314 if (obj==NULL) return NULL;
315 if (obj->type!=PDFTYPE_REF) return dup_pdfobj(obj);
316 if (obj->intval>xref->last) return NULL;
317 offs = xref->data[obj->intval];
318 if (offs<=0 || offs >= pdfbuf->len) return NULL;
320 p = pdfbuf->str + offs;
321 eof = pdfbuf->str + pdfbuf->len;
322 n = strtol(p, &p, 10);
323 if (n!=obj->intval) return NULL;
325 n = strtol(p, &p, 10);
327 if (strncmp(p, "obj", 3)) return NULL;
329 return parse_pdf_object(&p, eof);
332 // read the xref table of a PDF file in memory, and return the trailerdict
334 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
337 struct PdfObj *trailerdict, *obj;
340 if (strncmp(pdfbuf->str+offs, "xref", 4)) return NULL;
341 p = strstr(pdfbuf->str+offs, "trailer");
342 eof = pdfbuf->str + pdfbuf->len;
343 if (p==NULL) return NULL;
345 trailerdict = parse_pdf_object(&p, eof);
346 obj = get_dict_entry(trailerdict, "/Size");
347 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval-1>xref->last)
348 make_xref(xref, obj->intval-1, 0);
349 obj = get_dict_entry(trailerdict, "/Prev");
350 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval>0 && obj->intval!=offs) {
351 // recurse into older xref table
352 obj = parse_xref_table(pdfbuf, xref, obj->intval);
355 p = pdfbuf->str+offs+4;
357 if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
358 while (*p>='0' && *p<='9') {
359 start = strtol(p, &p, 10);
361 len = strtol(p, &p, 10);
363 if (len <= 0 || 20*len > eof-p) break;
364 if (start+len-1 > xref->last) make_xref(xref, start+len-1, 0);
365 for (i=start; i<start+len; i++) {
366 xref->data[i] = strtol(p, NULL, 10);
371 if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
375 // parse the page tree
377 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref,
378 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
380 struct PdfObj *obj, *kid;
383 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
384 if (obj == NULL || obj->type != PDFTYPE_NAME)
386 if (!strcmp(obj->str, "/Page")) {
388 pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
389 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
391 free_pdfobj(pages->resources);
392 pages->resources = obj;
394 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
396 free_pdfobj(pages->mediabox);
397 pages->mediabox = obj;
399 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
400 if (obj!=NULL && obj->type == PDFTYPE_INT)
401 pages->rotate = obj->intval;
405 else if (!strcmp(obj->str, "/Pages")) {
407 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Count"));
408 if (obj!=NULL && obj->type == PDFTYPE_INT &&
409 obj->intval>0 && obj->intval<=nmax) count = obj->intval;
412 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
414 for (i=0; i<count; i++) {
415 free_pdfobj(pages[i].resources);
416 pages[i].resources = dup_pdfobj(obj);
419 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
421 for (i=0; i<count; i++) {
422 free_pdfobj(pages[i].mediabox);
423 pages[i].mediabox = dup_pdfobj(obj);
426 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
427 if (obj!=NULL && obj->type == PDFTYPE_INT)
428 for (i=0; i<count; i++)
429 pages[i].rotate = obj->intval;
431 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Kids"));
432 if (obj!=NULL && obj->type == PDFTYPE_ARRAY) {
433 for (i=0; i<obj->num; i++) {
434 kid = get_pdfobj(pdfbuf, xref, obj->elts[i]);
436 j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
449 // parse a PDF file in memory
451 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
455 struct PdfObj *obj, *pages;
457 xref->n_alloc = xref->last = 0;
459 p = pdfbuf->str + pdfbuf->len-1;
461 while (*p!='s' && p!=pdfbuf->str) p--;
462 if (strncmp(p, "startxref", 9)) return FALSE; // fail
464 while (ispdfspace(*p) && p!=pdfbuf->str+pdfbuf->len) p++;
465 offs = strtol(p, NULL, 10);
466 if (offs <= 0 || offs > pdfbuf->len) return FALSE; // fail
467 pdfinfo->startxref = offs;
469 pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
470 if (pdfinfo->trailerdict == NULL) return FALSE; // fail
472 obj = get_pdfobj(pdfbuf, xref,
473 get_dict_entry(pdfinfo->trailerdict, "/Root"));
475 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
476 pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
479 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
480 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pages, "/Count"));
481 if (obj == NULL || obj->type != PDFTYPE_INT || obj->intval<=0)
482 { free_pdfobj(pdfinfo->trailerdict); free_pdfobj(pages);
483 free_pdfobj(obj); return FALSE; }
484 pdfinfo->npages = obj->intval;
487 pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
488 pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
494 // add an entry to the xref table
496 void make_xref(struct XrefTable *xref, int nobj, int offset)
498 if (xref->n_alloc <= nobj) {
499 xref->n_alloc = nobj + 10;
500 xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
502 if (xref->last < nobj) xref->last = nobj;
503 xref->data[nobj] = offset;
506 // a wrapper for deflate
508 GString *do_deflate(char *in, int len)
515 deflateInit(&zs, Z_DEFAULT_COMPRESSION);
516 zs.next_in = (Bytef *)in;
518 zs.avail_out = deflateBound(&zs, len);
519 out = g_string_sized_new(zs.avail_out);
520 zs.next_out = (Bytef *)out->str;
521 deflate(&zs, Z_FINISH);
522 out->len = zs.total_out;
527 // prefix to scale the original page
529 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
532 double v[4], t, xscl, yscl;
535 str = g_string_new("q ");
536 if (pgdesc->rotate == 90) {
537 g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
538 t = height; height = width; width = t;
540 if (pgdesc->rotate == 270) {
541 g_string_append_printf(str, "0 1 -1 0 %.2f 0 cm ", width);
542 t = height; height = width; width = t;
544 if (pgdesc->rotate == 180) {
545 g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
547 if (pgdesc->mediabox==NULL || pgdesc->mediabox->type != PDFTYPE_ARRAY ||
548 pgdesc->mediabox->num != 4) return str;
549 for (i=0; i<4; i++) {
550 if (pgdesc->mediabox->elts[i]->type == PDFTYPE_INT)
551 v[i] = pgdesc->mediabox->elts[i]->intval;
552 else if (pgdesc->mediabox->elts[i]->type == PDFTYPE_REAL)
553 v[i] = pgdesc->mediabox->elts[i]->realval;
556 if (v[0]>v[2]) { t = v[0]; v[0] = v[2]; v[2] = t; }
557 if (v[1]>v[3]) { t = v[1]; v[1] = v[3]; v[3] = t; }
558 if (v[2]-v[0] < 1. || v[3]-v[1] < 1.) return str;
559 xscl = width/(v[2]-v[0]);
560 yscl = height/(v[3]-v[1]);
561 g_string_append_printf(str, "%.4f 0 0 %.4f %.2f %.2f cm ",
562 xscl, yscl, -v[0]*xscl, -v[1]*yscl);
566 // add an entry to a subentry of a directory
568 struct PdfObj *mk_pdfname(char *name)
572 obj = g_malloc(sizeof(struct PdfObj));
573 obj->type = PDFTYPE_NAME;
574 obj->str = g_strdup(name);
578 struct PdfObj *mk_pdfref(int num)
582 obj = g_malloc(sizeof(struct PdfObj));
583 obj->type = PDFTYPE_REF;
589 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
591 if (a==NULL || b==NULL) return (a==b);
592 if (a->type!=b->type) return FALSE;
593 if (a->type == PDFTYPE_CST || a->type == PDFTYPE_INT)
594 return (a->intval == b->intval);
595 if (a->type == PDFTYPE_REAL)
596 return (a->realval == b->realval);
597 if (a->type == PDFTYPE_NAME)
598 return !strcmp(a->str, b->str);
599 if (a->type == PDFTYPE_REF)
600 return (a->intval == b->intval && a->num == b->num);
604 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
605 struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
611 for (i=0; i<obj->num; i++)
612 if (!strcmp(obj->names[i], section)) subpos = i;
616 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj*));
617 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
618 obj->names[subpos] = g_strdup(section);
619 obj->elts[subpos] = NULL;
621 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type==PDFTYPE_REF) {
622 sec = get_pdfobj(pdfbuf, xref, obj->elts[subpos]);
623 free_pdfobj(obj->elts[subpos]);
624 obj->elts[subpos] = sec;
626 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type!=type)
627 { free_pdfobj(obj->elts[subpos]); obj->elts[subpos] = NULL; }
628 if (obj->elts[subpos] == NULL) {
629 obj->elts[subpos] = sec = g_malloc(sizeof(struct PdfObj));
635 sec = obj->elts[subpos];
638 if (type==PDFTYPE_DICT) {
639 for (i=0; i<sec->num; i++)
640 if (!strcmp(sec->names[i], name)) subpos = i;
644 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
645 sec->names = g_realloc(sec->names, sec->num*sizeof(char *));
646 sec->names[subpos] = g_strdup(name);
647 sec->elts[subpos] = NULL;
649 free_pdfobj(sec->elts[subpos]);
650 sec->elts[subpos] = entry;
652 if (type==PDFTYPE_ARRAY) {
653 for (i=0; i<sec->num; i++)
654 if (iseq_obj(sec->elts[i], entry)) subpos = i;
658 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
659 sec->elts[subpos] = entry;
661 else free_pdfobj(entry);
665 // draw a page's background
667 void pdf_draw_solid_background(struct Page *pg, GString *str)
671 g_string_append_printf(str,
672 "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
673 RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
674 if (!ui.print_ruling) return;
675 if (pg->bg->ruling == RULING_NONE) return;
676 g_string_append_printf(str,
677 "%.2f %.2f %.2f RG %.2f w ",
678 RGBA_RGB(RULING_COLOR), RULING_THICKNESS);
679 if (pg->bg->ruling == RULING_GRAPH) {
680 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
681 g_string_append_printf(str, "%.2f 0 m %.2f %.2f l S ",
683 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
684 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
688 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
689 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
691 if (pg->bg->ruling == RULING_LINED)
692 g_string_append_printf(str,
693 "%.2f %.2f %.2f RG %.2f 0 m %.2f %.2f l S ",
694 RGBA_RGB(RULING_MARGIN_COLOR),
695 RULING_LEFTMARGIN, RULING_LEFTMARGIN, pg->height);
698 int pdf_draw_bitmap_background(struct Page *pg, GString *str,
699 struct XrefTable *xref, GString *pdfbuf)
705 int height, width, stride, x, y, chan;
707 if (pg->bg->type == BG_PDF) {
708 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
709 if (pgpdf == NULL) return -1;
710 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
711 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
712 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING)
713 gtk_main_iteration();
717 else pix = pg->bg->pixbuf;
719 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return -1;
720 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return -1;
722 width = gdk_pixbuf_get_width(pix);
723 height = gdk_pixbuf_get_height(pix);
724 stride = gdk_pixbuf_get_rowstride(pix);
725 chan = gdk_pixbuf_get_n_channels(pix);
726 if (chan!=3 && chan!=4) return -1;
728 g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
729 pg->width, -pg->height, pg->height);
731 p2 = buf = (char *)g_malloc(3*width*height);
732 for (y=0; y<height; y++) {
733 p1 = (char *)gdk_pixbuf_get_pixels(pix)+stride*y;
734 for (x=0; x<width; x++) {
735 *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
739 zpix = do_deflate(buf, 3*width*height);
742 make_xref(xref, xref->last+1, pdfbuf->len);
743 g_string_append_printf(pdfbuf,
744 "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
745 "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
746 "/BitsPerComponent 8 >> stream\n",
747 xref->last, zpix->len, width, height);
748 g_string_append_len(pdfbuf, zpix->str, zpix->len);
749 g_string_free(zpix, TRUE);
750 g_string_append(pdfbuf, "endstream\nendobj\n");
755 // draw a page's graphics
757 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter)
759 GList *layerlist, *itemlist;
763 double old_thickness;
767 old_rgba = 0x12345678; // not any values we use, so we'll reset them
770 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
771 l = (struct Layer *)layerlist->data;
772 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
773 item = (struct Item *)itemlist->data;
774 if (item->type == ITEM_STROKE) {
775 if ((item->brush.color_rgba & ~0xff) != old_rgba)
776 g_string_append_printf(str, "%.2f %.2f %.2f RG ",
777 RGBA_RGB(item->brush.color_rgba));
778 if (item->brush.thickness != old_thickness)
779 g_string_append_printf(str, "%.2f w ", item->brush.thickness);
780 if ((item->brush.color_rgba & 0xf0) != 0xf0) { // transparent
781 g_string_append(str, "q /XoHi gs ");
784 old_rgba = item->brush.color_rgba & ~0xff;
785 old_thickness = item->brush.thickness;
786 pt = item->path->coords;
787 g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
788 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
789 g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
790 g_string_append_printf(str,"S\n");
791 if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
792 g_string_append(str, "Q ");
798 // main printing function
800 /* we use the following object numbers, starting with n_obj_catalog:
801 0 the document catalog
803 2 the GS for the hiliters
804 3 ... the page objects
807 gboolean print_to_pdf(char *filename)
810 GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
811 int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
813 struct XrefTable xref;
818 gboolean annot, uses_pdf;
819 gboolean use_hiliter;
820 struct PdfInfo pdfinfo;
823 f = fopen(filename, "w");
824 if (f == NULL) return FALSE;
825 setlocale(LC_NUMERIC, "C");
829 for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
830 pg = (struct Page *)pglist->data;
831 if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
834 if (uses_pdf && bgpdf.status != STATUS_NOT_INIT &&
835 g_file_get_contents(bgpdf.tmpfile_copy, &buf, &len, NULL) &&
836 !strncmp(buf, "%PDF-1.", 7)) {
837 // parse the existing PDF file
838 pdfbuf = g_string_new_len(buf, len);
840 if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
841 annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
843 g_string_free(pdfbuf, TRUE);
844 if (xref.data != NULL) g_free(xref.data);
849 pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
850 xref.n_alloc = xref.last = 0;
854 // catalog and page tree
855 n_obj_catalog = xref.last+1;
856 n_obj_pages_offs = xref.last+4;
857 make_xref(&xref, n_obj_catalog, pdfbuf->len);
858 g_string_append_printf(pdfbuf,
859 "%d 0 obj\n<< /Type /Catalog /Pages %d 0 R >> endobj\n",
860 n_obj_catalog, n_obj_catalog+1);
861 make_xref(&xref, n_obj_catalog+1, pdfbuf->len);
862 g_string_append_printf(pdfbuf,
863 "%d 0 obj\n<< /Type /Pages /Kids [", n_obj_catalog+1);
864 for (i=0;i<journal.npages;i++)
865 g_string_append_printf(pdfbuf, "%d 0 R ", n_obj_pages_offs+i);
866 g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
867 make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
868 g_string_append_printf(pdfbuf,
869 "%d 0 obj\n<< /Type /ExtGState /CA 0.5 >> endobj\n",
871 xref.last = n_obj_pages_offs + journal.npages-1;
873 for (pglist = journal.pages, n_page = 0; pglist!=NULL;
874 pglist = pglist->next, n_page++) {
875 pg = (struct Page *)pglist->data;
877 // draw the background and page into pgstrm
878 pgstrm = g_string_new("");
879 g_string_printf(pgstrm, "q 1 0 0 -1 0 %.2f cm 1 J 1 j ", pg->height);
882 if (pg->bg->type == BG_SOLID)
883 pdf_draw_solid_background(pg, pgstrm);
884 else if (pg->bg->type == BG_PDF && annot &&
885 pdfinfo.pages[pg->bg->file_page_seq-1].contents!=NULL) {
886 make_xref(&xref, xref.last+1, pdfbuf->len);
887 n_obj_prefix = xref.last;
888 tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
889 pg->width, pg->height);
890 g_string_append_printf(pdfbuf,
891 "%d 0 obj\n<< /Length %d >> stream\n%s\nendstream\nendobj\n",
892 n_obj_prefix, tmpstr->len, tmpstr->str);
893 g_string_free(tmpstr, TRUE);
894 g_string_prepend(pgstrm, "Q ");
896 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
897 n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
898 // draw the page contents
900 pdf_draw_page(pg, pgstrm, &use_hiliter);
901 g_string_append_printf(pgstrm, "Q\n");
903 // deflate pgstrm and write it
904 zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
905 g_string_free(pgstrm, TRUE);
907 make_xref(&xref, xref.last+1, pdfbuf->len);
908 g_string_append_printf(pdfbuf,
909 "%d 0 obj\n<< /Length %d /Filter /FlateDecode>> stream\n",
910 xref.last, zpgstrm->len);
911 g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
912 g_string_free(zpgstrm, TRUE);
913 g_string_append(pdfbuf, "endstream\nendobj\n");
915 // write the page object
917 make_xref(&xref, n_obj_pages_offs+n_page, pdfbuf->len);
918 g_string_append_printf(pdfbuf,
919 "%d 0 obj\n<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %.2f %.2f] ",
920 n_obj_pages_offs+n_page, n_obj_catalog+1, pg->width, pg->height);
921 if (n_obj_prefix>0) {
922 obj = get_pdfobj(pdfbuf, &xref, pdfinfo.pages[pg->bg->file_page_seq-1].contents);
923 if (obj->type != PDFTYPE_ARRAY) {
925 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
927 g_string_append_printf(pdfbuf, "/Contents [%d 0 R ", n_obj_prefix);
928 if (obj->type == PDFTYPE_REF)
929 g_string_append_printf(pdfbuf, "%d %d R ", obj->intval, obj->num);
930 if (obj->type == PDFTYPE_ARRAY) {
931 for (i=0; i<obj->num; i++) {
932 show_pdfobj(obj->elts[i], pdfbuf);
933 g_string_append_c(pdfbuf, ' ');
937 g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
939 else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
940 g_string_append(pdfbuf, "/Resources ");
943 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
945 if (obj!=NULL && obj->type!=PDFTYPE_DICT)
946 { free_pdfobj(obj); obj=NULL; }
948 obj = g_malloc(sizeof(struct PdfObj));
949 obj->type = PDFTYPE_DICT;
954 add_dict_subentry(pdfbuf, &xref,
955 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
957 add_dict_subentry(pdfbuf, &xref,
958 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
960 add_dict_subentry(pdfbuf, &xref,
961 obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
963 add_dict_subentry(pdfbuf, &xref,
964 obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
965 show_pdfobj(obj, pdfbuf);
967 g_string_append(pdfbuf, " >> endobj\n");
971 startxref = pdfbuf->len;
972 if (annot) g_string_append_printf(pdfbuf,
973 "xref\n%d %d\n", n_obj_catalog, xref.last-n_obj_catalog+1);
974 else g_string_append_printf(pdfbuf,
975 "xref\n0 %d\n0000000000 65535 f \n", xref.last+1);
976 for (i=n_obj_catalog; i<=xref.last; i++)
977 g_string_append_printf(pdfbuf, "%010d 00000 n \n", xref.data[i]);
978 g_string_append_printf(pdfbuf,
979 "trailer\n<< /Size %d /Root %d 0 R ", xref.last+1, n_obj_catalog);
981 g_string_append_printf(pdfbuf, "/Prev %d ", pdfinfo.startxref);
982 // keeping encryption info somehow doesn't work.
983 // xournal can't annotate encrypted PDFs anyway...
985 obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
987 g_string_append_printf(pdfbuf, "/Encrypt ");
988 show_pdfobj(obj, pdfbuf);
992 g_string_append_printf(pdfbuf,
993 ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
997 free_pdfobj(pdfinfo.trailerdict);
998 if (pdfinfo.pages!=NULL)
999 for (i=0; i<pdfinfo.npages; i++) {
1000 free_pdfobj(pdfinfo.pages[i].resources);
1001 free_pdfobj(pdfinfo.pages[i].mediabox);
1002 free_pdfobj(pdfinfo.pages[i].contents);
1006 setlocale(LC_NUMERIC, "");
1007 if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
1009 g_string_free(pdfbuf, TRUE);
1013 g_string_free(pdfbuf, TRUE);
1017 /*********** Printing via libgnomeprint **********/
1019 // does the same job as update_canvas_bg(), but to a print context
1021 void print_background(GnomePrintContext *gpc, struct Page *pg, gboolean *abort)
1027 if (pg->bg->type == BG_SOLID) {
1028 gnome_print_setopacity(gpc, 1.0);
1029 gnome_print_setrgbcolor(gpc, RGBA_RGB(pg->bg->color_rgba));
1030 gnome_print_rect_filled(gpc, 0, 0, pg->width, pg->height);
1032 if (!ui.print_ruling) return;
1033 if (pg->bg->ruling == RULING_NONE) return;
1034 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_COLOR));
1035 gnome_print_setlinewidth(gpc, RULING_THICKNESS);
1037 if (pg->bg->ruling == RULING_GRAPH) {
1038 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
1039 gnome_print_line_stroked(gpc, x, 0, x, pg->height);
1040 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
1041 gnome_print_line_stroked(gpc, 0, y, pg->width, y);
1045 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
1046 gnome_print_line_stroked(gpc, 0, y, pg->width, y);
1047 if (pg->bg->ruling == RULING_LINED) {
1048 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_MARGIN_COLOR));
1049 gnome_print_line_stroked(gpc, RULING_LEFTMARGIN, 0, RULING_LEFTMARGIN, pg->height);
1053 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF) {
1054 if (pg->bg->type == BG_PDF) {
1055 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
1056 if (pgpdf == NULL) return;
1057 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
1058 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
1059 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING) {
1060 gtk_main_iteration();
1064 pix = pgpdf->pixbuf;
1066 else pix = pg->bg->pixbuf;
1067 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return;
1068 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return;
1069 gnome_print_gsave(gpc);
1070 gnome_print_scale(gpc, pg->width, -pg->height);
1071 gnome_print_translate(gpc, 0., -1.);
1072 if (gdk_pixbuf_get_n_channels(pix) == 3)
1073 gnome_print_rgbimage(gpc, gdk_pixbuf_get_pixels(pix),
1074 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1075 else if (gdk_pixbuf_get_n_channels(pix) == 4)
1076 gnome_print_rgbaimage(gpc, gdk_pixbuf_get_pixels(pix),
1077 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1078 gnome_print_grestore(gpc);
1083 void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
1084 double pgwidth, double pgheight, gboolean *abort)
1089 double old_thickness;
1090 GList *layerlist, *itemlist;
1096 if (pg==NULL) return;
1098 g_snprintf(tmp, 10, "Page %d", pageno);
1099 gnome_print_beginpage(gpc, (guchar *)tmp);
1100 gnome_print_gsave(gpc);
1102 scale = MIN(pgwidth/pg->width, pgheight/pg->height)*0.95;
1103 gnome_print_translate(gpc,
1104 (pgwidth - scale*pg->width)/2, (pgheight + scale*pg->height)/2);
1105 gnome_print_scale(gpc, scale, -scale);
1106 gnome_print_setlinejoin(gpc, 1); // round
1107 gnome_print_setlinecap(gpc, 1); // round
1109 print_background(gpc, pg, abort);
1111 old_rgba = 0x12345678; // not any values we use, so we'll reset them
1112 old_thickness = 0.0;
1114 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1116 l = (struct Layer *)layerlist->data;
1117 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1119 item = (struct Item *)itemlist->data;
1120 if (item->type == ITEM_STROKE) {
1121 if ((item->brush.color_rgba & ~0xff) != (old_rgba & ~0xff))
1122 gnome_print_setrgbcolor(gpc, RGBA_RGB(item->brush.color_rgba));
1123 if ((item->brush.color_rgba & 0xff) != (old_rgba & 0xff))
1124 gnome_print_setopacity(gpc, RGBA_ALPHA(item->brush.color_rgba));
1125 if (item->brush.thickness != old_thickness)
1126 gnome_print_setlinewidth(gpc, item->brush.thickness);
1127 old_rgba = item->brush.color_rgba;
1128 old_thickness = item->brush.thickness;
1129 gnome_print_newpath(gpc);
1130 pt = item->path->coords;
1131 gnome_print_moveto(gpc, pt[0], pt[1]);
1132 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1133 gnome_print_lineto(gpc, pt[0], pt[1]);
1134 gnome_print_stroke(gpc);
1139 gnome_print_grestore(gpc);
1140 gnome_print_showpage(gpc);
1143 void cb_print_abort(GtkDialog *dialog, gint response, gboolean *abort)
1148 void print_job_render(GnomePrintJob *gpj, int fromPage, int toPage)
1150 GnomePrintConfig *config;
1151 GnomePrintContext *gpc;
1152 GtkWidget *wait_dialog;
1153 double pgwidth, pgheight;
1157 config = gnome_print_job_get_config(gpj);
1158 gnome_print_config_get_page_size(config, &pgwidth, &pgheight);
1159 g_object_unref(G_OBJECT(config));
1161 gpc = gnome_print_job_get_context(gpj);
1164 wait_dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
1165 GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL, "Preparing print job");
1166 gtk_widget_show(wait_dialog);
1167 g_signal_connect(wait_dialog, "response", G_CALLBACK (cb_print_abort), &abort);
1169 for (i = fromPage; i <= toPage; i++) {
1170 #if GTK_CHECK_VERSION(2,6,0)
1171 if (!gtk_check_version(2, 6, 0))
1172 gtk_message_dialog_format_secondary_text(
1173 GTK_MESSAGE_DIALOG(wait_dialog), "Page %d", i+1);
1175 while (gtk_events_pending()) gtk_main_iteration();
1176 print_page(gpc, (struct Page *)g_list_nth_data(journal.pages, i), i+1,
1177 pgwidth, pgheight, &abort);
1180 #if GTK_CHECK_VERSION(2,6,0)
1181 if (!gtk_check_version(2, 6, 0))
1182 gtk_message_dialog_format_secondary_text(
1183 GTK_MESSAGE_DIALOG(wait_dialog), "Finalizing...");
1185 while (gtk_events_pending()) gtk_main_iteration();
1187 gnome_print_context_close(gpc);
1188 g_object_unref(G_OBJECT(gpc));
1190 gnome_print_job_close(gpj);
1191 if (!abort) gnome_print_job_print(gpj);
1192 g_object_unref(G_OBJECT(gpj));
1194 gtk_widget_destroy(wait_dialog);