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, NR::Matrix const &transform, float opacity)
31 {
32 NRMatrix const ntransform(transform);
33 return sp_print_bind(ctx, &ntransform, opacity);
34 }
36 unsigned int
37 sp_print_bind(SPPrintContext *ctx, NRMatrix 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, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
56 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
57 {
58 return ctx->module->fill(bpath, ctm, style, pbox, dbox, bbox);
59 }
61 unsigned int
62 sp_print_stroke(SPPrintContext *ctx, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
63 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
64 {
65 return ctx->module->stroke(bpath, 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 NRMatrix 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, NR::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 nr_arena_item_unref(mod->root);
117 mod->root = NULL;
118 nr_object_unref((NRObject *) mod->arena);
119 mod->arena = NULL;
120 }
122 return;
123 }
125 void
126 sp_print_document(Gtk::Window& parentWindow, SPDocument *doc)
127 {
128 sp_document_ensure_up_to_date(doc);
130 // Build arena
131 SPItem *base = SP_ITEM(sp_document_root(doc));
132 NRArena *arena = NRArena::create();
133 unsigned int dkey = sp_item_display_key_new(1);
134 NRArenaItem *root = sp_item_invoke_show(base, 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 sp_item_invoke_hide(base, dkey);
143 nr_arena_item_unref(root);
144 nr_object_unref((NRObject *) arena);
145 }
147 void
148 sp_print_document_to_file(SPDocument *doc, gchar const *filename)
149 {
150 Inkscape::Extension::Print *mod;
151 SPPrintContext context;
152 gchar const *oldconst;
153 gchar *oldoutput;
154 unsigned int ret;
156 sp_document_ensure_up_to_date(doc);
158 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_PS);
159 oldconst = mod->get_param_string("destination");
160 oldoutput = g_strdup(oldconst);
161 mod->set_param_string("destination", (gchar *)filename);
163 /* Start */
164 context.module = mod;
165 /* fixme: This has to go into module constructor somehow */
166 /* Create new arena */
167 mod->base = SP_ITEM(sp_document_root(doc));
168 mod->arena = NRArena::create();
169 mod->dkey = sp_item_display_key_new(1);
170 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
171 /* Print document */
172 ret = mod->begin(doc);
173 sp_item_invoke_print(mod->base, &context);
174 ret = mod->finish();
175 /* Release arena */
176 sp_item_invoke_hide(mod->base, mod->dkey);
177 mod->base = NULL;
178 nr_arena_item_unref(mod->root);
179 mod->root = NULL;
180 nr_object_unref((NRObject *) mod->arena);
181 mod->arena = NULL;
182 /* end */
184 mod->set_param_string("destination", oldoutput);
185 g_free(oldoutput);
187 return;
188 }
191 /*
192 Local Variables:
193 mode:c++
194 c-file-style:"stroustrup"
195 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
196 indent-tabs-mode:nil
197 fill-column:99
198 End:
199 */
200 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :