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 mod->base = SP_ITEM(sp_document_root(doc));
65 mod->arena = NRArena::create();
66 mod->dkey = sp_item_display_key_new(1);
67 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
69 /* Print document */
70 ret = mod->begin(doc);
71 if (ret) {
72 sp_item_invoke_print(mod->base, &context);
73 ret = mod->finish();
74 }
76 /* Release arena */
77 sp_item_invoke_hide(mod->base, mod->dkey);
78 mod->base = NULL;
79 nr_arena_item_unref(mod->root);
80 mod->root = NULL;
81 nr_object_unref((NRObject *) mod->arena);
82 mod->arena = NULL;
83 /* end */
85 mod->set_param_string("destination", oldoutput);
86 g_free(oldoutput);
88 return ret;
89 }
92 /**
93 \brief This function calls the print system with the filename
94 \param mod unused
95 \param doc Document to be saved
96 \param uri Filename to save to (probably will end in .pdf)
98 The most interesting thing that this function does is just attach
99 an '>' on the front of the filename. This is the syntax used to
100 tell the printing system to save to file.
101 */
102 void
103 CairoPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
104 {
105 Inkscape::Extension::Extension * ext;
106 unsigned int ret;
108 ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF);
109 if (ext == NULL)
110 return;
112 bool old_textToPath = FALSE;
113 bool new_textToPath = FALSE;
114 try {
115 old_textToPath = ext->get_param_bool("textToPath");
116 new_textToPath = mod->get_param_bool("textToPath");
117 ext->set_param_bool("textToPath", new_textToPath);
118 }
119 catch(...) {
120 g_warning("Parameter <textToPath> might not exists");
121 }
123 bool old_blurToBitmap = FALSE;
124 bool new_blurToBitmap = FALSE;
125 try {
126 old_blurToBitmap = ext->get_param_bool("blurToBitmap");
127 new_blurToBitmap = mod->get_param_bool("blurToBitmap");
128 ext->set_param_bool("blurToBitmap", new_blurToBitmap);
129 }
130 catch(...) {
131 g_warning("Parameter <blurToBitmap> might not exists");
132 }
134 gchar * final_name;
135 final_name = g_strdup_printf("> %s", uri);
136 ret = pdf_print_document_to_file(doc, final_name, 0, new_textToPath, new_blurToBitmap);
137 g_free(final_name);
139 try {
140 ext->set_param_bool("blurToBitmap", old_blurToBitmap);
141 }
142 catch(...) {
143 g_warning("Parameter <blurToBitmap> might not exists");
144 }
145 try {
146 ext->set_param_bool("textToPath", old_textToPath);
147 }
148 catch(...) {
149 g_warning("Parameter <textToPath> might not exists");
150 }
152 if (!ret)
153 throw Inkscape::Extension::Output::save_failed();
155 return;
156 }
158 #include "clear-n_.h"
159 /**
160 \brief A function allocate a copy of this function.
162 This is the definition of PDF out. This function just
163 calls the extension system with the memory allocated XML that
164 describes the data.
165 */
166 void
167 CairoPdfOutput::init (void)
168 {
169 Inkscape::Extension::build_from_mem(
170 "<inkscape-extension>\n"
171 "<name>Cairo PDF Output</name>\n"
172 "<id>org.inkscape.output.pdf.cairo</id>\n"
173 "<param name=\"PDFversion\" gui-text=\"" N_("Restrict to PDF version") "\" type=\"enum\" >\n"
174 "<item value='PDF14'>" N_("PDF 1.4") "</item>\n"
175 "</param>\n"
176 "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">false</param>\n"
177 "<param name=\"blurToBitmap\" gui-text=\"" N_("Convert blur effects to bitmaps") "\" type=\"boolean\">false</param>\n"
178 "<output>\n"
179 "<extension>.pdf</extension>\n"
180 "<mimetype>application/pdf</mimetype>\n"
181 "<filetypename>PDF via Cairo (*.pdf)</filetypename>\n"
182 "<filetypetooltip>PDF File</filetypetooltip>\n"
183 "</output>\n"
184 "</inkscape-extension>", new CairoPdfOutput());
186 return;
187 }
189 } } } /* namespace Inkscape, Extension, Implementation */
191 #endif /* HAVE_CAIRO_PDF */