Code

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