Code

more on cairo ps/pdf options
[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)
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  = ext->get_param_bool("textToPath");
113     bool new_textToPath  = mod->get_param_bool("textToPath");
114     ext->set_param_bool("textToPath", new_textToPath);
116     bool old_blurToBitmap  = ext->get_param_bool("blurToBitmap");
117     bool new_blurToBitmap  = mod->get_param_bool("blurToBitmap");
118     ext->set_param_bool("blurToBitmap", new_blurToBitmap);
120         gchar * final_name;
121         final_name = g_strdup_printf("> %s", uri);
122         ret = pdf_print_document_to_file(doc, final_name, 0, new_textToPath, new_blurToBitmap);
123         g_free(final_name);
125         if (!ret)
126             throw Inkscape::Extension::Output::save_failed();
128         return;
131 #include "clear-n_.h"
132 /**
133         \brief   A function allocate a copy of this function.
135         This is the definition of PDF out.  This function just
136         calls the extension system with the memory allocated XML that
137         describes the data.
138 */
139 void
140 CairoPdfOutput::init (void)
142         Inkscape::Extension::build_from_mem(
143                 "<inkscape-extension>\n"
144                         "<name>Cairo PDF Output</name>\n"
145                         "<id>org.inkscape.output.pdf.cairo</id>\n"
146                         "<param name=\"PDFversion\" gui-text=\"" N_("Restrict to PDF version") "\" type=\"enum\" >\n"
147                                 "<item value='PDF14'>" N_("PDF 1.4") "</item>\n"
148             "</param>\n"
149                         "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">true</param>\n"
150                         "<param name=\"blurToBitmap\" gui-text=\"" N_("Convert blur effects to bitmaps") "\" type=\"boolean\">false</param>\n"
151                         "<output>\n"
152                                 "<extension>.pdf</extension>\n"
153                                 "<mimetype>application/pdf</mimetype>\n"
154                                 "<filetypename>PDF via Cairo (*.pdf)</filetypename>\n"
155                                 "<filetypetooltip>PDF File</filetypetooltip>\n"
156                         "</output>\n"
157                 "</inkscape-extension>", new CairoPdfOutput());
159         return;
162 } } }  /* namespace Inkscape, Extension, Implementation */
164 #endif /* HAVE_CAIRO_PDF */