Code

Factor out 'create and apply' code for LPEs so that it can be called from everywhere
[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
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"
33 #ifdef WIN32
34 #include <windows.h>
35 #include <commdlg.h>
36 #include <cairo.h>
37 #include <cairo-win32.h>
41 static cairo_surface_t *
42 _cairo_win32_printing_surface_create (HDC hdc)
43 {
44     int x, y, x_dpi, y_dpi, x_off, y_off, depth;
45     XFORM xform;
46     cairo_surface_t *surface;
48     x = GetDeviceCaps (hdc, HORZRES);
49     y = GetDeviceCaps (hdc, VERTRES);
51     x_dpi = GetDeviceCaps (hdc, LOGPIXELSX);
52     y_dpi = GetDeviceCaps (hdc, LOGPIXELSY);
54     x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
55     y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
57     depth = GetDeviceCaps(hdc, BITSPIXEL);
59     SetGraphicsMode (hdc, GM_ADVANCED);
60     xform.eM11 = x_dpi/72.0;
61     xform.eM12 = 0;
62     xform.eM21 = 0;
63     xform.eM22 = y_dpi/72.0;
64     xform.eDx = -x_off;
65     xform.eDy = -y_off;
66     SetWorldTransform (hdc, &xform);
68     surface = cairo_win32_printing_surface_create (hdc);
69     
70     /**
71                 Read fallback dpi from device capabilities. Was a workaround for a bug patched
72                 in cairo 1.5.14. Without this, fallback defaults to 300dpi, which is quite acceptable.
73                 Going higher can cause spool size and memory problems.
74         */
75     // cairo_surface_set_fallback_resolution (surface, x_dpi, y_dpi);
77     return surface;
78 }
79 #endif
83 static void
84 draw_page (GtkPrintOperation */*operation*/,
85            GtkPrintContext   *context,
86            gint               /*page_nr*/,
87            gpointer           user_data)
88 {
89     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
90     //printf("%s %d\n",__FUNCTION__, page_nr);
92     if (junk->_tab->as_bitmap()) {
93         // Render as exported PNG
94         gdouble width = sp_document_width(junk->_doc);
95         gdouble height = sp_document_height(junk->_doc);
96         gdouble dpi = junk->_tab->bitmap_dpi();
97         std::string tmp_png;
98         std::string tmp_base = "inkscape-print-png-XXXXXX";
100         int tmp_fd;
101         if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
102             close(tmp_fd);
104             guint32 bgcolor = 0x00000000;
105             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
106             if (nv && nv->attribute("pagecolor"))
107                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
108             if (nv && nv->attribute("inkscape:pageopacity"))
109                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
111             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
112                 width, height,
113                 (unsigned long)(width * PT_PER_IN / PX_PER_IN),
114                 (unsigned long)(height * PT_PER_IN / PX_PER_IN),
115                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
117             // This doesn't seem to work:
118             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
119             //
120             // so we'll use a surface pattern blat instead...
121             //
122             // but the C++ interface isn't implemented in cairomm:
123             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
124             //
125             // so do it in C:
126             {
127                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
128                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
129                 // FIXME: why is the origin offset??
130                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
131             }
132             cairo_paint(gtk_print_context_get_cairo_context (context));
134             // Clean up
135             unlink (tmp_png.c_str());
136         }
137         else {
138             g_warning(_("Could not open temporary PNG for bitmap printing"));
139         }
140     }
141     else {
142         // Render as vectors
143         Inkscape::Extension::Internal::CairoRenderer renderer;
144         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
146         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
147         ctx->setTextToPath(false);
148         ctx->setFilterToBitmap(true);
149         ctx->setBitmapResolution(72);
151         cairo_t *cr = gtk_print_context_get_cairo_context (context);
152         cairo_surface_t *surface = cairo_get_target(cr);
155 /**
156         Call cairo_win32_printing_surface directly as a workaround until GTK uses this call.
157         When GTK uses cairo_win32_printing_surface this automatically reverts.
158 */
159 #ifdef WIN32
160         if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_WIN32) {
161         HDC dc = cairo_win32_surface_get_dc (surface);
162         surface = _cairo_win32_printing_surface_create (dc);
163         }
164 #endif
166         bool ret = ctx->setSurfaceTarget (surface, true);        if (ret) {
167             ret = renderer.setupDocument (ctx, junk->_doc);
168             if (ret) {
169                 renderer.renderItem(ctx, junk->_base);
170                 ret = ctx->finish();
171             }
172             else {
173                 g_warning(_("Could not set up Document"));
174             }
175         }
176         else {
177             g_warning(_("Failed to set CairoRenderContext"));
178         }
180         // Clean up
181         renderer.destroyContext(ctx);
182     }
186 static GObject*
187 create_custom_widget (GtkPrintOperation */*operation*/,
188                       gpointer           user_data)
190     //printf("%s\n",__FUNCTION__);
191     return G_OBJECT(user_data);
194 static void
195 begin_print (GtkPrintOperation *operation,
196              GtkPrintContext   */*context*/,
197              gpointer           /*user_data*/)
199     //printf("%s\n",__FUNCTION__);
200     gtk_print_operation_set_n_pages (operation, 1);
203 namespace Inkscape {
204 namespace UI {
205 namespace Dialog {
207 Print::Print(SPDocument *doc, SPItem *base) :
208     _doc (doc),
209     _base (base)
211     g_assert (_doc);
212     g_assert (_base);
214     _printop = gtk_print_operation_new ();
216     // set up dialog title, based on document name
217     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
218     Glib::ustring title = _("Print");
219     title += " ";
220     title += jobname;
221     gtk_print_operation_set_job_name (_printop, title.c_str());
223     // set up paper size to match the document size
224     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
225     GtkPageSetup *page_setup = gtk_page_setup_new();
226     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
227     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
228     GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
229                                 doc_width, doc_height, GTK_UNIT_POINTS);
230     gtk_page_setup_set_paper_size (page_setup, paper_size);
231 #ifndef WIN32
232     gtk_print_operation_set_default_page_setup (_printop, page_setup);
233 #endif
234     gtk_print_operation_set_use_full_page (_printop, TRUE);
236     // set up signals
237     _workaround._doc = _doc;
238     _workaround._base = _base;
239     _workaround._tab = &_tab;
240     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
241     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
242     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
244     // build custom preferences tab
245     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
248 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
250     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
251             parent_window.gobj(), NULL);
252     return Gtk::PRINT_OPERATION_RESULT_APPLY;
256 } // namespace Dialog
257 } // namespace UI
258 } // namespace Inkscape