]> git.donarmstrong.com Git - xournal.git/blob - src/xo-print.c
04493c9c010d59f3c4e79ae72173ec3154f557dd
[xournal.git] / src / xo-print.c
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <gtk/gtk.h>
6 #include <libgnomecanvas/libgnomecanvas.h>
7 #include <libgnomeprint/gnome-print-job.h>
8 #include <zlib.h>
9 #include <string.h>
10
11 #include "xournal.h"
12 #include "xo-misc.h"
13 #include "xo-paint.h"
14 #include "xo-print.h"
15 #include "xo-file.h"
16
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)
22
23 /*********** Printing to PDF ************/
24
25 gboolean ispdfspace(char c)
26 {
27   return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
28 }
29
30 gboolean ispdfdelim(char c)
31 {
32   return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
33           c=='{' || c=='}' || c=='/' || c=='%');
34 }
35
36 void skipspace(char **p, char *eof)
37 {
38   while (ispdfspace(**p) || **p=='%') {
39     if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
40     if (*p==eof) return;
41     (*p)++;
42   }
43 }
44
45 void free_pdfobj(struct PdfObj *obj)
46 {
47   int i;
48   
49   if (obj==NULL) return;
50   if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
51       obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
52     g_free(obj->str);
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]);
57     g_free(obj->elts);
58   }
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]);
62     g_free(obj->names);
63   }
64   g_free(obj);
65 }
66
67 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
68 {
69   struct PdfObj *dup;
70   int i;
71   
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);
78   }
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]);
84   }
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]);
89   }
90   return dup;
91 }
92
93 void show_pdfobj(struct PdfObj *obj, GString *str)
94 {
95   int i;
96   if (obj==NULL) return;
97   switch(obj->type) {
98     case PDFTYPE_CST:
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");
102       break;
103     case PDFTYPE_INT:
104       g_string_append_printf(str, "%d", obj->intval);
105       break;
106     case PDFTYPE_REAL:
107       g_string_append_printf(str, "%f", obj->realval);
108       break;
109     case PDFTYPE_STRING:
110       g_string_append_len(str, obj->str, obj->len);
111       break;
112     case PDFTYPE_NAME:
113       g_string_append(str, obj->str);
114       break;
115     case PDFTYPE_ARRAY:
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);
120       }
121       g_string_append_c(str, ']');
122       break;
123     case PDFTYPE_DICT:
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);
128       }
129       g_string_append(str, " >>");
130       break;
131     case PDFTYPE_REF:
132       g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
133       break;
134   }
135 }
136
137 void DEBUG_PRINTOBJ(struct PdfObj *obj)
138 {
139   GString *s = g_string_new("");
140   show_pdfobj(obj, s);
141   puts(s->str);
142   g_string_free(s, TRUE);
143 }
144
145 // parse a PDF object; returns NULL if fails
146 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
147
148 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
149 {
150   struct PdfObj *obj, *elt;
151   char *p, *q, *r, *eltname;
152   int stack;
153
154   obj = g_malloc(sizeof(struct PdfObj));
155   p = *ptr;
156   skipspace(&p, eof);
157   if (p==eof) { g_free(obj); return NULL; }
158   
159   // maybe a constant
160   if (!strncmp(p, "true", 4)) {
161     obj->type = PDFTYPE_CST;
162     obj->intval = 1;
163     *ptr = p+4;
164     return obj;
165   }
166   if (!strncmp(p, "false", 5)) {
167     obj->type = PDFTYPE_CST;
168     obj->intval = 0;
169     *ptr = p+5;
170     return obj;
171   }
172   if (!strncmp(p, "null", 4)) {
173     obj->type = PDFTYPE_CST;
174     obj->intval = -1;
175     *ptr = p+4;
176     return obj;
177   }
178
179   // or a number ?
180   obj->intval = strtol(p, &q, 10);
181   *ptr = q;
182   if (q!=p) {
183     if (*q == '.') {
184       obj->type = PDFTYPE_REAL;
185       obj->realval = strtod(p, ptr);
186       return obj;
187     }
188     if (ispdfspace(*q)) {
189       // check for indirect reference
190       skipspace(&q, eof);
191       obj->num = strtol(q, &r, 10);
192       if (r!=q) {
193         skipspace(&r, eof);
194         if (*r=='R') {
195           *ptr = r+1;
196           obj->type = PDFTYPE_REF;
197           return obj;
198         }
199       }
200     }
201     obj->type = PDFTYPE_INT;
202     return obj;
203   }
204
205   // a string ?
206   if (*p=='(') {
207     q=p+1; stack=1;
208     while (stack>0 && q!=eof) {
209       if (*q=='(') stack++;
210       if (*q==')') stack--;
211       if (*q=='\\') q++;
212       if (q!=eof) q++;
213     }
214     if (q==eof) { g_free(obj); return NULL; }
215     obj->type = PDFTYPE_STRING;
216     obj->len = q-p;
217     obj->str = g_malloc(obj->len+1);
218     obj->str[obj->len] = 0;
219     g_memmove(obj->str, p, obj->len);
220     *ptr = q;
221     return obj;
222   }  
223   if (*p=='<' && p[1]!='<') {
224     q=p+1;
225     while (*q!='>' && q!=eof) q++;
226     if (q==eof) { g_free(obj); return NULL; }
227     q++;
228     obj->type = PDFTYPE_STRING;
229     obj->len = q-p;
230     obj->str = g_malloc(obj->len+1);
231     obj->str[obj->len] = 0;
232     g_memmove(obj->str, p, obj->len);
233     *ptr = q;
234     return obj;
235   }
236   
237   // a name ?
238   if (*p=='/') {
239     q=p+1;
240     while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
241     obj->type = PDFTYPE_NAME;
242     obj->str = g_strndup(p, q-p);
243     *ptr = q;
244     return obj;
245   }
246
247   // an array ?
248   if (*p=='[') {
249     obj->type = PDFTYPE_ARRAY;
250     obj->num = 0;
251     obj->elts = NULL;
252     q=p+1; skipspace(&q, eof);
253     while (*q!=']') {
254       elt = parse_pdf_object(&q, eof);
255       if (elt==NULL) { free_pdfobj(obj); return NULL; }
256       obj->num++;
257       obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
258       obj->elts[obj->num-1] = elt;
259       skipspace(&q, eof);
260     }
261     *ptr = q+1;
262     return obj;
263   }
264
265   // a dictionary ?
266   if (*p=='<' && p[1]=='<') {
267     obj->type = PDFTYPE_DICT;
268     obj->num = 0;
269     obj->elts = NULL;
270     obj->names = NULL;
271     q=p+2; skipspace(&q, eof);
272     while (*q!='>' || q[1]!='>') {
273       if (*q!='/') { free_pdfobj(obj); return NULL; }
274       r=q+1;
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; }
280       obj->num++;
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;
285       skipspace(&q, eof);
286     }
287     *ptr = q+2;
288     return obj;
289   }
290
291   // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
292   
293   g_free(obj);
294   return NULL;
295 }
296
297 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
298 {
299   int i;
300   
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];
305   return NULL;
306 }
307
308 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
309 {
310   char *p, *eof;
311   int offs, n;
312
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;
318
319   p = pdfbuf->str + offs;
320   eof = pdfbuf->str + pdfbuf->len;
321   n = strtol(p, &p, 10);
322   if (n!=obj->intval) return NULL;
323   skipspace(&p, eof);
324   n = strtol(p, &p, 10);
325   skipspace(&p, eof);
326   if (strncmp(p, "obj", 3)) return NULL;
327   p+=3;
328   return parse_pdf_object(&p, eof);
329 }
330
331 // read the xref table of a PDF file in memory, and return the trailerdict
332
333 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
334 {
335   char *p, *q, *eof;
336   struct PdfObj *trailerdict, *obj;
337   int start, len, i;
338   
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;
343   p+=8;
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);
352     free_pdfobj(obj);
353   }
354   p = pdfbuf->str+offs+4;
355   skipspace(&p, eof);
356   if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
357   while (*p>='0' && *p<='9') {
358     start = strtol(p, &p, 10);
359     skipspace(&p, eof);
360     len = strtol(p, &p, 10);
361     skipspace(&p, eof);
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);
366       p+=20;
367     }
368     skipspace(&p, eof);
369   }
370   if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
371   return trailerdict;
372 }
373
374 // parse the page tree
375
376 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref, 
377                 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
378 {
379   struct PdfObj *obj, *kid;
380   int i, count, j;
381   
382   obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
383   if (obj == NULL || obj->type != PDFTYPE_NAME)
384     return 0;
385   if (!strcmp(obj->str, "/Page")) {
386     free_pdfobj(obj);
387     pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
388     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
389     if (obj!=NULL) {
390       free_pdfobj(pages->resources);
391       pages->resources = obj;
392     }
393     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
394     if (obj!=NULL) {
395       free_pdfobj(pages->mediabox);
396       pages->mediabox = obj;
397     }
398     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
399     if (obj!=NULL && obj->type == PDFTYPE_INT)
400       pages->rotate = obj->intval;
401     free_pdfobj(obj);
402     return 1;
403   }
404   else if (!strcmp(obj->str, "/Pages")) {
405     free_pdfobj(obj);
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;
409     else count = 0;
410     free_pdfobj(obj);
411     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
412     if (obj!=NULL)
413       for (i=0; i<count; i++) {
414         free_pdfobj(pages[i].resources);
415         pages[i].resources = dup_pdfobj(obj);
416       }
417     free_pdfobj(obj);
418     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
419     if (obj!=NULL)
420       for (i=0; i<count; i++) {
421         free_pdfobj(pages[i].mediabox);
422         pages[i].mediabox = dup_pdfobj(obj);
423       }
424     free_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;
429     free_pdfobj(obj);
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]);
434         if (kid!=NULL) {
435           j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
436           nmax -= j;
437           pages += j;
438           free_pdfobj(kid);
439         }
440       }
441     }
442     free_pdfobj(obj);
443     return count;
444   }
445   return 0;
446 }
447
448 // parse a PDF file in memory
449
450 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
451 {
452   char *p;
453   int i, offs;
454   struct PdfObj *obj, *pages;
455
456   xref->n_alloc = xref->last = 0;
457   xref->data = NULL;
458   p = pdfbuf->str + pdfbuf->len-1;
459   
460   while (*p!='s' && p!=pdfbuf->str) p--;
461   if (strncmp(p, "startxref", 9)) return FALSE; // fail
462   p+=9;
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;
467   
468   pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
469   if (pdfinfo->trailerdict == NULL) return FALSE; // fail
470   
471   obj = get_pdfobj(pdfbuf, xref,
472      get_dict_entry(pdfinfo->trailerdict, "/Root"));
473   if (obj == NULL)
474     { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
475   pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
476   free_pdfobj(obj);
477   if (pages == NULL)
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;
484   free_pdfobj(obj);
485   
486   pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
487   pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
488   free_pdfobj(pages);
489   
490   return TRUE;
491 }
492
493 // add an entry to the xref table
494
495 void make_xref(struct XrefTable *xref, int nobj, int offset)
496 {
497   if (xref->n_alloc <= nobj) {
498     xref->n_alloc = nobj + 10;
499     xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
500   }
501   if (xref->last < nobj) xref->last = nobj;
502   xref->data[nobj] = offset;
503 }
504
505 // a wrapper for deflate
506
507 GString *do_deflate(char *in, int len)
508 {
509   GString *out;
510   z_stream zs;
511   
512   zs.zalloc = Z_NULL;
513   zs.zfree = Z_NULL;
514   deflateInit(&zs, Z_DEFAULT_COMPRESSION);
515   zs.next_in = (Bytef *)in;
516   zs.avail_in = len;
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;
522   deflateEnd(&zs);
523   return out;
524 }
525
526 // prefix to scale the original page
527
528 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
529 {
530   GString *str;
531   double v[4], t, xscl, yscl;
532   int i;
533   
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;
538   }
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;
542   }
543   if (pgdesc->rotate == 180) {
544     g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
545   }
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;
553     else return str;
554   }
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);
562   return str;
563 }
564
565 // add an entry to a subentry of a directory
566
567 struct PdfObj *mk_pdfname(char *name)
568 {
569   struct PdfObj *obj;
570   
571   obj = g_malloc(sizeof(struct PdfObj));
572   obj->type = PDFTYPE_NAME;
573   obj->str = g_strdup(name);
574   return obj;
575 }
576
577 struct PdfObj *mk_pdfref(int num)
578 {
579   struct PdfObj *obj;
580   
581   obj = g_malloc(sizeof(struct PdfObj));
582   obj->type = PDFTYPE_REF;
583   obj->intval = num;
584   obj->num = 0;
585   return obj;
586 }
587
588 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
589 {
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);
600   return FALSE;
601 }
602
603 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
604    struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
605 {
606   struct PdfObj *sec;
607   int i, subpos;
608   
609   subpos = -1;
610   for (i=0; i<obj->num; i++) 
611     if (!strcmp(obj->names[i], section)) subpos = i;
612   if (subpos == -1) {
613     subpos = obj->num;
614     obj->num++;
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;
619   }
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;
624   }
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));
629     sec->type = type;
630     sec->num = 0;
631     sec->elts = NULL;
632     sec->names = NULL;
633   }
634   sec = obj->elts[subpos];
635
636   subpos = -1;
637   if (type==PDFTYPE_DICT) {
638     for (i=0; i<sec->num; i++) 
639       if (!strcmp(sec->names[i], name)) subpos = i;
640     if (subpos == -1) {
641       subpos = sec->num;
642       sec->num++;
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;
647     }
648     free_pdfobj(sec->elts[subpos]);
649     sec->elts[subpos] = entry;
650   } 
651   if (type==PDFTYPE_ARRAY) {
652     for (i=0; i<sec->num; i++)
653       if (iseq_obj(sec->elts[i], entry)) subpos = i;
654     if (subpos == -1) {
655       subpos = sec->num;
656       sec->num++;
657       sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
658       sec->elts[subpos] = entry;
659     }
660     else free_pdfobj(entry);
661   }
662 }
663
664 // draw a page's background
665
666 void pdf_draw_solid_background(struct Page *pg, GString *str)
667 {
668   double x, y;
669
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 ",
680         x, x, pg->height);
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 ",
683         y, pg->width, y);
684     return;
685   }
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 ",
688       y, pg->width, y);
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);
694 }
695
696 int pdf_draw_bitmap_background(struct Page *pg, GString *str, 
697                                 struct XrefTable *xref, GString *pdfbuf)
698 {
699   BgPdfPage *pgpdf;
700   GdkPixbuf *pix;
701   GString *zpix;
702   char *buf, *p1, *p2;
703   int height, width, stride, x, y, chan;
704   
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();
712     }
713     pix = pgpdf->pixbuf;
714   }
715   else pix = pg->bg->pixbuf;
716   
717   if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return -1;
718   if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return -1;
719   
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;
725
726   g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
727     pg->width, -pg->height, pg->height);
728   
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++);
734       if (chan==4) p1++;
735     }
736   }
737   zpix = do_deflate(buf, 3*width*height);
738   g_free(buf);
739
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");
749  
750   return xref->last;
751 }
752
753 // draw a page's graphics
754
755 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter)
756 {
757   GList *layerlist, *itemlist;
758   struct Layer *l;
759   struct Item *item;
760   guint old_rgba;
761   double old_thickness;
762   double *pt;
763   int i;
764   
765   old_rgba = 0x12345678;    // not any values we use, so we'll reset them
766   old_thickness = 0.0;
767
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 ");
780           *use_hiliter = TRUE;
781         }
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 ");
791       }
792     }
793   }
794 }
795
796 // main printing function
797
798 /* we use the following object numbers, starting with n_obj_catalog:
799     0 the document catalog
800     1 the page tree
801     2 the GS for the hiliters
802     3 ... the page objects
803 */
804
805 gboolean print_to_pdf(char *filename)
806 {
807   FILE *f;
808   GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
809   int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
810   int i, startxref;
811   struct XrefTable xref;
812   GList *pglist;
813   struct Page *pg;
814   char *buf;
815   unsigned int len;
816   gboolean annot, uses_pdf;
817   gboolean use_hiliter;
818   struct PdfInfo pdfinfo;
819   struct PdfObj *obj;
820   
821   f = fopen(filename, "w");
822   if (f == NULL) return FALSE;
823   annot = FALSE;
824   xref.data = NULL;
825   uses_pdf = 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;
829   }
830   
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);
836     g_free(buf);
837     if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
838     annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
839     if (!annot) {
840       g_string_free(pdfbuf, TRUE);
841       if (xref.data != NULL) g_free(xref.data);
842     }
843   }
844
845   if (!annot) {
846     pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
847     xref.n_alloc = xref.last = 0;
848     xref.data = NULL;
849   }
850     
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",
867      n_obj_catalog+2);
868   xref.last = n_obj_pages_offs + journal.npages-1;
869   
870   for (pglist = journal.pages, n_page = 0; pglist!=NULL;
871        pglist = pglist->next, n_page++) {
872     pg = (struct Page *)pglist->data;
873     
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);
877     n_obj_bgpix = -1;
878     n_obj_prefix = -1;
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 ");
892     }
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
896     use_hiliter = FALSE;
897     pdf_draw_page(pg, pgstrm, &use_hiliter);
898     g_string_append_printf(pgstrm, "Q\n");
899     
900     // deflate pgstrm and write it
901     zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
902     g_string_free(pgstrm, TRUE);
903     
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");
911     
912     // write the page object
913     
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) {
921         free_pdfobj(obj);
922         obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
923       }
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, ' ');
931         }
932       }
933       free_pdfobj(obj);
934       g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
935     }
936     else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
937     g_string_append(pdfbuf, "/Resources ");
938
939     if (n_obj_prefix>0)
940       obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
941     else obj = NULL;
942     if (obj!=NULL && obj->type!=PDFTYPE_DICT)
943       { free_pdfobj(obj); obj=NULL; }
944     if (obj==NULL) {
945       obj = g_malloc(sizeof(struct PdfObj));
946       obj->type = PDFTYPE_DICT;
947       obj->num = 0;
948       obj->elts = NULL;
949       obj->names = NULL;
950     }
951     add_dict_subentry(pdfbuf, &xref,
952         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
953     if (n_obj_bgpix>0)
954       add_dict_subentry(pdfbuf, &xref,
955         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
956     if (use_hiliter)
957       add_dict_subentry(pdfbuf, &xref,
958         obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
959     if (n_obj_bgpix>0)
960       add_dict_subentry(pdfbuf, &xref,
961         obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
962     show_pdfobj(obj, pdfbuf);
963     free_pdfobj(obj);
964     g_string_append(pdfbuf, " >> endobj\n");
965   }
966   
967   // PDF trailer
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);
977   if (annot) {
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...
981 /*    
982     obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
983     if (obj!=NULL) {
984       g_string_append_printf(pdfbuf, "/Encrypt ");
985       show_pdfobj(obj, pdfbuf);
986     } 
987 */
988   }
989   g_string_append_printf(pdfbuf, 
990     ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
991   
992   g_free(xref.data);
993   if (annot) {
994     free_pdfobj(pdfinfo.trailerdict);
995     // ...
996   }
997   
998   if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
999     fclose(f);
1000     g_string_free(pdfbuf, TRUE);
1001     return FALSE;
1002   }
1003   fclose(f);
1004   g_string_free(pdfbuf, TRUE);
1005   return TRUE;
1006 }
1007
1008 /*********** Printing via libgnomeprint **********/
1009
1010 // does the same job as update_canvas_bg(), but to a print context
1011
1012 void print_background(GnomePrintContext *gpc, struct Page *pg, gboolean *abort)
1013 {
1014   double x, y;
1015   GdkPixbuf *pix;
1016   BgPdfPage *pgpdf;
1017
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);
1022
1023     if (pg->bg->ruling == RULING_NONE) return;
1024     gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_COLOR));
1025     gnome_print_setlinewidth(gpc, RULING_THICKNESS);
1026     
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);
1032       return;
1033     }
1034     
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);
1040     }
1041     return;
1042   }
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();
1051           if (*abort) return;
1052         }
1053       }
1054       pix = pgpdf->pixbuf;
1055     }
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);
1069     return;
1070   }
1071 }
1072
1073 void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
1074                 double pgwidth, double pgheight, gboolean *abort)
1075 {
1076   char tmp[10];
1077   gdouble scale;
1078   guint old_rgba;
1079   double old_thickness;
1080   GList *layerlist, *itemlist;
1081   struct Layer *l;
1082   struct Item *item;
1083   int i;
1084   double *pt;
1085   
1086   if (pg==NULL) return;
1087   
1088   g_snprintf(tmp, 10, "Page %d", pageno);
1089   gnome_print_beginpage(gpc, (guchar *)tmp);
1090   gnome_print_gsave(gpc);
1091   
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
1098
1099   print_background(gpc, pg, abort);
1100
1101   old_rgba = 0x12345678;    // not any values we use, so we'll reset them
1102   old_thickness = 0.0;
1103
1104   for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1105     if (*abort) break;
1106     l = (struct Layer *)layerlist->data;
1107     for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1108       if (*abort) break;
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);
1125       }
1126     }
1127   }
1128   
1129   gnome_print_grestore(gpc);
1130   gnome_print_showpage(gpc);
1131 }
1132
1133 void cb_print_abort(GtkDialog *dialog, gint response, gboolean *abort)
1134 {
1135   *abort = TRUE;
1136 }
1137
1138 void print_job_render(GnomePrintJob *gpj, int fromPage, int toPage)
1139 {
1140   GnomePrintConfig *config;
1141   GnomePrintContext *gpc;
1142   GtkWidget *wait_dialog;
1143   double pgwidth, pgheight;
1144   int i;
1145   gboolean abort;
1146   
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));
1150
1151   gpc = gnome_print_job_get_context(gpj);
1152
1153   abort = FALSE;
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);
1158   
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); 
1164 #endif
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);
1168     if (abort) break;
1169   }
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...");
1174 #endif
1175   while (gtk_events_pending()) gtk_main_iteration();
1176
1177   gnome_print_context_close(gpc);  
1178   g_object_unref(G_OBJECT(gpc));  
1179
1180   gnome_print_job_close(gpj);
1181   if (!abort) gnome_print_job_print(gpj);
1182   g_object_unref(G_OBJECT(gpj));
1183
1184   gtk_widget_destroy(wait_dialog);
1185 }