]> Pileus Git - ~andy/gtk/blob - modules/printbackends/file/gtkprintbackendfile.c
Apply a patch by Christian Persch to support PostScript and PDF output.
[~andy/gtk] / modules / printbackends / file / gtkprintbackendfile.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintbackendpdf.c: Default implementation of GtkPrintBackend 
3  * for printing to a file
4  * Copyright (C) 2003, Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <config.h>
23
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <errno.h>
32 #include <cairo.h>
33 #include <cairo-pdf.h>
34 #include <cairo-ps.h>
35
36 #include <glib/gi18n-lib.h>
37
38 #include "gtkprintoperation.h"
39
40 #include "gtkprintbackend.h"
41 #include "gtkprintbackendfile.h"
42
43 #include "gtkprinter.h"
44 #include "gtkprinter-private.h"
45
46 typedef struct _GtkPrintBackendFileClass GtkPrintBackendFileClass;
47
48 #define GTK_PRINT_BACKEND_FILE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
49 #define GTK_IS_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_FILE))
50 #define GTK_PRINT_BACKEND_FILE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
51
52 #define _STREAM_MAX_CHUNK_SIZE 8192
53
54 static GType print_backend_file_type = 0;
55
56 struct _GtkPrintBackendFileClass
57 {
58   GtkPrintBackendClass parent_class;
59 };
60
61 struct _GtkPrintBackendFile
62 {
63   GtkPrintBackend parent_instance;
64 };
65
66 typedef enum
67 {
68   FORMAT_PDF,
69   FORMAT_PS,
70   N_FORMATS
71 } OutputFormat;
72
73 static const gchar* formats[N_FORMATS] =
74 {
75   "pdf",
76   "ps"
77 };
78
79 static GObjectClass *backend_parent_class;
80
81 static void                 gtk_print_backend_file_class_init      (GtkPrintBackendFileClass *class);
82 static void                 gtk_print_backend_file_init            (GtkPrintBackendFile      *impl);
83 static void                 file_printer_get_settings_from_options (GtkPrinter              *printer,
84                                                                     GtkPrinterOptionSet     *options,
85                                                                     GtkPrintSettings        *settings);
86 static GtkPrinterOptionSet *file_printer_get_options               (GtkPrinter              *printer,
87                                                                     GtkPrintSettings        *settings,
88                                                                     GtkPageSetup            *page_setup,
89                                                                     GtkPrintCapabilities     capabilities);
90 static void                 file_printer_prepare_for_print         (GtkPrinter              *printer,
91                                                                     GtkPrintJob             *print_job,
92                                                                     GtkPrintSettings        *settings,
93                                                                     GtkPageSetup            *page_setup);
94 static void                 gtk_print_backend_file_print_stream    (GtkPrintBackend         *print_backend,
95                                                                     GtkPrintJob             *job,
96                                                                     GIOChannel              *data_io,
97                                                                     GtkPrintJobCompleteFunc  callback,
98                                                                     gpointer                 user_data,
99                                                                     GDestroyNotify           dnotify);
100 static cairo_surface_t *    file_printer_create_cairo_surface      (GtkPrinter              *printer,
101                                                                     GtkPrintSettings        *settings,
102                                                                     gdouble                  width,
103                                                                     gdouble                  height,
104                                                                     GIOChannel              *cache_io);
105
106 static void
107 gtk_print_backend_file_register_type (GTypeModule *module)
108 {
109   static const GTypeInfo print_backend_file_info =
110   {
111     sizeof (GtkPrintBackendFileClass),
112     NULL,               /* base_init */
113     NULL,               /* base_finalize */
114     (GClassInitFunc) gtk_print_backend_file_class_init,
115     NULL,               /* class_finalize */
116     NULL,               /* class_data */
117     sizeof (GtkPrintBackendFile),
118     0,          /* n_preallocs */
119     (GInstanceInitFunc) gtk_print_backend_file_init,
120   };
121
122   print_backend_file_type = g_type_module_register_type (module,
123                                                          GTK_TYPE_PRINT_BACKEND,
124                                                          "GtkPrintBackendFile",
125                                                          &print_backend_file_info, 0);
126 }
127
128 G_MODULE_EXPORT void 
129 pb_module_init (GTypeModule *module)
130 {
131   gtk_print_backend_file_register_type (module);
132 }
133
134 G_MODULE_EXPORT void 
135 pb_module_exit (void)
136 {
137
138 }
139   
140 G_MODULE_EXPORT GtkPrintBackend * 
141 pb_module_create (void)
142 {
143   return gtk_print_backend_file_new ();
144 }
145
146 /*
147  * GtkPrintBackendFile
148  */
149 GType
150 gtk_print_backend_file_get_type (void)
151 {
152   return print_backend_file_type;
153 }
154
155 /**
156  * gtk_print_backend_file_new:
157  *
158  * Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
159  * implements the #GtkPrintBackend interface with direct access to
160  * the filesystem using Unix/Linux API calls
161  *
162  * Return value: the new #GtkPrintBackendFile object
163  **/
164 GtkPrintBackend *
165 gtk_print_backend_file_new (void)
166 {
167   return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
168 }
169
170 static void
171 gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
172 {
173   GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
174
175   backend_parent_class = g_type_class_peek_parent (class);
176
177   backend_class->print_stream = gtk_print_backend_file_print_stream;
178   backend_class->printer_create_cairo_surface = file_printer_create_cairo_surface;
179   backend_class->printer_get_options = file_printer_get_options;
180   backend_class->printer_get_settings_from_options = file_printer_get_settings_from_options;
181   backend_class->printer_prepare_for_print = file_printer_prepare_for_print;
182 }
183
184 static OutputFormat
185 format_from_settings (GtkPrintSettings *settings)
186 {
187   const gchar *value;
188   gint i;
189
190   if (settings == NULL)
191     return FORMAT_PDF;
192
193   value = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
194   if (value == NULL)
195     return FORMAT_PDF;
196
197   for (i = 0; i < N_FORMATS; ++i)
198     if (strcmp (value, formats[i]) == 0)
199       break;
200   g_assert (i < N_FORMATS);
201
202   return (OutputFormat) i;
203 }
204
205 static gchar *
206 filename_from_settings (GtkPrintSettings *settings)
207 {
208   gchar *filename;
209
210   filename = NULL;
211   if (settings)
212     {
213       const gchar *uri;
214
215       uri = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI);
216       if (uri)
217         filename = g_filename_from_uri (uri, NULL, NULL);
218     }
219   /* FIXME: shouldn't we error out if we get an URI we cannot handle,
220    * rather than to print to some random file somewhere?
221    */
222   if (filename == NULL)
223     { 
224       OutputFormat format;
225
226       format = format_from_settings (settings);
227
228       filename = g_strdup_printf ("output.%s", formats[format]);
229     }
230
231   return filename;
232 }
233
234 static cairo_status_t
235 _cairo_write (void                *closure,
236               const unsigned char *data,
237               unsigned int         length)
238 {
239   GIOChannel *io = (GIOChannel *)closure;
240   gsize written;
241   GError *error;
242
243   error = NULL;
244
245   GTK_NOTE (PRINTING,
246             g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
247
248   while (length > 0) 
249     {
250       g_io_channel_write_chars (io, data, length, &written, &error);
251
252       if (error != NULL)
253         {
254           GTK_NOTE (PRINTING,
255                      g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
256
257           g_error_free (error);
258           return CAIRO_STATUS_WRITE_ERROR;
259         }    
260
261       GTK_NOTE (PRINTING,
262                 g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
263       
264       data += written;
265       length -= written;
266     }
267
268   return CAIRO_STATUS_SUCCESS;
269 }
270
271
272 static cairo_surface_t *
273 file_printer_create_cairo_surface (GtkPrinter       *printer,
274                                    GtkPrintSettings *settings,
275                                    gdouble           width, 
276                                    gdouble           height,
277                                    GIOChannel       *cache_io)
278 {
279   cairo_surface_t *surface;
280   OutputFormat format;
281
282   format = format_from_settings (settings);
283
284   if (format == FORMAT_PDF)
285     surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
286   else
287     surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
288
289   /* TODO: DPI from settings object? */
290   cairo_surface_set_fallback_resolution (surface, 300, 300);
291
292   return surface;
293 }
294
295 typedef struct {
296   GtkPrintBackend *backend;
297   GtkPrintJobCompleteFunc callback;
298   GtkPrintJob *job;
299   GIOChannel *target_io;
300   gpointer user_data;
301   GDestroyNotify dnotify;
302 } _PrintStreamData;
303
304 static void
305 file_print_cb (GtkPrintBackendFile *print_backend,
306                GError              *error,
307                gpointer            user_data)
308 {
309   _PrintStreamData *ps = (_PrintStreamData *) user_data;
310
311   if (ps->target_io != NULL)
312     g_io_channel_unref (ps->target_io);
313
314   if (ps->callback)
315     ps->callback (ps->job, ps->user_data, error);
316
317   if (ps->dnotify)
318     ps->dnotify (ps->user_data);
319
320   gtk_print_job_set_status (ps->job,
321                             (error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
322
323   if (ps->job)
324     g_object_unref (ps->job);
325  
326   g_free (ps);
327 }
328
329 static gboolean
330 file_write (GIOChannel   *source,
331             GIOCondition  con,
332             gpointer      user_data)
333 {
334   gchar buf[_STREAM_MAX_CHUNK_SIZE];
335   gsize bytes_read;
336   GError *error;
337   GIOStatus read_status;
338   _PrintStreamData *ps = (_PrintStreamData *) user_data;
339
340   error = NULL;
341
342   read_status = 
343     g_io_channel_read_chars (source,
344                              buf,
345                              _STREAM_MAX_CHUNK_SIZE,
346                              &bytes_read,
347                              &error);
348
349   if (read_status != G_IO_STATUS_ERROR)
350     {
351       gsize bytes_written;
352
353       g_io_channel_write_chars (ps->target_io, 
354                                 buf, 
355                                 bytes_read, 
356                                 &bytes_written, 
357                                 &error);
358     }
359
360   if (error != NULL || read_status == G_IO_STATUS_EOF)
361     {
362       file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
363
364       if (error != NULL)
365         {
366           GTK_NOTE (PRINTING,
367                     g_print ("FILE Backend: %s\n", error->message));
368
369           g_error_free (error);
370         }
371
372       return FALSE;
373     }
374
375   GTK_NOTE (PRINTING,
376             g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
377
378   return TRUE;
379 }
380
381 static void
382 gtk_print_backend_file_print_stream (GtkPrintBackend        *print_backend,
383                                      GtkPrintJob            *job,
384                                      GIOChannel             *data_io,
385                                      GtkPrintJobCompleteFunc callback,
386                                      gpointer                user_data,
387                                      GDestroyNotify          dnotify)
388 {
389   GError *internal_error = NULL;
390   GtkPrinter *printer;
391   _PrintStreamData *ps;
392   GtkPrintSettings *settings;
393   gchar *filename = NULL; 
394
395   printer = gtk_print_job_get_printer (job);
396   settings = gtk_print_job_get_settings (job);
397
398   ps = g_new0 (_PrintStreamData, 1);
399   ps->callback = callback;
400   ps->user_data = user_data;
401   ps->dnotify = dnotify;
402   ps->job = g_object_ref (job);
403   ps->backend = print_backend;
404
405   internal_error = NULL;
406   filename = filename_from_settings (settings);
407   
408   ps->target_io = g_io_channel_new_file (filename, "w", &internal_error);
409
410   g_free (filename);
411
412   if (internal_error == NULL)
413     g_io_channel_set_encoding (ps->target_io, NULL, &internal_error);
414
415   if (internal_error != NULL)
416     {
417       file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
418                     internal_error, ps);
419
420       g_error_free (internal_error);
421       return;
422     }
423
424   g_io_add_watch (data_io, 
425                   G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
426                   (GIOFunc) file_write,
427                   ps);
428 }
429
430 static void
431 gtk_print_backend_file_init (GtkPrintBackendFile *backend)
432 {
433   GtkPrinter *printer;
434   
435   printer = g_object_new (GTK_TYPE_PRINTER,
436                           "name", _("Print to File"),
437                           "backend", backend,
438                           "is-virtual", TRUE,
439                           NULL); 
440
441   gtk_printer_set_has_details (printer, TRUE);
442   gtk_printer_set_icon_name (printer, "gtk-floppy");
443   gtk_printer_set_is_active (printer, TRUE);
444
445   gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
446   g_object_unref (printer);
447
448   gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
449 }
450
451 static GtkPrinterOptionSet *
452 file_printer_get_options (GtkPrinter           *printer,
453                           GtkPrintSettings     *settings,
454                           GtkPageSetup         *page_setup,
455                           GtkPrintCapabilities  capabilities)
456 {
457   GtkPrinterOptionSet *set;
458   GtkPrinterOption *option;
459   const gchar *n_up[] = { "1" };
460   const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript") };
461   const gchar *supported_formats[N_FORMATS];
462   gchar *display_format_names[N_FORMATS];
463   gint n_formats = 0;
464   OutputFormat format;
465   gchar *filename;
466   gint current_format;
467
468   format = format_from_settings (settings);
469   filename = filename_from_settings (settings);
470
471   set = gtk_printer_option_set_new ();
472
473   option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE);
474   gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
475                                          (char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */);
476   gtk_printer_option_set (option, "1");
477   gtk_printer_option_set_add (set, option);
478   g_object_unref (option);
479
480   option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"), GTK_PRINTER_OPTION_TYPE_FILESAVE);
481   gtk_printer_option_set (option, filename);
482   option->group = g_strdup ("GtkPrintDialogExtension");
483   gtk_printer_option_set_add (set, option);
484
485   if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
486     {
487       if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
488         {
489           if (format == FORMAT_PDF)
490             current_format = n_formats;
491           supported_formats[n_formats] = formats[FORMAT_PDF];
492           display_format_names[n_formats] = _(format_names[FORMAT_PDF]);
493           n_formats++;
494         }
495       if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
496         {
497           if (format == FORMAT_PS)
498             current_format = n_formats;
499           supported_formats[n_formats] = formats[FORMAT_PS];
500           display_format_names[n_formats] = _(format_names[FORMAT_PS]);
501           n_formats++;
502         }
503     }
504   else
505     {
506       current_format = format;
507       for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
508         {
509           supported_formats[n_formats] = formats[n_formats];
510           display_format_names[n_formats] = _(format_names[n_formats]);
511         }
512     }
513
514   if (n_formats > 1)
515     {
516       option = gtk_printer_option_new ("output-file-format", _("_Output format"), 
517                                        GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
518       option->group = g_strdup ("GtkPrintDialogExtension");
519
520       gtk_printer_option_choices_from_array (option, n_formats,
521                                              (char **) supported_formats,
522                                              display_format_names);
523       gtk_printer_option_set (option, supported_formats[current_format]);
524       gtk_printer_option_set_add (set, option);
525       
526       g_object_unref (option);
527     }
528
529   return set;
530 }
531
532 static void
533 file_printer_get_settings_from_options (GtkPrinter          *printer,
534                                         GtkPrinterOptionSet *options,
535                                         GtkPrintSettings    *settings)
536 {
537   GtkPrinterOption *option;
538
539   option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input");
540   gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value);
541
542   option = gtk_printer_option_set_lookup (options, "output-file-format");
543   gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value);
544 }
545
546 static void
547 file_printer_prepare_for_print (GtkPrinter       *printer,
548                                 GtkPrintJob      *print_job,
549                                 GtkPrintSettings *settings,
550                                 GtkPageSetup     *page_setup)
551 {
552   gdouble scale;
553
554   print_job->print_pages = gtk_print_settings_get_print_pages (settings);
555   print_job->page_ranges = NULL;
556   print_job->num_page_ranges = 0;
557   
558   if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
559     print_job->page_ranges =
560       gtk_print_settings_get_page_ranges (settings,
561                                           &print_job->num_page_ranges);
562   
563   print_job->collate = gtk_print_settings_get_collate (settings);
564   print_job->reverse = gtk_print_settings_get_reverse (settings);
565   print_job->num_copies = gtk_print_settings_get_n_copies (settings);
566
567   scale = gtk_print_settings_get_scale (settings);
568   if (scale != 100.0)
569     print_job->scale = scale/100.0;
570
571   print_job->page_set = gtk_print_settings_get_page_set (settings);
572   print_job->rotate_to_orientation = TRUE;
573 }