1 /** \file
2 * Frontend to printing
3 */
4 /*
5 * Author:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * Kees Cook <kees@outflux.net>
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
10 *
11 * This code is in public domain
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include "inkscape.h"
19 #include "desktop.h"
20 #include "sp-item.h"
21 #include "extension/print.h"
22 #include "extension/system.h"
23 #include "print.h"
25 #include "ui/dialog/print.h"
28 /* Identity typedef */
30 unsigned int sp_print_bind(SPPrintContext *ctx, Geom::Matrix const &transform, float opacity)
31 {
32 Geom::Matrix const ntransform(transform);
33 return sp_print_bind(ctx, &ntransform, opacity);
34 }
36 unsigned int
37 sp_print_bind(SPPrintContext *ctx, Geom::Matrix const *transform, float opacity)
38 {
39 return ctx->module->bind(transform, opacity);
40 }
42 unsigned int
43 sp_print_release(SPPrintContext *ctx)
44 {
45 return ctx->module->release();
46 }
48 unsigned int
49 sp_print_comment(SPPrintContext *ctx, char const *comment)
50 {
51 return ctx->module->comment(comment);
52 }
54 unsigned int
55 sp_print_fill(SPPrintContext *ctx, Geom::PathVector const &pathv, Geom::Matrix const *ctm, SPStyle const *style,
56 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
57 {
58 return ctx->module->fill(pathv, ctm, style, pbox, dbox, bbox);
59 }
61 unsigned int
62 sp_print_stroke(SPPrintContext *ctx, Geom::PathVector const &pathv, Geom::Matrix const *ctm, SPStyle const *style,
63 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
64 {
65 return ctx->module->stroke(pathv, ctm, style, pbox, dbox, bbox);
66 }
68 unsigned int
69 sp_print_image_R8G8B8A8_N(SPPrintContext *ctx,
70 guchar *px, unsigned int w, unsigned int h, unsigned int rs,
71 Geom::Matrix const *transform, SPStyle const *style)
72 {
73 return ctx->module->image(px, w, h, rs, transform, style);
74 }
76 unsigned int sp_print_text(SPPrintContext *ctx, char const *text, Geom::Point p,
77 SPStyle const *style)
78 {
79 return ctx->module->text(text, p, style);
80 }
82 #include "display/nr-arena.h"
83 #include "display/nr-arena-item.h"
85 /* UI */
87 void
88 sp_print_preview_document(SPDocument *doc)
89 {
90 Inkscape::Extension::Print *mod;
91 unsigned int ret;
93 doc->ensureUpToDate();
95 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_DEFAULT);
97 ret = mod->set_preview();
99 if (ret) {
100 SPPrintContext context;
101 context.module = mod;
103 /* fixme: This has to go into module constructor somehow */
104 /* Create new arena */
105 mod->base = SP_ITEM(doc->getRoot());
106 mod->arena = NRArena::create();
107 mod->dkey = SPItem::display_key_new(1);
108 mod->root = (mod->base)->invoke_show(mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
109 /* Print document */
110 ret = mod->begin(doc);
111 (mod->base)->invoke_print(&context);
112 ret = mod->finish();
113 /* Release arena */
114 (mod->base)->invoke_hide(mod->dkey);
115 mod->base = NULL;
116 mod->root = NULL;
117 nr_object_unref((NRObject *) mod->arena);
118 mod->arena = NULL;
119 }
121 return;
122 }
124 void
125 sp_print_document(Gtk::Window& parentWindow, SPDocument *doc)
126 {
127 doc->ensureUpToDate();
129 // Build arena
130 SPItem *base = SP_ITEM(doc->getRoot());
131 NRArena *arena = NRArena::create();
132 unsigned int dkey = SPItem::display_key_new(1);
133 // TODO investigate why we are grabbing root and then ignoring it.
134 NRArenaItem *root = base->invoke_show(arena, dkey, SP_ITEM_SHOW_DISPLAY);
136 // Run print dialog
137 Inkscape::UI::Dialog::Print printop(doc,base);
138 Gtk::PrintOperationResult res = printop.run(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG, parentWindow);
139 (void)res; // TODO handle this
141 // Release arena
142 base->invoke_hide(dkey);
143 nr_object_unref((NRObject *) arena);
144 }
146 void
147 sp_print_document_to_file(SPDocument *doc, gchar const *filename)
148 {
149 Inkscape::Extension::Print *mod;
150 SPPrintContext context;
151 gchar const *oldconst;
152 gchar *oldoutput;
153 unsigned int ret;
155 doc->ensureUpToDate();
157 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
158 oldconst = mod->get_param_string("destination");
159 oldoutput = g_strdup(oldconst);
160 mod->set_param_string("destination", (gchar *)filename);
162 /* Start */
163 context.module = mod;
164 /* fixme: This has to go into module constructor somehow */
165 /* Create new arena */
166 mod->base = SP_ITEM(doc->getRoot());
167 mod->arena = NRArena::create();
168 mod->dkey = SPItem::display_key_new(1);
169 mod->root = (mod->base)->invoke_show(mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
170 /* Print document */
171 ret = mod->begin(doc);
172 (mod->base)->invoke_print(&context);
173 ret = mod->finish();
174 /* Release arena */
175 (mod->base)->invoke_hide(mod->dkey);
176 mod->base = NULL;
177 mod->root = NULL;
178 nr_object_unref((NRObject *) mod->arena);
179 mod->arena = NULL;
180 /* end */
182 mod->set_param_string("destination", oldoutput);
183 g_free(oldoutput);
185 return;
186 }
189 /*
190 Local Variables:
191 mode:c++
192 c-file-style:"stroustrup"
193 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
194 indent-tabs-mode:nil
195 fill-column:99
196 End:
197 */
198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :