Code

patch by Adrian Johnson for 166678
[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 #include <windows.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"
25 #include "document.h"
27 #include "unit-constants.h"
28 #include "helper/png-write.h"
29 #include "svg/svg-color.h"
30 #include "io/sys.h"
34 static void
35 draw_page (GtkPrintOperation *operation,
36            GtkPrintContext   *context,
37            gint               /*page_nr*/,
38            gpointer           user_data)
39 {
40     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
41     //printf("%s %d\n",__FUNCTION__, page_nr);
43     if (junk->_tab->as_bitmap()) {
44         // Render as exported PNG
45         gdouble width = sp_document_width(junk->_doc);
46         gdouble height = sp_document_height(junk->_doc);
47         gdouble dpi = junk->_tab->bitmap_dpi();
48         std::string tmp_png;
49         std::string tmp_base = "inkscape-print-png-XXXXXX";
51         int tmp_fd;
52         if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
53             close(tmp_fd);
55             guint32 bgcolor = 0x00000000;
56             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
57             if (nv && nv->attribute("pagecolor"))
58                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
59             if (nv && nv->attribute("inkscape:pageopacity"))
60                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
62             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
63                 width, height,
64                 (unsigned long)(width * dpi / PX_PER_IN),
65                 (unsigned long)(height * dpi / PX_PER_IN),
66                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
68             // This doesn't seem to work:
69             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
70             //
71             // so we'll use a surface pattern blat instead...
72             //
73             // but the C++ interface isn't implemented in cairomm:
74             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
75             //
76             // so do it in C:
77             {
78                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
79                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
80                 cairo_matrix_t m;
81                 cairo_get_matrix(cr, &m);
82                 cairo_scale(cr, PT_PER_IN / dpi, PT_PER_IN / dpi);
83                 // FIXME: why is the origin offset??
84                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
85                 cairo_paint(cr);
86                 cairo_set_matrix(cr, &m);
87             }
89             // Clean up
90             unlink (tmp_png.c_str());
91         }
92         else {
93             g_warning(_("Could not open temporary PNG for bitmap printing"));
94         }
95     }
96     else {
97         // Render as vectors
98         Inkscape::Extension::Internal::CairoRenderer renderer;
99         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
101         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
102         ctx->setTextToPath(false);
103         ctx->setFilterToBitmap(true);
104         ctx->setBitmapResolution(72);
106         cairo_t *cr = gtk_print_context_get_cairo_context (context);
107         cairo_surface_t *surface = cairo_get_target(cr);
108         cairo_matrix_t ctm;
109         cairo_get_matrix(cr, &ctm);
110 #ifdef WIN32
111         //Gtk+ does not take the non printable area into account
112         //http://bugzilla.gnome.org/show_bug.cgi?id=381371
113         //
114         // This workaround translates the origin from the top left of the
115         // printable area to the top left of the page.
116         GtkPrintSettings *settings = gtk_print_operation_get_print_settings(operation);
117         const gchar *printerName = gtk_print_settings_get_printer(settings);
118         HDC hdc = CreateDC("WINSPOOL", printerName, NULL, NULL);
119         if (hdc) {
120             cairo_matrix_t mat;
121             int x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
122             int y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
123             cairo_matrix_init_translate(&mat, -x_off, -y_off);
124             cairo_matrix_multiply (&ctm, &ctm, &mat);
125             DeleteDC(hdc);
126         }
127 #endif             
128         bool ret = ctx->setSurfaceTarget (surface, true, &ctm);
129         if (ret) {
130             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
131             if (ret) {
132                 renderer.renderItem(ctx, junk->_base);
133                 ret = ctx->finish();
134             }
135             else {
136                 g_warning(_("Could not set up Document"));
137             }
138         }
139         else {
140             g_warning(_("Failed to set CairoRenderContext"));
141         }
143         // Clean up
144         renderer.destroyContext(ctx);
145     }
149 static GObject*
150 create_custom_widget (GtkPrintOperation */*operation*/,
151                       gpointer           user_data)
153     //printf("%s\n",__FUNCTION__);
154     return G_OBJECT(user_data);
157 static void
158 begin_print (GtkPrintOperation *operation,
159              GtkPrintContext   */*context*/,
160              gpointer           /*user_data*/)
162     //printf("%s\n",__FUNCTION__);
163     gtk_print_operation_set_n_pages (operation, 1);
166 namespace Inkscape {
167 namespace UI {
168 namespace Dialog {
170 Print::Print(SPDocument *doc, SPItem *base) :
171     _doc (doc),
172     _base (base)
174     g_assert (_doc);
175     g_assert (_base);
177     _printop = gtk_print_operation_new ();
179     // set up dialog title, based on document name
180     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
181     Glib::ustring title = _("Print");
182     title += " ";
183     title += jobname;
184     gtk_print_operation_set_job_name (_printop, title.c_str());
186     // set up paper size to match the document size
187     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
188     GtkPageSetup *page_setup = gtk_page_setup_new();
189     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
190     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
191     GtkPaperSize *paper_size;
192     if (doc_width > doc_height) {
193         gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
194         paper_size = gtk_paper_size_new_custom("custom", "custom",
195                                                doc_height, doc_width, GTK_UNIT_POINTS);
196     } else {
197         gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT);
198         paper_size = gtk_paper_size_new_custom("custom", "custom",
199                                                doc_width, doc_height, GTK_UNIT_POINTS);
200     }
202     gtk_page_setup_set_paper_size (page_setup, paper_size);
203     gtk_print_operation_set_default_page_setup (_printop, page_setup);
204     gtk_print_operation_set_use_full_page (_printop, TRUE);
206     // set up signals
207     _workaround._doc = _doc;
208     _workaround._base = _base;
209     _workaround._tab = &_tab;
210     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
211     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
212     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
214     // build custom preferences tab
215     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
218 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
220     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
221             parent_window.gobj(), NULL);
222     return Gtk::PRINT_OPERATION_RESULT_APPLY;
226 } // namespace Dialog
227 } // namespace UI
228 } // namespace Inkscape
230 /*
231   Local Variables:
232   mode:c++
233   c-file-style:"stroustrup"
234   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
235   indent-tabs-mode:nil
236   fill-column:99
237   End:
238 */
239 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :