1ca4b46d995dfc05f468c3a26c39b4dcef86e722
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"
30 namespace Inkscape {
31 namespace UI {
32 namespace Dialog {
34 void
35 Print::_draw_page (const Glib::RefPtr<Gtk::PrintContext> &context,
36 int page_nr)
37 {
38 if (_tab.as_bitmap()) {
39 // Render as exported PNG
40 gdouble width = sp_document_width(_doc);
41 gdouble height = sp_document_height(_doc);
42 gdouble dpi = _tab.bitmap_dpi();
43 std::string tmp_png;
44 std::string tmp_base = "inkscape-print-png-XXXXXX";
46 int tmp_fd;
47 if ( (tmp_fd = Glib::file_open_tmp (tmp_png, tmp_base)) >= 0) {
48 close(tmp_fd);
50 guint32 bgcolor = 0x00000000;
51 Inkscape::XML::Node *nv = sp_repr_lookup_name (_doc->rroot, "sodipodi:namedview");
52 if (nv && nv->attribute("pagecolor"))
53 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
54 if (nv && nv->attribute("inkscape:pageopacity"))
55 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
57 sp_export_png_file(_doc, tmp_png.c_str(), 0.0, 0.0,
58 width, height,
59 (unsigned long)width * dpi / PX_PER_IN,
60 (unsigned long)height * dpi / PX_PER_IN,
61 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
63 // This doesn't seem to work:
64 //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
65 //
66 // so we'll use a surface pattern blat instead...
67 //
68 // but the C++ interface isn't implemented in cairomm:
69 //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
70 //
71 // so do it in C:
72 {
73 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
74 cairo_t *cr = context->get_cairo_context ()->cobj();
75 // FIXME: why is the origin offset??
76 cairo_set_source_surface(cr, png->cobj(), -20.0, -20.0);
77 }
78 context->get_cairo_context ()->paint ();
80 // Clean up
81 unlink (tmp_png.c_str());
82 }
83 else {
84 g_warning(_("Could not open temporary PNG for bitmap printing"));
85 }
86 }
87 else {
88 // Render as vectors
89 Inkscape::Extension::Internal::CairoRenderer renderer;
90 Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
91 bool ret = ctx->setSurfaceTarget (context->get_cairo_context ()->get_target ()->cobj(), true);
92 if (ret) {
93 ret = renderer.setupDocument (ctx, _doc);
94 if (ret) {
95 renderer.renderItem(ctx, _base);
96 ret = ctx->finish();
97 }
98 else {
99 g_warning(_("Could not set up Document"));
100 }
101 }
102 else {
103 g_warning(_("Failed to set CairoRenderContext"));
104 }
106 // Clean up
107 renderer.destroyContext(ctx);
108 }
110 }
112 Gtk::Widget *
113 Print::_create_custom_widget ()
114 {
115 return &_tab;
116 }
118 void
119 Print::_custom_widget_apply (Gtk::Widget *widget)
120 {
121 g_warning (_("custom widget apply"));
122 }
124 Print::Print(SPDocument *doc, SPItem *base) :
125 _doc (doc),
126 _base (base),
127 _tab ()
128 {
129 g_assert (_doc);
130 g_assert (_base);
132 _printop = Gtk::PrintOperation::create();
134 // set up dialog title, based on document name
135 gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
136 Glib::ustring title = _("Print");
137 title += " ";
138 title += jobname;
139 _printop->set_job_name (title);
140 _printop->set_n_pages (1);
142 // set up paper size to match the document size
143 Glib::RefPtr<Gtk::PageSetup> page_setup = Gtk::PageSetup::create();
144 gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
145 gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
146 Gtk::PaperSize paper_size(Glib::ustring("custom"), Glib::ustring("custom"),
147 doc_width, doc_height, Gtk::UNIT_POINTS);
148 page_setup->set_paper_size (paper_size);
149 _printop->set_default_page_setup (page_setup);
151 // build custom preferences tab
152 _printop->set_custom_tab_label (Glib::ustring(_("Rendering")));
153 _printop->signal_create_custom_widget().connect(sigc::mem_fun(*this, &Print::_create_custom_widget));
154 // _printop->signal_custom_widget_apply().connect(sigc::mem_fun(*this, &Print::_custom_widget_apply));
156 // register actual page surface drawing callback
157 _printop->signal_draw_page().connect(sigc::mem_fun(*this, &Print::_draw_page));
159 }
161 Gtk::PrintOperationResult
162 Print::run(Gtk::PrintOperationAction action)
163 {
164 Gtk::PrintOperationResult res;
165 res = _printop->run (action);
166 return res;
167 }
170 } // namespace Dialog
171 } // namespace UI
172 } // namespace Inkscape