1 #define __SP_PRINT_C__
3 /** \file
4 * Frontend to printing
5 */
6 /*
7 * Author:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 *
10 * This code is in public domain
11 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
19 #include "sp-item.h"
20 #include "extension/print.h"
21 #include "extension/system.h"
22 #include "print.h"
24 #include <unit-constants.h>
26 #ifdef HAVE_GTK_UNIX_PRINT
27 # include <gtk/gtk.h>
28 # include <glibmm/i18n.h>
29 # include <gtk/gtkprintunixdialog.h>
30 # include <unistd.h> // close, unlink
31 # include <cstdio>
32 using std::fprintf;
33 #endif
35 #if 0
36 # include <extension/internal/ps.h>
38 # ifdef WIN32
39 # include <extension/internal/win32.h>
40 # endif
41 #endif
43 /* Identity typedef */
45 unsigned int sp_print_bind(SPPrintContext *ctx, NR::Matrix const &transform, float opacity)
46 {
47 NRMatrix const ntransform(transform);
48 return sp_print_bind(ctx, &ntransform, opacity);
49 }
51 unsigned int
52 sp_print_bind(SPPrintContext *ctx, NRMatrix const *transform, float opacity)
53 {
54 return ctx->module->bind(transform, opacity);
55 }
57 unsigned int
58 sp_print_release(SPPrintContext *ctx)
59 {
60 return ctx->module->release();
61 }
63 unsigned int
64 sp_print_comment(SPPrintContext *ctx, char const *comment)
65 {
66 return ctx->module->comment(comment);
67 }
69 unsigned int
70 sp_print_fill(SPPrintContext *ctx, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
71 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
72 {
73 return ctx->module->fill(bpath, ctm, style, pbox, dbox, bbox);
74 }
76 unsigned int
77 sp_print_stroke(SPPrintContext *ctx, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
78 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
79 {
80 return ctx->module->stroke(bpath, ctm, style, pbox, dbox, bbox);
81 }
83 unsigned int
84 sp_print_image_R8G8B8A8_N(SPPrintContext *ctx,
85 guchar *px, unsigned int w, unsigned int h, unsigned int rs,
86 NRMatrix const *transform, SPStyle const *style)
87 {
88 return ctx->module->image(px, w, h, rs, transform, style);
89 }
91 unsigned int sp_print_text(SPPrintContext *ctx, char const *text, NR::Point p,
92 SPStyle const *style)
93 {
94 return ctx->module->text(text, p, style);
95 }
97 #include "display/nr-arena.h"
98 #include "display/nr-arena-item.h"
100 /* UI */
102 void
103 sp_print_preview_document(SPDocument *doc)
104 {
105 Inkscape::Extension::Print *mod;
106 unsigned int ret;
108 sp_document_ensure_up_to_date(doc);
110 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_DEFAULT);
112 ret = mod->set_preview();
114 if (ret) {
115 SPPrintContext context;
116 context.module = mod;
118 /* fixme: This has to go into module constructor somehow */
119 /* Create new arena */
120 mod->base = SP_ITEM(sp_document_root(doc));
121 mod->arena = NRArena::create();
122 mod->dkey = sp_item_display_key_new(1);
123 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
124 /* Print document */
125 ret = mod->begin(doc);
126 sp_item_invoke_print(mod->base, &context);
127 ret = mod->finish();
128 /* Release arena */
129 sp_item_invoke_hide(mod->base, mod->dkey);
130 mod->base = NULL;
131 nr_arena_item_unref(mod->root);
132 mod->root = NULL;
133 nr_object_unref((NRObject *) mod->arena);
134 mod->arena = NULL;
135 }
137 return;
138 }
140 #ifdef HAVE_GTK_UNIX_PRINT
141 static void
142 unix_print_complete (GtkPrintJob */*print_job*/,
143 gpointer /*user_data*/,
144 GError *error)
145 {
146 fprintf(stderr,"print job finished: %s\n",error ? error->message : "no error");
147 }
149 static void
150 unix_print_dialog (const gchar * ps_file, const gchar * jobname, gdouble doc_width, gdouble doc_height)
151 {
152 Glib::ustring title = _("Print");
153 title += " ";
154 title += jobname;
155 GtkWidget* dlg = gtk_print_unix_dialog_new(title.c_str(), NULL);
157 // force output system to only handle our pre-generated PS output
158 gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG(dlg),
159 GTK_PRINT_CAPABILITY_GENERATE_PS);
161 /*
162 * It would be nice to merge the PrintPS::setup routine with a custom
163 * configuration dialog:
165 gtk_print_unix_dialog_add_custom_tab (GtkPrintUnixDialog *dialog,
166 GtkWidget *child,
167 GtkWidget *tab_label);
168 */
170 int const response = gtk_dialog_run(GTK_DIALOG(dlg));
172 if (response == GTK_RESPONSE_OK) {
173 GtkPrinter* printer = gtk_print_unix_dialog_get_selected_printer(GTK_PRINT_UNIX_DIALOG(dlg));
175 if (gtk_printer_accepts_ps (printer)) {
176 GtkPageSetup *page_setup = gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dlg));
178 //It's important to set the right paper size here, otherwise it will
179 //default to letter; if for example an A4 is printed as a letter, then
180 //part of it will be truncated even when printing on A4 paper
182 //TODO: let the user decide upon the paper size, by enabling
183 //the drop down widget in the printing dialog. For now, we'll simply
184 //take the document's dimensions and communicate these to the printer
185 //driver
187 GtkPaperSize *page_size = gtk_paper_size_new_custom("custom", "custom", doc_width, doc_height, GTK_UNIT_POINTS);
189 gtk_page_setup_set_paper_size (page_setup, page_size);
191 GtkPrintJob* job = gtk_print_job_new (jobname, printer,
192 gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dlg)),
193 page_setup);
195 GtkPaperSize* tmp = gtk_page_setup_get_paper_size(gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dlg)));
196 (void)tmp;
198 GError * error = NULL;
199 if ( gtk_print_job_set_source_file (job, ps_file, &error)) {
200 gtk_print_job_send (job, unix_print_complete, NULL, NULL);
201 }
202 else {
203 g_warning(_("Could not set print source: %s"),error ? error->message : _("unknown error"));
204 }
205 if (error) g_error_free(error);
206 }
207 else {
208 g_warning(_("Printer '%s' does not support PS output"), gtk_printer_get_name (printer));
209 }
210 }
211 else if (response == GTK_RESPONSE_APPLY) {
212 // since we didn't include the Preview capability,
213 // this should never happen.
214 g_warning(_("Print Preview not available"));
215 }
217 gtk_widget_destroy(dlg);
218 }
219 #endif // HAVE_GTK_UNIX_PRINT
222 void
223 sp_print_document(SPDocument *doc, unsigned int direct)
224 {
225 Inkscape::Extension::Print *mod;
226 unsigned int ret;
227 #ifdef HAVE_GTK_UNIX_PRINT
228 Glib::ustring tmpfile = "";
229 #endif
231 sp_document_ensure_up_to_date(doc);
233 if (direct) {
234 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
235 } else {
236 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_DEFAULT);
238 #ifdef HAVE_GTK_UNIX_PRINT
239 // unix print dialog reads from the exported tempfile
240 gchar *filename = NULL;
241 GError *error = NULL;
242 gint tmpfd = g_file_open_tmp("inkscape-ps-XXXXXX",
243 &filename,
244 &error);
245 if (tmpfd<0) {
246 g_warning(_("Failed to create tempfile for printing: %s"),
247 error ? error->message : _("unknown error"));
248 if (error) g_error_free(error);
249 return;
250 }
251 tmpfile = filename;
252 g_free(filename);
253 close(tmpfd);
255 Glib::ustring destination = ">" + tmpfile;
256 mod->set_param_string("destination", destination.c_str());
257 #endif
258 }
260 ret = mod->setup();
262 if (ret) {
263 SPPrintContext context;
264 context.module = mod;
266 /* fixme: This has to go into module constructor somehow */
267 /* Create new arena */
268 mod->base = SP_ITEM(sp_document_root(doc));
269 mod->arena = NRArena::create();
270 mod->dkey = sp_item_display_key_new(1);
271 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
272 /* Print document */
273 ret = mod->begin(doc);
274 sp_item_invoke_print(mod->base, &context);
275 ret = mod->finish();
276 /* Release arena */
277 sp_item_invoke_hide(mod->base, mod->dkey);
278 mod->base = NULL;
279 nr_arena_item_unref(mod->root);
280 mod->root = NULL;
281 nr_object_unref((NRObject *) mod->arena);
282 mod->arena = NULL;
284 #ifdef HAVE_GTK_UNIX_PRINT
285 // redirect output to new print dialog
287 // width and height in pt
288 gdouble width = sp_document_width(doc) * PT_PER_PX;
289 gdouble height = sp_document_height(doc) * PT_PER_PX;
291 unix_print_dialog(tmpfile.c_str(),doc->name ? doc->name : _("SVG Document"), width, height);
292 unlink(tmpfile.c_str());
293 // end redirected new print dialog
294 #endif
295 }
297 return;
298 }
300 void
301 sp_print_document_to_file(SPDocument *doc, gchar const *filename)
302 {
303 Inkscape::Extension::Print *mod;
304 SPPrintContext context;
305 gchar const *oldconst;
306 gchar *oldoutput;
307 unsigned int ret;
309 sp_document_ensure_up_to_date(doc);
311 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
312 oldconst = mod->get_param_string("destination");
313 oldoutput = g_strdup(oldconst);
314 mod->set_param_string("destination", (gchar *)filename);
316 /* Start */
317 context.module = mod;
318 /* fixme: This has to go into module constructor somehow */
319 /* Create new arena */
320 mod->base = SP_ITEM(sp_document_root(doc));
321 mod->arena = NRArena::create();
322 mod->dkey = sp_item_display_key_new(1);
323 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
324 /* Print document */
325 ret = mod->begin(doc);
326 sp_item_invoke_print(mod->base, &context);
327 ret = mod->finish();
328 /* Release arena */
329 sp_item_invoke_hide(mod->base, mod->dkey);
330 mod->base = NULL;
331 nr_arena_item_unref(mod->root);
332 mod->root = NULL;
333 nr_object_unref((NRObject *) mod->arena);
334 mod->arena = NULL;
335 /* end */
337 mod->set_param_string("destination", oldoutput);
338 g_free(oldoutput);
340 return;
341 }
344 /*
345 Local Variables:
346 mode:c++
347 c-file-style:"stroustrup"
348 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
349 indent-tabs-mode:nil
350 fill-column:99
351 End:
352 */
353 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :