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