]> git.donarmstrong.com Git - xournal.git/blobdiff - src/xo-print.c
Image patch
[xournal.git] / src / xo-print.c
index 04493c9c010d59f3c4e79ae72173ec3154f557dd..029500ecba345d808c4ce4901227b185294dc32e 100644 (file)
@@ -1,14 +1,43 @@
+/*
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This software is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of  
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
+#define PANGO_ENABLE_BACKEND /* to access PangoFcFont.font_pattern */
+
 #include <gtk/gtk.h>
 #include <libgnomecanvas/libgnomecanvas.h>
-#include <libgnomeprint/gnome-print-job.h>
 #include <zlib.h>
 #include <string.h>
+#include <locale.h>
+#include <pango/pango.h>
+#include <pango/pangofc-font.h>
+#include <pango/pangoft2.h>
+#include <fontconfig/fontconfig.h>
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+#define NO_MAPPERS
+#define NO_TYPE3
+#define NO_TYPE42
+#include "ttsubset/sft.h"
 
 #include "xournal.h"
+#include "xo-support.h"
 #include "xo-misc.h"
 #include "xo-paint.h"
 #include "xo-print.h"
@@ -182,7 +211,7 @@ struct PdfObj *parse_pdf_object(char **ptr, char *eof)
   if (q!=p) {
     if (*q == '.') {
       obj->type = PDFTYPE_REAL;
-      obj->realval = strtod(p, ptr);
+      obj->realval = g_ascii_strtod(p, ptr);
       return obj;
     }
     if (ispdfspace(*q)) {
@@ -332,7 +361,7 @@ struct PdfObj *get_pdfobj(GString *pdfbuf, struct XrefTable *xref, struct PdfObj
 
 struct PdfObj *parse_xref_table(GString *pdfbuf, struct XrefTable *xref, int offs)
 {
-  char *p, *q, *eof;
+  char *p, *eof;
   struct PdfObj *trailerdict, *obj;
   int start, len, i;
   
@@ -450,7 +479,7 @@ int pdf_getpageinfo(GString *pdfbuf, struct XrefTable *xref,
 gboolean pdf_parse_info(GString *pdfbuf, struct PdfInfo *pdfinfo, struct XrefTable *xref)
 {
   char *p;
-  int i, offs;
+  int offs;
   struct PdfObj *obj, *pages;
 
   xref->n_alloc = xref->last = 0;
@@ -530,8 +559,9 @@ GString *make_pdfprefix(struct PdfPageDesc *pgdesc, double width, double height)
   GString *str;
   double v[4], t, xscl, yscl;
   int i;
-  
-  str = g_string_new("q ");
+
+  // push 3 times in case code to be annotated has unbalanced q/Q (B of A., ...)
+  str = g_string_new("q q q "); 
   if (pgdesc->rotate == 90) {
     g_string_append_printf(str, "0 -1 1 0 0 %.2f cm ", height);
     t = height; height = width; width = t;
@@ -670,6 +700,7 @@ void pdf_draw_solid_background(struct Page *pg, GString *str)
   g_string_append_printf(str, 
     "%.2f %.2f %.2f rg 0 0 %.2f %.2f re f ",
     RGBA_RGB(pg->bg->color_rgba), pg->width, pg->height);
+  if (!ui.print_ruling) return;
   if (pg->bg->ruling == RULING_NONE) return;
   g_string_append_printf(str,
     "%.2f %.2f %.2f RG %.2f w ",
@@ -699,29 +730,34 @@ int pdf_draw_bitmap_background(struct Page *pg, GString *str,
   BgPdfPage *pgpdf;
   GdkPixbuf *pix;
   GString *zpix;
+  PopplerPage *pdfpage;
   char *buf, *p1, *p2;
   int height, width, stride, x, y, chan;
+  double pgheight, pgwidth;
   
   if (pg->bg->type == BG_PDF) {
-    pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
-    if (pgpdf == NULL) return -1;
-    if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
-      add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
-      while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING)
-        gtk_main_iteration();
-    }
-    pix = pgpdf->pixbuf;
+    if (!bgpdf.document) return -1;
+    pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
+    if (!pdfpage) return -1;
+    poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
+    width = (int) (PDFTOPPM_PRINTING_DPI * pgwidth/72.0);
+    height = (int) (PDFTOPPM_PRINTING_DPI * pgheight/72.0);
+    pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
+    wrapper_poppler_page_render_to_pixbuf(
+       pdfpage, 0, 0, width, height, PDFTOPPM_PRINTING_DPI/72.0, 0, pix);
+    g_object_unref(pdfpage);
   }
-  else pix = pg->bg->pixbuf;
-  
-  if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return -1;
-  if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return -1;
+  else pix = g_object_ref(pg->bg->pixbuf);
   
+  if (gdk_pixbuf_get_bits_per_sample(pix) != 8 ||
+      gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB)
+    { g_object_unref(pix); return -1; }
+                    
   width = gdk_pixbuf_get_width(pix);
   height = gdk_pixbuf_get_height(pix);
   stride = gdk_pixbuf_get_rowstride(pix);
   chan = gdk_pixbuf_get_n_channels(pix);
-  if (chan!=3 && chan!=4) return -1;
+  if (chan!=3 && chan!=4) { g_object_unref(pix); return -1; }
 
   g_string_append_printf(str, "q %.2f 0 0 %.2f 0 %.2f cm /ImBg Do Q ",
     pg->width, -pg->height, pg->height);
@@ -736,10 +772,11 @@ int pdf_draw_bitmap_background(struct Page *pg, GString *str,
   }
   zpix = do_deflate(buf, 3*width*height);
   g_free(buf);
+  g_object_unref(pix);
 
   make_xref(xref, xref->last+1, pdfbuf->len);
   g_string_append_printf(pdfbuf, 
-    "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
+    "%d 0 obj\n<< /Length %zu /Filter /FlateDecode /Type /Xobject "
     "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
     "/BitsPerComponent 8 >> stream\n",
     xref->last, zpix->len, width, height);
@@ -750,20 +787,379 @@ int pdf_draw_bitmap_background(struct Page *pg, GString *str,
   return xref->last;
 }
 
+gboolean pdf_draw_image(PdfImage *image, struct XrefTable *xref, GString *pdfbuf)
+{
+  char *buf, *p1, *p2;
+  int height, width, stride, x, y, chan;
+  GString *zpix;
+
+  if (gdk_pixbuf_get_bits_per_sample(image->pixbuf) != 8 ||
+      gdk_pixbuf_get_colorspace(image->pixbuf) != GDK_COLORSPACE_RGB) {
+    return FALSE;
+  }
+
+  width = gdk_pixbuf_get_width(image->pixbuf);
+  height = gdk_pixbuf_get_height(image->pixbuf);
+  stride = gdk_pixbuf_get_rowstride(image->pixbuf);
+  chan = gdk_pixbuf_get_n_channels(image->pixbuf);
+  if (!((chan==3 && !image->has_alpha) || (chan==4 && image->has_alpha))) {
+    return FALSE;
+  }
+
+  p2 = buf = (char *)g_malloc(3*width*height);
+  for (y=0; y<height; y++) {
+    p1 = (char *)gdk_pixbuf_get_pixels(image->pixbuf)+stride*y;
+    for (x=0; x<width; x++) {
+      *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
+      if (chan==4) p1++;
+    }
+  }
+  zpix = do_deflate(buf, 3*width*height);
+  g_free(buf);
+
+  xref->data[image->n_obj] = pdfbuf->len;
+  g_string_append_printf(pdfbuf, 
+    "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
+    "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
+    "/BitsPerComponent 8 ",
+    image->n_obj, zpix->len, width, height);
+  if (image->has_alpha) {
+    g_string_append_printf(pdfbuf, 
+      "/SMask %d 0 R ",
+      image->n_obj_smask);
+  }
+  g_string_append_printf(pdfbuf, " >> stream\n");
+
+  g_string_append_len(pdfbuf, zpix->str, zpix->len);
+  g_string_free(zpix, TRUE);
+  g_string_append(pdfbuf, "endstream\nendobj\n");
+
+  if (image->has_alpha) {
+    p2 = buf = (char *)g_malloc(width*height);
+    for (y=0; y<height; y++) {
+      p1 = (char *)gdk_pixbuf_get_pixels(image->pixbuf)+stride*y;
+      for (x=0; x<width; x++) {
+        p1+=3;                  /* skip the RGB */
+        *(p2++)=*(p1++);        /* just copy the alpha */
+      }
+    }
+    zpix = do_deflate(buf, width*height);
+    g_free(buf);
+    
+    xref->data[image->n_obj_smask] = pdfbuf->len;
+    g_string_append_printf(pdfbuf, 
+      "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
+      "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceGray "
+      "/BitsPerComponent 8 >> stream\n",
+      image->n_obj_smask, zpix->len, width, height);
+
+    g_string_append_len(pdfbuf, zpix->str, zpix->len);
+    g_string_free(zpix, TRUE);
+    g_string_append(pdfbuf, "endstream\nendobj\n");
+  }
+
+  return TRUE;
+}
+
+
+// manipulate Pdf fonts
+
+struct PdfFont *new_pdffont(struct XrefTable *xref, GList **fonts,
+   char *filename, int font_id, FT_Face face, int glyph_page)
+{
+  GList *list;
+  struct PdfFont *font;
+  int i;
+  const char *s;
+  
+  for (list = *fonts; list!=NULL; list = list->next) {
+    font = (struct PdfFont *)list->data;
+    if (!strcmp(font->filename, filename) && font->font_id == font_id 
+        && font->glyph_page == glyph_page)
+          { font->used_in_this_page = TRUE; return font; }
+  }
+  font = g_malloc(sizeof(struct PdfFont));
+  *fonts = g_list_append(*fonts, font);
+  font->n_obj = xref->last+1;
+  make_xref(xref, xref->last+1, 0); // will give it a value later
+  font->filename = g_strdup(filename);
+  font->font_id = font_id;
+  font->glyph_page = glyph_page;
+  font->used_in_this_page = TRUE;
+  font->num_glyphs_used = 0;
+  for (i=0; i<256; i++) {
+    font->glyphmap[i] = -1;
+    font->advance[i] = 0;
+    font->glyphpsnames[i] = NULL;
+  }
+  font->glyphmap[0] = 0;
+  // fill in info from the FT_Face
+  font->is_truetype = FT_IS_SFNT(face);
+  font->nglyphs = face->num_glyphs;
+  font->ft2ps = 1000.0 / face->units_per_EM;
+  font->ascender = (int)(font->ft2ps*face->ascender);
+  font->descender = (int)(font->ft2ps*face->descender);
+  if (face->bbox.xMin < -100000 || face->bbox.xMin > 100000) font->xmin = 0;
+  else font->xmin = (int)(font->ft2ps*face->bbox.xMin);
+  if (face->bbox.xMax < -100000 || face->bbox.xMax > 100000) font->xmax = 0;
+  else font->xmax = (int)(font->ft2ps*face->bbox.xMax);
+  if (face->bbox.yMin < -100000 || face->bbox.yMin > 100000) font->ymin = 0;
+  else font->ymin = (int)(font->ft2ps*face->bbox.yMin);
+  if (face->bbox.yMax < -100000 || face->bbox.yMax > 100000) font->ymax = 0;
+  else font->ymax = (int)(font->ft2ps*face->bbox.yMax);
+  if (font->is_truetype) font->flags = 4; // symbolic
+  else {
+    font->flags = 4; // symbolic
+    if (FT_IS_FIXED_WIDTH(face)) font->flags |= 1;
+    if (face->style_flags & FT_STYLE_FLAG_ITALIC) font->flags |= 64;
+  }
+  s = FT_Get_Postscript_Name(face);
+  if (s==NULL) s = "Noname";
+  if (glyph_page) font->fontname = g_strdup_printf("%s_%03d", s, glyph_page);
+  else font->fontname = g_strdup(s);
+  return font;
+}
+
+#define pfb_get_length(x) (((x)[3]<<24) + ((x)[2]<<16) + ((x)[1]<<8) + (x)[0])
+#define T1_SEGMENT_1_END "currentfile eexec"
+#define T1_SEGMENT_3_END "cleartomark"
+
+void embed_pdffont(GString *pdfbuf, struct XrefTable *xref, struct PdfFont *font)
+{
+  // this code inspired by libgnomeprint
+  gboolean fallback, is_binary;
+  guchar encoding[256];
+  gushort glyphs[256];
+  int i, j, num;
+  guint32 len1, len2;
+  guint32 tt_len;
+  gsize t1_len;
+  TrueTypeFont *ttfnt;
+  char *seg1, *seg2;
+  char *fontdata, *p;
+  char prefix[8];
+  int nobj_fontprog, nobj_descr, lastchar;
+  
+  fallback = FALSE;
+  // embed the font file: TrueType case
+  if (font->is_truetype) {
+    glyphs[0] = encoding[0] = 0;
+    num = 1;
+    for (i=1; i<=255; i++) 
+      if (font->glyphmap[i]>=0) {
+        font->glyphmap[i] = num;
+        glyphs[num] = 255*font->glyph_page+i-1;
+        encoding[num] = i;
+        num++;
+      }
+    font->num_glyphs_used = num-1;
+    if (OpenTTFont(font->filename, 0, &ttfnt) == SF_OK) {
+      if (CreateTTFromTTGlyphs_tomemory(ttfnt, (guint8**)&fontdata, &tt_len, glyphs, encoding, num, 
+                   0, NULL, TTCF_AutoName | TTCF_IncludeOS2) == SF_OK) {
+        make_xref(xref, xref->last+1, pdfbuf->len);
+        nobj_fontprog = xref->last;
+        g_string_append_printf(pdfbuf, 
+          "%d 0 obj\n<< /Length %u /Length1 %u >> stream\n",
+          nobj_fontprog, tt_len, tt_len);
+        g_string_append_len(pdfbuf, fontdata, tt_len);
+        g_string_append(pdfbuf, "endstream\nendobj\n");
+        g_free(fontdata);
+      }
+      else fallback = TRUE;
+      CloseTTFont(ttfnt);
+    } 
+    else fallback = TRUE;
+  } else {
+  // embed the font file: Type1 case
+    if (g_file_get_contents(font->filename, &fontdata, &t1_len, NULL) && t1_len>=8) {
+      if (fontdata[0]==(char)0x80 && fontdata[1]==(char)0x01) {
+        is_binary = TRUE;
+        len1 = pfb_get_length((unsigned char *)fontdata+2);
+        if (fontdata[len1+6]!=(char)0x80 || fontdata[len1+7]!=(char)0x02) fallback = TRUE;
+        else {
+          len2 = pfb_get_length((unsigned char *)fontdata+len1+8);
+          if (fontdata[len1+len2+12]!=(char)0x80 || fontdata[len1+len2+13]!=(char)0x01)
+            fallback = TRUE;
+        }
+      }
+      else if (!strncmp(fontdata, "%!PS", 4)) {
+        is_binary = FALSE;
+        p = strstr(fontdata, T1_SEGMENT_1_END) + strlen(T1_SEGMENT_1_END);
+        if (p==NULL) fallback = TRUE;
+        else {
+          if (*p=='\n' || *p=='\r') p++;
+          if (*p=='\n' || *p=='\r') p++;
+          len1 = p-fontdata;
+          p = g_strrstr_len(fontdata, t1_len, T1_SEGMENT_3_END);
+          if (p==NULL) fallback = TRUE;
+          else {
+            // rewind 512 zeros
+            i = 512; p--;
+            while (i>0 && p!=fontdata && (*p=='0' || *p=='\r' || *p=='\n')) {
+              if (*p=='0') i--;
+              p--;
+            }
+            while (p!=fontdata && (*p=='\r' || *p=='\n')) p--;
+            p++;
+            if (i>0) fallback = TRUE;
+            else len2 = p-fontdata-len1;
+          }
+        }
+      }
+      else fallback = TRUE;
+      if (!fallback) {
+        if (is_binary) {
+          seg1 = fontdata+6;
+          seg2 = fontdata + len1 + 12;
+        } else {
+          seg1 = fontdata;
+          seg2 = g_malloc(len2/2);
+          j=0;
+          p = fontdata+len1;
+          while (p+1 < fontdata+len1+len2) {
+            if (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') { p++; continue; }
+            if (p[0]>'9') { p[0]|=0x20; p[0]-=39; }
+            if (p[1]>'9') { p[1]|=0x20; p[1]-=39; }
+            seg2[j++] = ((p[0]-'0')<<4) + (p[1]-'0');
+            p+=2;
+          }
+          len2 = j;
+        }
+        make_xref(xref, xref->last+1, pdfbuf->len);
+        nobj_fontprog = xref->last;
+        g_string_append_printf(pdfbuf, 
+          "%d 0 obj\n<< /Length %u /Length1 %u /Length2 %u /Length3 0 >> stream\n",
+          nobj_fontprog, len1+len2, len1, len2);
+        g_string_append_len(pdfbuf, seg1, len1);
+        g_string_append_len(pdfbuf, seg2, len2);
+        g_string_append(pdfbuf, "endstream\nendobj\n");
+        if (!is_binary) g_free(seg2);
+      }
+      g_free(fontdata);
+    }
+    else fallback = TRUE;
+  }
+  
+  // next, the font descriptor
+  if (!fallback) {
+    make_xref(xref, xref->last+1, pdfbuf->len);
+    nobj_descr = xref->last;
+    g_string_append_printf(pdfbuf,
+      "%d 0 obj\n<< /Type /FontDescriptor /FontName /%s /Flags %d "
+      "/FontBBox [%d %d %d %d] /ItalicAngle 0 /Ascent %d "
+      "/Descent %d /CapHeight %d /StemV 100 /%s %d 0 R >> endobj\n",
+      nobj_descr, font->fontname, font->flags, 
+      font->xmin, font->ymin, font->xmax, font->ymax, 
+      font->ascender, -font->descender, font->ascender, 
+      font->is_truetype ? "FontFile2":"FontFile",
+      nobj_fontprog);
+  }
+  
+  // finally, the font itself
+  /* Note: in Type1 case, font->glyphmap maps charcodes to glyph no's
+     in TrueType case, encoding lists the used charcodes by index,
+                       glyphs   list the used glyph no's by index
+                       font->glyphmap maps charcodes to indices        */
+  xref->data[font->n_obj] = pdfbuf->len;
+  if (font->is_truetype) lastchar = encoding[font->num_glyphs_used];
+  else lastchar = font->num_glyphs_used;
+  if (fallback) {
+    font->is_truetype = FALSE;
+    g_free(font->fontname);
+    font->fontname = g_strdup("Helvetica");
+  }
+  prefix[0]=0;
+  if (font->is_truetype) {
+    num = font->glyph_page;
+    for (i=0; i<6; i++) { prefix[i] = 'A'+(num%26); num/=26; }
+    prefix[6]='+'; prefix[7]=0;
+  }
+  g_string_append_printf(pdfbuf,
+    "%d 0 obj\n<< /Type /Font /Subtype /%s /BaseFont /%s%s /Name /F%d ",
+    font->n_obj, font->is_truetype?"TrueType":"Type1",
+    prefix, font->fontname, font->n_obj);
+  if (!fallback) {
+    g_string_append_printf(pdfbuf,
+      "/FontDescriptor %d 0 R /FirstChar 0 /LastChar %d /Widths [",
+      nobj_descr, lastchar);
+    for (i=0; i<=lastchar; i++)
+      g_string_append_printf(pdfbuf, "%d ", font->advance[i]);
+    g_string_append(pdfbuf, "] ");
+  }
+  if (!font->is_truetype) { /* encoding */
+    g_string_append(pdfbuf, "/Encoding << /Type /Encoding "
+      "/BaseEncoding /MacRomanEncoding /Differences [1 ");
+    for (i=1; i<=lastchar; i++) {
+      g_string_append_printf(pdfbuf, "/%s ", font->glyphpsnames[i]);
+      g_free(font->glyphpsnames[i]);
+    }
+    g_string_append(pdfbuf, "] >> ");
+  }
+  g_string_append(pdfbuf, ">> endobj\n");
+}
+
+// Pdf images
+
+struct PdfImage *new_pdfimage(struct XrefTable *xref, GList **images, GdkPixbuf *pixbuf)
+{
+  GList *list;
+  struct PdfImage *image;
+  
+  image = g_malloc(sizeof(struct PdfImage));
+  *images = g_list_append(*images, image);
+  image->n_obj = xref->last+1;
+  make_xref(xref, xref->last+1, 0); // will give it a value later
+  image->has_alpha = gdk_pixbuf_get_has_alpha(pixbuf);
+  if (image->has_alpha) {
+    image->n_obj_smask = xref->last+1;
+    make_xref(xref, xref->last+1, 0); // will give it a value later
+  }
+  image->pixbuf = pixbuf;
+
+  return image;
+}
+
 // draw a page's graphics
 
-void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter)
+void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter, 
+                   struct XrefTable *xref, GList **pdffonts, GList **pdfimages)
 {
-  GList *layerlist, *itemlist;
+  GList *layerlist, *itemlist, *tmplist;
   struct Layer *l;
   struct Item *item;
-  guint old_rgba;
+  guint old_rgba, old_text_rgba;
   double old_thickness;
   double *pt;
-  int i;
+  int i, j;
+  PangoFontDescription *font_desc;
+  PangoContext *context;
+  PangoLayout *layout;
+  PangoLayoutIter *iter;
+  PangoRectangle logical_rect;
+  PangoLayoutRun *run;
+  PangoFcFont *fcfont;
+  PangoFontMap *fontmap;
+  FcPattern *pattern;
+  int baseline, advance;
+  int glyph_no, glyph_page, current_page;
+  char *filename;
+  char tmpstr[200];
+  int font_id;
+  FT_Face ftface;
+  struct PdfFont *cur_font;
+  struct PdfImage *cur_image;
+  gboolean in_string;
   
-  old_rgba = 0x12345678;    // not any values we use, so we'll reset them
+  old_rgba = old_text_rgba = 0x12345678;    // not any values we use, so we'll reset them
   old_thickness = 0.0;
+  for (tmplist = *pdffonts; tmplist!=NULL; tmplist = tmplist->next) {
+    cur_font = (struct PdfFont *)tmplist->data;
+    cur_font->used_in_this_page = FALSE;
+  }
+  for (tmplist = *pdfimages; tmplist!=NULL; tmplist = tmplist->next) {
+    cur_image = (struct PdfImage *)tmplist->data;
+    cur_image->used_in_this_page = FALSE;
+  }
 
   for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
     l = (struct Layer *)layerlist->data;
@@ -782,13 +1178,115 @@ void pdf_draw_page(struct Page *pg, GString *str, gboolean *use_hiliter)
         old_rgba = item->brush.color_rgba & ~0xff;
         old_thickness = item->brush.thickness;
         pt = item->path->coords;
-        g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
-        for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
-          g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
-        g_string_append_printf(str,"S\n");
+        if (!item->brush.variable_width) {
+          g_string_append_printf(str, "%.2f %.2f m ", pt[0], pt[1]);
+          for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
+            g_string_append_printf(str, "%.2f %.2f l ", pt[0], pt[1]);
+          g_string_append_printf(str,"S\n");
+          old_thickness = item->brush.thickness;
+        } else {
+          for (i=0; i<item->path->num_points-1; i++, pt+=2)
+            g_string_append_printf(str, "%.2f w %.2f %.2f m %.2f %.2f l S\n", 
+               item->widths[i], pt[0], pt[1], pt[2], pt[3]);
+          old_thickness = 0.0;
+        }
         if ((item->brush.color_rgba & 0xf0) != 0xf0) // undo transparent
           g_string_append(str, "Q ");
       }
+      else if (item->type == ITEM_TEXT) {
+        if ((item->brush.color_rgba & ~0xff) != old_text_rgba)
+          g_string_append_printf(str, "%.2f %.2f %.2f rg ",
+            RGBA_RGB(item->brush.color_rgba));
+        old_text_rgba = item->brush.color_rgba & ~0xff;
+        fontmap = pango_ft2_font_map_new();
+        pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP (fontmap), 72, 72);
+        context = pango_context_new();
+        pango_context_set_font_map(context, fontmap);
+        layout = pango_layout_new(context);
+        g_object_unref(fontmap);
+        g_object_unref(context);
+        font_desc = pango_font_description_from_string(item->font_name);
+        pango_font_description_set_absolute_size(font_desc,
+          item->font_size*PANGO_SCALE);
+        pango_layout_set_font_description(layout, font_desc);
+        pango_font_description_free(font_desc);
+        pango_layout_set_text(layout, item->text, -1);
+        // this code inspired by the code in libgnomeprint
+        iter = pango_layout_get_iter(layout);
+        do {
+          run = pango_layout_iter_get_run(iter);
+          if (run==NULL) continue;
+          pango_layout_iter_get_run_extents (iter, NULL, &logical_rect);
+          baseline = pango_layout_iter_get_baseline (iter);
+          if (!PANGO_IS_FC_FONT(run->item->analysis.font)) continue;
+          fcfont = PANGO_FC_FONT(run->item->analysis.font);
+          pattern = fcfont->font_pattern;
+          if (FcPatternGetString(pattern, FC_FILE, 0, (unsigned char **)&filename) != FcResultMatch ||
+              FcPatternGetInteger(pattern, FC_INDEX, 0, &font_id) != FcResultMatch)
+                continue;
+          ftface = pango_fc_font_lock_face(fcfont);
+          current_page = -1;
+          cur_font = NULL;
+          g_string_append_printf(str, "BT %.2f 0 0 %.2f %.2f %.2f Tm ",
+            item->font_size, -item->font_size,
+            item->bbox.left + (gdouble) logical_rect.x/PANGO_SCALE,
+            item->bbox.top + (gdouble) baseline/PANGO_SCALE);
+          in_string = FALSE;
+          for (i=0; i<run->glyphs->num_glyphs; i++) {
+            glyph_no = run->glyphs->glyphs[i].glyph;
+            if (FT_IS_SFNT(ftface)) glyph_page = glyph_no/255;
+            else glyph_page = 0;
+            if (glyph_page != current_page) {
+              cur_font = new_pdffont(xref, pdffonts, filename, font_id,
+                 ftface, glyph_page);
+              if (in_string) g_string_append(str, ") Tj ");
+              in_string = FALSE;
+              g_string_append_printf(str, "/F%d 1 Tf ", cur_font->n_obj);
+            }
+            current_page = glyph_page;
+            FT_Load_Glyph(ftface, glyph_no, FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
+            advance = (int)(ftface->glyph->metrics.horiAdvance * cur_font->ft2ps + 0.5);
+            if (!in_string) g_string_append_c(str, '(');
+            in_string = TRUE;
+            if (cur_font->is_truetype) {
+              if (glyph_no) glyph_no = (glyph_no%255)+1;
+              cur_font->glyphmap[glyph_no] = glyph_no;
+            }
+            else {
+              for (j=1; j<=cur_font->num_glyphs_used; j++)
+                if (cur_font->glyphmap[j] == glyph_no) break;
+              if (j==256) j=0; // font is full, what do we do?
+              if (j>cur_font->num_glyphs_used) {
+                cur_font->glyphmap[j] = glyph_no; 
+                cur_font->num_glyphs_used++;
+                if (FT_Get_Glyph_Name(ftface, glyph_no, tmpstr, 200) == FT_Err_Ok)
+                  cur_font->glyphpsnames[j] = g_strdup(tmpstr);
+                else cur_font->glyphpsnames[j] = g_strdup(".notdef");
+              }
+              glyph_no = j;
+            }
+            cur_font->advance[glyph_no] = advance;
+            if (glyph_no=='\\' || glyph_no == '(' || glyph_no == ')' || glyph_no == 10 || glyph_no == 13) 
+              g_string_append_c(str, '\\');
+            if (glyph_no==10) g_string_append_c(str,'n');
+            else if (glyph_no==13) g_string_append_c(str,'r');
+            else g_string_append_c(str, glyph_no);
+          }
+          if (in_string) g_string_append(str, ") Tj ");
+          g_string_append(str, "ET ");
+          pango_fc_font_unlock_face(fcfont);
+        } while (pango_layout_iter_next_run(iter));
+        pango_layout_iter_free(iter);
+        g_object_unref(layout);
+      }
+      else if  (item->type == ITEM_IMAGE) {
+        cur_image = new_pdfimage(xref, pdfimages, item->image);
+       cur_image->used_in_this_page = TRUE;
+        g_string_append_printf(str, "\nq 1 0 0 1 %.2f %.2f cm %.2f 0 0 %.2f 0 %.2f cm /Im%d Do Q ",
+           item->bbox.left, item->bbox.top, // translation
+           item->bbox.right-item->bbox.left, item->bbox.top-item->bbox.bottom, item->bbox.bottom-item->bbox.top, // scaling
+           cur_image->n_obj);
+      }
     }
   }
 }
@@ -811,29 +1309,32 @@ gboolean print_to_pdf(char *filename)
   struct XrefTable xref;
   GList *pglist;
   struct Page *pg;
-  char *buf;
-  unsigned int len;
   gboolean annot, uses_pdf;
   gboolean use_hiliter;
   struct PdfInfo pdfinfo;
   struct PdfObj *obj;
+  GList *pdffonts, *pdfimages, *list;
+  struct PdfFont *font;
+  struct PdfImage *image;
+  char *tmpbuf;
   
-  f = fopen(filename, "w");
+  f = fopen(filename, "wb");
   if (f == NULL) return FALSE;
+  setlocale(LC_NUMERIC, "C");
   annot = FALSE;
   xref.data = NULL;
   uses_pdf = FALSE;
+  pdffonts = NULL;
+  pdfimages = NULL;
   for (pglist = journal.pages; pglist!=NULL; pglist = pglist->next) {
     pg = (struct Page *)pglist->data;
     if (pg->bg->type == BG_PDF) uses_pdf = TRUE;
   }
   
   if (uses_pdf && bgpdf.status != STATUS_NOT_INIT && 
-      g_file_get_contents(bgpdf.tmpfile_copy, &buf, &len, NULL) &&
-      !strncmp(buf, "%PDF-1.", 7)) {
+      bgpdf.file_contents!=NULL && !strncmp(bgpdf.file_contents, "%PDF-1.", 7)) {
     // parse the existing PDF file
-    pdfbuf = g_string_new_len(buf, len);
-    g_free(buf);
+    pdfbuf = g_string_new_len(bgpdf.file_contents, bgpdf.file_length);
     if (pdfbuf->str[7]<'4') pdfbuf->str[7] = '4'; // upgrade to 1.4
     annot = pdf_parse_info(pdfbuf, &pdfinfo, &xref);
     if (!annot) {
@@ -863,8 +1364,8 @@ gboolean print_to_pdf(char *filename)
   g_string_append_printf(pdfbuf, "] /Count %d >> endobj\n", journal.npages);
   make_xref(&xref, n_obj_catalog+2, pdfbuf->len);
   g_string_append_printf(pdfbuf, 
-    "%d 0 obj\n<< /Type /ExtGState /CA 0.5 >> endobj\n",
-     n_obj_catalog+2);
+    "%d 0 obj\n<< /Type /ExtGState /CA %.2f >> endobj\n",
+     n_obj_catalog+2, ui.hiliter_opacity);
   xref.last = n_obj_pages_offs + journal.npages-1;
   
   for (pglist = journal.pages, n_page = 0; pglist!=NULL;
@@ -885,16 +1386,16 @@ gboolean print_to_pdf(char *filename)
       tmpstr = make_pdfprefix(pdfinfo.pages+(pg->bg->file_page_seq-1),
                               pg->width, pg->height);
       g_string_append_printf(pdfbuf,
-        "%d 0 obj\n<< /Length %d >> stream\n%s\nendstream\nendobj\n",
+        "%d 0 obj\n<< /Length %zu >> stream\n%s\nendstream\nendobj\n",
         n_obj_prefix, tmpstr->len, tmpstr->str);
       g_string_free(tmpstr, TRUE);
-      g_string_prepend(pgstrm, "Q ");
+      g_string_prepend(pgstrm, "Q Q Q ");
     }
     else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF)
       n_obj_bgpix = pdf_draw_bitmap_background(pg, pgstrm, &xref, pdfbuf);
     // draw the page contents
     use_hiliter = FALSE;
-    pdf_draw_page(pg, pgstrm, &use_hiliter);
+    pdf_draw_page(pg, pgstrm, &use_hiliter, &xref, &pdffonts, &pdfimages);
     g_string_append_printf(pgstrm, "Q\n");
     
     // deflate pgstrm and write it
@@ -903,7 +1404,7 @@ gboolean print_to_pdf(char *filename)
     
     make_xref(&xref, xref.last+1, pdfbuf->len);
     g_string_append_printf(pdfbuf, 
-      "%d 0 obj\n<< /Length %d /Filter /FlateDecode>> stream\n",
+      "%d 0 obj\n<< /Length %zu /Filter /FlateDecode>> stream\n",
       xref.last, zpgstrm->len);
     g_string_append_len(pdfbuf, zpgstrm->str, zpgstrm->len);
     g_string_free(zpgstrm, TRUE);
@@ -950,7 +1451,7 @@ gboolean print_to_pdf(char *filename)
     }
     add_dict_subentry(pdfbuf, &xref,
         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/PDF"));
-    if (n_obj_bgpix>0)
+    if (n_obj_bgpix>0 || pdfimages!=NULL)
       add_dict_subentry(pdfbuf, &xref,
         obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/ImageC"));
     if (use_hiliter)
@@ -959,11 +1460,49 @@ gboolean print_to_pdf(char *filename)
     if (n_obj_bgpix>0)
       add_dict_subentry(pdfbuf, &xref,
         obj, "/XObject", PDFTYPE_DICT, "/ImBg", mk_pdfref(n_obj_bgpix));
+    for (list=pdffonts; list!=NULL; list = list->next) {
+      font = (struct PdfFont *)list->data;
+      if (font->used_in_this_page) {
+        add_dict_subentry(pdfbuf, &xref,
+          obj, "/ProcSet", PDFTYPE_ARRAY, NULL, mk_pdfname("/Text"));
+        tmpbuf = g_strdup_printf("/F%d", font->n_obj);
+        add_dict_subentry(pdfbuf, &xref,
+          obj, "/Font", PDFTYPE_DICT, tmpbuf, mk_pdfref(font->n_obj));
+        g_free(tmpbuf);
+      }
+    }
+    for (list=pdfimages; list!=NULL; list = list->next) {
+      image = (struct PdfImage *)list->data;
+      if (image->used_in_this_page) {
+        tmpbuf = g_strdup_printf("/Im%d", image->n_obj);
+        add_dict_subentry(pdfbuf, &xref,
+          obj, "/XObject", PDFTYPE_DICT, tmpbuf, mk_pdfref(image->n_obj));
+        g_free(tmpbuf);
+      }
+    }
     show_pdfobj(obj, pdfbuf);
     free_pdfobj(obj);
     g_string_append(pdfbuf, " >> endobj\n");
   }
   
+  // after the pages, we insert fonts and images
+  for (list = pdffonts; list!=NULL; list = list->next) {
+    font = (struct PdfFont *)list->data;
+    embed_pdffont(pdfbuf, &xref, font);
+    g_free(font->filename);
+    g_free(font->fontname);
+    g_free(font);
+  }
+  g_list_free(pdffonts);
+  for (list = pdfimages; list!=NULL; list = list->next) {
+    image = (struct PdfImage *)list->data;
+    if (!pdf_draw_image(image, &xref, pdfbuf)) {
+      return FALSE;
+    }
+    g_free(image);
+  }
+  g_list_free(pdfimages);
+  
   // PDF trailer
   startxref = pdfbuf->len;
   if (annot) g_string_append_printf(pdfbuf,
@@ -992,9 +1531,15 @@ gboolean print_to_pdf(char *filename)
   g_free(xref.data);
   if (annot) {
     free_pdfobj(pdfinfo.trailerdict);
-    // ...
+    if (pdfinfo.pages!=NULL)
+      for (i=0; i<pdfinfo.npages; i++) {
+        free_pdfobj(pdfinfo.pages[i].resources);
+        free_pdfobj(pdfinfo.pages[i].mediabox);
+        free_pdfobj(pdfinfo.pages[i].contents);
+      }
   }
   
+  setlocale(LC_NUMERIC, "");
   if (fwrite(pdfbuf->str, 1, pdfbuf->len, f) < pdfbuf->len) {
     fclose(f);
     g_string_free(pdfbuf, TRUE);
@@ -1005,76 +1550,79 @@ gboolean print_to_pdf(char *filename)
   return TRUE;
 }
 
-/*********** Printing via libgnomeprint **********/
+/*********** Printing via gtk-print **********/
+
+#if GTK_CHECK_VERSION(2, 10, 0)
 
 // does the same job as update_canvas_bg(), but to a print context
 
-void print_background(GnomePrintContext *gpc, struct Page *pg, gboolean *abort)
+void print_background(cairo_t *cr, struct Page *pg)
 {
   double x, y;
   GdkPixbuf *pix;
   BgPdfPage *pgpdf;
+  PopplerPage *pdfpage;
+  cairo_surface_t *cr_pixbuf;
+  double pgwidth, pgheight;
 
   if (pg->bg->type == BG_SOLID) {
-    gnome_print_setopacity(gpc, 1.0);
-    gnome_print_setrgbcolor(gpc, RGBA_RGB(pg->bg->color_rgba));
-    gnome_print_rect_filled(gpc, 0, 0, pg->width, pg->height);
-
+    cairo_set_source_rgb(cr, RGBA_RGB(pg->bg->color_rgba));
+    cairo_rectangle(cr, 0, 0, pg->width, pg->height);
+    cairo_fill(cr);
+    if (!ui.print_ruling) return;
     if (pg->bg->ruling == RULING_NONE) return;
-    gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_COLOR));
-    gnome_print_setlinewidth(gpc, RULING_THICKNESS);
-    
+    cairo_set_source_rgb(cr, RGBA_RGB(RULING_COLOR));
+    cairo_set_line_width(cr, RULING_THICKNESS);
+
     if (pg->bg->ruling == RULING_GRAPH) {
       for (x=RULING_GRAPHSPACING; x<pg->width-1; x+=RULING_GRAPHSPACING)
-        gnome_print_line_stroked(gpc, x, 0, x, pg->height);
+        { cairo_move_to(cr, x, 0); cairo_line_to(cr, x, pg->height); }
       for (y=RULING_GRAPHSPACING; y<pg->height-1; y+=RULING_GRAPHSPACING)
-        gnome_print_line_stroked(gpc, 0, y, pg->width, y);
+        { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
+      cairo_stroke(cr);
       return;
     }
     
     for (y=RULING_TOPMARGIN; y<pg->height-1; y+=RULING_SPACING)
-      gnome_print_line_stroked(gpc, 0, y, pg->width, y);
+      { cairo_move_to(cr, 0, y); cairo_line_to(cr, pg->width, y); }
+    cairo_stroke(cr);
     if (pg->bg->ruling == RULING_LINED) {
-      gnome_print_setrgbcolor(gpc, RGBA_RGB(RULING_MARGIN_COLOR));
-      gnome_print_line_stroked(gpc, RULING_LEFTMARGIN, 0, RULING_LEFTMARGIN, pg->height);
+      cairo_set_source_rgb(cr, RGBA_RGB(RULING_MARGIN_COLOR));
+      cairo_move_to(cr, RULING_LEFTMARGIN, 0);
+      cairo_line_to(cr, RULING_LEFTMARGIN, pg->height);
+      cairo_stroke(cr);
     }
     return;
   }
-  else if (pg->bg->type == BG_PIXMAP || pg->bg->type == BG_PDF) {
-    if (pg->bg->type == BG_PDF) {
-      pgpdf = (struct BgPdfPage *)g_list_nth_data(bgpdf.pages, pg->bg->file_page_seq-1);
-      if (pgpdf == NULL) return;
-      if (pgpdf->dpi != PDFTOPPM_PRINTING_DPI) {
-        add_bgpdf_request(pg->bg->file_page_seq, 0, TRUE);
-        while (pgpdf->dpi != PDFTOPPM_PRINTING_DPI && bgpdf.status == STATUS_RUNNING) {
-          gtk_main_iteration();
-          if (*abort) return;
-        }
-      }
-      pix = pgpdf->pixbuf;
-    }
-    else pix = pg->bg->pixbuf;
-    if (gdk_pixbuf_get_bits_per_sample(pix) != 8) return;
-    if (gdk_pixbuf_get_colorspace(pix) != GDK_COLORSPACE_RGB) return;
-    gnome_print_gsave(gpc);
-    gnome_print_scale(gpc, pg->width, -pg->height);
-    gnome_print_translate(gpc, 0., -1.);
-    if (gdk_pixbuf_get_n_channels(pix) == 3)
-       gnome_print_rgbimage(gpc, gdk_pixbuf_get_pixels(pix),
-         gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
-    else if (gdk_pixbuf_get_n_channels(pix) == 4)
-       gnome_print_rgbaimage(gpc, gdk_pixbuf_get_pixels(pix),
-         gdk_pixbuf_get_width(pix), gdk_pixbuf_get_height(pix), gdk_pixbuf_get_rowstride(pix));
-    gnome_print_grestore(gpc);
-    return;
+  else
+  if (pg->bg->type == BG_PDF) {
+    if (!bgpdf.document) return;
+    pdfpage = poppler_document_get_page(bgpdf.document, pg->bg->file_page_seq-1);
+    if (!pdfpage) return;
+    poppler_page_get_size(pdfpage, &pgwidth, &pgheight);
+    cairo_save(cr);
+    cairo_scale(cr, pg->width/pgwidth, pg->height/pgheight);
+    poppler_page_render(pdfpage, cr);
+    cairo_restore(cr);
+    g_object_unref(pdfpage);
+  }
+  else 
+  if (pg->bg->type == BG_PIXMAP) {
+    cairo_save(cr);
+    cairo_scale(cr, pg->width/gdk_pixbuf_get_width(pg->bg->pixbuf),
+                    pg->height/gdk_pixbuf_get_height(pg->bg->pixbuf));
+    gdk_cairo_set_source_pixbuf(cr, pg->bg->pixbuf, 0, 0);
+    cairo_rectangle(cr, 0, 0, gdk_pixbuf_get_width(pg->bg->pixbuf), gdk_pixbuf_get_height(pg->bg->pixbuf));
+    cairo_fill(cr);
+    cairo_restore(cr);
   }
 }
 
-void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
-                double pgwidth, double pgheight, gboolean *abort)
+void print_job_render_page(GtkPrintOperation *print, GtkPrintContext *context, gint pageno, gpointer user_data)
 {
-  char tmp[10];
-  gdouble scale;
+  cairo_t *cr;
+  gdouble width, height, scale;
+  struct Page *pg;
   guint old_rgba;
   double old_thickness;
   GList *layerlist, *itemlist;
@@ -1082,104 +1630,81 @@ void print_page(GnomePrintContext *gpc, struct Page *pg, int pageno,
   struct Item *item;
   int i;
   double *pt;
+  PangoFontDescription *font_desc;
+  PangoLayout *layout;
+        
+  pg = (struct Page *)g_list_nth_data(journal.pages, pageno);
+  cr = gtk_print_context_get_cairo_context(context);
+  width = gtk_print_context_get_width(context);
+  height = gtk_print_context_get_height(context);
+  scale = MIN(width/pg->width, height/pg->height);
   
-  if (pg==NULL) return;
-  
-  g_snprintf(tmp, 10, "Page %d", pageno);
-  gnome_print_beginpage(gpc, (guchar *)tmp);
-  gnome_print_gsave(gpc);
+  cairo_translate(cr, (width-scale*pg->width)/2, (height-scale*pg->height)/2);
+  cairo_scale(cr, scale, scale);
+  cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
+  cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
   
-  scale = MIN(pgwidth/pg->width, pgheight/pg->height)*0.95;
-  gnome_print_translate(gpc,
-     (pgwidth - scale*pg->width)/2, (pgheight + scale*pg->height)/2);
-  gnome_print_scale(gpc, scale, -scale);
-  gnome_print_setlinejoin(gpc, 1); // round
-  gnome_print_setlinecap(gpc, 1); // round
-
-  print_background(gpc, pg, abort);
+  print_background(cr, pg);
 
-  old_rgba = 0x12345678;    // not any values we use, so we'll reset them
+  old_rgba = predef_colors_rgba[COLOR_BLACK];
+  cairo_set_source_rgb(cr, 0, 0, 0);
   old_thickness = 0.0;
 
   for (layerlist = pg->layers; layerlist!=NULL; layerlist = layerlist->next) {
-    if (*abort) break;
     l = (struct Layer *)layerlist->data;
     for (itemlist = l->items; itemlist!=NULL; itemlist = itemlist->next) {
-      if (*abort) break;
       item = (struct Item *)itemlist->data;
-      if (item->type == ITEM_STROKE) {
-        if ((item->brush.color_rgba & ~0xff) != (old_rgba & ~0xff))
-          gnome_print_setrgbcolor(gpc, RGBA_RGB(item->brush.color_rgba));
-        if ((item->brush.color_rgba & 0xff) != (old_rgba & 0xff))
-          gnome_print_setopacity(gpc, RGBA_ALPHA(item->brush.color_rgba));
-        if (item->brush.thickness != old_thickness)
-          gnome_print_setlinewidth(gpc, item->brush.thickness);
+      if (item->type == ITEM_STROKE || item->type == ITEM_TEXT) {
+        if (item->brush.color_rgba != old_rgba)
+          cairo_set_source_rgba(cr, RGBA_RGB(item->brush.color_rgba),
+                                    RGBA_ALPHA(item->brush.color_rgba));
         old_rgba = item->brush.color_rgba;
-        old_thickness = item->brush.thickness;
-        gnome_print_newpath(gpc);
+      }
+      if (item->type == ITEM_STROKE) {    
+        if (item->brush.thickness != old_thickness)
+          cairo_set_line_width(cr, item->brush.thickness);
         pt = item->path->coords;
-        gnome_print_moveto(gpc, pt[0], pt[1]);
-        for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
-          gnome_print_lineto(gpc, pt[0], pt[1]);
-        gnome_print_stroke(gpc);
+        if (!item->brush.variable_width) {
+          cairo_move_to(cr, pt[0], pt[1]);
+          for (i=1, pt+=2; i<item->path->num_points; i++, pt+=2)
+            cairo_line_to(cr, pt[0], pt[1]);
+          cairo_stroke(cr);
+          old_thickness = item->brush.thickness;
+        } else {
+          for (i=0; i<item->path->num_points-1; i++, pt+=2) {
+            cairo_move_to(cr, pt[0], pt[1]);
+            cairo_set_line_width(cr, item->widths[i]);
+            cairo_line_to(cr, pt[2], pt[3]);
+            cairo_stroke(cr);
+          }
+          old_thickness = 0.0;
+        }
+      }
+      if (item->type == ITEM_TEXT) {
+        layout = gtk_print_context_create_pango_layout(context);
+        font_desc = pango_font_description_from_string(item->font_name);
+        if (item->font_size)
+          pango_font_description_set_absolute_size(font_desc,
+            item->font_size*PANGO_SCALE);
+        pango_layout_set_font_description(layout, font_desc);
+        pango_font_description_free(font_desc);
+        pango_layout_set_text(layout, item->text, -1);
+        cairo_move_to(cr, item->bbox.left, item->bbox.top);
+        pango_cairo_show_layout(cr, layout);
+        g_object_unref(layout);
+      }
+      if (item->type == ITEM_IMAGE) {
+        double scalex = (item->bbox.right-item->bbox.left)/gdk_pixbuf_get_width(item->image);
+        double scaley = (item->bbox.bottom-item->bbox.top)/gdk_pixbuf_get_height(item->image);
+        cairo_scale(cr, scalex, scaley);
+        gdk_cairo_set_source_pixbuf(cr,item->image, item->bbox.left/scalex, item->bbox.top/scaley);
+        cairo_scale(cr, 1/scalex, 1/scaley);
+        cairo_paint(cr);
+        old_rgba = predef_colors_rgba[COLOR_BLACK];
+        cairo_set_source_rgb(cr, 0, 0, 0);
       }
     }
   }
-  
-  gnome_print_grestore(gpc);
-  gnome_print_showpage(gpc);
-}
-
-void cb_print_abort(GtkDialog *dialog, gint response, gboolean *abort)
-{
-  *abort = TRUE;
 }
 
-void print_job_render(GnomePrintJob *gpj, int fromPage, int toPage)
-{
-  GnomePrintConfig *config;
-  GnomePrintContext *gpc;
-  GtkWidget *wait_dialog;
-  double pgwidth, pgheight;
-  int i;
-  gboolean abort;
-  
-  config = gnome_print_job_get_config(gpj);
-  gnome_print_config_get_page_size(config, &pgwidth, &pgheight);
-  g_object_unref(G_OBJECT(config));
-
-  gpc = gnome_print_job_get_context(gpj);
-
-  abort = FALSE;
-  wait_dialog = gtk_message_dialog_new(GTK_WINDOW(winMain), GTK_DIALOG_MODAL,
-     GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL, "Preparing print job");
-  gtk_widget_show(wait_dialog);
-  g_signal_connect(wait_dialog, "response", G_CALLBACK (cb_print_abort), &abort);
-  
-  for (i = fromPage; i <= toPage; i++) {
-#if GTK_CHECK_VERSION(2,6,0)
-    if (!gtk_check_version(2, 6, 0))
-      gtk_message_dialog_format_secondary_text(
-             GTK_MESSAGE_DIALOG(wait_dialog), "Page %d", i+1); 
-#endif
-    while (gtk_events_pending()) gtk_main_iteration();
-    print_page(gpc, (struct Page *)g_list_nth_data(journal.pages, i), i+1,
-                                             pgwidth, pgheight, &abort);
-    if (abort) break;
-  }
-#if GTK_CHECK_VERSION(2,6,0)
-  if (!gtk_check_version(2, 6, 0))
-    gtk_message_dialog_format_secondary_text(
-              GTK_MESSAGE_DIALOG(wait_dialog), "Finalizing...");
 #endif
-  while (gtk_events_pending()) gtk_main_iteration();
-
-  gnome_print_context_close(gpc);  
-  g_object_unref(G_OBJECT(gpc));  
-
-  gnome_print_job_close(gpj);
-  if (!abort) gnome_print_job_print(gpj);
-  g_object_unref(G_OBJECT(gpj));
-
-  gtk_widget_destroy(wait_dialog);
-}