Code

marked forgotten translatable message
[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"
25 #include "unit-constants.h"
26 #include "helper/png-write.h"
27 #include "svg/svg-color.h"
28 #include "io/sys.h"
32 #ifdef WIN32
33 #include <windows.h>
34 #include <commdlg.h>
35 #include <cairo.h>
36 #include <cairo-win32.h>
40 static cairo_surface_t *
41 _cairo_win32_printing_surface_create (HDC hdc)
42 {
43     int x, y, x_dpi, y_dpi, x_off, y_off, depth;
44     XFORM xform;
45     cairo_surface_t *surface;
47     x = GetDeviceCaps (hdc, HORZRES);
48     y = GetDeviceCaps (hdc, VERTRES);
50     x_dpi = GetDeviceCaps (hdc, LOGPIXELSX);
51     y_dpi = GetDeviceCaps (hdc, LOGPIXELSY);
53     x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
54     y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
56     depth = GetDeviceCaps(hdc, BITSPIXEL);
58     SetGraphicsMode (hdc, GM_ADVANCED);
59     xform.eM11 = x_dpi/72.0;
60     xform.eM12 = 0;
61     xform.eM21 = 0;
62     xform.eM22 = y_dpi/72.0;
63     xform.eDx = -x_off;
64     xform.eDy = -y_off;
65     SetWorldTransform (hdc, &xform);
67     surface = cairo_win32_printing_surface_create (hdc);
68     
69     /**
70                 Read fallback dpi from device capabilities. Was a workaround for a bug patched
71                 in cairo 1.5.14. Without this, fallback defaults to 300dpi, which is quite acceptable.
72                 Going higher can cause spool size and memory problems.
73         */
74     // cairo_surface_set_fallback_resolution (surface, x_dpi, y_dpi);
76     return surface;
77 }
78 #endif
82 static void
83 draw_page (GtkPrintOperation */*operation*/,
84            GtkPrintContext   *context,
85            gint               /*page_nr*/,
86            gpointer           user_data)
87 {
88     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
89     //printf("%s %d\n",__FUNCTION__, page_nr);
91     if (junk->_tab->as_bitmap()) {
92         // Render as exported PNG
93         gdouble width = sp_document_width(junk->_doc);
94         gdouble height = sp_document_height(junk->_doc);
95         gdouble dpi = junk->_tab->bitmap_dpi();
96         std::string tmp_png;
97         std::string tmp_base = "inkscape-print-png-XXXXXX";
99         int tmp_fd;
100         if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
101             close(tmp_fd);
103             guint32 bgcolor = 0x00000000;
104             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
105             if (nv && nv->attribute("pagecolor"))
106                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
107             if (nv && nv->attribute("inkscape:pageopacity"))
108                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
110             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
111                 width, height,
112                 (unsigned long)(width * dpi / PX_PER_IN),
113                 (unsigned long)(height * dpi / PX_PER_IN),
114                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
116             // This doesn't seem to work:
117             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
118             //
119             // so we'll use a surface pattern blat instead...
120             //
121             // but the C++ interface isn't implemented in cairomm:
122             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
123             //
124             // so do it in C:
125             {
126                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
127                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
128                 cairo_matrix_t m;
129                 cairo_get_matrix(cr, &m);
130                 cairo_scale(cr, PT_PER_IN / dpi, PT_PER_IN / dpi);
131                 // FIXME: why is the origin offset??
132                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
133                 cairo_paint(cr);
134                 cairo_set_matrix(cr, &m);
135             }
137             // Clean up
138             unlink (tmp_png.c_str());
139         }
140         else {
141             g_warning(_("Could not open temporary PNG for bitmap printing"));
142         }
143     }
144     else {
145         // Render as vectors
146         Inkscape::Extension::Internal::CairoRenderer renderer;
147         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
149         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
150         ctx->setTextToPath(false);
151         ctx->setFilterToBitmap(true);
152         ctx->setBitmapResolution(72);
154         cairo_t *cr = gtk_print_context_get_cairo_context (context);
155         cairo_surface_t *surface = cairo_get_target(cr);
158 /**
159         Call cairo_win32_printing_surface directly as a workaround until GTK uses this call.
160         When GTK uses cairo_win32_printing_surface this automatically reverts.
161 */
162 #ifdef WIN32
163         if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_WIN32) {
164         HDC dc = cairo_win32_surface_get_dc (surface);
165         surface = _cairo_win32_printing_surface_create (dc);
166         }
167 #endif
169         bool ret = ctx->setSurfaceTarget (surface, true);        if (ret) {
170             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
171             if (ret) {
172                 renderer.renderItem(ctx, junk->_base);
173                 ret = ctx->finish();
174             }
175             else {
176                 g_warning(_("Could not set up Document"));
177             }
178         }
179         else {
180             g_warning(_("Failed to set CairoRenderContext"));
181         }
183         // Clean up
184         renderer.destroyContext(ctx);
185     }
189 static GObject*
190 create_custom_widget (GtkPrintOperation */*operation*/,
191                       gpointer           user_data)
193     //printf("%s\n",__FUNCTION__);
194     return G_OBJECT(user_data);
197 static void
198 begin_print (GtkPrintOperation *operation,
199              GtkPrintContext   */*context*/,
200              gpointer           /*user_data*/)
202     //printf("%s\n",__FUNCTION__);
203     gtk_print_operation_set_n_pages (operation, 1);
206 namespace Inkscape {
207 namespace UI {
208 namespace Dialog {
210 Print::Print(SPDocument *doc, SPItem *base) :
211     _doc (doc),
212     _base (base)
214     g_assert (_doc);
215     g_assert (_base);
217     _printop = gtk_print_operation_new ();
219     // set up dialog title, based on document name
220     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
221     Glib::ustring title = _("Print");
222     title += " ";
223     title += jobname;
224     gtk_print_operation_set_job_name (_printop, title.c_str());
226     // set up paper size to match the document size
227     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
228     GtkPageSetup *page_setup = gtk_page_setup_new();
229     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
230     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
231     GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
232                                 doc_width, doc_height, GTK_UNIT_POINTS);
233     gtk_page_setup_set_paper_size (page_setup, paper_size);
234 #ifndef WIN32
235     gtk_print_operation_set_default_page_setup (_printop, page_setup);
236 #endif
237     gtk_print_operation_set_use_full_page (_printop, TRUE);
239     // set up signals
240     _workaround._doc = _doc;
241     _workaround._base = _base;
242     _workaround._tab = &_tab;
243     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
244     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
245     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
247     // build custom preferences tab
248     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
251 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
253     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
254             parent_window.gobj(), NULL);
255     return Gtk::PRINT_OPERATION_RESULT_APPLY;
259 } // namespace Dialog
260 } // namespace UI
261 } // namespace Inkscape
263 /*
264   Local Variables:
265   mode:c++
266   c-file-style:"stroustrup"
267   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
268   indent-tabs-mode:nil
269   fill-column:99
270   End:
271 */
272 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :