Code

move %%EOF to end of output document
[inkscape.git] / src / extension / internal / pdf-out.cpp
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 void\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     /* Print document */\r
66     ret = mod->begin(doc);\r
67     sp_item_invoke_print(mod->base, &context);\r
68     ret = mod->finish();\r
69     /* Release arena */\r
70     sp_item_invoke_hide(mod->base, mod->dkey);\r
71     mod->base = NULL;\r
72     nr_arena_item_unref(mod->root);\r
73     mod->root = NULL;\r
74     nr_object_unref((NRObject *) mod->arena);\r
75     mod->arena = NULL;\r
76 /* end */\r
77 \r
78     mod->set_param_string("destination", oldoutput);\r
79     g_free(oldoutput);\r
80 \r
81     return;\r
82 }\r
83 \r
84 \r
85 /**\r
86     \brief  This function calls the print system with the filename\r
87         \param  mod   unused\r
88         \param  doc   Document to be saved\r
89     \param  uri   Filename to save to (probably will end in .pdf)\r
90 \r
91         The most interesting thing that this function does is just attach\r
92         an '>' on the front of the filename.  This is the syntax used to\r
93         tell the printing system to save to file.\r
94 */\r
95 void\r
96 PdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)\r
97 {\r
98     Inkscape::Extension::Extension * ext;\r
99 \r
100     ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_PDF);\r
101     if (ext == NULL)\r
102         return;\r
103 \r
104     bool old_textToPath  = ext->get_param_bool("textToPath");\r
105     bool new_val         = mod->get_param_bool("textToPath");\r
106     ext->set_param_bool("textToPath", new_val);\r
107 \r
108         gchar * final_name;\r
109         final_name = g_strdup_printf("> %s", uri);\r
110         pdf_print_document_to_file(doc, final_name);\r
111         g_free(final_name);\r
112 \r
113     ext->set_param_bool("textToPath", old_textToPath);\r
114 \r
115         return;\r
116 }\r
117 \r
118 /**\r
119         \brief   A function allocate a copy of this function.\r
120 \r
121         This is the definition of PDF out.  This function just\r
122         calls the extension system with the memory allocated XML that\r
123         describes the data.\r
124 */\r
125 void\r
126 PdfOutput::init (void)\r
127 {\r
128         Inkscape::Extension::build_from_mem(\r
129                 "<inkscape-extension>\n"\r
130                         "<name>PDF Output</name>\n"\r
131                         "<id>org.inkscape.output.pdf</id>\n"\r
132                         "<param name=\"textToPath\" gui-text=\"Text to Path\" type=\"boolean\">true</param>\n"\r
133                         "<output>\n"\r
134                                 "<extension>.pdf</extension>\n"\r
135                                 "<mimetype>application/pdf</mimetype>\n"\r
136                                 "<filetypename>PDF (*.pdf)</filetypename>\n"\r
137                                 "<filetypetooltip>PDF File</filetypetooltip>\n"\r
138                         "</output>\n"\r
139                 "</inkscape-extension>", new PdfOutput());\r
140 \r
141         return;\r
142 }\r
143 \r
144 } } }  /* namespace Inkscape, Extension, Implementation */\r