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