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.
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.
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.
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.
25 #include <sys/types.h>
33 #include <cairo-pdf.h>
36 #include <glib/gi18n-lib.h>
38 #include "gtkprintoperation.h"
40 #include "gtkprintbackend.h"
41 #include "gtkprintbackendfile.h"
43 #include "gtkprinter.h"
44 #include "gtkprinter-private.h"
46 typedef struct _GtkPrintBackendFileClass GtkPrintBackendFileClass;
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))
52 #define _STREAM_MAX_CHUNK_SIZE 8192
54 static GType print_backend_file_type = 0;
56 struct _GtkPrintBackendFileClass
58 GtkPrintBackendClass parent_class;
61 struct _GtkPrintBackendFile
63 GtkPrintBackend parent_instance;
73 static const gchar* formats[N_FORMATS] =
79 static GObjectClass *backend_parent_class;
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,
97 GtkPrintJobCompleteFunc callback,
99 GDestroyNotify dnotify);
100 static cairo_surface_t * file_printer_create_cairo_surface (GtkPrinter *printer,
101 GtkPrintSettings *settings,
104 GIOChannel *cache_io);
106 static GList * file_printer_list_papers (GtkPrinter *printer);
109 gtk_print_backend_file_register_type (GTypeModule *module)
111 static const GTypeInfo print_backend_file_info =
113 sizeof (GtkPrintBackendFileClass),
114 NULL, /* base_init */
115 NULL, /* base_finalize */
116 (GClassInitFunc) gtk_print_backend_file_class_init,
117 NULL, /* class_finalize */
118 NULL, /* class_data */
119 sizeof (GtkPrintBackendFile),
121 (GInstanceInitFunc) gtk_print_backend_file_init,
124 print_backend_file_type = g_type_module_register_type (module,
125 GTK_TYPE_PRINT_BACKEND,
126 "GtkPrintBackendFile",
127 &print_backend_file_info, 0);
131 pb_module_init (GTypeModule *module)
133 gtk_print_backend_file_register_type (module);
137 pb_module_exit (void)
142 G_MODULE_EXPORT GtkPrintBackend *
143 pb_module_create (void)
145 return gtk_print_backend_file_new ();
149 * GtkPrintBackendFile
152 gtk_print_backend_file_get_type (void)
154 return print_backend_file_type;
158 * gtk_print_backend_file_new:
160 * Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
161 * implements the #GtkPrintBackend interface with direct access to
162 * the filesystem using Unix/Linux API calls
164 * Return value: the new #GtkPrintBackendFile object
167 gtk_print_backend_file_new (void)
169 return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
173 gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
175 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
177 backend_parent_class = g_type_class_peek_parent (class);
179 backend_class->print_stream = gtk_print_backend_file_print_stream;
180 backend_class->printer_create_cairo_surface = file_printer_create_cairo_surface;
181 backend_class->printer_get_options = file_printer_get_options;
182 backend_class->printer_get_settings_from_options = file_printer_get_settings_from_options;
183 backend_class->printer_prepare_for_print = file_printer_prepare_for_print;
184 backend_class->printer_list_papers = file_printer_list_papers;
187 /* return N_FORMATS if no explicit format in the settings */
189 format_from_settings (GtkPrintSettings *settings)
194 if (settings == NULL)
197 value = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
201 for (i = 0; i < N_FORMATS; ++i)
202 if (strcmp (value, formats[i]) == 0)
205 g_assert (i < N_FORMATS);
207 return (OutputFormat) i;
211 output_file_from_settings (GtkPrintSettings *settings,
212 const gchar *default_format)
217 uri = g_strdup (gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI));
221 const gchar *extension;
222 gchar *name, *locale_name, *path;
225 extension = default_format;
230 format = format_from_settings (settings);
231 extension = format == FORMAT_PS ? "ps" : "pdf";
234 /* default filename used for print-to-file */
235 name = g_strdup_printf (_("output.%s"), extension);
236 locale_name = g_filename_from_utf8 (name, -1, NULL, NULL, NULL);
239 if (locale_name != NULL)
241 path = g_build_filename (g_get_current_dir (), locale_name, NULL);
242 g_free (locale_name);
244 uri = g_filename_to_uri (path, NULL, NULL);
252 static cairo_status_t
253 _cairo_write (void *closure,
254 const unsigned char *data,
257 GIOChannel *io = (GIOChannel *)closure;
264 g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
268 g_io_channel_write_chars (io, (const gchar *) data, length, &written, &error);
273 g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
275 g_error_free (error);
276 return CAIRO_STATUS_WRITE_ERROR;
280 g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
286 return CAIRO_STATUS_SUCCESS;
290 static cairo_surface_t *
291 file_printer_create_cairo_surface (GtkPrinter *printer,
292 GtkPrintSettings *settings,
295 GIOChannel *cache_io)
297 cairo_surface_t *surface;
300 format = format_from_settings (settings);
302 if (format == FORMAT_PS)
303 surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
305 surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
307 /* TODO: DPI from settings object? */
308 cairo_surface_set_fallback_resolution (surface, 300, 300);
314 GtkPrintBackend *backend;
315 GtkPrintJobCompleteFunc callback;
317 GIOChannel *target_io;
319 GDestroyNotify dnotify;
323 file_print_cb (GtkPrintBackendFile *print_backend,
327 _PrintStreamData *ps = (_PrintStreamData *) user_data;
329 if (ps->target_io != NULL)
330 g_io_channel_unref (ps->target_io);
333 ps->callback (ps->job, ps->user_data, error);
336 ps->dnotify (ps->user_data);
338 gtk_print_job_set_status (ps->job,
339 (error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
342 g_object_unref (ps->job);
348 file_write (GIOChannel *source,
352 gchar buf[_STREAM_MAX_CHUNK_SIZE];
355 GIOStatus read_status;
356 _PrintStreamData *ps = (_PrintStreamData *) user_data;
361 g_io_channel_read_chars (source,
363 _STREAM_MAX_CHUNK_SIZE,
367 if (read_status != G_IO_STATUS_ERROR)
371 g_io_channel_write_chars (ps->target_io,
378 if (error != NULL || read_status == G_IO_STATUS_EOF)
380 file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
385 g_print ("FILE Backend: %s\n", error->message));
387 g_error_free (error);
394 g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
400 gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
403 GtkPrintJobCompleteFunc callback,
405 GDestroyNotify dnotify)
407 GError *internal_error = NULL;
409 _PrintStreamData *ps;
410 GtkPrintSettings *settings;
411 gchar *uri, *filename;
413 printer = gtk_print_job_get_printer (job);
414 settings = gtk_print_job_get_settings (job);
416 ps = g_new0 (_PrintStreamData, 1);
417 ps->callback = callback;
418 ps->user_data = user_data;
419 ps->dnotify = dnotify;
420 ps->job = g_object_ref (job);
421 ps->backend = print_backend;
423 internal_error = NULL;
424 uri = output_file_from_settings (settings, NULL);
425 filename = g_filename_from_uri (uri, NULL, &internal_error);
428 if (filename == NULL)
431 ps->target_io = g_io_channel_new_file (filename, "w", &internal_error);
435 if (internal_error == NULL)
436 g_io_channel_set_encoding (ps->target_io, NULL, &internal_error);
439 if (internal_error != NULL)
441 file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
444 g_error_free (internal_error);
448 g_io_add_watch (data_io,
449 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
450 (GIOFunc) file_write,
455 gtk_print_backend_file_init (GtkPrintBackendFile *backend)
459 printer = g_object_new (GTK_TYPE_PRINTER,
460 "name", _("Print to File"),
465 gtk_printer_set_has_details (printer, TRUE);
466 gtk_printer_set_icon_name (printer, "gtk-floppy");
467 gtk_printer_set_is_active (printer, TRUE);
469 gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
470 g_object_unref (printer);
472 gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
475 static GtkPrinterOptionSet *
476 file_printer_get_options (GtkPrinter *printer,
477 GtkPrintSettings *settings,
478 GtkPageSetup *page_setup,
479 GtkPrintCapabilities capabilities)
481 GtkPrinterOptionSet *set;
482 GtkPrinterOption *option;
483 const gchar *n_up[] = { "1" };
484 const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript") };
485 const gchar *supported_formats[N_FORMATS];
486 gchar *display_format_names[N_FORMATS];
490 gint current_format = 0;
492 format = format_from_settings (settings);
494 set = gtk_printer_option_set_new ();
496 option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE);
497 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
498 (char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */);
499 gtk_printer_option_set (option, "1");
500 gtk_printer_option_set_add (set, option);
501 g_object_unref (option);
503 if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
505 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
507 if (format == FORMAT_PDF || format == N_FORMATS)
510 current_format = n_formats;
512 supported_formats[n_formats] = formats[FORMAT_PDF];
513 display_format_names[n_formats] = _(format_names[FORMAT_PDF]);
516 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
518 if (format == FORMAT_PS || format == N_FORMATS)
519 current_format = n_formats;
520 supported_formats[n_formats] = formats[FORMAT_PS];
521 display_format_names[n_formats] = _(format_names[FORMAT_PS]);
527 current_format = format == FORMAT_PS ? FORMAT_PS : FORMAT_PDF;
528 for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
530 supported_formats[n_formats] = formats[n_formats];
531 display_format_names[n_formats] = _(format_names[n_formats]);
535 uri = output_file_from_settings (settings, supported_formats[current_format]);
537 option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"),
538 GTK_PRINTER_OPTION_TYPE_FILESAVE);
539 gtk_printer_option_set (option, uri);
541 option->group = g_strdup ("GtkPrintDialogExtension");
542 gtk_printer_option_set_add (set, option);
546 option = gtk_printer_option_new ("output-file-format", _("_Output format"),
547 GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
548 option->group = g_strdup ("GtkPrintDialogExtension");
550 gtk_printer_option_choices_from_array (option, n_formats,
551 (char **) supported_formats,
552 display_format_names);
553 gtk_printer_option_set (option, supported_formats[current_format]);
554 gtk_printer_option_set_add (set, option);
556 g_object_unref (option);
563 file_printer_get_settings_from_options (GtkPrinter *printer,
564 GtkPrinterOptionSet *options,
565 GtkPrintSettings *settings)
567 GtkPrinterOption *option;
569 option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input");
570 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value);
572 option = gtk_printer_option_set_lookup (options, "output-file-format");
574 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value);
579 file_printer_prepare_for_print (GtkPrinter *printer,
580 GtkPrintJob *print_job,
581 GtkPrintSettings *settings,
582 GtkPageSetup *page_setup)
586 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
587 print_job->page_ranges = NULL;
588 print_job->num_page_ranges = 0;
590 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
591 print_job->page_ranges =
592 gtk_print_settings_get_page_ranges (settings,
593 &print_job->num_page_ranges);
595 print_job->collate = gtk_print_settings_get_collate (settings);
596 print_job->reverse = gtk_print_settings_get_reverse (settings);
597 print_job->num_copies = gtk_print_settings_get_n_copies (settings);
599 scale = gtk_print_settings_get_scale (settings);
601 print_job->scale = scale/100.0;
603 print_job->page_set = gtk_print_settings_get_page_set (settings);
604 print_job->rotate_to_orientation = TRUE;
608 file_printer_list_papers (GtkPrinter *printer)
610 GList *result = NULL;
612 GtkPageSetup *page_setup;
614 papers = gtk_paper_size_get_paper_sizes (TRUE);
616 for (p = papers; p; p = p->next)
618 GtkPaperSize *paper_size = p->data;
620 page_setup = gtk_page_setup_new ();
621 gtk_page_setup_set_paper_size (page_setup, paper_size);
622 gtk_paper_size_free (paper_size);
623 result = g_list_prepend (result, page_setup);
626 g_list_free (papers);
628 return g_list_reverse (result);