6 #include <libgnomecanvas/libgnomecanvas.h>
7 #include <libgnomeprint/gnome-print-job.h>
17 #define RGBA_RED(rgba) (((rgba>>24)&0xff)/255.0)
18 #define RGBA_GREEN(rgba) (((rgba>>16)&0xff)/255.0)
19 #define RGBA_BLUE(rgba) (((rgba>>8)&0xff)/255.0)
20 #define RGBA_ALPHA(rgba) (((rgba>>0)&0xff)/255.0)
21 #define RGBA_RGB(rgba) RGBA_RED(rgba), RGBA_GREEN(rgba), RGBA_BLUE(rgba)
23 /*********** Printing to PDF ************/
25 gboolean ispdfspace(char c)
27 return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
30 gboolean ispdfdelim(char c)
32 return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
33 c=='{' || c=='}' || c=='/' || c=='%');
36 void skipspace(char **p, char *eof)
38 while (ispdfspace(**p) || **p=='%') {
39 if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
45 void free_pdfobj(struct PdfObj *obj)
49 if (obj==NULL) return;
50 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
51 obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
53 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
54 obj->type == PDFTYPE_STREAM) && obj->num>0) {
55 for (i=0; i<obj->num; i++)
56 free_pdfobj(obj->elts[i]);
59 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
60 for (i=0; i<obj->num; i++)
61 g_free(obj->names[i]);
67 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
72 if (obj==NULL) return NULL;
73 dup = g_memdup(obj, sizeof(struct PdfObj));
74 if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
75 obj->type == PDFTYPE_STREAM) && obj->str!=NULL) {
76 if (obj->type == PDFTYPE_NAME) obj->len = strlen(obj->str);
77 dup->str = g_memdup(obj->str, obj->len+1);
79 if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
80 obj->type == PDFTYPE_STREAM) && obj->num>0) {
81 dup->elts = g_malloc(obj->num*sizeof(struct PdfObj *));
82 for (i=0; i<obj->num; i++)
83 dup->elts[i] = dup_pdfobj(obj->elts[i]);
85 if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
86 dup->names = g_malloc(obj->num*sizeof(char *));
87 for (i=0; i<obj->num; i++)
88 dup->names[i] = g_strdup(obj->names[i]);
93 void show_pdfobj(struct PdfObj *obj, GString *str)
96 if (obj==NULL) return;
99 if (obj->intval==1) g_string_append(str, "true");
100 if (obj->intval==0) g_string_append(str, "false");
101 if (obj->intval==-1) g_string_append(str, "null");
104 g_string_append_printf(str, "%d", obj->intval);
107 g_string_append_printf(str, "%f", obj->realval);
110 g_string_append_len(str, obj->str, obj->len);
113 g_string_append(str, obj->str);
116 g_string_append_c(str, '[');
117 for (i=0;i<obj->num;i++) {
118 if (i) g_string_append_c(str, ' ');
119 show_pdfobj(obj->elts[i], str);
121 g_string_append_c(str, ']');
124 g_string_append(str, "<<");
125 for (i=0;i<obj->num;i++) {
126 g_string_append_printf(str, " %s ", obj->names[i]);
127 show_pdfobj(obj->elts[i], str);
129 g_string_append(str, " >>");
132 g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
137 void DEBUG_PRINTOBJ(struct PdfObj *obj)
139 GString *s = g_string_new("");
142 g_string_free(s, TRUE);
145 // parse a PDF object; returns NULL if fails
146 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
148 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
150 struct PdfObj *obj, *elt;
151 char *p, *q, *r, *eltname;
154 obj = g_malloc(sizeof(struct PdfObj));
157 if (p==eof) { g_free(obj); return NULL; }
160 if (!strncmp(p, "true", 4)) {
161 obj->type = PDFTYPE_CST;
166 if (!strncmp(p, "false", 5)) {
167 obj->type = PDFTYPE_CST;
172 if (!strncmp(p, "null", 4)) {
173 obj->type = PDFTYPE_CST;
180 obj->intval = strtol(p, &q, 10);
184 obj->type = PDFTYPE_REAL;
185 obj->realval = strtod(p, ptr);
188 if (ispdfspace(*q)) {
189 // check for indirect reference
191 obj->num = strtol(q, &r, 10);
196 obj->type = PDFTYPE_REF;
201 obj->type = PDFTYPE_INT;
208 while (stack>0 && q!=eof) {
209 if (*q=='(') stack++;
210 if (*q==')') stack--;
214 if (q==eof) { g_free(obj); return NULL; }
215 obj->type = PDFTYPE_STRING;
217 obj->str = g_malloc(obj->len+1);
218 obj->str[obj->len] = 0;
219 g_memmove(obj->str, p, obj->len);
223 if (*p=='<' && p[1]!='<') {
225 while (*q!='>' && q!=eof) q++;
226 if (q==eof) { g_free(obj); return NULL; }
228 obj->type = PDFTYPE_STRING;
230 obj->str = g_malloc(obj->len+1);
231 obj->str[obj->len] = 0;
232 g_memmove(obj->str, p, obj->len);
240 while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
241 obj->type = PDFTYPE_NAME;
242 obj->str = g_strndup(p, q-p);
249 obj->type = PDFTYPE_ARRAY;
252 q=p+1; skipspace(&q, eof);
254 elt = parse_pdf_object(&q, eof);
255 if (elt==NULL) { free_pdfobj(obj); return NULL; }
257 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
258 obj->elts[obj->num-1] = elt;
266 if (*p=='<' && p[1]=='<') {
267 obj->type = PDFTYPE_DICT;
271 q=p+2; skipspace(&q, eof);
272 while (*q!='>' || q[1]!='>') {
273 if (*q!='/') { free_pdfobj(obj); return NULL; }
275 while (!ispdfspace(*r) && !ispdfdelim(*r)) r++;
276 eltname = g_strndup(q, r-q);
277 q=r; skipspace(&q, eof);
278 elt = parse_pdf_object(&q, eof);
279 if (elt==NULL) { g_free(eltname); free_pdfobj(obj); return NULL; }
281 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
282 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
283 obj->elts[obj->num-1] = elt;
284 obj->names[obj->num-1] = eltname;
291 // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
297 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
301 if (dict==NULL) return NULL;
302 if (dict->type != PDFTYPE_DICT) return NULL;
303 for (i=0; i<dict->num; i++)
304 if (!strcmp(dict->names[i], name)) return dict->elts[i];
308 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
313 if (obj==NULL) return NULL;
314 if (obj->type!=PDFTYPE_REF) return dup_pdfobj(obj);
315 if (obj->intval>xref->last) return NULL;
316 offs = xref->data[obj->intval];
317 if (offs<=0 || offs >= pdfbuf->len) return NULL;
319 p = pdfbuf->str + offs;
320 eof = pdfbuf->str + pdfbuf->len;
321 n = strtol(p, &p, 10);
322 if (n!=obj->intval) return NULL;
324 n = strtol(p, &p, 10);
326 if (strncmp(p, "obj", 3)) return NULL;
328 return parse_pdf_object(&p, eof);
331 // read the xref table of a PDF file in memory, and return the trailerdict
333 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
336 struct PdfObj *trailerdict, *obj;
339 if (strncmp(pdfbuf->str+offs, "xref", 4)) return NULL;
340 p = strstr(pdfbuf->str+offs, "trailer");
341 eof = pdfbuf->str + pdfbuf->len;
342 if (p==NULL) return NULL;
344 trailerdict = parse_pdf_object(&p, eof);
345 obj = get_dict_entry(trailerdict, "/Size");
346 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval-1>xref->last)
347 make_xref(xref, obj->intval-1, 0);
348 obj = get_dict_entry(trailerdict, "/Prev");
349 if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval>0 && obj->intval!=offs) {
350 // recurse into older xref table
351 obj = parse_xref_table(pdfbuf, xref, obj->intval);
354 p = pdfbuf->str+offs+4;
356 if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
357 while (*p>='0' && *p<='9') {
358 start = strtol(p, &p, 10);
360 len = strtol(p, &p, 10);
362 if (len <= 0 || 20*len > eof-p) break;
363 if (start+len-1 > xref->last) make_xref(xref, start+len-1, 0);
364 for (i=start; i<start+len; i++) {
365 xref->data[i] = strtol(p, NULL, 10);
370 if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
374 // parse the page tree
376 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref,
377 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
379 struct PdfObj *obj, *kid;
382 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
383 if (obj == NULL || obj->type != PDFTYPE_NAME)
385 if (!strcmp(obj->str, "/Page")) {
387 pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
388 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
390 free_pdfobj(pages->resources);
391 pages->resources = obj;
393 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
395 free_pdfobj(pages->mediabox);
396 pages->mediabox = obj;
398 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
399 if (obj!=NULL && obj->type == PDFTYPE_INT)
400 pages->rotate = obj->intval;
404 else if (!strcmp(obj->str, "/Pages")) {
406 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Count"));
407 if (obj!=NULL && obj->type == PDFTYPE_INT &&
408 obj->intval>0 && obj->intval<=nmax) count = obj->intval;
411 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
413 for (i=0; i<count; i++) {
414 free_pdfobj(pages[i].resources);
415 pages[i].resources = dup_pdfobj(obj);
418 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
420 for (i=0; i<count; i++) {
421 free_pdfobj(pages[i].mediabox);
422 pages[i].mediabox = dup_pdfobj(obj);
425 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
426 if (obj!=NULL && obj->type == PDFTYPE_INT)
427 for (i=0; i<count; i++)
428 pages[i].rotate = obj->intval;
430 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Kids"));
431 if (obj!=NULL && obj->type == PDFTYPE_ARRAY) {
432 for (i=0; i<obj->num; i++) {
433 kid = get_pdfobj(pdfbuf, xref, obj->elts[i]);
435 j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
448 // parse a PDF file in memory
450 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
454 struct PdfObj *obj, *pages;
456 xref->n_alloc = xref->last = 0;
458 p = pdfbuf->str + pdfbuf->len-1;
460 while (*p!='s' && p!=pdfbuf->str) p--;
461 if (strncmp(p, "startxref", 9)) return FALSE; // fail
463 while (ispdfspace(*p) && p!=pdfbuf->str+pdfbuf->len) p++;
464 offs = strtol(p, NULL, 10);
465 if (offs <= 0 || offs > pdfbuf->len) return FALSE; // fail
466 pdfinfo->startxref = offs;
468 pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
469 if (pdfinfo->trailerdict == NULL) return FALSE; // fail
471 obj = get_pdfobj(pdfbuf, xref,
472 get_dict_entry(pdfinfo->trailerdict, "/Root"));
474 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
475 pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
478 { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
479 obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pages, "/Count"));
480 if (obj == NULL || obj->type != PDFTYPE_INT || obj->intval<=0)
481 { free_pdfobj(pdfinfo->trailerdict); free_pdfobj(pages);
482 free_pdfobj(obj); return FALSE; }
483 pdfinfo->npages = obj->intval;
486 pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
487 pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
493 // add an entry to the xref table
495 void make_xref(struct XrefTable *xref, int nobj, int offset)
497 if (xref->n_alloc <= nobj) {
498 xref->n_alloc = nobj + 10;
499 xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
501 if (xref->last < nobj) xref->last = nobj;
502 xref->data[nobj] = offset;
505 // a wrapper for deflate
507 GString *do_deflate(char *in, int len)
514 deflateInit(&zs, Z_DEFAULT_COMPRESSION);
515 zs.next_in = (Bytef *)in;
517 zs.avail_out = deflateBound(&zs, len);
518 out = g_string_sized_new(zs.avail_out);
519 zs.next_out = (Bytef *)out->str;
520 deflate(&zs, Z_FINISH);
521 out->len = zs.total_out;
526 // prefix to scale the original page
528 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
531 double v[4], t, xscl, yscl;
534 str = g_string_new("q ");
535 if (pgdesc->rotate == 90) {
536 g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
537 t = height; height = width; width = t;
539 if (pgdesc->rotate == 270) {
540 g_string_append_printf(str, "0 1 -1 0 %.2f 0 cm ", width);
541 t = height; height = width; width = t;
543 if (pgdesc->rotate == 180) {
544 g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
546 if (pgdesc->mediabox==NULL || pgdesc->mediabox->type != PDFTYPE_ARRAY ||
547 pgdesc->mediabox->num != 4) return str;
548 for (i=0; i<4; i++) {
549 if (pgdesc->mediabox->elts[i]->type == PDFTYPE_INT)
550 v[i] = pgdesc->mediabox->elts[i]->intval;
551 else if (pgdesc->mediabox->elts[i]->type == PDFTYPE_REAL)
552 v[i] = pgdesc->mediabox->elts[i]->realval;
555 if (v[0]>v[2]) { t = v[0]; v[0] = v[2]; v[2] = t; }
556 if (v[1]>v[3]) { t = v[1]; v[1] = v[3]; v[3] = t; }
557 if (v[2]-v[0] < 1. || v[3]-v[1] < 1.) return str;
558 xscl = width/(v[2]-v[0]);
559 yscl = height/(v[3]-v[1]);
560 g_string_append_printf(str, "%.4f 0 0 %.4f %.2f %.2f cm ",
561 xscl, yscl, -v[0]*xscl, -v[1]*yscl);
565 // add an entry to a subentry of a directory
567 struct PdfObj *mk_pdfname(char *name)
571 obj = g_malloc(sizeof(struct PdfObj));
572 obj->type = PDFTYPE_NAME;
573 obj->str = g_strdup(name);
577 struct PdfObj *mk_pdfref(int num)
581 obj = g_malloc(sizeof(struct PdfObj));
582 obj->type = PDFTYPE_REF;
588 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
590 if (a==NULL || b==NULL) return (a==b);
591 if (a->type!=b->type) return FALSE;
592 if (a->type == PDFTYPE_CST || a->type == PDFTYPE_INT)
593 return (a->intval == b->intval);
594 if (a->type == PDFTYPE_REAL)
595 return (a->realval == b->realval);
596 if (a->type == PDFTYPE_NAME)
597 return !strcmp(a->str, b->str);
598 if (a->type == PDFTYPE_REF)
599 return (a->intval == b->intval && a->num == b->num);
603 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
604 struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
610 for (i=0; i<obj->num; i++)
611 if (!strcmp(obj->names[i], section)) subpos = i;
615 obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj*));
616 obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
617 obj->names[subpos] = g_strdup(section);
618 obj->elts[subpos] = NULL;
620 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type==PDFTYPE_REF) {
621 sec = get_pdfobj(pdfbuf, xref, obj->elts[subpos]);
622 free_pdfobj(obj->elts[subpos]);
623 obj->elts[subpos] = sec;
625 if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type!=type)
626 { free_pdfobj(obj->elts[subpos]); obj->elts[subpos] = NULL; }
627 if (obj->elts[subpos] == NULL) {
628 obj->elts[subpos] = sec = g_malloc(sizeof(struct PdfObj));
634 sec = obj->elts[subpos];
637 if (type==PDFTYPE_DICT) {
638 for (i=0; i<sec->num; i++)
639 if (!strcmp(sec->names[i], name)) subpos = i;
643 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
644 sec->names = g_realloc(sec->names, sec->num*sizeof(char *));
645 sec->names[subpos] = g_strdup(name);
646 sec->elts[subpos] = NULL;
648 free_pdfobj(sec->elts[subpos]);
649 sec->elts[subpos] = entry;
651 if (type==PDFTYPE_ARRAY) {
652 for (i=0; i<sec->num; i++)
653 if (iseq_obj(sec->elts[i], entry)) subpos = i;
657 sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
658 sec->elts[subpos] = entry;
660 else free_pdfobj(entry);
664 // draw a page's background
666 void pdf_draw_solid_background(struct Page *pg, GString *str)
670 g_string_append_printf(str,
671 "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
672 RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
673 if (pg->bg->ruling == RULING_NONE) return;
674 g_string_append_printf(str,
675 "%.2f %.2f %.2f RG %.2f w ",
676 RGBA_RGB(RULING_COLOR), RULING_THICKNESS);
677 if (pg->bg->ruling == RULING_GRAPH) {
678 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
679 g_string_append_printf(str, "%.2f 0 m %.2f %.2f l S ",
681 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
682 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
686 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
687 g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
689 if (pg->bg->ruling == RULING_LINED)
690 g_string_append_printf(str,
691 "%.2f %.2f %.2f RG %.2f 0 m %.2f %.2f l S ",
692 RGBA_RGB(RULING_MARGIN_COLOR),
693 RULING_LEFTMARGIN, RULING_LEFTMARGIN, pg->height);
696 int pdf_draw_bitmap_background(struct Page *pg, GString *str,
697 struct XrefTable *xref, GString *pdfbuf)
703 int height, width, stride, x, y, chan;
705 if (pg->bg->type == BG_PDF) {
706 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
707 if (pgpdf == NULL) return -1;
708 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
709 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
710 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING)
711 gtk_main_iteration();
715 else pix = pg->bg->pixbuf;
717 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return -1;
718 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return -1;
720 width = gdk_pixbuf_get_width(pix);
721 height = gdk_pixbuf_get_height(pix);
722 stride = gdk_pixbuf_get_rowstride(pix);
723 chan = gdk_pixbuf_get_n_channels(pix);
724 if (chan!=3 && chan!=4) return -1;
726 g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
727 pg->width, -pg->height, pg->height);
729 p2 = buf = (char *)g_malloc(3*width*height);
730 for (y=0; y<height; y++) {
731 p1 = (char *)gdk_pixbuf_get_pixels(pix)+stride*y;
732 for (x=0; x<width; x++) {
733 *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
737 zpix = do_deflate(buf, 3*width*height);
740 make_xref(xref, xref->last+1, pdfbuf->len);
741 g_string_append_printf(pdfbuf,
742 "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
743 "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
744 "/BitsPerComponent 8 >> stream\n",
745 xref->last, zpix->len, width, height);
746 g_string_append_len(pdfbuf, zpix->str, zpix->len);
747 g_string_free(zpix, TRUE);
748 g_string_append(pdfbuf, "endstream\nendobj\n");
753 // draw a page's graphics
755 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter)
757 GList *layerlist, *itemlist;
761 double old_thickness;
765 old_rgba = 0x12345678; // not any values we use, so we'll reset them
768 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
769 l = (struct Layer *)layerlist->data;
770 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
771 item = (struct Item *)itemlist->data;
772 if (item->type == ITEM_STROKE) {
773 if ((item->brush.color_rgba & ~0xff) != old_rgba)
774 g_string_append_printf(str, "%.2f %.2f %.2f RG ",
775 RGBA_RGB(item->brush.color_rgba));
776 if (item->brush.thickness != old_thickness)
777 g_string_append_printf(str, "%.2f w ", item->brush.thickness);
778 if ((item->brush.color_rgba & 0xf0) != 0xf0) { // transparent
779 g_string_append(str, "q /XoHi gs ");
782 old_rgba = item->brush.color_rgba & ~0xff;
783 old_thickness = item->brush.thickness;
784 pt = item->path->coords;
785 g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
786 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
787 g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
788 g_string_append_printf(str,"S\n");
789 if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
790 g_string_append(str, "Q ");
796 // main printing function
798 /* we use the following object numbers, starting with n_obj_catalog:
799 0 the document catalog
801 2 the GS for the hiliters
802 3 ... the page objects
805 gboolean print_to_pdf(char *filename)
808 GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
809 int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
811 struct XrefTable xref;
816 gboolean annot, uses_pdf;
817 gboolean use_hiliter;
818 struct PdfInfo pdfinfo;
821 f = fopen(filename, "w");
822 if (f == NULL) return FALSE;
826 for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
827 pg = (struct Page *)pglist->data;
828 if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
831 if (uses_pdf && bgpdf.status != STATUS_NOT_INIT &&
832 g_file_get_contents(bgpdf.tmpfile_copy, &buf, &len, NULL) &&
833 !strncmp(buf, "%PDF-1.", 7)) {
834 // parse the existing PDF file
835 pdfbuf = g_string_new_len(buf, len);
837 if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
838 annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
840 g_string_free(pdfbuf, TRUE);
841 if (xref.data != NULL) g_free(xref.data);
846 pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
847 xref.n_alloc = xref.last = 0;
851 // catalog and page tree
852 n_obj_catalog = xref.last+1;
853 n_obj_pages_offs = xref.last+4;
854 make_xref(&xref, n_obj_catalog, pdfbuf->len);
855 g_string_append_printf(pdfbuf,
856 "%d 0 obj\n<< /Type /Catalog /Pages %d 0 R >> endobj\n",
857 n_obj_catalog, n_obj_catalog+1);
858 make_xref(&xref, n_obj_catalog+1, pdfbuf->len);
859 g_string_append_printf(pdfbuf,
860 "%d 0 obj\n<< /Type /Pages /Kids [", n_obj_catalog+1);
861 for (i=0;i<journal.npages;i++)
862 g_string_append_printf(pdfbuf, "%d 0 R ", n_obj_pages_offs+i);
863 g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
864 make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
865 g_string_append_printf(pdfbuf,
866 "%d 0 obj\n<< /Type /ExtGState /CA 0.5 >> endobj\n",
868 xref.last = n_obj_pages_offs + journal.npages-1;
870 for (pglist = journal.pages, n_page = 0; pglist!=NULL;
871 pglist = pglist->next, n_page++) {
872 pg = (struct Page *)pglist->data;
874 // draw the background and page into pgstrm
875 pgstrm = g_string_new("");
876 g_string_printf(pgstrm, "q 1 0 0 -1 0 %.2f cm 1 J 1 j ", pg->height);
879 if (pg->bg->type == BG_SOLID)
880 pdf_draw_solid_background(pg, pgstrm);
881 else if (pg->bg->type == BG_PDF && annot &&
882 pdfinfo.pages[pg->bg->file_page_seq-1].contents!=NULL) {
883 make_xref(&xref, xref.last+1, pdfbuf->len);
884 n_obj_prefix = xref.last;
885 tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
886 pg->width, pg->height);
887 g_string_append_printf(pdfbuf,
888 "%d 0 obj\n<< /Length %d >> stream\n%s\nendstream\nendobj\n",
889 n_obj_prefix, tmpstr->len, tmpstr->str);
890 g_string_free(tmpstr, TRUE);
891 g_string_prepend(pgstrm, "Q ");
893 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
894 n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
895 // draw the page contents
897 pdf_draw_page(pg, pgstrm, &use_hiliter);
898 g_string_append_printf(pgstrm, "Q\n");
900 // deflate pgstrm and write it
901 zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
902 g_string_free(pgstrm, TRUE);
904 make_xref(&xref, xref.last+1, pdfbuf->len);
905 g_string_append_printf(pdfbuf,
906 "%d 0 obj\n<< /Length %d /Filter /FlateDecode>> stream\n",
907 xref.last, zpgstrm->len);
908 g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
909 g_string_free(zpgstrm, TRUE);
910 g_string_append(pdfbuf, "endstream\nendobj\n");
912 // write the page object
914 make_xref(&xref, n_obj_pages_offs+n_page, pdfbuf->len);
915 g_string_append_printf(pdfbuf,
916 "%d 0 obj\n<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %.2f %.2f] ",
917 n_obj_pages_offs+n_page, n_obj_catalog+1, pg->width, pg->height);
918 if (n_obj_prefix>0) {
919 obj = get_pdfobj(pdfbuf, &xref, pdfinfo.pages[pg->bg->file_page_seq-1].contents);
920 if (obj->type != PDFTYPE_ARRAY) {
922 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
924 g_string_append_printf(pdfbuf, "/Contents [%d 0 R ", n_obj_prefix);
925 if (obj->type == PDFTYPE_REF)
926 g_string_append_printf(pdfbuf, "%d %d R ", obj->intval, obj->num);
927 if (obj->type == PDFTYPE_ARRAY) {
928 for (i=0; i<obj->num; i++) {
929 show_pdfobj(obj->elts[i], pdfbuf);
930 g_string_append_c(pdfbuf, ' ');
934 g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
936 else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
937 g_string_append(pdfbuf, "/Resources ");
940 obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
942 if (obj!=NULL && obj->type!=PDFTYPE_DICT)
943 { free_pdfobj(obj); obj=NULL; }
945 obj = g_malloc(sizeof(struct PdfObj));
946 obj->type = PDFTYPE_DICT;
951 add_dict_subentry(pdfbuf, &xref,
952 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
954 add_dict_subentry(pdfbuf, &xref,
955 obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
957 add_dict_subentry(pdfbuf, &xref,
958 obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
960 add_dict_subentry(pdfbuf, &xref,
961 obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
962 show_pdfobj(obj, pdfbuf);
964 g_string_append(pdfbuf, " >> endobj\n");
968 startxref = pdfbuf->len;
969 if (annot) g_string_append_printf(pdfbuf,
970 "xref\n%d %d\n", n_obj_catalog, xref.last-n_obj_catalog+1);
971 else g_string_append_printf(pdfbuf,
972 "xref\n0 %d\n0000000000 65535 f \n", xref.last+1);
973 for (i=n_obj_catalog; i<=xref.last; i++)
974 g_string_append_printf(pdfbuf, "%010d 00000 n \n", xref.data[i]);
975 g_string_append_printf(pdfbuf,
976 "trailer\n<< /Size %d /Root %d 0 R ", xref.last+1, n_obj_catalog);
978 g_string_append_printf(pdfbuf, "/Prev %d ", pdfinfo.startxref);
979 // keeping encryption info somehow doesn't work.
980 // xournal can't annotate encrypted PDFs anyway...
982 obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
984 g_string_append_printf(pdfbuf, "/Encrypt ");
985 show_pdfobj(obj, pdfbuf);
989 g_string_append_printf(pdfbuf,
990 ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
994 free_pdfobj(pdfinfo.trailerdict);
998 if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
1000 g_string_free(pdfbuf, TRUE);
1004 g_string_free(pdfbuf, TRUE);
1008 /*********** Printing via libgnomeprint **********/
1010 // does the same job as update_canvas_bg(), but to a print context
1012 void print_background(GnomePrintContext *gpc, struct Page *pg, gboolean *abort)
1018 if (pg->bg->type == BG_SOLID) {
1019 gnome_print_setopacity(gpc, 1.0);
1020 gnome_print_setrgbcolor(gpc, RGBA_RGB(pg->bg->color_rgba));
1021 gnome_print_rect_filled(gpc, 0, 0, pg->width, pg->height);
1023 if (pg->bg->ruling == RULING_NONE) return;
1024 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_COLOR));
1025 gnome_print_setlinewidth(gpc, RULING_THICKNESS);
1027 if (pg->bg->ruling == RULING_GRAPH) {
1028 for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
1029 gnome_print_line_stroked(gpc, x, 0, x, pg->height);
1030 for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
1031 gnome_print_line_stroked(gpc, 0, y, pg->width, y);
1035 for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
1036 gnome_print_line_stroked(gpc, 0, y, pg->width, y);
1037 if (pg->bg->ruling == RULING_LINED) {
1038 gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_MARGIN_COLOR));
1039 gnome_print_line_stroked(gpc, RULING_LEFTMARGIN, 0, RULING_LEFTMARGIN, pg->height);
1043 else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF) {
1044 if (pg->bg->type == BG_PDF) {
1045 pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
1046 if (pgpdf == NULL) return;
1047 if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
1048 add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
1049 while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING) {
1050 gtk_main_iteration();
1054 pix = pgpdf->pixbuf;
1056 else pix = pg->bg->pixbuf;
1057 if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return;
1058 if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return;
1059 gnome_print_gsave(gpc);
1060 gnome_print_scale(gpc, pg->width, -pg->height);
1061 gnome_print_translate(gpc, 0., -1.);
1062 if (gdk_pixbuf_get_n_channels(pix) == 3)
1063 gnome_print_rgbimage(gpc, gdk_pixbuf_get_pixels(pix),
1064 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1065 else if (gdk_pixbuf_get_n_channels(pix) == 4)
1066 gnome_print_rgbaimage(gpc, gdk_pixbuf_get_pixels(pix),
1067 gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
1068 gnome_print_grestore(gpc);
1073 void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
1074 double pgwidth, double pgheight, gboolean *abort)
1079 double old_thickness;
1080 GList *layerlist, *itemlist;
1086 if (pg==NULL) return;
1088 g_snprintf(tmp, 10, "Page %d", pageno);
1089 gnome_print_beginpage(gpc, (guchar *)tmp);
1090 gnome_print_gsave(gpc);
1092 scale = MIN(pgwidth/pg->width, pgheight/pg->height)*0.95;
1093 gnome_print_translate(gpc,
1094 (pgwidth - scale*pg->width)/2, (pgheight + scale*pg->height)/2);
1095 gnome_print_scale(gpc, scale, -scale);
1096 gnome_print_setlinejoin(gpc, 1); // round
1097 gnome_print_setlinecap(gpc, 1); // round
1099 print_background(gpc, pg, abort);
1101 old_rgba = 0x12345678; // not any values we use, so we'll reset them
1102 old_thickness = 0.0;
1104 for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1106 l = (struct Layer *)layerlist->data;
1107 for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1109 item = (struct Item *)itemlist->data;
1110 if (item->type == ITEM_STROKE) {
1111 if ((item->brush.color_rgba & ~0xff) != (old_rgba & ~0xff))
1112 gnome_print_setrgbcolor(gpc, RGBA_RGB(item->brush.color_rgba));
1113 if ((item->brush.color_rgba & 0xff) != (old_rgba & 0xff))
1114 gnome_print_setopacity(gpc, RGBA_ALPHA(item->brush.color_rgba));
1115 if (item->brush.thickness != old_thickness)
1116 gnome_print_setlinewidth(gpc, item->brush.thickness);
1117 old_rgba = item->brush.color_rgba;
1118 old_thickness = item->brush.thickness;
1119 gnome_print_newpath(gpc);
1120 pt = item->path->coords;
1121 gnome_print_moveto(gpc, pt[0], pt[1]);
1122 for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1123 gnome_print_lineto(gpc, pt[0], pt[1]);
1124 gnome_print_stroke(gpc);
1129 gnome_print_grestore(gpc);
1130 gnome_print_showpage(gpc);
1133 void cb_print_abort(GtkDialog *dialog, gint response, gboolean *abort)
1138 void print_job_render(GnomePrintJob *gpj, int fromPage, int toPage)
1140 GnomePrintConfig *config;
1141 GnomePrintContext *gpc;
1142 GtkWidget *wait_dialog;
1143 double pgwidth, pgheight;
1147 config = gnome_print_job_get_config(gpj);
1148 gnome_print_config_get_page_size(config, &pgwidth, &pgheight);
1149 g_object_unref(G_OBJECT(config));
1151 gpc = gnome_print_job_get_context(gpj);
1154 wait_dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
1155 GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL, "Preparing print job");
1156 gtk_widget_show(wait_dialog);
1157 g_signal_connect(wait_dialog, "response", G_CALLBACK (cb_print_abort), &abort);
1159 for (i = fromPage; i <= toPage; i++) {
1160 #if GTK_CHECK_VERSION(2,6,0)
1161 if (!gtk_check_version(2, 6, 0))
1162 gtk_message_dialog_format_secondary_text(
1163 GTK_MESSAGE_DIALOG(wait_dialog), "Page %d", i+1);
1165 while (gtk_events_pending()) gtk_main_iteration();
1166 print_page(gpc, (struct Page *)g_list_nth_data(journal.pages, i), i+1,
1167 pgwidth, pgheight, &abort);
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), "Finalizing...");
1175 while (gtk_events_pending()) gtk_main_iteration();
1177 gnome_print_context_close(gpc);
1178 g_object_unref(G_OBJECT(gpc));
1180 gnome_print_job_close(gpj);
1181 if (!abort) gnome_print_job_print(gpj);
1182 g_object_unref(G_OBJECT(gpj));
1184 gtk_widget_destroy(wait_dialog);