Code

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