Code

A bunch of i18n-related issues fixed, ru.po slightly updated. Please do not forget...
[inkscape.git] / src / extension / internal / cairo-pdf-out.cpp
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)
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;
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)
169         Inkscape::Extension::build_from_mem(
170                 "<inkscape-extension>\n"
171                         "<name>" N_("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             "<param name=\"resolution\" gui-text=\"" N_("Preferred resolution (DPI) of bitmaps") "\" type=\"int\" min=\"72\" max=\"2400\">90</param>\n"
179             "<output>\n"
180                                 "<extension>.pdf</extension>\n"
181                                 "<mimetype>application/pdf</mimetype>\n"
182                                 "<filetypename>" N_("PDF via Cairo (*.pdf)") "</filetypename>\n"
183                                 "<filetypetooltip>" N_("PDF File") "</filetypetooltip>\n"
184                         "</output>\n"
185                 "</inkscape-extension>", new CairoPdfOutput());
187         return;
190 } } }  /* namespace Inkscape, Extension, Implementation */
192 #endif /* HAVE_CAIRO_PDF */