2007-09-19 [colin] 3.0.1cvs8
[claws.git] / src / printing.c
1 /* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
2  * Copyright (C) 2007 Holger Berndt <hb@claws-mail.org> 
3  * and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "printing.h"
25 #include "image_viewer.h"
26
27 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
28
29 #include "gtkutils.h"
30 #include "prefs_common.h"
31
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
35 #include <pango/pango.h>
36 #include <string.h>
37
38 typedef struct {
39   PangoLayout *layout;
40   PangoContext *pango_context;
41   char *text;
42   GList *page_breaks;
43   GtkTextBuffer *buffer;
44   gint sel_start;
45   gint sel_end;
46   GHashTable *images;
47   gint img_cnt;
48   gboolean print_started;
49   gchar *old_print_preview;
50 } PrintData;
51
52
53 /* callbacks */
54 static void cb_begin_print(GtkPrintOperation*, GtkPrintContext*, gpointer);
55 static void cb_draw_page(GtkPrintOperation*, GtkPrintContext*, gint, gpointer);
56
57 /* variables */
58 static GtkPrintSettings *settings   = NULL;
59 static GtkPageSetup     *page_setup = NULL;
60
61 /* other static functions */
62 static void     printing_layout_set_text_attributes(PrintData*, GtkPrintContext *);
63 static gboolean printing_is_pango_gdk_color_equal(PangoColor*, GdkColor*); 
64 static gint     printing_text_iter_get_offset_bytes(PrintData *, const GtkTextIter*);
65
66 static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context, gint page_nr, gpointer user_data);
67
68 #define PREVIEW_SCALE 72
69 static void preview_destroy (GtkWindow *window, GtkPrintOperationPreview *preview)
70 {
71   gtk_print_operation_preview_end_preview (preview);
72 }
73
74 static gboolean preview_close(GtkWidget *widget, GdkEventAny *event,
75                                  gpointer data)
76 {
77         if (event->type == GDK_KEY_PRESS)
78                 if (((GdkEventKey *)event)->keyval != GDK_Escape)
79                         return FALSE;
80
81         gtk_widget_destroy(widget);
82         return FALSE;
83 }
84
85 static void preview_size_allocate_cb(GtkWidget *widget,
86                                          GtkAllocation *allocation)
87 {
88         g_return_if_fail(allocation != NULL);
89
90         prefs_common.print_previewwin_width = allocation->width;
91         prefs_common.print_previewwin_height = allocation->height;
92 }
93
94 static gboolean cb_preview (GtkPrintOperation        *operation,
95                                      GtkPrintOperationPreview *preview,
96                                      GtkPrintContext          *context,
97                                      GtkWindow                *parent,
98                                      PrintData                *print_data)
99 {
100   GtkPageSetup    *page_setup = gtk_print_context_get_page_setup (context);
101   GtkPaperSize    *paper_size;
102   gdouble          paper_width;
103   gdouble          paper_height;
104   gdouble          top_margin;
105   gdouble          bottom_margin;
106   gdouble          left_margin;
107   gdouble          right_margin;
108   gint             preview_width;
109   gint             preview_height;
110   gint num_pages = 0, i = 0;
111   cairo_t         *cr;
112   cairo_surface_t *surface;
113   GtkPageOrientation orientation;
114   cairo_status_t   status;
115   gchar           *fname;
116   GtkWidget *dialog = NULL;
117   GtkWidget *image, *notebook, *scrolled_window;
118   GSList *pages = NULL, *cur;
119   static GdkGeometry geometry;
120
121   paper_size      = gtk_page_setup_get_paper_size    (page_setup);
122   paper_width     = gtk_paper_size_get_width         (paper_size, GTK_UNIT_INCH);
123   paper_height    = gtk_paper_size_get_height        (paper_size,  GTK_UNIT_INCH);
124   top_margin      = gtk_page_setup_get_top_margin    (page_setup, GTK_UNIT_INCH);
125   bottom_margin   = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_INCH);
126   left_margin     = gtk_page_setup_get_left_margin   (page_setup, GTK_UNIT_INCH);
127   right_margin    = gtk_page_setup_get_right_margin  (page_setup, GTK_UNIT_INCH);
128
129   /* the print context does not have the page orientation, it is transformed */
130   orientation     = gtk_page_setup_get_orientation (page_setup);
131
132   if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
133       orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
134     {
135       preview_width  = PREVIEW_SCALE * paper_width;
136       preview_height = PREVIEW_SCALE * paper_height;
137     }
138   else
139     {
140       preview_width  = PREVIEW_SCALE * paper_height;
141       preview_height = PREVIEW_SCALE * paper_width;
142     }
143
144
145   num_pages = 1;  
146   for (i = 0; i < num_pages; i++) {
147     surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
148                                               preview_width, preview_height);
149
150     if (CAIRO_STATUS_SUCCESS != cairo_surface_status (surface)) {
151       g_message ("Unable to create preview (not enough memory?)");
152       return TRUE;
153     }
154
155     cr = cairo_create (surface);
156     gtk_print_context_set_cairo_context (context, cr, PREVIEW_SCALE, PREVIEW_SCALE);
157
158     /* fill page with white */
159     cairo_set_source_rgb (cr, 1, 1, 1);
160     cairo_new_path (cr);
161     cairo_rectangle (cr, 0, 0, preview_width, preview_height);
162     cairo_fill (cr);
163
164     cairo_translate (cr, left_margin * PREVIEW_SCALE, right_margin * PREVIEW_SCALE);
165
166     claws_draw_page (operation, context, i, print_data);
167     num_pages = g_list_length(print_data->page_breaks) + 1;
168
169     fname = get_tmp_file();
170     status = cairo_surface_write_to_png (surface, fname);
171     cairo_destroy (cr);
172     cairo_surface_destroy (surface);
173     if (status == CAIRO_STATUS_SUCCESS) {
174         image = gtk_image_new_from_file (fname);
175         g_unlink (fname);
176         g_free (fname);
177         pages = g_slist_prepend(pages, image);
178         debug_print("added one page\n");
179     }
180   }
181   pages = g_slist_reverse(pages);
182   
183   dialog = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "print_preview");
184
185   if (!geometry.min_height) {
186           geometry.min_width = 600;
187           geometry.min_height = 400;
188   }
189
190   gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
191                                 GDK_HINT_MIN_SIZE);
192   gtk_widget_set_size_request(dialog, prefs_common.print_previewwin_width,
193                               prefs_common.print_previewwin_height);
194
195   gtk_window_set_title(GTK_WINDOW(dialog), _("Print preview"));
196   notebook = gtk_notebook_new();
197   gtk_container_add(GTK_CONTAINER(dialog), notebook);
198   i = 0;
199   for (cur = pages; cur; cur = cur->next) {
200     image = (GtkImage *)cur->data;
201     if (gtk_print_operation_preview_is_selected(preview, i)) {
202       scrolled_window = gtk_scrolled_window_new(NULL, NULL);
203       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
204                                        GTK_POLICY_AUTOMATIC,
205                                        GTK_POLICY_AUTOMATIC);
206       gtk_scrolled_window_add_with_viewport
207               (GTK_SCROLLED_WINDOW(scrolled_window),
208                image);
209       debug_print("page %d sel\n", i);
210       gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, NULL);
211     }
212     i++;
213   }
214   g_slist_free(pages);
215   
216   gtk_widget_show_all(dialog);
217   
218   g_signal_connect (dialog, "destroy",
219                     G_CALLBACK (preview_destroy), preview);
220   g_signal_connect (dialog, "key_press_event",
221                     G_CALLBACK (preview_close), preview);
222  g_signal_connect(G_OBJECT(dialog), "size_allocate",
223                   G_CALLBACK(preview_size_allocate_cb), NULL);
224   
225   return TRUE;
226 }
227
228 void printing_print(GtkTextView *text_view, GtkWindow *parent, gint sel_start, gint sel_end)
229 {
230   GtkPrintOperation *op;
231   GtkPrintOperationResult res;
232   PrintData *print_data;
233   GtkTextIter start, end;
234   GtkTextBuffer *buffer;
235
236   op = gtk_print_operation_new();
237
238   print_data = g_new0(PrintData,1);
239
240   print_data->images = g_hash_table_new(g_direct_hash, g_direct_equal);
241   print_data->pango_context=gtk_widget_get_pango_context(GTK_WIDGET(text_view));
242
243   /* get text */
244   buffer = gtk_text_view_get_buffer(text_view);
245   print_data->buffer = buffer;
246   print_data->sel_start = sel_start;
247   print_data->sel_end = sel_end;
248   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
249     gtk_text_buffer_get_start_iter(buffer, &start);
250     gtk_text_buffer_get_end_iter(buffer, &end);
251   } else {
252     gtk_text_buffer_get_iter_at_offset(buffer, &start, print_data->sel_start);
253     gtk_text_buffer_get_iter_at_offset(buffer, &end, print_data->sel_end);
254   }
255
256   print_data->text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
257
258   if (settings == NULL) {
259     settings = gtk_print_settings_new();
260     gtk_print_settings_set_use_color(settings, prefs_common.print_use_color);
261     gtk_print_settings_set_collate(settings, prefs_common.print_use_collate);
262     gtk_print_settings_set_reverse(settings, prefs_common.print_use_reverse);
263     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
264   }
265   if (page_setup == NULL) {
266     GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
267     page_setup = gtk_page_setup_new();
268     gtk_page_setup_set_paper_size(page_setup, paper);
269     gtk_paper_size_free(paper);
270     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
271   }
272   
273   /* Config for printing */
274   gtk_print_operation_set_print_settings(op, settings);
275   gtk_print_operation_set_default_page_setup(op, page_setup);
276
277   /* signals */
278   g_signal_connect(op, "begin_print", G_CALLBACK(cb_begin_print), print_data);
279   g_signal_connect(op, "draw_page", G_CALLBACK(cb_draw_page), print_data);
280   g_signal_connect(op, "preview", G_CALLBACK(cb_preview), print_data);
281
282   /* Start printing process */
283   res = gtk_print_operation_run(op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
284                                 parent, NULL);
285
286   if(res == GTK_PRINT_OPERATION_RESULT_ERROR) {
287     GError *error = NULL;
288     gtk_print_operation_get_error(op, &error);
289     debug_print("Error printing message: %s",
290             error ? error->message : "no details");
291   }
292   else if(res == GTK_PRINT_OPERATION_RESULT_APPLY) {
293     /* store settings for next printing session */
294     if(settings != NULL)
295       g_object_unref(settings);
296     settings = g_object_ref(gtk_print_operation_get_print_settings(op));
297     prefs_common.print_use_color = gtk_print_settings_get_use_color(settings);
298     prefs_common.print_use_collate = gtk_print_settings_get_collate(settings);
299     prefs_common.print_use_reverse = gtk_print_settings_get_reverse(settings);
300     prefs_common.print_use_duplex = gtk_print_settings_get_duplex(settings);
301   }
302
303   g_hash_table_destroy(print_data->images);
304   if(print_data->text)
305     g_free(print_data->text);
306   g_list_free(print_data->page_breaks);
307   if(print_data->layout)
308     g_object_unref(print_data->layout);
309
310   if (print_data->old_print_preview) {
311     g_object_set(gtk_settings_get_default(),
312                   "gtk-print-preview-command", print_data->old_print_preview, NULL);
313     g_free(print_data->old_print_preview);
314     print_data->old_print_preview = NULL;
315   }
316
317   g_free(print_data);
318
319   g_object_unref(op);
320   debug_print("printing_print finished\n");
321 }
322
323 void printing_page_setup(GtkWindow *parent)
324 {
325   GtkPageSetup *new_page_setup;
326
327   if(settings == NULL) {
328     settings = gtk_print_settings_new();
329     gtk_print_settings_set_use_color(settings, prefs_common.print_use_color);
330     gtk_print_settings_set_collate(settings, prefs_common.print_use_collate);
331     gtk_print_settings_set_reverse(settings, prefs_common.print_use_reverse);
332     gtk_print_settings_set_duplex(settings, prefs_common.print_use_duplex);
333   }
334   if (page_setup == NULL) {
335     GtkPaperSize *paper = gtk_paper_size_new(prefs_common.print_paper_type);
336     page_setup = gtk_page_setup_new();
337     gtk_page_setup_set_paper_size(page_setup, paper);
338     gtk_paper_size_free(paper);
339     gtk_page_setup_set_orientation(page_setup, prefs_common.print_paper_orientation);
340   }
341
342   new_page_setup = gtk_print_run_page_setup_dialog(parent,page_setup,settings);
343
344   if(page_setup)
345     g_object_unref(page_setup);
346
347   page_setup = new_page_setup;
348   
349   g_free(prefs_common.print_paper_type);
350   prefs_common.print_paper_type = g_strdup(gtk_paper_size_get_name(
351                 gtk_page_setup_get_paper_size(page_setup)));
352   prefs_common.print_paper_orientation = gtk_page_setup_get_orientation(page_setup);
353 }
354
355 static void cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
356                            gpointer user_data)
357 {
358   double width, height;
359   int num_lines;
360   double page_height;
361   GList *page_breaks;
362   PrintData *print_data;
363   PangoFontDescription *desc;
364   int start, ii;
365   PangoLayoutIter *iter;
366   double start_pos;
367   double line_height =0.;
368
369   print_data = (PrintData*) user_data;
370
371   if (print_data->print_started)
372     return;
373   debug_print("Preparing print job...\n");
374         
375   width  = gtk_print_context_get_width(context);
376   height = gtk_print_context_get_height(context);
377
378   if (print_data->layout == NULL)
379     print_data->layout = gtk_print_context_create_pango_layout(context);
380         
381   if(prefs_common.use_different_print_font)
382     desc = pango_font_description_from_string(prefs_common.printfont);
383   else
384     desc = pango_font_description_copy(
385                 pango_context_get_font_description(print_data->pango_context));
386
387   pango_layout_set_font_description(print_data->layout, desc);
388   pango_font_description_free(desc);
389
390   pango_layout_set_width(print_data->layout, width * PANGO_SCALE);
391   pango_layout_set_text(print_data->layout, print_data->text, -1);
392
393   printing_layout_set_text_attributes(print_data, context);
394
395   num_lines = pango_layout_get_line_count(print_data->layout);
396
397   page_breaks = NULL;
398   page_height = 0;
399   start = 0;
400   ii = 0;
401   start_pos = 0.;
402   iter = pango_layout_get_iter(print_data->layout);
403   do {
404     PangoRectangle logical_rect;
405     PangoLayoutLine *line;
406     PangoAttrShape *attr = NULL;
407     int baseline;
408
409     if(ii >= start) {
410       line = pango_layout_iter_get_line(iter);
411
412       pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
413       baseline = pango_layout_iter_get_baseline(iter);
414
415       if ((attr = g_hash_table_lookup(print_data->images, GINT_TO_POINTER(pango_layout_iter_get_index(iter)))) != NULL) {
416         line_height = (double)gdk_pixbuf_get_height(GDK_PIXBUF(attr->data));
417       } else {
418         line_height = ((double)logical_rect.height) / PANGO_SCALE;
419       }
420     }
421     if((page_height + line_height) > height) {
422       page_breaks = g_list_prepend(page_breaks, GINT_TO_POINTER(ii));
423       page_height = 0;
424     }
425
426     page_height += line_height;
427     ii++;
428   } while(ii < num_lines && pango_layout_iter_next_line(iter));
429   pango_layout_iter_free(iter);
430
431   page_breaks = g_list_reverse(page_breaks);
432   gtk_print_operation_set_n_pages(op, g_list_length(page_breaks) + 1);
433         
434   print_data->page_breaks = page_breaks;
435
436   debug_print("Starting print job...\n");
437   print_data->print_started = TRUE;
438 }
439
440 static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *pixbuf)
441 {
442           cairo_surface_t *surface;
443           cairo_format_t format;
444           static const cairo_user_data_key_t key;
445           guchar *pixels = g_malloc(
446                                 4*
447                                 gdk_pixbuf_get_width(pixbuf)*
448                                 gdk_pixbuf_get_height(pixbuf));
449           guchar *src_pixels = gdk_pixbuf_get_pixels (pixbuf);
450           gint width = gdk_pixbuf_get_width(pixbuf);
451           gint height = gdk_pixbuf_get_height(pixbuf);
452           gint nchans = gdk_pixbuf_get_n_channels (pixbuf);
453           gint stride = gdk_pixbuf_get_rowstride (pixbuf);
454           gint j;
455
456           if (nchans == 3)
457             format = CAIRO_FORMAT_RGB24;
458           else
459             format = CAIRO_FORMAT_ARGB32;
460           surface = cairo_image_surface_create_for_data (pixels,         
461                                                 format, width, height, 4*width);
462           cairo_surface_set_user_data (surface, &key, 
463                 pixels, (cairo_destroy_func_t)g_free);
464
465           for (j = height; j; j--) {
466                 guchar *p = src_pixels;
467                 guchar *q = pixels;
468
469                 if (nchans == 3) {
470                         guchar *end = p + 3 * width;
471
472                         while (p < end) {
473 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
474                                 q[0] = p[2];
475                                 q[1] = p[1];
476                                 q[2] = p[0];
477 #else
478                                 q[1] = p[0];
479                                 q[2] = p[1];
480                                 q[3] = p[2];
481 #endif
482                                 p += 3;
483                                 q += 4;
484                         }
485                 } else {
486                         guchar *end = p + 4 * width;
487                         guint t1,t2,t3;
488
489 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
490
491                         while (p < end) {
492 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
493                                 MULT(q[0], p[2], p[3], t1);
494                                 MULT(q[1], p[1], p[3], t2);
495                                 MULT(q[2], p[0], p[3], t3);
496                                 q[3] = p[3];
497 #else
498                                 q[0] = p[3];
499                                 MULT(q[1], p[0], p[3], t1);
500                                 MULT(q[2], p[1], p[3], t2);
501                                 MULT(q[3], p[2], p[3], t3);
502 #endif
503
504                                 p += 4;
505                                 q += 4;
506                         }
507
508 #undef MULT
509                 }
510
511                 src_pixels += stride;
512                 pixels += 4 * width;
513         }
514                 
515         return surface;
516 }
517
518 static gboolean claws_draw_page(GtkPrintOperation *op, GtkPrintContext *context, gint page_nr, gpointer user_data)
519 {
520   cairo_t *cr;
521   PrintData *print_data;
522   int start, end, ii;
523   GList *pagebreak;
524   PangoLayoutIter *iter;
525   double start_pos;
526   gboolean notlast = TRUE;
527   print_data = (PrintData*) user_data;
528
529   if (print_data->print_started == FALSE)
530     cb_begin_print(op, context, print_data);
531   if (page_nr == 0) {
532     start = 0;
533   } else {
534     pagebreak = g_list_nth(print_data->page_breaks, page_nr - 1);
535     start = GPOINTER_TO_INT(pagebreak->data);
536   }
537
538   pagebreak = g_list_nth(print_data->page_breaks, page_nr);
539   if(pagebreak == NULL)
540     end = pango_layout_get_line_count(print_data->layout);
541   else
542     end = GPOINTER_TO_INT(pagebreak->data);
543
544   cr = gtk_print_context_get_cairo_context(context);
545   cairo_set_source_rgb(cr, 0., 0., 0.);
546
547   ii = 0;
548   start_pos = 0.;
549   iter = pango_layout_get_iter(print_data->layout);
550   do {
551     PangoRectangle logical_rect;
552     PangoLayoutLine *line;
553     PangoAttrShape *attr = NULL;
554     int baseline;
555
556     if(ii >= start) {
557       line = pango_layout_iter_get_line(iter);
558
559       pango_layout_iter_get_line_extents(iter, NULL, &logical_rect);
560       baseline = pango_layout_iter_get_baseline(iter);
561
562       if(ii == start)
563         start_pos = ((double)logical_rect.y) / PANGO_SCALE;
564       
565       cairo_move_to(cr,
566                     ((double)logical_rect.x) / PANGO_SCALE,
567                     ((double)baseline) / PANGO_SCALE - start_pos);
568                     
569       if ((attr = g_hash_table_lookup(print_data->images, GINT_TO_POINTER(pango_layout_iter_get_index(iter)))) != NULL) {
570           cairo_surface_t *surface;
571
572           surface = pixbuf_to_surface(GDK_PIXBUF(attr->data));
573           cairo_set_source_surface (cr, surface, 
574                 ((double)logical_rect.x) / PANGO_SCALE, 
575                 ((double)baseline) / PANGO_SCALE - start_pos);
576           cairo_paint (cr);
577           cairo_surface_destroy (surface);
578           g_object_unref(GDK_PIXBUF(attr->data));
579       } else {
580         pango_cairo_show_layout_line(cr, line);
581       }
582     }
583     ii++;
584   } while(ii < end && (notlast = pango_layout_iter_next_line(iter)));
585   pango_layout_iter_free(iter);
586   return TRUE;
587 }
588
589 static void cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
590                          int page_nr, gpointer user_data)
591 {
592   claws_draw_page(op, context, page_nr, user_data);
593   debug_print("Sent page %d to printer\n", page_nr+1);
594 }
595
596 static void printing_layout_set_text_attributes(PrintData *print_data, GtkPrintContext *context)
597 {
598   GtkTextIter iter;
599   PangoAttrList *attr_list;
600   PangoAttribute *attr;
601   GSList *open_attrs, *attr_walk;
602
603   attr_list = pango_attr_list_new();
604   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
605     gtk_text_buffer_get_start_iter(print_data->buffer, &iter);
606   } else {
607     gtk_text_buffer_get_iter_at_offset(print_data->buffer, &iter, print_data->sel_start);
608   }
609
610   open_attrs = NULL;
611   do {
612     gboolean fg_set, bg_set, under_set, strike_set;
613     GSList *tags, *tag_walk;
614     GtkTextTag *tag;
615     GdkColor *color;
616     PangoUnderline underline;
617     gboolean strikethrough;
618     GdkPixbuf *image;
619
620     if (prefs_common.print_imgs && (image = gtk_text_iter_get_pixbuf(&iter)) != NULL) {
621       PangoRectangle rect = {0, 0, 0, 0};
622       gint startpos = printing_text_iter_get_offset_bytes(print_data, &iter);
623       gint h = gdk_pixbuf_get_height(image);
624       gint w = gdk_pixbuf_get_width(image);
625       gint a_h = gtk_print_context_get_height(context);
626       gint a_w = gtk_print_context_get_width(context);
627       gint r_h, r_w;
628       GdkPixbuf *scaled = NULL;
629       image_viewer_get_resized_size(w, h, a_w, a_h, &r_w, &r_h);
630       rect.x = 0;
631       rect.y = 0;
632       rect.width = r_w * PANGO_SCALE;
633       rect.height = r_h * PANGO_SCALE;
634       
635       scaled = gdk_pixbuf_scale_simple(image, r_w, r_h, GDK_INTERP_BILINEAR);
636       attr = pango_attr_shape_new_with_data (&rect, &rect,
637                scaled, NULL, NULL);
638       attr->start_index = startpos;
639       attr->end_index = startpos+1;
640       pango_attr_list_insert(attr_list, attr);
641       g_hash_table_insert(print_data->images, GINT_TO_POINTER(startpos), attr);
642       print_data->img_cnt++;
643     }
644
645     if(gtk_text_iter_ends_tag(&iter, NULL)) {
646       PangoAttrColor *attr_color;
647       PangoAttrInt   *attr_int;
648
649       tags = gtk_text_iter_get_toggled_tags(&iter, FALSE);
650       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
651         gboolean found;
652
653         tag = GTK_TEXT_TAG(tag_walk->data);
654         g_object_get(G_OBJECT(tag),
655                      "background-set", &bg_set,
656                      "foreground-set", &fg_set,
657                      "underline-set",&under_set,
658                      "strikethrough-set", &strike_set,
659                      NULL);
660
661         if(fg_set) {
662           found = FALSE;
663           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
664             attr = (PangoAttribute*)attr_walk->data;
665             if(attr->klass->type == PANGO_ATTR_FOREGROUND) {
666               attr_color = (PangoAttrColor*) attr;
667               g_object_get(G_OBJECT(tag), "foreground_gdk", &color, NULL);
668               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
669                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
670                 pango_attr_list_insert(attr_list, attr);
671                 found = TRUE;
672                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
673                 break;
674               }
675             }
676           }
677           if(!found)
678             debug_print("Error generating attribute list.\n");
679         }
680
681         if(bg_set) {
682           found = FALSE;
683           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
684             attr = (PangoAttribute*)attr_walk->data;
685             if(attr->klass->type == PANGO_ATTR_BACKGROUND) {
686               attr_color = (PangoAttrColor*) attr;
687               g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
688               if(printing_is_pango_gdk_color_equal(&(attr_color->color), color)) {
689                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
690                 pango_attr_list_insert(attr_list, attr);
691                 found = TRUE;
692                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
693                 break;
694               }
695             }
696           }
697           if(!found)
698             debug_print("Error generating attribute list.\n");
699         }
700
701         if(under_set) {
702           found = FALSE;
703           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
704             attr = (PangoAttribute*)attr_walk->data;
705             if(attr->klass->type == PANGO_ATTR_UNDERLINE) {
706               attr_int = (PangoAttrInt*)attr;
707               g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
708               if(attr_int->value == underline) {
709                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
710                 pango_attr_list_insert(attr_list, attr);
711                 found = TRUE;
712                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
713                 break;
714               }
715             }
716           }
717           if(!found)
718             debug_print("Error generating attribute list.\n");
719         }
720
721         if(strike_set) {
722           found = FALSE;
723           for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
724             attr = (PangoAttribute*)attr_walk->data;
725             if(attr->klass->type == PANGO_ATTR_STRIKETHROUGH) {
726               attr_int = (PangoAttrInt*)attr;
727               g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
728               if(attr_int->value == strikethrough) {
729                 attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
730                 pango_attr_list_insert(attr_list, attr);
731                 found = TRUE;
732                 open_attrs = g_slist_delete_link(open_attrs, attr_walk);
733                 break;
734               }
735             }
736           }
737           if(!found)
738             debug_print("Error generating attribute list.\n");
739         }
740
741       }
742       g_slist_free(tags);
743     }
744
745     if(gtk_text_iter_begins_tag(&iter, NULL)) {
746       tags = gtk_text_iter_get_toggled_tags(&iter, TRUE);
747       for(tag_walk = tags; tag_walk != NULL; tag_walk = tag_walk->next) {
748         tag = GTK_TEXT_TAG(tag_walk->data);
749         g_object_get(G_OBJECT(tag),
750                      "background-set", &bg_set,
751                      "foreground-set", &fg_set,
752                      "underline-set", &under_set,
753                      "strikethrough-set", &strike_set,
754                      NULL);
755         if(fg_set) {
756           g_object_get(G_OBJECT(tag), "foreground-gdk", &color, NULL);
757           attr = pango_attr_foreground_new(color->red,color->green,color->blue);
758           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
759           open_attrs = g_slist_prepend(open_attrs, attr);
760         }
761         if(bg_set) {
762           g_object_get(G_OBJECT(tag), "background-gdk", &color, NULL);
763           attr = pango_attr_background_new(color->red,color->green,color->blue);
764           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
765           open_attrs = g_slist_prepend(open_attrs, attr);
766         }
767         if(under_set) {
768           g_object_get(G_OBJECT(tag), "underline", &underline, NULL);
769           attr = pango_attr_underline_new(underline);
770           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
771           open_attrs = g_slist_prepend(open_attrs, attr);         
772         }
773         if(strike_set) {
774           g_object_get(G_OBJECT(tag), "strikethrough", &strikethrough, NULL);
775           attr = pango_attr_strikethrough_new(strikethrough);
776           attr->start_index = printing_text_iter_get_offset_bytes(print_data, &iter);
777           open_attrs = g_slist_prepend(open_attrs, attr);         
778         }
779       }
780       g_slist_free(tags);
781     }
782     
783   } while(!gtk_text_iter_is_end(&iter) && gtk_text_iter_forward_to_tag_toggle(&iter, NULL));
784           
785   /* close all open attributes */
786   for(attr_walk = open_attrs; attr_walk != NULL; attr_walk = attr_walk->next) {
787     attr = (PangoAttribute*) attr_walk->data;
788     attr->end_index = printing_text_iter_get_offset_bytes(print_data, &iter);
789     pango_attr_list_insert(attr_list, attr);
790   }
791   g_slist_free(open_attrs);
792
793   pango_layout_set_attributes(print_data->layout, attr_list);
794   pango_attr_list_unref(attr_list);
795 }
796
797 static gboolean printing_is_pango_gdk_color_equal(PangoColor *p, GdkColor *g)
798 {
799   return ((p->red == g->red) && (p->green == g->green) && (p->blue == g->blue));
800 }
801
802 /* Pango has it's attribute in bytes, but GtkTextIter gets only an offset
803  * in characters, so here we're returning an offset in bytes. 
804  */
805 static gint printing_text_iter_get_offset_bytes(PrintData *print_data, const GtkTextIter *iter)
806 {
807   gint off_chars;
808   gint off_bytes;
809   gchar *text;
810   GtkTextIter start;
811
812   off_chars = gtk_text_iter_get_offset(iter);
813   if (print_data->sel_start < 0 || print_data->sel_end <= print_data->sel_start) {
814     gtk_text_buffer_get_start_iter(gtk_text_iter_get_buffer(iter), &start);
815   } else {
816     gtk_text_buffer_get_iter_at_offset(gtk_text_iter_get_buffer(iter), &start, print_data->sel_start);
817   }
818   text = gtk_text_iter_get_text(&start, iter);
819   off_bytes = strlen(text);
820   g_free(text);
821   return off_bytes;
822 }
823
824 #endif /* GTK+ >= 2.10.0 */