93dad1e6436f9b1bc280d2a693a49007d21209b7
1 /*
2 * A quick hack to use the print output to write out a file. This
3 * then makes 'save as...' PDF.
4 *
5 * Authors:
6 * Ted Gould <ted@gould.cx>
7 * Ulf Erikson <ulferikson@users.sf.net>
8 *
9 * Copyright (C) 2004-2006 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #ifdef HAVE_CAIRO_PDF
20 #include "cairo-pdf-out.h"
21 #include <print.h>
22 #include "extension/system.h"
23 #include "extension/print.h"
24 #include "extension/db.h"
25 #include "extension/output.h"
26 #include "display/nr-arena.h"
27 #include "display/nr-arena-item.h"
28 #include "sp-path.h"
30 namespace Inkscape {
31 namespace Extension {
32 namespace Internal {
34 bool
35 CairoPdfOutput::check (Inkscape::Extension::Extension * module)
36 {
37 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF))
38 return FALSE;
40 return TRUE;
41 }
44 static unsigned int
45 pdf_print_document_to_file(SPDocument *doc, gchar const *filename, unsigned int pdf_level, bool texttopath, bool filtertobitmap)
46 {
47 Inkscape::Extension::Print *mod;
48 SPPrintContext context;
49 gchar const *oldconst;
50 gchar *oldoutput;
51 unsigned int ret;
53 sp_document_ensure_up_to_date(doc);
55 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_CAIRO_PDF);
56 oldconst = mod->get_param_string("destination");
57 oldoutput = g_strdup(oldconst);
58 mod->set_param_string("destination", (gchar *)filename);
60 /* Start */
61 context.module = mod;
62 /* fixme: This has to go into module constructor somehow */
63 /* Create new arena */
64 const gchar* exportId = mod->get_param_string("exportId");
65 bool exportDrawing = mod->get_param_bool("exportDrawing");
66 bool exportCanvas = mod->get_param_bool("exportCanvas");
67 if (exportId && strcmp(exportId, "")) {
68 // we want to export the given item only
69 mod->base = SP_ITEM(doc->getObjectById(exportId));
70 if (exportCanvas)
71 mod->set_param_bool("pageBoundingBox", TRUE);
72 else
73 mod->set_param_bool("pageBoundingBox", FALSE);
74 } else {
75 // we want to export the entire document from root
76 mod->base = SP_ITEM(sp_document_root(doc));
77 if (exportDrawing)
78 mod->set_param_bool("pageBoundingBox", FALSE);
79 else
80 mod->set_param_bool("pageBoundingBox", TRUE);
81 }
82 mod->arena = NRArena::create();
83 mod->dkey = sp_item_display_key_new(1);
84 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
86 /* Print document */
87 ret = mod->begin(doc);
88 if (ret) {
89 sp_item_invoke_print(mod->base, &context);
90 ret = mod->finish();
91 }
93 /* Release arena */
94 sp_item_invoke_hide(mod->base, mod->dkey);
95 mod->base = NULL;
96 nr_arena_item_unref(mod->root);
97 mod->root = NULL;
98 nr_object_unref((NRObject *) mod->arena);
99 mod->arena = NULL;
100 /* end */
102 mod->set_param_string("destination", oldoutput);
103 g_free(oldoutput);
105 return ret;
106 }
109 /**
110 \brief This function calls the print system with the filename
111 \param mod unused
112 \param doc Document to be saved
113 \param uri Filename to save to (probably will end in .pdf)
115 The most interesting thing that this function does is just attach
116 an '>' on the front of the filename. This is the syntax used to
117 tell the printing system to save to file.
118 */
119 void
120 CairoPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
121 {
122 Inkscape::Extension::Extension * ext;
123 unsigned int ret;
125 ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF);
126 if (ext == NULL)
127 return;
129 bool old_textToPath = FALSE;
130 bool new_textToPath = FALSE;
131 try {
132 old_textToPath = ext->get_param_bool("textToPath");
133 new_textToPath = mod->get_param_bool("textToPath");
134 ext->set_param_bool("textToPath", new_textToPath);
135 }
136 catch(...) {
137 g_warning("Parameter <textToPath> might not exist");
138 }
140 bool old_blurToBitmap = FALSE;
141 bool new_blurToBitmap = FALSE;
142 try {
143 old_blurToBitmap = ext->get_param_bool("blurToBitmap");
144 new_blurToBitmap = mod->get_param_bool("blurToBitmap");
145 ext->set_param_bool("blurToBitmap", new_blurToBitmap);
146 }
147 catch(...) {
148 g_warning("Parameter <blurToBitmap> might not exist");
149 }
151 const gchar* old_exportId = NULL;
152 const gchar* new_exportId = NULL;
153 try {
154 old_exportId = ext->get_param_string("exportId");
155 new_exportId = mod->get_param_string("exportId");
156 ext->set_param_string("exportId", new_exportId);
157 }
158 catch(...) {
159 g_warning("Parameter <exportId> might not exist");
160 }
162 bool old_exportDrawing = false;
163 bool new_exportDrawing = false;
164 try {
165 old_exportDrawing = ext->get_param_bool("exportDrawing");
166 new_exportDrawing = mod->get_param_bool("exportDrawing");
167 ext->set_param_bool("exportDrawing", new_exportDrawing);
168 }
169 catch(...) {
170 g_warning("Parameter <exportDrawing> might not exist");
171 }
173 bool old_exportCanvas = false;
174 bool new_exportCanvas = false;
175 try {
176 old_exportCanvas = ext->get_param_bool("exportCanvas");
177 new_exportCanvas = mod->get_param_bool("exportCanvas");
178 ext->set_param_bool("exportCanvas", new_exportCanvas);
179 }
180 catch(...) {
181 g_warning("Parameter <exportCanvas> might not exist");
182 }
184 gchar * final_name;
185 final_name = g_strdup_printf("> %s", uri);
186 ret = pdf_print_document_to_file(doc, final_name, 0, new_textToPath, new_blurToBitmap);
187 g_free(final_name);
189 try {
190 ext->set_param_bool("blurToBitmap", old_blurToBitmap);
191 }
192 catch(...) {
193 g_warning("Parameter <blurToBitmap> might not exist");
194 }
195 try {
196 ext->set_param_bool("textToPath", old_textToPath);
197 }
198 catch(...) {
199 g_warning("Parameter <textToPath> might not exist");
200 }
201 try {
202 ext->set_param_string("exportId", old_exportId);
203 }
204 catch(...) {
205 g_warning("Parameter <exportId> might not exist");
206 }
207 try {
208 ext->set_param_bool("exportDrawing", old_exportDrawing);
209 }
210 catch(...) {
211 g_warning("Parameter <exportDrawing> might not exist");
212 }
213 try {
214 ext->set_param_bool("exportCanvas", old_exportCanvas);
215 }
216 catch(...) {
217 g_warning("Parameter <exportCanvas> might not exist");
218 }
220 if (!ret)
221 throw Inkscape::Extension::Output::save_failed();
223 return;
224 }
226 #include "clear-n_.h"
227 /**
228 \brief A function allocate a copy of this function.
230 This is the definition of PDF out. This function just
231 calls the extension system with the memory allocated XML that
232 describes the data.
233 */
234 void
235 CairoPdfOutput::init (void)
236 {
237 Inkscape::Extension::build_from_mem(
238 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
239 "<name>" N_("Cairo PDF Output") "</name>\n"
240 "<id>org.inkscape.output.pdf.cairo</id>\n"
241 "<param name=\"PDFversion\" gui-text=\"" N_("Restrict to PDF version") "\" type=\"enum\" >\n"
242 "<_item value='PDF14'>" N_("PDF 1.4") "</_item>\n"
243 "</param>\n"
244 "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">false</param>\n"
245 "<param name=\"blurToBitmap\" gui-text=\"" N_("Convert blur effects to bitmaps") "\" type=\"boolean\">false</param>\n"
246 "<param name=\"resolution\" gui-text=\"" N_("Preferred resolution (DPI) of bitmaps") "\" type=\"int\" min=\"72\" max=\"2400\">90</param>\n"
247 "<param name=\"exportDrawing\" gui-text=\"" N_("Export drawing, not page") "\" type=\"boolean\">false</param>\n"
248 "<param name=\"exportCanvas\" gui-text=\"" N_("Export canvas") "\" type=\"boolean\">false</param>\n"
249 "<param name=\"exportId\" gui-text=\"" N_("Limit export to the object with ID") "\" type=\"string\"></param>\n"
250 "<output>\n"
251 "<extension>.pdf</extension>\n"
252 "<mimetype>application/pdf</mimetype>\n"
253 "<filetypename>" N_("PDF via Cairo (*.pdf)") "</filetypename>\n"
254 "<filetypetooltip>" N_("PDF File") "</filetypetooltip>\n"
255 "</output>\n"
256 "</inkscape-extension>", new CairoPdfOutput());
258 return;
259 }
261 } } } /* namespace Inkscape, Extension, Implementation */
263 #endif /* HAVE_CAIRO_PDF */
265 /*
266 Local Variables:
267 mode:c++
268 c-file-style:"stroustrup"
269 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
270 indent-tabs-mode:nil
271 fill-column:99
272 End:
273 */
274 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :