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);
107 gtk_print_backend_file_register_type (GTypeModule *module)
109 static const GTypeInfo print_backend_file_info =
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),
119 (GInstanceInitFunc) gtk_print_backend_file_init,
122 print_backend_file_type = g_type_module_register_type (module,
123 GTK_TYPE_PRINT_BACKEND,
124 "GtkPrintBackendFile",
125 &print_backend_file_info, 0);
129 pb_module_init (GTypeModule *module)
131 gtk_print_backend_file_register_type (module);
135 pb_module_exit (void)
140 G_MODULE_EXPORT GtkPrintBackend *
141 pb_module_create (void)
143 return gtk_print_backend_file_new ();
147 * GtkPrintBackendFile
150 gtk_print_backend_file_get_type (void)
152 return print_backend_file_type;
156 * gtk_print_backend_file_new:
158 * Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
159 * implements the #GtkPrintBackend interface with direct access to
160 * the filesystem using Unix/Linux API calls
162 * Return value: the new #GtkPrintBackendFile object
165 gtk_print_backend_file_new (void)
167 return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
171 gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
173 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
175 backend_parent_class = g_type_class_peek_parent (class);
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;
185 format_from_settings (GtkPrintSettings *settings)
190 if (settings == NULL)
193 value = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
197 for (i = 0; i < N_FORMATS; ++i)
198 if (strcmp (value, formats[i]) == 0)
200 g_assert (i < N_FORMATS);
202 return (OutputFormat) i;
206 filename_from_settings (GtkPrintSettings *settings)
215 uri = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI);
217 filename = g_filename_from_uri (uri, NULL, NULL);
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?
222 if (filename == NULL)
226 format = format_from_settings (settings);
228 filename = g_strdup_printf ("output.%s", formats[format]);
234 static cairo_status_t
235 _cairo_write (void *closure,
236 const unsigned char *data,
239 GIOChannel *io = (GIOChannel *)closure;
246 g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
250 g_io_channel_write_chars (io, data, length, &written, &error);
255 g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
257 g_error_free (error);
258 return CAIRO_STATUS_WRITE_ERROR;
262 g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
268 return CAIRO_STATUS_SUCCESS;
272 static cairo_surface_t *
273 file_printer_create_cairo_surface (GtkPrinter *printer,
274 GtkPrintSettings *settings,
277 GIOChannel *cache_io)
279 cairo_surface_t *surface;
282 format = format_from_settings (settings);
284 if (format == FORMAT_PDF)
285 surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
287 surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
289 /* TODO: DPI from settings object? */
290 cairo_surface_set_fallback_resolution (surface, 300, 300);
296 GtkPrintBackend *backend;
297 GtkPrintJobCompleteFunc callback;
299 GIOChannel *target_io;
301 GDestroyNotify dnotify;
305 file_print_cb (GtkPrintBackendFile *print_backend,
309 _PrintStreamData *ps = (_PrintStreamData *) user_data;
311 if (ps->target_io != NULL)
312 g_io_channel_unref (ps->target_io);
315 ps->callback (ps->job, ps->user_data, error);
318 ps->dnotify (ps->user_data);
320 gtk_print_job_set_status (ps->job,
321 (error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
324 g_object_unref (ps->job);
330 file_write (GIOChannel *source,
334 gchar buf[_STREAM_MAX_CHUNK_SIZE];
337 GIOStatus read_status;
338 _PrintStreamData *ps = (_PrintStreamData *) user_data;
343 g_io_channel_read_chars (source,
345 _STREAM_MAX_CHUNK_SIZE,
349 if (read_status != G_IO_STATUS_ERROR)
353 g_io_channel_write_chars (ps->target_io,
360 if (error != NULL || read_status == G_IO_STATUS_EOF)
362 file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
367 g_print ("FILE Backend: %s\n", error->message));
369 g_error_free (error);
376 g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
382 gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
385 GtkPrintJobCompleteFunc callback,
387 GDestroyNotify dnotify)
389 GError *internal_error = NULL;
391 _PrintStreamData *ps;
392 GtkPrintSettings *settings;
393 gchar *filename = NULL;
395 printer = gtk_print_job_get_printer (job);
396 settings = gtk_print_job_get_settings (job);
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;
405 internal_error = NULL;
406 filename = filename_from_settings (settings);
408 ps->target_io = g_io_channel_new_file (filename, "w", &internal_error);
412 if (internal_error == NULL)
413 g_io_channel_set_encoding (ps->target_io, NULL, &internal_error);
415 if (internal_error != NULL)
417 file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
420 g_error_free (internal_error);
424 g_io_add_watch (data_io,
425 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
426 (GIOFunc) file_write,
431 gtk_print_backend_file_init (GtkPrintBackendFile *backend)
435 printer = g_object_new (GTK_TYPE_PRINTER,
436 "name", _("Print to File"),
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);
445 gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
446 g_object_unref (printer);
448 gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
451 static GtkPrinterOptionSet *
452 file_printer_get_options (GtkPrinter *printer,
453 GtkPrintSettings *settings,
454 GtkPageSetup *page_setup,
455 GtkPrintCapabilities capabilities)
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];
468 format = format_from_settings (settings);
469 filename = filename_from_settings (settings);
471 set = gtk_printer_option_set_new ();
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);
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);
485 if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
487 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
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]);
495 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
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]);
506 current_format = format;
507 for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
509 supported_formats[n_formats] = formats[n_formats];
510 display_format_names[n_formats] = _(format_names[n_formats]);
516 option = gtk_printer_option_new ("output-file-format", _("_Output format"),
517 GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
518 option->group = g_strdup ("GtkPrintDialogExtension");
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);
526 g_object_unref (option);
533 file_printer_get_settings_from_options (GtkPrinter *printer,
534 GtkPrinterOptionSet *options,
535 GtkPrintSettings *settings)
537 GtkPrinterOption *option;
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);
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);
547 file_printer_prepare_for_print (GtkPrinter *printer,
548 GtkPrintJob *print_job,
549 GtkPrintSettings *settings,
550 GtkPageSetup *page_setup)
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;
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);
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);
567 scale = gtk_print_settings_get_scale (settings);
569 print_job->scale = scale/100.0;
571 print_job->page_set = gtk_print_settings_get_page_set (settings);
572 print_job->rotate_to_orientation = TRUE;