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)
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 gchar * final_name;
113 final_name = g_strdup_printf("> %s", uri);
114 ret = pdf_print_document_to_file(doc, final_name);
115 g_free(final_name);
117 if (!ret)
118 throw Inkscape::Extension::Output::save_failed();
120 return;
121 }
123 /**
124 \brief A function allocate a copy of this function.
126 This is the definition of PDF out. This function just
127 calls the extension system with the memory allocated XML that
128 describes the data.
129 */
130 void
131 CairoPdfOutput::init (void)
132 {
133 Inkscape::Extension::build_from_mem(
134 "<inkscape-extension>\n"
135 "<name>Cairo PDF Output</name>\n"
136 "<id>org.inkscape.output.pdf.cairo</id>\n"
137 "<output>\n"
138 "<extension>.pdf</extension>\n"
139 "<mimetype>application/pdf</mimetype>\n"
140 "<filetypename>Cairo PDF (*.pdf)</filetypename>\n"
141 "<filetypetooltip>PDF File</filetypetooltip>\n"
142 "</output>\n"
143 "</inkscape-extension>", new CairoPdfOutput());
145 return;
146 }
148 } } } /* namespace Inkscape, Extension, Implementation */
150 #endif /* HAVE_CAIRO_PDF */