Code

apply printing w32 patch https://bugs.launchpad.net/inkscape/+bug/179988/comments/176
[inkscape.git] / src / ui / dialog / print.cpp
1 /** @file
2  * @brief Print dialog
3  */
4 /* Authors:
5  *   Kees Cook <kees@outflux.net>
6  *
7  * Copyright (C) 2007 Kees Cook
8  * Released under GNU GPL.  Read the file 'COPYING' for more information.
9  */
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 #ifdef WIN32
15 #include <io.h>
16 #endif
18 #include <gtkmm/stock.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"
24 #include "document.h"
26 #include "unit-constants.h"
27 #include "helper/png-write.h"
28 #include "svg/svg-color.h"
29 #include "io/sys.h"
33 static void
34 draw_page (GtkPrintOperation */*operation*/,
35            GtkPrintContext   *context,
36            gint               /*page_nr*/,
37            gpointer           user_data)
38 {
39     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
40     //printf("%s %d\n",__FUNCTION__, page_nr);
42     if (junk->_tab->as_bitmap()) {
43         // Render as exported PNG
44         gdouble width = sp_document_width(junk->_doc);
45         gdouble height = sp_document_height(junk->_doc);
46         gdouble dpi = junk->_tab->bitmap_dpi();
47         std::string tmp_png;
48         std::string tmp_base = "inkscape-print-png-XXXXXX";
50         int tmp_fd;
51         if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
52             close(tmp_fd);
54             guint32 bgcolor = 0x00000000;
55             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
56             if (nv && nv->attribute("pagecolor"))
57                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
58             if (nv && nv->attribute("inkscape:pageopacity"))
59                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
61             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
62                 width, height,
63                 (unsigned long)(width * dpi / PX_PER_IN),
64                 (unsigned long)(height * dpi / PX_PER_IN),
65                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
67             // This doesn't seem to work:
68             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
69             //
70             // so we'll use a surface pattern blat instead...
71             //
72             // but the C++ interface isn't implemented in cairomm:
73             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
74             //
75             // so do it in C:
76             {
77                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
78                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
79                 cairo_matrix_t m;
80                 cairo_get_matrix(cr, &m);
81                 cairo_scale(cr, PT_PER_IN / dpi, PT_PER_IN / dpi);
82                 // FIXME: why is the origin offset??
83                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
84                 cairo_paint(cr);
85                 cairo_set_matrix(cr, &m);
86             }
88             // Clean up
89             unlink (tmp_png.c_str());
90         }
91         else {
92             g_warning(_("Could not open temporary PNG for bitmap printing"));
93         }
94     }
95     else {
96         // Render as vectors
97         Inkscape::Extension::Internal::CairoRenderer renderer;
98         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
100         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
101         ctx->setTextToPath(false);
102         ctx->setFilterToBitmap(true);
103         ctx->setBitmapResolution(72);
105         cairo_t *cr = gtk_print_context_get_cairo_context (context);
106         cairo_surface_t *surface = cairo_get_target(cr);
107         cairo_matrix_t ctm;
108         cairo_get_matrix(cr, &ctm);
109         bool ret = ctx->setSurfaceTarget (surface, true, &ctm);
110         if (ret) {
111             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
112             if (ret) {
113                 renderer.renderItem(ctx, junk->_base);
114                 ret = ctx->finish();
115             }
116             else {
117                 g_warning(_("Could not set up Document"));
118             }
119         }
120         else {
121             g_warning(_("Failed to set CairoRenderContext"));
122         }
124         // Clean up
125         renderer.destroyContext(ctx);
126     }
130 static GObject*
131 create_custom_widget (GtkPrintOperation */*operation*/,
132                       gpointer           user_data)
134     //printf("%s\n",__FUNCTION__);
135     return G_OBJECT(user_data);
138 static void
139 begin_print (GtkPrintOperation *operation,
140              GtkPrintContext   */*context*/,
141              gpointer           /*user_data*/)
143     //printf("%s\n",__FUNCTION__);
144     gtk_print_operation_set_n_pages (operation, 1);
147 namespace Inkscape {
148 namespace UI {
149 namespace Dialog {
151 Print::Print(SPDocument *doc, SPItem *base) :
152     _doc (doc),
153     _base (base)
155     g_assert (_doc);
156     g_assert (_base);
158     _printop = gtk_print_operation_new ();
160     // set up dialog title, based on document name
161     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
162     Glib::ustring title = _("Print");
163     title += " ";
164     title += jobname;
165     gtk_print_operation_set_job_name (_printop, title.c_str());
167     // set up paper size to match the document size
168     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
169     GtkPageSetup *page_setup = gtk_page_setup_new();
170     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
171     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
172     GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
173                                 doc_width, doc_height, GTK_UNIT_POINTS);
174     gtk_page_setup_set_paper_size (page_setup, paper_size);
175 #ifndef WIN32
176     gtk_print_operation_set_default_page_setup (_printop, page_setup);
177 #endif
178     gtk_print_operation_set_use_full_page (_printop, TRUE);
180     // set up signals
181     _workaround._doc = _doc;
182     _workaround._base = _base;
183     _workaround._tab = &_tab;
184     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
185     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
186     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
188     // build custom preferences tab
189     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
192 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
194     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
195             parent_window.gobj(), NULL);
196     return Gtk::PRINT_OPERATION_RESULT_APPLY;
200 } // namespace Dialog
201 } // namespace UI
202 } // namespace Inkscape
204 /*
205   Local Variables:
206   mode:c++
207   c-file-style:"stroustrup"
208   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
209   indent-tabs-mode:nil
210   fill-column:99
211   End:
212 */
213 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :