]> git.donarmstrong.com Git - xournal.git/blob - src/xo-print.c
fix x86_64 bug in PDF export code (#2897699)
[xournal.git] / src / xo-print.c
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #define PANGO_ENABLE_BACKEND /* to access PangoFcFont.font_pattern */
6
7 #include <gtk/gtk.h>
8 #include <libgnomecanvas/libgnomecanvas.h>
9 #include <zlib.h>
10 #include <string.h>
11 #include <locale.h>
12 #include <pango/pango.h>
13 #include <pango/pangofc-font.h>
14 #include <pango/pangoft2.h>
15 #include <fontconfig/fontconfig.h>
16 #include <ft2build.h>
17 #include FT_FREETYPE_H
18
19 #define NO_MAPPERS
20 #define NO_TYPE3
21 #define NO_TYPE42
22 #include "ttsubset/sft.h"
23
24 #include "xournal.h"
25 #include "xo-support.h"
26 #include "xo-misc.h"
27 #include "xo-paint.h"
28 #include "xo-print.h"
29 #include "xo-file.h"
30
31 #define RGBA_RED(rgba) (((rgba>>24)&0xff)/255.0)
32 #define RGBA_GREEN(rgba) (((rgba>>16)&0xff)/255.0)
33 #define RGBA_BLUE(rgba) (((rgba>>8)&0xff)/255.0)
34 #define RGBA_ALPHA(rgba) (((rgba>>0)&0xff)/255.0)
35 #define RGBA_RGB(rgba) RGBA_RED(rgba), RGBA_GREEN(rgba), RGBA_BLUE(rgba)
36
37 /*********** Printing to PDF ************/
38
39 gboolean ispdfspace(char c)
40 {
41   return (c==0 || c==9 || c==10 || c==12 || c==13 || c==' ');
42 }
43
44 gboolean ispdfdelim(char c)
45 {
46   return (c=='(' || c==')' || c=='<' || c=='>' || c=='[' || c==']' ||
47           c=='{' || c=='}' || c=='/' || c=='%');
48 }
49
50 void skipspace(char **p, char *eof)
51 {
52   while (ispdfspace(**p) || **p=='%') {
53     if (**p=='%') while (*p!=eof && **p!=10 && **p!=13) (*p)++;
54     if (*p==eof) return;
55     (*p)++;
56   }
57 }
58
59 void free_pdfobj(struct PdfObj *obj)
60 {
61   int i;
62   
63   if (obj==NULL) return;
64   if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
65       obj->type == PDFTYPE_STREAM) && obj->str!=NULL)
66     g_free(obj->str);
67   if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
68       obj->type == PDFTYPE_STREAM) && obj->num>0) {
69     for (i=0; i<obj->num; i++)
70       free_pdfobj(obj->elts[i]);
71     g_free(obj->elts);
72   }
73   if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
74     for (i=0; i<obj->num; i++)
75       g_free(obj->names[i]);
76     g_free(obj->names);
77   }
78   g_free(obj);
79 }
80
81 struct PdfObj *dup_pdfobj(struct PdfObj *obj)
82 {
83   struct PdfObj *dup;
84   int i;
85   
86   if (obj==NULL) return NULL;
87   dup = g_memdup(obj, sizeof(struct PdfObj));
88   if ((obj->type == PDFTYPE_STRING || obj->type == PDFTYPE_NAME ||
89       obj->type == PDFTYPE_STREAM) && obj->str!=NULL) {
90     if (obj->type == PDFTYPE_NAME) obj->len = strlen(obj->str);
91     dup->str = g_memdup(obj->str, obj->len+1);
92   }
93   if ((obj->type == PDFTYPE_ARRAY || obj->type == PDFTYPE_DICT ||
94       obj->type == PDFTYPE_STREAM) && obj->num>0) {
95     dup->elts = g_malloc(obj->num*sizeof(struct PdfObj *));
96     for (i=0; i<obj->num; i++)
97       dup->elts[i] = dup_pdfobj(obj->elts[i]);
98   }
99   if ((obj->type == PDFTYPE_DICT || obj->type == PDFTYPE_STREAM) && obj->num>0) {
100     dup->names = g_malloc(obj->num*sizeof(char *));
101     for (i=0; i<obj->num; i++)
102       dup->names[i] = g_strdup(obj->names[i]);
103   }
104   return dup;
105 }
106
107 void show_pdfobj(struct PdfObj *obj, GString *str)
108 {
109   int i;
110   if (obj==NULL) return;
111   switch(obj->type) {
112     case PDFTYPE_CST:
113       if (obj->intval==1) g_string_append(str, "true");
114       if (obj->intval==0) g_string_append(str, "false");
115       if (obj->intval==-1) g_string_append(str, "null");
116       break;
117     case PDFTYPE_INT:
118       g_string_append_printf(str, "%d", obj->intval);
119       break;
120     case PDFTYPE_REAL:
121       g_string_append_printf(str, "%f", obj->realval);
122       break;
123     case PDFTYPE_STRING:
124       g_string_append_len(str, obj->str, obj->len);
125       break;
126     case PDFTYPE_NAME:
127       g_string_append(str, obj->str);
128       break;
129     case PDFTYPE_ARRAY:
130       g_string_append_c(str, '[');
131       for (i=0;i<obj->num;i++) {
132         if (i) g_string_append_c(str, ' ');
133         show_pdfobj(obj->elts[i], str);
134       }
135       g_string_append_c(str, ']');
136       break;
137     case PDFTYPE_DICT:
138       g_string_append(str, "<<");
139       for (i=0;i<obj->num;i++) {
140         g_string_append_printf(str, " %s ", obj->names[i]); 
141         show_pdfobj(obj->elts[i], str);
142       }
143       g_string_append(str, " >>");
144       break;
145     case PDFTYPE_REF:
146       g_string_append_printf(str, "%d %d R", obj->intval, obj->num);
147       break;
148   }
149 }
150
151 void DEBUG_PRINTOBJ(struct PdfObj *obj)
152 {
153   GString *s = g_string_new("");
154   show_pdfobj(obj, s);
155   puts(s->str);
156   g_string_free(s, TRUE);
157 }
158
159 // parse a PDF object; returns NULL if fails
160 // THIS PARSER DOES NOT RECOGNIZE STREAMS YET
161
162 struct PdfObj *parse_pdf_object(char **ptr, char *eof)
163 {
164   struct PdfObj *obj, *elt;
165   char *p, *q, *r, *eltname;
166   int stack;
167
168   obj = g_malloc(sizeof(struct PdfObj));
169   p = *ptr;
170   skipspace(&p, eof);
171   if (p==eof) { g_free(obj); return NULL; }
172   
173   // maybe a constant
174   if (!strncmp(p, "true", 4)) {
175     obj->type = PDFTYPE_CST;
176     obj->intval = 1;
177     *ptr = p+4;
178     return obj;
179   }
180   if (!strncmp(p, "false", 5)) {
181     obj->type = PDFTYPE_CST;
182     obj->intval = 0;
183     *ptr = p+5;
184     return obj;
185   }
186   if (!strncmp(p, "null", 4)) {
187     obj->type = PDFTYPE_CST;
188     obj->intval = -1;
189     *ptr = p+4;
190     return obj;
191   }
192
193   // or a number ?
194   obj->intval = strtol(p, &q, 10);
195   *ptr = q;
196   if (q!=p) {
197     if (*q == '.') {
198       obj->type = PDFTYPE_REAL;
199       obj->realval = g_ascii_strtod(p, ptr);
200       return obj;
201     }
202     if (ispdfspace(*q)) {
203       // check for indirect reference
204       skipspace(&q, eof);
205       obj->num = strtol(q, &r, 10);
206       if (r!=q) {
207         skipspace(&r, eof);
208         if (*r=='R') {
209           *ptr = r+1;
210           obj->type = PDFTYPE_REF;
211           return obj;
212         }
213       }
214     }
215     obj->type = PDFTYPE_INT;
216     return obj;
217   }
218
219   // a string ?
220   if (*p=='(') {
221     q=p+1; stack=1;
222     while (stack>0 && q!=eof) {
223       if (*q=='(') stack++;
224       if (*q==')') stack--;
225       if (*q=='\\') q++;
226       if (q!=eof) q++;
227     }
228     if (q==eof) { g_free(obj); return NULL; }
229     obj->type = PDFTYPE_STRING;
230     obj->len = q-p;
231     obj->str = g_malloc(obj->len+1);
232     obj->str[obj->len] = 0;
233     g_memmove(obj->str, p, obj->len);
234     *ptr = q;
235     return obj;
236   }  
237   if (*p=='<' && p[1]!='<') {
238     q=p+1;
239     while (*q!='>' && q!=eof) q++;
240     if (q==eof) { g_free(obj); return NULL; }
241     q++;
242     obj->type = PDFTYPE_STRING;
243     obj->len = q-p;
244     obj->str = g_malloc(obj->len+1);
245     obj->str[obj->len] = 0;
246     g_memmove(obj->str, p, obj->len);
247     *ptr = q;
248     return obj;
249   }
250   
251   // a name ?
252   if (*p=='/') {
253     q=p+1;
254     while (!ispdfspace(*q) && !ispdfdelim(*q)) q++;
255     obj->type = PDFTYPE_NAME;
256     obj->str = g_strndup(p, q-p);
257     *ptr = q;
258     return obj;
259   }
260
261   // an array ?
262   if (*p=='[') {
263     obj->type = PDFTYPE_ARRAY;
264     obj->num = 0;
265     obj->elts = NULL;
266     q=p+1; skipspace(&q, eof);
267     while (*q!=']') {
268       elt = parse_pdf_object(&q, eof);
269       if (elt==NULL) { free_pdfobj(obj); return NULL; }
270       obj->num++;
271       obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
272       obj->elts[obj->num-1] = elt;
273       skipspace(&q, eof);
274     }
275     *ptr = q+1;
276     return obj;
277   }
278
279   // a dictionary ?
280   if (*p=='<' && p[1]=='<') {
281     obj->type = PDFTYPE_DICT;
282     obj->num = 0;
283     obj->elts = NULL;
284     obj->names = NULL;
285     q=p+2; skipspace(&q, eof);
286     while (*q!='>' || q[1]!='>') {
287       if (*q!='/') { free_pdfobj(obj); return NULL; }
288       r=q+1;
289       while (!ispdfspace(*r) && !ispdfdelim(*r)) r++;
290       eltname = g_strndup(q, r-q);
291       q=r; skipspace(&q, eof);
292       elt = parse_pdf_object(&q, eof);
293       if (elt==NULL) { g_free(eltname); free_pdfobj(obj); return NULL; }
294       obj->num++;
295       obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj *));
296       obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
297       obj->elts[obj->num-1] = elt;
298       obj->names[obj->num-1] = eltname;
299       skipspace(&q, eof);
300     }
301     *ptr = q+2;
302     return obj;
303   }
304
305   // DOES NOT RECOGNIZE STREAMS YET (handle as subcase of dictionary)
306   
307   g_free(obj);
308   return NULL;
309 }
310
311 struct PdfObj *get_dict_entry(struct PdfObj *dict, char *name)
312 {
313   int i;
314   
315   if (dict==NULL) return NULL;
316   if (dict->type != PDFTYPE_DICT) return NULL;
317   for (i=0; i<dict->num; i++) 
318     if (!strcmp(dict->names[i], name)) return dict->elts[i];
319   return NULL;
320 }
321
322 struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj *obj)
323 {
324   char *p, *eof;
325   int offs, n;
326
327   if (obj==NULL) return NULL;
328   if (obj->type!=PDFTYPE_REF) return dup_pdfobj(obj);
329   if (obj->intval>xref->last) return NULL;
330   offs = xref->data[obj->intval];
331   if (offs<=0 || offs >= pdfbuf->len) return NULL;
332
333   p = pdfbuf->str + offs;
334   eof = pdfbuf->str + pdfbuf->len;
335   n = strtol(p, &p, 10);
336   if (n!=obj->intval) return NULL;
337   skipspace(&p, eof);
338   n = strtol(p, &p, 10);
339   skipspace(&p, eof);
340   if (strncmp(p, "obj", 3)) return NULL;
341   p+=3;
342   return parse_pdf_object(&p, eof);
343 }
344
345 // read the xref table of a PDF file in memory, and return the trailerdict
346
347 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
348 {
349   char *p, *eof;
350   struct PdfObj *trailerdict, *obj;
351   int start, len, i;
352   
353   if (strncmp(pdfbuf->str+offs, "xref", 4)) return NULL;
354   p = strstr(pdfbuf->str+offs, "trailer");
355   eof = pdfbuf->str + pdfbuf->len;
356   if (p==NULL) return NULL;
357   p+=8;
358   trailerdict = parse_pdf_object(&p, eof);
359   obj = get_dict_entry(trailerdict, "/Size");
360   if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval-1>xref->last)
361     make_xref(xref, obj->intval-1, 0);
362   obj = get_dict_entry(trailerdict, "/Prev");
363   if (obj!=NULL && obj->type == PDFTYPE_INT && obj->intval>0 && obj->intval!=offs) {
364     // recurse into older xref table
365     obj = parse_xref_table(pdfbuf, xref, obj->intval);
366     free_pdfobj(obj);
367   }
368   p = pdfbuf->str+offs+4;
369   skipspace(&p, eof);
370   if (*p<'0' || *p>'9') { free_pdfobj(trailerdict); return NULL; }
371   while (*p>='0' && *p<='9') {
372     start = strtol(p, &p, 10);
373     skipspace(&p, eof);
374     len = strtol(p, &p, 10);
375     skipspace(&p, eof);
376     if (len <= 0 || 20*len > eof-p) break;
377     if (start+len-1 > xref->last) make_xref(xref, start+len-1, 0);
378     for (i=start; i<start+len; i++) {
379       xref->data[i] = strtol(p, NULL, 10);
380       p+=20;
381     }
382     skipspace(&p, eof);
383   }
384   if (*p!='t') { free_pdfobj(trailerdict); return NULL; }
385   return trailerdict;
386 }
387
388 // parse the page tree
389
390 int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref, 
391                 struct PdfObj *pgtree, int nmax, struct PdfPageDesc *pages)
392 {
393   struct PdfObj *obj, *kid;
394   int i, count, j;
395   
396   obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Type"));
397   if (obj == NULL || obj->type != PDFTYPE_NAME)
398     return 0;
399   if (!strcmp(obj->str, "/Page")) {
400     free_pdfobj(obj);
401     pages->contents = dup_pdfobj(get_dict_entry(pgtree, "/Contents"));
402     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
403     if (obj!=NULL) {
404       free_pdfobj(pages->resources);
405       pages->resources = obj;
406     }
407     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
408     if (obj!=NULL) {
409       free_pdfobj(pages->mediabox);
410       pages->mediabox = obj;
411     }
412     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
413     if (obj!=NULL && obj->type == PDFTYPE_INT)
414       pages->rotate = obj->intval;
415     free_pdfobj(obj);
416     return 1;
417   }
418   else if (!strcmp(obj->str, "/Pages")) {
419     free_pdfobj(obj);
420     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Count"));
421     if (obj!=NULL && obj->type == PDFTYPE_INT && 
422         obj->intval>0 && obj->intval<=nmax) count = obj->intval;
423     else count = 0;
424     free_pdfobj(obj);
425     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Resources"));
426     if (obj!=NULL)
427       for (i=0; i<count; i++) {
428         free_pdfobj(pages[i].resources);
429         pages[i].resources = dup_pdfobj(obj);
430       }
431     free_pdfobj(obj);
432     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/MediaBox"));
433     if (obj!=NULL)
434       for (i=0; i<count; i++) {
435         free_pdfobj(pages[i].mediabox);
436         pages[i].mediabox = dup_pdfobj(obj);
437       }
438     free_pdfobj(obj);
439     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Rotate"));
440     if (obj!=NULL && obj->type == PDFTYPE_INT)
441       for (i=0; i<count; i++)
442         pages[i].rotate = obj->intval;
443     free_pdfobj(obj);
444     obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pgtree, "/Kids"));
445     if (obj!=NULL && obj->type == PDFTYPE_ARRAY) {
446       for (i=0; i<obj->num; i++) {
447         kid = get_pdfobj(pdfbuf, xref, obj->elts[i]);
448         if (kid!=NULL) {
449           j = pdf_getpageinfo(pdfbuf, xref, kid, nmax, pages);
450           nmax -= j;
451           pages += j;
452           free_pdfobj(kid);
453         }
454       }
455     }
456     free_pdfobj(obj);
457     return count;
458   }
459   return 0;
460 }
461
462 // parse a PDF file in memory
463
464 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
465 {
466   char *p;
467   int offs;
468   struct PdfObj *obj, *pages;
469
470   xref->n_alloc = xref->last = 0;
471   xref->data = NULL;
472   p = pdfbuf->str + pdfbuf->len-1;
473   
474   while (*p!='s' && p!=pdfbuf->str) p--;
475   if (strncmp(p, "startxref", 9)) return FALSE; // fail
476   p+=9;
477   while (ispdfspace(*p) && p!=pdfbuf->str+pdfbuf->len) p++;
478   offs = strtol(p, NULL, 10);
479   if (offs <= 0 || offs > pdfbuf->len) return FALSE; // fail
480   pdfinfo->startxref = offs;
481   
482   pdfinfo->trailerdict = parse_xref_table(pdfbuf, xref, offs);
483   if (pdfinfo->trailerdict == NULL) return FALSE; // fail
484   
485   obj = get_pdfobj(pdfbuf, xref,
486      get_dict_entry(pdfinfo->trailerdict, "/Root"));
487   if (obj == NULL)
488     { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
489   pages = get_pdfobj(pdfbuf, xref, get_dict_entry(obj, "/Pages"));
490   free_pdfobj(obj);
491   if (pages == NULL)
492     { free_pdfobj(pdfinfo->trailerdict); return FALSE; }
493   obj = get_pdfobj(pdfbuf, xref, get_dict_entry(pages, "/Count"));
494   if (obj == NULL || obj->type != PDFTYPE_INT || obj->intval<=0) 
495     { free_pdfobj(pdfinfo->trailerdict); free_pdfobj(pages); 
496       free_pdfobj(obj); return FALSE; }
497   pdfinfo->npages = obj->intval;
498   free_pdfobj(obj);
499   
500   pdfinfo->pages = g_malloc0(pdfinfo->npages*sizeof(struct PdfPageDesc));
501   pdf_getpageinfo(pdfbuf, xref, pages, pdfinfo->npages, pdfinfo->pages);
502   free_pdfobj(pages);
503   
504   return TRUE;
505 }
506
507 // add an entry to the xref table
508
509 void make_xref(struct XrefTable *xref, int nobj, int offset)
510 {
511   if (xref->n_alloc <= nobj) {
512     xref->n_alloc = nobj + 10;
513     xref->data = g_realloc(xref->data, xref->n_alloc*sizeof(int));
514   }
515   if (xref->last < nobj) xref->last = nobj;
516   xref->data[nobj] = offset;
517 }
518
519 // a wrapper for deflate
520
521 GString *do_deflate(char *in, int len)
522 {
523   GString *out;
524   z_stream zs;
525   
526   zs.zalloc = Z_NULL;
527   zs.zfree = Z_NULL;
528   deflateInit(&zs, Z_DEFAULT_COMPRESSION);
529   zs.next_in = (Bytef *)in;
530   zs.avail_in = len;
531   zs.avail_out = deflateBound(&zs, len);
532   out = g_string_sized_new(zs.avail_out);
533   zs.next_out = (Bytef *)out->str;
534   deflate(&zs, Z_FINISH);
535   out->len = zs.total_out;
536   deflateEnd(&zs);
537   return out;
538 }
539
540 // prefix to scale the original page
541
542 GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
543 {
544   GString *str;
545   double v[4], t, xscl, yscl;
546   int i;
547
548   // push 3 times in case code to be annotated has unbalanced q/Q (B of A., ...)
549   str = g_string_new("q q q "); 
550   if (pgdesc->rotate == 90) {
551     g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
552     t = height; height = width; width = t;
553   }
554   if (pgdesc->rotate == 270) {
555     g_string_append_printf(str, "0 1 -1 0 %.2f 0 cm ", width);
556     t = height; height = width; width = t;
557   }
558   if (pgdesc->rotate == 180) {
559     g_string_append_printf(str, "-1 0 0 -1 %.2f %.2f cm ", width, height);
560   }
561   if (pgdesc->mediabox==NULL || pgdesc->mediabox->type != PDFTYPE_ARRAY ||
562       pgdesc->mediabox->num != 4) return str;
563   for (i=0; i<4; i++) {
564     if (pgdesc->mediabox->elts[i]->type == PDFTYPE_INT)
565       v[i] = pgdesc->mediabox->elts[i]->intval;
566     else if (pgdesc->mediabox->elts[i]->type == PDFTYPE_REAL)
567       v[i] = pgdesc->mediabox->elts[i]->realval;
568     else return str;
569   }
570   if (v[0]>v[2]) { t = v[0]; v[0] = v[2]; v[2] = t; }
571   if (v[1]>v[3]) { t = v[1]; v[1] = v[3]; v[3] = t; }
572   if (v[2]-v[0] < 1. || v[3]-v[1] < 1.) return str;
573   xscl = width/(v[2]-v[0]);
574   yscl = height/(v[3]-v[1]);
575   g_string_append_printf(str, "%.4f 0 0 %.4f %.2f %.2f cm ",
576     xscl, yscl, -v[0]*xscl, -v[1]*yscl);
577   return str;
578 }
579
580 // add an entry to a subentry of a directory
581
582 struct PdfObj *mk_pdfname(char *name)
583 {
584   struct PdfObj *obj;
585   
586   obj = g_malloc(sizeof(struct PdfObj));
587   obj->type = PDFTYPE_NAME;
588   obj->str = g_strdup(name);
589   return obj;
590 }
591
592 struct PdfObj *mk_pdfref(int num)
593 {
594   struct PdfObj *obj;
595   
596   obj = g_malloc(sizeof(struct PdfObj));
597   obj->type = PDFTYPE_REF;
598   obj->intval = num;
599   obj->num = 0;
600   return obj;
601 }
602
603 gboolean iseq_obj(struct PdfObj *a, struct PdfObj *b)
604 {
605   if (a==NULL || b==NULL) return (a==b);
606   if (a->type!=b->type) return FALSE;
607   if (a->type == PDFTYPE_CST || a->type == PDFTYPE_INT)
608     return (a->intval == b->intval);
609   if (a->type == PDFTYPE_REAL)
610     return (a->realval == b->realval);
611   if (a->type == PDFTYPE_NAME)
612     return !strcmp(a->str, b->str);
613   if (a->type == PDFTYPE_REF)
614     return (a->intval == b->intval && a->num == b->num);
615   return FALSE;
616 }
617
618 void add_dict_subentry(GString *pdfbuf, struct XrefTable *xref,
619    struct PdfObj *obj, char *section, int type, char *name, struct PdfObj *entry)
620 {
621   struct PdfObj *sec;
622   int i, subpos;
623   
624   subpos = -1;
625   for (i=0; i<obj->num; i++) 
626     if (!strcmp(obj->names[i], section)) subpos = i;
627   if (subpos == -1) {
628     subpos = obj->num;
629     obj->num++;
630     obj->elts = g_realloc(obj->elts, obj->num*sizeof(struct PdfObj*));
631     obj->names = g_realloc(obj->names, obj->num*sizeof(char *));
632     obj->names[subpos] = g_strdup(section);
633     obj->elts[subpos] = NULL;
634   }
635   if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type==PDFTYPE_REF) {
636     sec = get_pdfobj(pdfbuf, xref, obj->elts[subpos]);
637     free_pdfobj(obj->elts[subpos]);
638     obj->elts[subpos] = sec;
639   }
640   if (obj->elts[subpos]!=NULL && obj->elts[subpos]->type!=type)
641     { free_pdfobj(obj->elts[subpos]); obj->elts[subpos] = NULL; }
642   if (obj->elts[subpos] == NULL) {
643     obj->elts[subpos] = sec = g_malloc(sizeof(struct PdfObj));
644     sec->type = type;
645     sec->num = 0;
646     sec->elts = NULL;
647     sec->names = NULL;
648   }
649   sec = obj->elts[subpos];
650
651   subpos = -1;
652   if (type==PDFTYPE_DICT) {
653     for (i=0; i<sec->num; i++) 
654       if (!strcmp(sec->names[i], name)) subpos = i;
655     if (subpos == -1) {
656       subpos = sec->num;
657       sec->num++;
658       sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
659       sec->names = g_realloc(sec->names, sec->num*sizeof(char *));
660       sec->names[subpos] = g_strdup(name);
661       sec->elts[subpos] = NULL;
662     }
663     free_pdfobj(sec->elts[subpos]);
664     sec->elts[subpos] = entry;
665   } 
666   if (type==PDFTYPE_ARRAY) {
667     for (i=0; i<sec->num; i++)
668       if (iseq_obj(sec->elts[i], entry)) subpos = i;
669     if (subpos == -1) {
670       subpos = sec->num;
671       sec->num++;
672       sec->elts = g_realloc(sec->elts, sec->num*sizeof(struct PdfObj*));
673       sec->elts[subpos] = entry;
674     }
675     else free_pdfobj(entry);
676   }
677 }
678
679 // draw a page's background
680
681 void pdf_draw_solid_background(struct Page *pg, GString *str)
682 {
683   double x, y;
684
685   g_string_append_printf(str, 
686     "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
687     RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
688   if (!ui.print_ruling) return;
689   if (pg->bg->ruling == RULING_NONE) return;
690   g_string_append_printf(str,
691     "%.2f %.2f %.2f RG %.2f w ",
692     RGBA_RGB(RULING_COLOR), RULING_THICKNESS);
693   if (pg->bg->ruling == RULING_GRAPH) {
694     for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
695       g_string_append_printf(str, "%.2f 0 m %.2f %.2f l S ",
696         x, x, pg->height);
697     for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
698       g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
699         y, pg->width, y);
700     return;
701   }
702   for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
703     g_string_append_printf(str, "0 %.2f m %.2f %.2f l S ",
704       y, pg->width, y);
705   if (pg->bg->ruling == RULING_LINED)
706     g_string_append_printf(str, 
707       "%.2f %.2f %.2f RG %.2f 0 m %.2f %.2f l S ",
708       RGBA_RGB(RULING_MARGIN_COLOR), 
709       RULING_LEFTMARGIN, RULING_LEFTMARGIN, pg->height);
710 }
711
712 int pdf_draw_bitmap_background(struct Page *pg, GString *str, 
713                                 struct XrefTable *xref, GString *pdfbuf)
714 {
715   BgPdfPage *pgpdf;
716   GdkPixbuf *pix;
717   GString *zpix;
718   PopplerPage *pdfpage;
719   char *buf, *p1, *p2;
720   int height, width, stride, x, y, chan;
721   double pgheight, pgwidth;
722   
723   if (pg->bg->type == BG_PDF) {
724     if (!bgpdf.document) return -1;
725     pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
726     if (!pdfpage) return -1;
727     poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
728     width = (int) (PDFTOPPM_PRINTING_DPI * pgwidth/72.0);
729     height = (int) (PDFTOPPM_PRINTING_DPI * pgheight/72.0);
730     pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
731     poppler_page_render_to_pixbuf(
732        pdfpage, 0, 0, width, height, PDFTOPPM_PRINTING_DPI/72.0, 0, pix);
733     g_object_unref(pdfpage);
734   }
735   else pix = g_object_ref(pg->bg->pixbuf);
736   
737   if (gdk_pixbuf_get_bits_per_sample(pix) != 8 ||
738       gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB)
739     { g_object_unref(pix); return -1; }
740                     
741   width = gdk_pixbuf_get_width(pix);
742   height = gdk_pixbuf_get_height(pix);
743   stride = gdk_pixbuf_get_rowstride(pix);
744   chan = gdk_pixbuf_get_n_channels(pix);
745   if (chan!=3 && chan!=4) { g_object_unref(pix); return -1; }
746
747   g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
748     pg->width, -pg->height, pg->height);
749   
750   p2 = buf = (char *)g_malloc(3*width*height);
751   for (y=0; y<height; y++) {
752     p1 = (char *)gdk_pixbuf_get_pixels(pix)+stride*y;
753     for (x=0; x<width; x++) {
754       *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
755       if (chan==4) p1++;
756     }
757   }
758   zpix = do_deflate(buf, 3*width*height);
759   g_free(buf);
760   g_object_unref(pix);
761
762   make_xref(xref, xref->last+1, pdfbuf->len);
763   g_string_append_printf(pdfbuf, 
764     "%d 0 obj\n<< /Length %zu /Filter /FlateDecode /Type /Xobject "
765     "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
766     "/BitsPerComponent 8 >> stream\n",
767     xref->last, zpix->len, width, height);
768   g_string_append_len(pdfbuf, zpix->str, zpix->len);
769   g_string_free(zpix, TRUE);
770   g_string_append(pdfbuf, "endstream\nendobj\n");
771  
772   return xref->last;
773 }
774
775 // manipulate Pdf fonts
776
777 struct PdfFont *new_pdffont(struct XrefTable *xref, GList **fonts,
778    char *filename, int font_id, FT_Face face, int glyph_page)
779 {
780   GList *list;
781   struct PdfFont *font;
782   int i;
783   const char *s;
784   
785   for (list = *fonts; list!=NULL; list = list->next) {
786     font = (struct PdfFont *)list->data;
787     if (!strcmp(font->filename, filename) && font->font_id == font_id 
788         && font->glyph_page == glyph_page)
789           { font->used_in_this_page = TRUE; return font; }
790   }
791   font = g_malloc(sizeof(struct PdfFont));
792   *fonts = g_list_append(*fonts, font);
793   font->n_obj = xref->last+1;
794   make_xref(xref, xref->last+1, 0); // will give it a value later
795   font->filename = g_strdup(filename);
796   font->font_id = font_id;
797   font->glyph_page = glyph_page;
798   font->used_in_this_page = TRUE;
799   font->num_glyphs_used = 0;
800   for (i=0; i<256; i++) {
801     font->glyphmap[i] = -1;
802     font->advance[i] = 0;
803     font->glyphpsnames[i] = NULL;
804   }
805   font->glyphmap[0] = 0;
806   // fill in info from the FT_Face
807   font->is_truetype = FT_IS_SFNT(face);
808   font->nglyphs = face->num_glyphs;
809   font->ft2ps = 1000.0 / face->units_per_EM;
810   font->ascender = (int)(font->ft2ps*face->ascender);
811   font->descender = (int)(font->ft2ps*face->descender);
812   if (face->bbox.xMin < -100000 || face->bbox.xMin > 100000) font->xmin = 0;
813   else font->xmin = (int)(font->ft2ps*face->bbox.xMin);
814   if (face->bbox.xMax < -100000 || face->bbox.xMax > 100000) font->xmax = 0;
815   else font->xmax = (int)(font->ft2ps*face->bbox.xMax);
816   if (face->bbox.yMin < -100000 || face->bbox.yMin > 100000) font->ymin = 0;
817   else font->ymin = (int)(font->ft2ps*face->bbox.yMin);
818   if (face->bbox.yMax < -100000 || face->bbox.yMax > 100000) font->ymax = 0;
819   else font->ymax = (int)(font->ft2ps*face->bbox.yMax);
820   if (font->is_truetype) font->flags = 4; // symbolic
821   else {
822     font->flags = 4; // symbolic
823     if (FT_IS_FIXED_WIDTH(face)) font->flags |= 1;
824     if (face->style_flags & FT_STYLE_FLAG_ITALIC) font->flags |= 64;
825   }
826   s = FT_Get_Postscript_Name(face);
827   if (s==NULL) s = "Noname";
828   if (glyph_page) font->fontname = g_strdup_printf("%s_%03d", s, glyph_page);
829   else font->fontname = g_strdup(s);
830   return font;
831 }
832
833 #define pfb_get_length(x) (((x)[3]<<24) + ((x)[2]<<16) + ((x)[1]<<8) + (x)[0])
834 #define T1_SEGMENT_1_END "currentfile eexec"
835 #define T1_SEGMENT_3_END "cleartomark"
836
837 void embed_pdffont(GString *pdfbuf, struct XrefTable *xref, struct PdfFont *font)
838 {
839   // this code inspired by libgnomeprint
840   gboolean fallback, is_binary;
841   guchar encoding[256];
842   gushort glyphs[256];
843   int i, j, num;
844   guint32 len1, len2;
845   guint32 tt_len;
846   gsize t1_len;
847   TrueTypeFont *ttfnt;
848   char *seg1, *seg2;
849   char *fontdata, *p;
850   char prefix[8];
851   int nobj_fontprog, nobj_descr, lastchar;
852   
853   fallback = FALSE;
854   // embed the font file: TrueType case
855   if (font->is_truetype) {
856     glyphs[0] = encoding[0] = 0;
857     num = 1;
858     for (i=1; i<=255; i++) 
859       if (font->glyphmap[i]>=0) {
860         font->glyphmap[i] = num;
861         glyphs[num] = 255*font->glyph_page+i-1;
862         encoding[num] = i;
863         num++;
864       }
865     font->num_glyphs_used = num-1;
866     if (OpenTTFont(font->filename, 0, &ttfnt) == SF_OK) {
867       if (CreateTTFromTTGlyphs_tomemory(ttfnt, (guint8**)&fontdata, &tt_len, glyphs, encoding, num, 
868                    0, NULL, TTCF_AutoName | TTCF_IncludeOS2) == SF_OK) {
869         make_xref(xref, xref->last+1, pdfbuf->len);
870         nobj_fontprog = xref->last;
871         g_string_append_printf(pdfbuf, 
872           "%d 0 obj\n<< /Length %u /Length1 %u >> stream\n",
873           nobj_fontprog, tt_len, tt_len);
874         g_string_append_len(pdfbuf, fontdata, tt_len);
875         g_string_append(pdfbuf, "endstream\nendobj\n");
876         g_free(fontdata);
877       }
878       else fallback = TRUE;
879       CloseTTFont(ttfnt);
880     } 
881     else fallback = TRUE;
882   } else {
883   // embed the font file: Type1 case
884     if (g_file_get_contents(font->filename, &fontdata, &t1_len, NULL) && t1_len>=8) {
885       if (fontdata[0]==(char)0x80 && fontdata[1]==(char)0x01) {
886         is_binary = TRUE;
887         len1 = pfb_get_length((unsigned char *)fontdata+2);
888         if (fontdata[len1+6]!=(char)0x80 || fontdata[len1+7]!=(char)0x02) fallback = TRUE;
889         else {
890           len2 = pfb_get_length((unsigned char *)fontdata+len1+8);
891           if (fontdata[len1+len2+12]!=(char)0x80 || fontdata[len1+len2+13]!=(char)0x01)
892             fallback = TRUE;
893         }
894       }
895       else if (!strncmp(fontdata, "%!PS", 4)) {
896         is_binary = FALSE;
897         p = strstr(fontdata, T1_SEGMENT_1_END) + strlen(T1_SEGMENT_1_END);
898         if (p==NULL) fallback = TRUE;
899         else {
900           if (*p=='\n' || *p=='\r') p++;
901           if (*p=='\n' || *p=='\r') p++;
902           len1 = p-fontdata;
903           p = g_strrstr_len(fontdata, t1_len, T1_SEGMENT_3_END);
904           if (p==NULL) fallback = TRUE;
905           else {
906             // rewind 512 zeros
907             i = 512; p--;
908             while (i>0 && p!=fontdata && (*p=='0' || *p=='\r' || *p=='\n')) {
909               if (*p=='0') i--;
910               p--;
911             }
912             while (p!=fontdata && (*p=='\r' || *p=='\n')) p--;
913             p++;
914             if (i>0) fallback = TRUE;
915             else len2 = p-fontdata-len1;
916           }
917         }
918       }
919       else fallback = TRUE;
920       if (!fallback) {
921         if (is_binary) {
922           seg1 = fontdata+6;
923           seg2 = fontdata + len1 + 12;
924         } else {
925           seg1 = fontdata;
926           seg2 = g_malloc(len2/2);
927           j=0;
928           p = fontdata+len1;
929           while (p+1 < fontdata+len1+len2) {
930             if (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') { p++; continue; }
931             if (p[0]>'9') { p[0]|=0x20; p[0]-=39; }
932             if (p[1]>'9') { p[1]|=0x20; p[1]-=39; }
933             seg2[j++] = ((p[0]-'0')<<4) + (p[1]-'0');
934             p+=2;
935           }
936           len2 = j;
937         }
938         make_xref(xref, xref->last+1, pdfbuf->len);
939         nobj_fontprog = xref->last;
940         g_string_append_printf(pdfbuf, 
941           "%d 0 obj\n<< /Length %u /Length1 %u /Length2 %u /Length3 0 >> stream\n",
942           nobj_fontprog, len1+len2, len1, len2);
943         g_string_append_len(pdfbuf, seg1, len1);
944         g_string_append_len(pdfbuf, seg2, len2);
945         g_string_append(pdfbuf, "endstream\nendobj\n");
946         if (!is_binary) g_free(seg2);
947       }
948       g_free(fontdata);
949     }
950     else fallback = TRUE;
951   }
952   
953   // next, the font descriptor
954   if (!fallback) {
955     make_xref(xref, xref->last+1, pdfbuf->len);
956     nobj_descr = xref->last;
957     g_string_append_printf(pdfbuf,
958       "%d 0 obj\n<< /Type /FontDescriptor /FontName /%s /Flags %d "
959       "/FontBBox [%d %d %d %d] /ItalicAngle 0 /Ascent %d "
960       "/Descent %d /CapHeight %d /StemV 100 /%s %d 0 R >> endobj\n",
961       nobj_descr, font->fontname, font->flags, 
962       font->xmin, font->ymin, font->xmax, font->ymax, 
963       font->ascender, -font->descender, font->ascender, 
964       font->is_truetype ? "FontFile2":"FontFile",
965       nobj_fontprog);
966   }
967   
968   // finally, the font itself
969   /* Note: in Type1 case, font->glyphmap maps charcodes to glyph no's
970      in TrueType case, encoding lists the used charcodes by index,
971                        glyphs   list the used glyph no's by index
972                        font->glyphmap maps charcodes to indices        */
973   xref->data[font->n_obj] = pdfbuf->len;
974   if (font->is_truetype) lastchar = encoding[font->num_glyphs_used];
975   else lastchar = font->num_glyphs_used;
976   if (fallback) {
977     font->is_truetype = FALSE;
978     g_free(font->fontname);
979     font->fontname = g_strdup("Helvetica");
980   }
981   prefix[0]=0;
982   if (font->is_truetype) {
983     num = font->glyph_page;
984     for (i=0; i<6; i++) { prefix[i] = 'A'+(num%26); num/=26; }
985     prefix[6]='+'; prefix[7]=0;
986   }
987   g_string_append_printf(pdfbuf,
988     "%d 0 obj\n<< /Type /Font /Subtype /%s /BaseFont /%s%s /Name /F%d ",
989     font->n_obj, font->is_truetype?"TrueType":"Type1",
990     prefix, font->fontname, font->n_obj);
991   if (!fallback) {
992     g_string_append_printf(pdfbuf,
993       "/FontDescriptor %d 0 R /FirstChar 0 /LastChar %d /Widths [",
994       nobj_descr, lastchar);
995     for (i=0; i<=lastchar; i++)
996       g_string_append_printf(pdfbuf, "%d ", font->advance[i]);
997     g_string_append(pdfbuf, "] ");
998   }
999   if (!font->is_truetype) { /* encoding */
1000     g_string_append(pdfbuf, "/Encoding << /Type /Encoding "
1001       "/BaseEncoding /MacRomanEncoding /Differences [1 ");
1002     for (i=1; i<=lastchar; i++) {
1003       g_string_append_printf(pdfbuf, "/%s ", font->glyphpsnames[i]);
1004       g_free(font->glyphpsnames[i]);
1005     }
1006     g_string_append(pdfbuf, "] >> ");
1007   }
1008   g_string_append(pdfbuf, ">> endobj\n");
1009 }
1010
1011 // draw a page's graphics
1012
1013 void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter, 
1014                   struct XrefTable *xref, GList **pdffonts)
1015 {
1016   GList *layerlist, *itemlist, *tmplist;
1017   struct Layer *l;
1018   struct Item *item;
1019   guint old_rgba, old_text_rgba;
1020   double old_thickness;
1021   double *pt;
1022   int i, j;
1023   PangoFontDescription *font_desc;
1024   PangoContext *context;
1025   PangoLayout *layout;
1026   PangoLayoutIter *iter;
1027   PangoRectangle logical_rect;
1028   PangoLayoutRun *run;
1029   PangoFcFont *fcfont;
1030   PangoFontMap *fontmap;
1031   FcPattern *pattern;
1032   int baseline, advance;
1033   int glyph_no, glyph_page, current_page;
1034   char *filename;
1035   char tmpstr[200];
1036   int font_id;
1037   FT_Face ftface;
1038   struct PdfFont *cur_font;
1039   gboolean in_string;
1040   
1041   old_rgba = old_text_rgba = 0x12345678;    // not any values we use, so we'll reset them
1042   old_thickness = 0.0;
1043   for (tmplist = *pdffonts; tmplist!=NULL; tmplist = tmplist->next) {
1044     cur_font = (struct PdfFont *)tmplist->data;
1045     cur_font->used_in_this_page = FALSE;
1046   }
1047
1048   for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1049     l = (struct Layer *)layerlist->data;
1050     for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1051       item = (struct Item *)itemlist->data;
1052       if (item->type == ITEM_STROKE) {
1053         if ((item->brush.color_rgba & ~0xff) != old_rgba)
1054           g_string_append_printf(str, "%.2f %.2f %.2f RG ",
1055             RGBA_RGB(item->brush.color_rgba));
1056         if (item->brush.thickness != old_thickness)
1057           g_string_append_printf(str, "%.2f w ", item->brush.thickness);
1058         if ((item->brush.color_rgba & 0xf0) != 0xf0) { // transparent
1059           g_string_append(str, "q /XoHi gs ");
1060           *use_hiliter = TRUE;
1061         }
1062         old_rgba = item->brush.color_rgba & ~0xff;
1063         old_thickness = item->brush.thickness;
1064         pt = item->path->coords;
1065         if (!item->brush.variable_width) {
1066           g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
1067           for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1068             g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
1069           g_string_append_printf(str,"S\n");
1070           old_thickness = item->brush.thickness;
1071         } else {
1072           for (i=0; i<item->path->num_points-1; i++, pt+=2)
1073             g_string_append_printf(str, "%.2f w %.2f %.2f m %.2f %.2f l S\n", 
1074                item->widths[i], pt[0], pt[1], pt[2], pt[3]);
1075           old_thickness = 0.0;
1076         }
1077         if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
1078           g_string_append(str, "Q ");
1079       }
1080       else if (item->type == ITEM_TEXT) {
1081         if ((item->brush.color_rgba & ~0xff) != old_text_rgba)
1082           g_string_append_printf(str, "%.2f %.2f %.2f rg ",
1083             RGBA_RGB(item->brush.color_rgba));
1084         old_text_rgba = item->brush.color_rgba & ~0xff;
1085         fontmap = pango_ft2_font_map_new();
1086         pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP (fontmap), 72, 72);
1087         context = pango_context_new();
1088         pango_context_set_font_map(context, fontmap);
1089         layout = pango_layout_new(context);
1090         g_object_unref(fontmap);
1091         g_object_unref(context);
1092         font_desc = pango_font_description_from_string(item->font_name);
1093         pango_font_description_set_absolute_size(font_desc,
1094           item->font_size*PANGO_SCALE);
1095         pango_layout_set_font_description(layout, font_desc);
1096         pango_font_description_free(font_desc);
1097         pango_layout_set_text(layout, item->text, -1);
1098         // this code inspired by the code in libgnomeprint
1099         iter = pango_layout_get_iter(layout);
1100         do {
1101           run = pango_layout_iter_get_run(iter);
1102           if (run==NULL) continue;
1103           pango_layout_iter_get_run_extents (iter, NULL, &logical_rect);
1104           baseline = pango_layout_iter_get_baseline (iter);
1105           if (!PANGO_IS_FC_FONT(run->item->analysis.font)) continue;
1106           fcfont = PANGO_FC_FONT(run->item->analysis.font);
1107           pattern = fcfont->font_pattern;
1108           if (FcPatternGetString(pattern, FC_FILE, 0, (unsigned char **)&filename) != FcResultMatch ||
1109               FcPatternGetInteger(pattern, FC_INDEX, 0, &font_id) != FcResultMatch)
1110                 continue;
1111           ftface = pango_fc_font_lock_face(fcfont);
1112           current_page = -1;
1113           cur_font = NULL;
1114           g_string_append_printf(str, "BT %.2f 0 0 %.2f %.2f %.2f Tm ",
1115             item->font_size, -item->font_size,
1116             item->bbox.left + (gdouble) logical_rect.x/PANGO_SCALE,
1117             item->bbox.top + (gdouble) baseline/PANGO_SCALE);
1118           in_string = FALSE;
1119           for (i=0; i<run->glyphs->num_glyphs; i++) {
1120             glyph_no = run->glyphs->glyphs[i].glyph;
1121             if (FT_IS_SFNT(ftface)) glyph_page = glyph_no/255;
1122             else glyph_page = 0;
1123             if (glyph_page != current_page) {
1124               cur_font = new_pdffont(xref, pdffonts, filename, font_id,
1125                  ftface, glyph_page);
1126               if (in_string) g_string_append(str, ") Tj ");
1127               in_string = FALSE;
1128               g_string_append_printf(str, "/F%d 1 Tf ", cur_font->n_obj);
1129             }
1130             current_page = glyph_page;
1131             FT_Load_Glyph(ftface, glyph_no, FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
1132             advance = (int)(ftface->glyph->metrics.horiAdvance * cur_font->ft2ps + 0.5);
1133             if (!in_string) g_string_append_c(str, '(');
1134             in_string = TRUE;
1135             if (cur_font->is_truetype) {
1136               if (glyph_no) glyph_no = (glyph_no%255)+1;
1137               cur_font->glyphmap[glyph_no] = glyph_no;
1138             }
1139             else {
1140               for (j=1; j<=cur_font->num_glyphs_used; j++)
1141                 if (cur_font->glyphmap[j] == glyph_no) break;
1142               if (j==256) j=0; // font is full, what do we do?
1143               if (j>cur_font->num_glyphs_used) {
1144                 cur_font->glyphmap[j] = glyph_no; 
1145                 cur_font->num_glyphs_used++;
1146                 if (FT_Get_Glyph_Name(ftface, glyph_no, tmpstr, 200) == FT_Err_Ok)
1147                   cur_font->glyphpsnames[j] = g_strdup(tmpstr);
1148                 else cur_font->glyphpsnames[j] = g_strdup(".notdef");
1149               }
1150               glyph_no = j;
1151             }
1152             cur_font->advance[glyph_no] = advance;
1153             if (glyph_no=='\\' || glyph_no == '(' || glyph_no == ')' || glyph_no == 10 || glyph_no == 13) 
1154               g_string_append_c(str, '\\');
1155             if (glyph_no==10) g_string_append_c(str,'n');
1156             else if (glyph_no==13) g_string_append_c(str,'r');
1157             else g_string_append_c(str, glyph_no);
1158           }
1159           if (in_string) g_string_append(str, ") Tj ");
1160           g_string_append(str, "ET ");
1161           pango_fc_font_unlock_face(fcfont);
1162         } while (pango_layout_iter_next_run(iter));
1163         pango_layout_iter_free(iter);
1164         g_object_unref(layout);
1165       }
1166     }
1167   }
1168 }
1169
1170 // main printing function
1171
1172 /* we use the following object numbers, starting with n_obj_catalog:
1173     0 the document catalog
1174     1 the page tree
1175     2 the GS for the hiliters
1176     3 ... the page objects
1177 */
1178
1179 gboolean print_to_pdf(char *filename)
1180 {
1181   FILE *f;
1182   GString *pdfbuf, *pgstrm, *zpgstrm, *tmpstr;
1183   int n_obj_catalog, n_obj_pages_offs, n_page, n_obj_bgpix, n_obj_prefix;
1184   int i, startxref;
1185   struct XrefTable xref;
1186   GList *pglist;
1187   struct Page *pg;
1188   gboolean annot, uses_pdf;
1189   gboolean use_hiliter;
1190   struct PdfInfo pdfinfo;
1191   struct PdfObj *obj;
1192   GList *pdffonts, *list;
1193   struct PdfFont *font;
1194   char *tmpbuf;
1195   
1196   f = fopen(filename, "wb");
1197   if (f == NULL) return FALSE;
1198   setlocale(LC_NUMERIC, "C");
1199   annot = FALSE;
1200   xref.data = NULL;
1201   uses_pdf = FALSE;
1202   pdffonts = NULL;
1203   for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
1204     pg = (struct Page *)pglist->data;
1205     if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
1206   }
1207   
1208   if (uses_pdf && bgpdf.status != STATUS_NOT_INIT && 
1209       bgpdf.file_contents!=NULL && !strncmp(bgpdf.file_contents, "%PDF-1.", 7)) {
1210     // parse the existing PDF file
1211     pdfbuf = g_string_new_len(bgpdf.file_contents, bgpdf.file_length);
1212     if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
1213     annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
1214     if (!annot) {
1215       g_string_free(pdfbuf, TRUE);
1216       if (xref.data != NULL) g_free(xref.data);
1217     }
1218   }
1219
1220   if (!annot) {
1221     pdfbuf = g_string_new("%PDF-1.4\n%\370\357\365\362\n");
1222     xref.n_alloc = xref.last = 0;
1223     xref.data = NULL;
1224   }
1225     
1226   // catalog and page tree
1227   n_obj_catalog = xref.last+1;
1228   n_obj_pages_offs = xref.last+4;
1229   make_xref(&xref, n_obj_catalog, pdfbuf->len);
1230   g_string_append_printf(pdfbuf, 
1231     "%d 0 obj\n<< /Type /Catalog /Pages %d 0 R >> endobj\n",
1232      n_obj_catalog, n_obj_catalog+1);
1233   make_xref(&xref, n_obj_catalog+1, pdfbuf->len);
1234   g_string_append_printf(pdfbuf,
1235     "%d 0 obj\n<< /Type /Pages /Kids [", n_obj_catalog+1);
1236   for (i=0;i<journal.npages;i++)
1237     g_string_append_printf(pdfbuf, "%d 0 R ", n_obj_pages_offs+i);
1238   g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
1239   make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
1240   g_string_append_printf(pdfbuf, 
1241     "%d 0 obj\n<< /Type /ExtGState /CA %.2f >> endobj\n",
1242      n_obj_catalog+2, ui.hiliter_opacity);
1243   xref.last = n_obj_pages_offs + journal.npages-1;
1244   
1245   for (pglist = journal.pages, n_page = 0; pglist!=NULL;
1246        pglist = pglist->next, n_page++) {
1247     pg = (struct Page *)pglist->data;
1248     
1249     // draw the background and page into pgstrm
1250     pgstrm = g_string_new("");
1251     g_string_printf(pgstrm, "q 1 0 0 -1 0 %.2f cm 1 J 1 j ", pg->height);
1252     n_obj_bgpix = -1;
1253     n_obj_prefix = -1;
1254     if (pg->bg->type == BG_SOLID)
1255       pdf_draw_solid_background(pg, pgstrm);
1256     else if (pg->bg->type == BG_PDF && annot && 
1257              pdfinfo.pages[pg->bg->file_page_seq-1].contents!=NULL) {
1258       make_xref(&xref, xref.last+1, pdfbuf->len);
1259       n_obj_prefix = xref.last;
1260       tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
1261                               pg->width, pg->height);
1262       g_string_append_printf(pdfbuf,
1263         "%d 0 obj\n<< /Length %zu >> stream\n%s\nendstream\nendobj\n",
1264         n_obj_prefix, tmpstr->len, tmpstr->str);
1265       g_string_free(tmpstr, TRUE);
1266       g_string_prepend(pgstrm, "Q Q Q ");
1267     }
1268     else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
1269       n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
1270     // draw the page contents
1271     use_hiliter = FALSE;
1272     pdf_draw_page(pg, pgstrm, &use_hiliter, &xref, &pdffonts);
1273     g_string_append_printf(pgstrm, "Q\n");
1274     
1275     // deflate pgstrm and write it
1276     zpgstrm = do_deflate(pgstrm->str, pgstrm->len);
1277     g_string_free(pgstrm, TRUE);
1278     
1279     make_xref(&xref, xref.last+1, pdfbuf->len);
1280     g_string_append_printf(pdfbuf, 
1281       "%d 0 obj\n<< /Length %zu /Filter /FlateDecode>> stream\n",
1282       xref.last, zpgstrm->len);
1283     g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
1284     g_string_free(zpgstrm, TRUE);
1285     g_string_append(pdfbuf, "endstream\nendobj\n");
1286     
1287     // write the page object
1288     
1289     make_xref(&xref, n_obj_pages_offs+n_page, pdfbuf->len);
1290     g_string_append_printf(pdfbuf, 
1291       "%d 0 obj\n<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %.2f %.2f] ",
1292       n_obj_pages_offs+n_page, n_obj_catalog+1, pg->width, pg->height);
1293     if (n_obj_prefix>0) {
1294       obj = get_pdfobj(pdfbuf, &xref, pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1295       if (obj->type != PDFTYPE_ARRAY) {
1296         free_pdfobj(obj);
1297         obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].contents);
1298       }
1299       g_string_append_printf(pdfbuf, "/Contents [%d 0 R ", n_obj_prefix);
1300       if (obj->type == PDFTYPE_REF) 
1301         g_string_append_printf(pdfbuf, "%d %d R ", obj->intval, obj->num);
1302       if (obj->type == PDFTYPE_ARRAY) {
1303         for (i=0; i<obj->num; i++) {
1304           show_pdfobj(obj->elts[i], pdfbuf);
1305           g_string_append_c(pdfbuf, ' ');
1306         }
1307       }
1308       free_pdfobj(obj);
1309       g_string_append_printf(pdfbuf, "%d 0 R] ", xref.last);
1310     }
1311     else g_string_append_printf(pdfbuf, "/Contents %d 0 R ", xref.last);
1312     g_string_append(pdfbuf, "/Resources ");
1313
1314     if (n_obj_prefix>0)
1315       obj = dup_pdfobj(pdfinfo.pages[pg->bg->file_page_seq-1].resources);
1316     else obj = NULL;
1317     if (obj!=NULL && obj->type!=PDFTYPE_DICT)
1318       { free_pdfobj(obj); obj=NULL; }
1319     if (obj==NULL) {
1320       obj = g_malloc(sizeof(struct PdfObj));
1321       obj->type = PDFTYPE_DICT;
1322       obj->num = 0;
1323       obj->elts = NULL;
1324       obj->names = NULL;
1325     }
1326     add_dict_subentry(pdfbuf, &xref,
1327         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
1328     if (n_obj_bgpix>0)
1329       add_dict_subentry(pdfbuf, &xref,
1330         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
1331     if (use_hiliter)
1332       add_dict_subentry(pdfbuf, &xref,
1333         obj, "/ExtGState", PDFTYPE_DICT, "/XoHi", mk_pdfref(n_obj_catalog+2));
1334     if (n_obj_bgpix>0)
1335       add_dict_subentry(pdfbuf, &xref,
1336         obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
1337     for (list=pdffonts; list!=NULL; list = list->next) {
1338       font = (struct PdfFont *)list->data;
1339       if (font->used_in_this_page) {
1340         add_dict_subentry(pdfbuf, &xref,
1341           obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/Text"));
1342         tmpbuf = g_strdup_printf("/F%d", font->n_obj);
1343         add_dict_subentry(pdfbuf, &xref,
1344           obj, "/Font", PDFTYPE_DICT, tmpbuf, mk_pdfref(font->n_obj));
1345         g_free(tmpbuf);
1346       }
1347     }
1348     show_pdfobj(obj, pdfbuf);
1349     free_pdfobj(obj);
1350     g_string_append(pdfbuf, " >> endobj\n");
1351   }
1352   
1353   // after the pages, we insert fonts
1354   for (list = pdffonts; list!=NULL; list = list->next) {
1355     font = (struct PdfFont *)list->data;
1356     embed_pdffont(pdfbuf, &xref, font);
1357     g_free(font->filename);
1358     g_free(font->fontname);
1359     g_free(font);
1360   }
1361   g_list_free(pdffonts);
1362   
1363   // PDF trailer
1364   startxref = pdfbuf->len;
1365   if (annot) g_string_append_printf(pdfbuf,
1366         "xref\n%d %d\n", n_obj_catalog, xref.last-n_obj_catalog+1);
1367   else g_string_append_printf(pdfbuf, 
1368         "xref\n0 %d\n0000000000 65535 f \n", xref.last+1);
1369   for (i=n_obj_catalog; i<=xref.last; i++)
1370     g_string_append_printf(pdfbuf, "%010d 00000 n \n", xref.data[i]);
1371   g_string_append_printf(pdfbuf, 
1372     "trailer\n<< /Size %d /Root %d 0 R ", xref.last+1, n_obj_catalog);
1373   if (annot) {
1374     g_string_append_printf(pdfbuf, "/Prev %d ", pdfinfo.startxref);
1375     // keeping encryption info somehow doesn't work.
1376     // xournal can't annotate encrypted PDFs anyway...
1377 /*    
1378     obj = get_dict_entry(pdfinfo.trailerdict, "/Encrypt");
1379     if (obj!=NULL) {
1380       g_string_append_printf(pdfbuf, "/Encrypt ");
1381       show_pdfobj(obj, pdfbuf);
1382     } 
1383 */
1384   }
1385   g_string_append_printf(pdfbuf, 
1386     ">>\nstartxref\n%d\n%%%%EOF\n", startxref);
1387   
1388   g_free(xref.data);
1389   if (annot) {
1390     free_pdfobj(pdfinfo.trailerdict);
1391     if (pdfinfo.pages!=NULL)
1392       for (i=0; i<pdfinfo.npages; i++) {
1393         free_pdfobj(pdfinfo.pages[i].resources);
1394         free_pdfobj(pdfinfo.pages[i].mediabox);
1395         free_pdfobj(pdfinfo.pages[i].contents);
1396       }
1397   }
1398   
1399   setlocale(LC_NUMERIC, "");
1400   if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
1401     fclose(f);
1402     g_string_free(pdfbuf, TRUE);
1403     return FALSE;
1404   }
1405   fclose(f);
1406   g_string_free(pdfbuf, TRUE);
1407   return TRUE;
1408 }
1409
1410 /*********** Printing via gtk-print **********/
1411
1412 #if GTK_CHECK_VERSION(2, 10, 0)
1413
1414 // does the same job as update_canvas_bg(), but to a print context
1415
1416 void print_background(cairo_t *cr, struct Page *pg)
1417 {
1418   double x, y;
1419   GdkPixbuf *pix;
1420   BgPdfPage *pgpdf;
1421   PopplerPage *pdfpage;
1422   cairo_surface_t *cr_pixbuf;
1423   double pgwidth, pgheight;
1424
1425   if (pg->bg->type == BG_SOLID) {
1426     cairo_set_source_rgb(cr, RGBA_RGB(pg->bg->color_rgba));
1427     cairo_rectangle(cr, 0, 0, pg->width, pg->height);
1428     cairo_fill(cr);
1429     if (!ui.print_ruling) return;
1430     if (pg->bg->ruling == RULING_NONE) return;
1431     cairo_set_source_rgb(cr, RGBA_RGB(RULING_COLOR));
1432     cairo_set_line_width(cr, RULING_THICKNESS);
1433
1434     if (pg->bg->ruling == RULING_GRAPH) {
1435       for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
1436         { cairo_move_to(cr, x, 0); cairo_line_to(cr, x, pg->height); }
1437       for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
1438         { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
1439       cairo_stroke(cr);
1440       return;
1441     }
1442     
1443     for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
1444       { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
1445     cairo_stroke(cr);
1446     if (pg->bg->ruling == RULING_LINED) {
1447       cairo_set_source_rgb(cr, RGBA_RGB(RULING_MARGIN_COLOR));
1448       cairo_move_to(cr, RULING_LEFTMARGIN, 0);
1449       cairo_line_to(cr, RULING_LEFTMARGIN, pg->height);
1450       cairo_stroke(cr);
1451     }
1452     return;
1453   }
1454   else
1455   if (pg->bg->type == BG_PDF) {
1456     if (!bgpdf.document) return;
1457     pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
1458     if (!pdfpage) return;
1459     poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
1460     cairo_save(cr);
1461     cairo_scale(cr, pg->width/pgwidth, pg->height/pgheight);
1462     poppler_page_render(pdfpage, cr);
1463     cairo_restore(cr);
1464     g_object_unref(pdfpage);
1465   }
1466   else 
1467   if (pg->bg->type == BG_PIXMAP) {
1468     cairo_save(cr);
1469     cairo_scale(cr, pg->width/gdk_pixbuf_get_width(pg->bg->pixbuf),
1470                     pg->height/gdk_pixbuf_get_height(pg->bg->pixbuf));
1471     gdk_cairo_set_source_pixbuf(cr, pg->bg->pixbuf, 0, 0);
1472     cairo_rectangle(cr, 0, 0, gdk_pixbuf_get_width(pg->bg->pixbuf), gdk_pixbuf_get_height(pg->bg->pixbuf));
1473     cairo_fill(cr);
1474     cairo_restore(cr);
1475   }
1476 }
1477
1478 void print_job_render_page(GtkPrintOperation *print, GtkPrintContext *context, gint pageno, gpointer user_data)
1479 {
1480   cairo_t *cr;
1481   gdouble width, height, scale;
1482   struct Page *pg;
1483   guint old_rgba;
1484   double old_thickness;
1485   GList *layerlist, *itemlist;
1486   struct Layer *l;
1487   struct Item *item;
1488   int i;
1489   double *pt;
1490   PangoFontDescription *font_desc;
1491   PangoLayout *layout;
1492         
1493   pg = (struct Page *)g_list_nth_data(journal.pages, pageno);
1494   cr = gtk_print_context_get_cairo_context(context);
1495   width = gtk_print_context_get_width(context);
1496   height = gtk_print_context_get_height(context);
1497   scale = MIN(width/pg->width, height/pg->height);
1498   
1499   cairo_translate(cr, (width-scale*pg->width)/2, (height-scale*pg->height)/2);
1500   cairo_scale(cr, scale, scale);
1501   cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
1502   cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
1503   
1504   print_background(cr, pg);
1505
1506   old_rgba = predef_colors_rgba[COLOR_BLACK];
1507   cairo_set_source_rgb(cr, 0, 0, 0);
1508   old_thickness = 0.0;
1509
1510   for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
1511     l = (struct Layer *)layerlist->data;
1512     for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
1513       item = (struct Item *)itemlist->data;
1514       if (item->type == ITEM_STROKE || item->type == ITEM_TEXT) {
1515         if (item->brush.color_rgba != old_rgba)
1516           cairo_set_source_rgba(cr, RGBA_RGB(item->brush.color_rgba),
1517                                     RGBA_ALPHA(item->brush.color_rgba));
1518         old_rgba = item->brush.color_rgba;
1519       }
1520       if (item->type == ITEM_STROKE) {    
1521         if (item->brush.thickness != old_thickness)
1522           cairo_set_line_width(cr, item->brush.thickness);
1523         pt = item->path->coords;
1524         if (!item->brush.variable_width) {
1525           cairo_move_to(cr, pt[0], pt[1]);
1526           for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
1527             cairo_line_to(cr, pt[0], pt[1]);
1528           cairo_stroke(cr);
1529           old_thickness = item->brush.thickness;
1530         } else {
1531           for (i=0; i<item->path->num_points-1; i++, pt+=2) {
1532             cairo_move_to(cr, pt[0], pt[1]);
1533             cairo_set_line_width(cr, item->widths[i]);
1534             cairo_line_to(cr, pt[2], pt[3]);
1535             cairo_stroke(cr);
1536           }
1537           old_thickness = 0.0;
1538         }
1539       }
1540       if (item->type == ITEM_TEXT) {
1541         layout = gtk_print_context_create_pango_layout(context);
1542         font_desc = pango_font_description_from_string(item->font_name);
1543         if (item->font_size)
1544           pango_font_description_set_absolute_size(font_desc,
1545             item->font_size*PANGO_SCALE);
1546         pango_layout_set_font_description(layout, font_desc);
1547         pango_font_description_free(font_desc);
1548         pango_layout_set_text(layout, item->text, -1);
1549         cairo_move_to(cr, item->bbox.left, item->bbox.top);
1550         pango_cairo_show_layout(cr, layout);
1551         g_object_unref(layout);
1552       }
1553     }
1554   }
1555 }
1556
1557 #endif