Code

Improved version reporting. Add SVN revision and custom status to
[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 #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 * dpi / PX_PER_IN),
114                 (unsigned long)(height * dpi / 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                 cairo_matrix_t m;
130                 cairo_get_matrix(cr, &m);
131                 cairo_scale(cr, PT_PER_IN / dpi, PT_PER_IN / dpi);
132                 // FIXME: why is the origin offset??
133                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
134                 cairo_paint(cr);
135                 cairo_set_matrix(cr, &m);
136             }
138             // Clean up
139             unlink (tmp_png.c_str());
140         }
141         else {
142             g_warning(_("Could not open temporary PNG for bitmap printing"));
143         }
144     }
145     else {
146         // Render as vectors
147         Inkscape::Extension::Internal::CairoRenderer renderer;
148         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
150         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
151         ctx->setTextToPath(false);
152         ctx->setFilterToBitmap(true);
153         ctx->setBitmapResolution(72);
155         cairo_t *cr = gtk_print_context_get_cairo_context (context);
156         cairo_surface_t *surface = cairo_get_target(cr);
159 /**
160         Call cairo_win32_printing_surface directly as a workaround until GTK uses this call.
161         When GTK uses cairo_win32_printing_surface this automatically reverts.
162 */
163 #ifdef WIN32
164         if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_WIN32) {
165         HDC dc = cairo_win32_surface_get_dc (surface);
166         surface = _cairo_win32_printing_surface_create (dc);
167         }
168 #endif
170         bool ret = ctx->setSurfaceTarget (surface, true);        if (ret) {
171             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
172             if (ret) {
173                 renderer.renderItem(ctx, junk->_base);
174                 ret = ctx->finish();
175             }
176             else {
177                 g_warning(_("Could not set up Document"));
178             }
179         }
180         else {
181             g_warning(_("Failed to set CairoRenderContext"));
182         }
184         // Clean up
185         renderer.destroyContext(ctx);
186     }
190 static GObject*
191 create_custom_widget (GtkPrintOperation */*operation*/,
192                       gpointer           user_data)
194     //printf("%s\n",__FUNCTION__);
195     return G_OBJECT(user_data);
198 static void
199 begin_print (GtkPrintOperation *operation,
200              GtkPrintContext   */*context*/,
201              gpointer           /*user_data*/)
203     //printf("%s\n",__FUNCTION__);
204     gtk_print_operation_set_n_pages (operation, 1);
207 namespace Inkscape {
208 namespace UI {
209 namespace Dialog {
211 Print::Print(SPDocument *doc, SPItem *base) :
212     _doc (doc),
213     _base (base)
215     g_assert (_doc);
216     g_assert (_base);
218     _printop = gtk_print_operation_new ();
220     // set up dialog title, based on document name
221     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
222     Glib::ustring title = _("Print");
223     title += " ";
224     title += jobname;
225     gtk_print_operation_set_job_name (_printop, title.c_str());
227     // set up paper size to match the document size
228     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
229     GtkPageSetup *page_setup = gtk_page_setup_new();
230     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
231     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
232     GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
233                                 doc_width, doc_height, GTK_UNIT_POINTS);
234     gtk_page_setup_set_paper_size (page_setup, paper_size);
235 #ifndef WIN32
236     gtk_print_operation_set_default_page_setup (_printop, page_setup);
237 #endif
238     gtk_print_operation_set_use_full_page (_printop, TRUE);
240     // set up signals
241     _workaround._doc = _doc;
242     _workaround._base = _base;
243     _workaround._tab = &_tab;
244     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
245     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
246     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
248     // build custom preferences tab
249     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
252 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
254     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
255             parent_window.gobj(), NULL);
256     return Gtk::PRINT_OPERATION_RESULT_APPLY;
260 } // namespace Dialog
261 } // namespace UI
262 } // namespace Inkscape
264 /*
265   Local Variables:
266   mode:c++
267   c-file-style:"stroustrup"
268   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
269   indent-tabs-mode:nil
270   fill-column:99
271   End:
272 */
273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :