Code

r13108@tres: ted | 2006-07-30 12:27:51 -0700
[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         gchar * final_name;\r
105         final_name = g_strdup_printf("> %s", uri);\r
106         pdf_print_document_to_file(doc, final_name);\r
107         g_free(final_name);\r
108 \r
109         return;\r
110 }\r
111 \r
112 /**\r
113         \brief   A function allocate a copy of this function.\r
114 \r
115         This is the definition of PDF out.  This function just\r
116         calls the extension system with the memory allocated XML that\r
117         describes the data.\r
118 */\r
119 void\r
120 PdfOutput::init (void)\r
121 {\r
122         Inkscape::Extension::build_from_mem(\r
123                 "<inkscape-extension>\n"\r
124                         "<name>PDF Output</name>\n"\r
125                         "<id>org.inkscape.output.pdf</id>\n"\r
126                         "<output>\n"\r
127                                 "<extension>.pdf</extension>\n"\r
128                                 "<mimetype>application/pdf</mimetype>\n"\r
129                                 "<filetypename>PDF (*.pdf)</filetypename>\n"\r
130                                 "<filetypetooltip>PDF File</filetypetooltip>\n"\r
131                         "</output>\n"\r
132                 "</inkscape-extension>", new PdfOutput());\r
133 \r
134         return;\r
135 }\r
136 \r
137 } } }  /* namespace Inkscape, Extension, Implementation */\r