1 #define __SP_PRINT_C__
3 /** \file
4 * Frontend to printing
5 */
6 /*
7 * Author:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Kees Cook <kees@outflux.net>
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 sp_document_ensure_up_to_date(doc);
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(sp_document_root(doc));
106 mod->arena = NRArena::create();
107 mod->dkey = sp_item_display_key_new(1);
108 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
109 /* Print document */
110 ret = mod->begin(doc);
111 sp_item_invoke_print(mod->base, &context);
112 ret = mod->finish();
113 /* Release arena */
114 sp_item_invoke_hide(mod->base, 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 sp_document_ensure_up_to_date(doc);
129 // Build arena
130 SPItem *base = SP_ITEM(sp_document_root(doc));
131 NRArena *arena = NRArena::create();
132 unsigned int dkey = sp_item_display_key_new(1);
133 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
135 // Run print dialog
136 Inkscape::UI::Dialog::Print printop(doc,base);
137 Gtk::PrintOperationResult res = printop.run(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG, parentWindow);
138 (void)res; // TODO handle this
140 // Release arena
141 sp_item_invoke_hide(base, dkey);
142 nr_object_unref((NRObject *) arena);
143 }
145 void
146 sp_print_document_to_file(SPDocument *doc, gchar const *filename)
147 {
148 Inkscape::Extension::Print *mod;
149 SPPrintContext context;
150 gchar const *oldconst;
151 gchar *oldoutput;
152 unsigned int ret;
154 sp_document_ensure_up_to_date(doc);
156 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
157 oldconst = mod->get_param_string("destination");
158 oldoutput = g_strdup(oldconst);
159 mod->set_param_string("destination", (gchar *)filename);
161 /* Start */
162 context.module = mod;
163 /* fixme: This has to go into module constructor somehow */
164 /* Create new arena */
165 mod->base = SP_ITEM(sp_document_root(doc));
166 mod->arena = NRArena::create();
167 mod->dkey = sp_item_display_key_new(1);
168 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
169 /* Print document */
170 ret = mod->begin(doc);
171 sp_item_invoke_print(mod->base, &context);
172 ret = mod->finish();
173 /* Release arena */
174 sp_item_invoke_hide(mod->base, mod->dkey);
175 mod->base = NULL;
176 mod->root = NULL;
177 nr_object_unref((NRObject *) mod->arena);
178 mod->arena = NULL;
179 /* end */
181 mod->set_param_string("destination", oldoutput);
182 g_free(oldoutput);
184 return;
185 }
188 /*
189 Local Variables:
190 mode:c++
191 c-file-style:"stroustrup"
192 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
193 indent-tabs-mode:nil
194 fill-column:99
195 End:
196 */
197 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :