1 /**
2 * \brief Print dialog
3 *
4 * Authors:
5 * Kees Cook <kees@outflux.net>
6 *
7 * Copyright (C) 2007 Kees Cook
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15 #ifdef WIN32
16 #include <io.h>
17 #endif
19 #include <gtkmm/stock.h>
20 #include "print.h"
22 #include "extension/internal/cairo-render-context.h"
23 #include "extension/internal/cairo-renderer.h"
24 #include "ui/widget/rendering-options.h"
26 #include "unit-constants.h"
27 #include "helper/png-write.h"
28 #include "svg/svg-color.h"
29 #include "io/sys.h"
32 static void
33 draw_page (GtkPrintOperation */*operation*/,
34 GtkPrintContext *context,
35 gint /*page_nr*/,
36 gpointer user_data)
37 {
38 struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
39 //printf("%s %d\n",__FUNCTION__, page_nr);
41 if (junk->_tab->as_bitmap()) {
42 // Render as exported PNG
43 gdouble width = sp_document_width(junk->_doc);
44 gdouble height = sp_document_height(junk->_doc);
45 gdouble dpi = junk->_tab->bitmap_dpi();
46 std::string tmp_png;
47 std::string tmp_base = "inkscape-print-png-XXXXXX";
49 int tmp_fd;
50 if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
51 close(tmp_fd);
53 guint32 bgcolor = 0x00000000;
54 Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
55 if (nv && nv->attribute("pagecolor"))
56 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
57 if (nv && nv->attribute("inkscape:pageopacity"))
58 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
60 sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
61 width, height,
62 (unsigned long)(width * PT_PER_IN / PX_PER_IN),
63 (unsigned long)(height * PT_PER_IN / PX_PER_IN),
64 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
66 // This doesn't seem to work:
67 //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
68 //
69 // so we'll use a surface pattern blat instead...
70 //
71 // but the C++ interface isn't implemented in cairomm:
72 //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
73 //
74 // so do it in C:
75 {
76 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
77 cairo_t *cr = gtk_print_context_get_cairo_context (context);
78 // FIXME: why is the origin offset??
79 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
80 }
81 cairo_paint(gtk_print_context_get_cairo_context (context));
83 // Clean up
84 unlink (tmp_png.c_str());
85 }
86 else {
87 g_warning(_("Could not open temporary PNG for bitmap printing"));
88 }
89 }
90 else {
91 // Render as vectors
92 Inkscape::Extension::Internal::CairoRenderer renderer;
93 Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
94 bool ret = ctx->setSurfaceTarget (cairo_get_target (gtk_print_context_get_cairo_context (context)), true);
95 if (ret) {
96 ret = renderer.setupDocument (ctx, junk->_doc);
97 if (ret) {
98 renderer.renderItem(ctx, junk->_base);
99 ret = ctx->finish();
100 }
101 else {
102 g_warning(_("Could not set up Document"));
103 }
104 }
105 else {
106 g_warning(_("Failed to set CairoRenderContext"));
107 }
109 // Clean up
110 renderer.destroyContext(ctx);
111 }
113 }
115 static GObject*
116 create_custom_widget (GtkPrintOperation */*operation*/,
117 gpointer user_data)
118 {
119 //printf("%s\n",__FUNCTION__);
120 return G_OBJECT(user_data);
121 }
123 static void
124 begin_print (GtkPrintOperation *operation,
125 GtkPrintContext */*context*/,
126 gpointer /*user_data*/)
127 {
128 //printf("%s\n",__FUNCTION__);
129 gtk_print_operation_set_n_pages (operation, 1);
130 }
132 namespace Inkscape {
133 namespace UI {
134 namespace Dialog {
136 Print::Print(SPDocument *doc, SPItem *base) :
137 _doc (doc),
138 _base (base)
139 {
140 g_assert (_doc);
141 g_assert (_base);
143 _printop = gtk_print_operation_new ();
145 // set up dialog title, based on document name
146 gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
147 Glib::ustring title = _("Print");
148 title += " ";
149 title += jobname;
150 gtk_print_operation_set_job_name (_printop, title.c_str());
152 // set up paper size to match the document size
153 GtkPageSetup *page_setup = gtk_page_setup_new();
154 gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
155 gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
156 GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
157 doc_width, doc_height, GTK_UNIT_POINTS);
158 gtk_page_setup_set_paper_size (page_setup, paper_size);
159 gtk_print_operation_set_default_page_setup (_printop, page_setup);
161 // set up signals
162 _workaround._doc = _doc;
163 _workaround._base = _base;
164 _workaround._tab = &_tab;
165 g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
166 g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
167 g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
169 // build custom preferences tab
170 gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
171 }
173 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
174 {
175 gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
176 parent_window.gobj(), NULL);
177 return Gtk::PRINT_OPERATION_RESULT_APPLY;
178 }
181 } // namespace Dialog
182 } // namespace UI
183 } // namespace Inkscape