1 /*\r
2 * A quick hack to use the print output to write out a file. This\r
3 * then makes 'save as...' PDF.\r
4 *\r
5 * Authors:\r
6 * Ted Gould <ted@gould.cx>\r
7 * Ulf Erikson <ulferikson@users.sf.net>\r
8 *\r
9 * Copyright (C) 2004-2006 Authors\r
10 *\r
11 * Released under GNU GPL, read the file 'COPYING' for more information\r
12 */\r
13 \r
14 #ifdef HAVE_CONFIG_H\r
15 # include <config.h>\r
16 #endif\r
17 \r
18 #ifdef HAVE_CAIRO_PDF\r
19 \r
20 #include "cairo-pdf-out.h"\r
21 #include <print.h>\r
22 #include "extension/system.h"\r
23 #include "extension/print.h"\r
24 #include "extension/db.h"\r
25 #include "extension/output.h"\r
26 #include "display/nr-arena.h"\r
27 #include "display/nr-arena-item.h"\r
28 #include "sp-path.h"\r
29 \r
30 namespace Inkscape {\r
31 namespace Extension {\r
32 namespace Internal {\r
33 \r
34 bool\r
35 CairoPdfOutput::check (Inkscape::Extension::Extension * module)\r
36 {\r
37 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF))\r
38 return FALSE;\r
39 \r
40 return TRUE;\r
41 }\r
42 \r
43 \r
44 static unsigned int\r
45 pdf_print_document_to_file(SPDocument *doc, gchar const *filename)\r
46 {\r
47 Inkscape::Extension::Print *mod;\r
48 SPPrintContext context;\r
49 gchar const *oldconst;\r
50 gchar *oldoutput;\r
51 unsigned int ret;\r
52 \r
53 sp_document_ensure_up_to_date(doc);\r
54 \r
55 mod = Inkscape::Extension::get_print(SP_MODULE_KEY_PRINT_CAIRO_PDF);\r
56 oldconst = mod->get_param_string("destination");\r
57 oldoutput = g_strdup(oldconst);\r
58 mod->set_param_string("destination", (gchar *)filename);\r
59 \r
60 /* Start */\r
61 context.module = mod;\r
62 /* fixme: This has to go into module constructor somehow */\r
63 /* Create new arena */\r
64 mod->base = SP_ITEM(sp_document_root(doc));\r
65 mod->arena = NRArena::create();\r
66 mod->dkey = sp_item_display_key_new(1);\r
67 mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);\r
68 \r
69 /* Print document */\r
70 ret = mod->begin(doc);\r
71 if (ret) {\r
72 sp_item_invoke_print(mod->base, &context);\r
73 ret = mod->finish();\r
74 }\r
75 \r
76 /* Release arena */\r
77 sp_item_invoke_hide(mod->base, mod->dkey);\r
78 mod->base = NULL;\r
79 nr_arena_item_unref(mod->root);\r
80 mod->root = NULL;\r
81 nr_object_unref((NRObject *) mod->arena);\r
82 mod->arena = NULL;\r
83 /* end */\r
84 \r
85 mod->set_param_string("destination", oldoutput);\r
86 g_free(oldoutput);\r
87 \r
88 return ret;\r
89 }\r
90 \r
91 \r
92 /**\r
93 \brief This function calls the print system with the filename\r
94 \param mod unused\r
95 \param doc Document to be saved\r
96 \param uri Filename to save to (probably will end in .pdf)\r
97 \r
98 The most interesting thing that this function does is just attach\r
99 an '>' on the front of the filename. This is the syntax used to\r
100 tell the printing system to save to file.\r
101 */\r
102 void\r
103 CairoPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)\r
104 {\r
105 Inkscape::Extension::Extension * ext;\r
106 unsigned int ret;\r
107 \r
108 ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PDF);\r
109 if (ext == NULL)\r
110 return;\r
111 \r
112 gchar * final_name;\r
113 final_name = g_strdup_printf("> %s", uri);\r
114 ret = pdf_print_document_to_file(doc, final_name);\r
115 g_free(final_name);\r
116 \r
117 if (!ret)\r
118 throw Inkscape::Extension::Output::save_failed();\r
119 \r
120 return;\r
121 }\r
122 \r
123 /**\r
124 \brief A function allocate a copy of this function.\r
125 \r
126 This is the definition of PDF out. This function just\r
127 calls the extension system with the memory allocated XML that\r
128 describes the data.\r
129 */\r
130 void\r
131 CairoPdfOutput::init (void)\r
132 {\r
133 Inkscape::Extension::build_from_mem(\r
134 "<inkscape-extension>\n"\r
135 "<name>Cairo PDF Output</name>\n"\r
136 "<id>org.inkscape.output.pdf.cairo</id>\n"\r
137 "<output>\n"\r
138 "<extension>.pdf</extension>\n"\r
139 "<mimetype>application/pdf</mimetype>\n"\r
140 "<filetypename>Cairo PDF (*.pdf)</filetypename>\n"\r
141 "<filetypetooltip>PDF File</filetypetooltip>\n"\r
142 "</output>\n"\r
143 "</inkscape-extension>", new CairoPdfOutput());\r
144 \r
145 return;\r
146 }\r
147 \r
148 } } } /* namespace Inkscape, Extension, Implementation */\r
149 \r
150 #endif /* HAVE_CAIRO_PDF */\r