735c3cf7a87b7ad31e98dedd6cbc7e96dcd4d31e
1 /*
2 * A quick hack to use the Cairo renderer to write out a file. This
3 * then makes 'save as...' PS.
4 *
5 * Authors:
6 * Ted Gould <ted@gould.cx>
7 * Ulf Erikson <ulferikson@users.sf.net>
8 * Adib Taraben <theAdib@yahoo.com>
9 *
10 * Copyright (C) 2004-2006 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #ifdef HAVE_CAIRO_PDF
21 #include "cairo-ps-out.h"
22 #include "cairo-render-context.h"
23 #include "cairo-renderer.h"
24 #include <print.h>
25 #include "extension/system.h"
26 #include "extension/print.h"
27 #include "extension/db.h"
28 #include "extension/output.h"
29 #include "display/nr-arena.h"
30 #include "display/nr-arena-item.h"
32 #include "display/curve.h"
33 #include "display/canvas-bpath.h"
34 #include "sp-item.h"
35 #include "style.h"
36 #include "sp-root.h"
37 #include "sp-shape.h"
39 #include "io/sys.h"
41 namespace Inkscape {
42 namespace Extension {
43 namespace Internal {
45 bool
46 CairoPsOutput::check (Inkscape::Extension::Extension * module)
47 {
48 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PS))
49 return FALSE;
51 return TRUE;}
53 static bool
54 ps_print_document_to_file(SPDocument *doc, gchar const *filename, unsigned int level, bool texttopath, bool filtertobitmap, int resolution)
55 {
56 CairoRenderer *renderer;
57 CairoRenderContext *ctx;
59 sp_document_ensure_up_to_date(doc);
61 /* Start */
62 /* Create new arena */
63 SPItem *base = SP_ITEM(sp_document_root(doc));
64 NRArena *arena = NRArena::create();
65 unsigned dkey = sp_item_display_key_new(1);
66 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
68 /* Create renderer and context */
69 renderer = new CairoRenderer();
70 ctx = renderer->createContext();
71 ctx->setPSLevel(level);
72 ctx->setTextToPath(texttopath);
73 ctx->setFilterToBitmap(filtertobitmap);
74 ctx->setBitmapResolution(resolution);
76 bool ret = ctx->setPsTarget(filename);
77 if(ret) {
78 /* Render document */
79 ret = renderer->setupDocument(ctx, doc);
80 if (ret) {
81 renderer->renderItem(ctx, base);
82 ret = ctx->finish();
83 }
84 }
85 renderer->destroyContext(ctx);
87 /* Release arena */
88 sp_item_invoke_hide(base, dkey);
89 nr_arena_item_unref(root);
90 nr_object_unref((NRObject *) arena);
91 /* end */
92 delete renderer;
94 return ret;
96 }
99 /**
100 \brief This function calls the output module with the filename
101 \param mod unused
102 \param doc Document to be saved
103 \param uri Filename to save to (probably will end in .ps)
104 */
105 void
106 CairoPsOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
107 {
108 Inkscape::Extension::Extension * ext;
109 unsigned int ret;
111 ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PS);
112 if (ext == NULL)
113 return;
115 const gchar *old_level = NULL;
116 const gchar *new_level = NULL;
117 int level = 1;
118 try {
119 old_level = ext->get_param_enum("PSlevel");
120 new_level = mod->get_param_enum("PSlevel");
121 if((new_level != NULL) && (g_ascii_strcasecmp("PS2", new_level) == 0))
122 level = 0;
123 // ext->set_param_enum("PSlevel", new_level);
124 }
125 catch(...) {
126 g_warning("Parameter <PSlevel> might not exists");
127 }
129 bool old_textToPath = FALSE;
130 bool new_textToPath = FALSE;
131 try {
132 old_textToPath = ext->get_param_bool("textToPath");
133 new_textToPath = mod->get_param_bool("textToPath");
134 ext->set_param_bool("textToPath", new_textToPath);
135 }
136 catch(...) {
137 g_warning("Parameter <textToPath> might not exists");
138 }
140 bool old_blurToBitmap = FALSE;
141 bool new_blurToBitmap = FALSE;
142 try {
143 old_blurToBitmap = ext->get_param_bool("blurToBitmap");
144 new_blurToBitmap = mod->get_param_bool("blurToBitmap");
145 ext->set_param_bool("blurToBitmap", new_blurToBitmap);
146 }
147 catch(...) {
148 g_warning("Parameter <blurToBitmap> might not exists");
149 }
151 int old_bitmapResolution = 72;
152 int new_bitmapResolution = 72;
153 try {
154 old_bitmapResolution = ext->get_param_int("resolution");
155 new_bitmapResolution = mod->get_param_int("resolution");
156 ext->set_param_int("resolution", new_bitmapResolution);
157 }
158 catch(...) {
159 g_warning("Parameter <resolution> might not exists");
160 }
162 gchar * final_name;
163 final_name = g_strdup_printf("> %s", uri);
164 ret = ps_print_document_to_file(doc, final_name, level, new_textToPath, new_blurToBitmap, new_bitmapResolution);
165 g_free(final_name);
167 try {
168 ext->set_param_int("resolution", old_bitmapResolution);
169 }
170 catch(...) {
171 g_warning("Parameter <resolution> might not exists");
172 }
173 try {
174 ext->set_param_bool("blurToBitmap", old_blurToBitmap);
175 }
176 catch(...) {
177 g_warning("Parameter <blurToBitmap> might not exists");
178 }
179 try {
180 ext->set_param_bool("textToPath", old_textToPath);
181 }
182 catch(...) {
183 g_warning("Parameter <textToPath> might not exists");
184 }
185 try {
186 // ext->set_param_enum("PSlevel", old_level);
187 }
188 catch(...) {
189 g_warning("Parameter <PSlevel> might not exists");
190 }
193 if (!ret)
194 throw Inkscape::Extension::Output::save_failed();
196 return;
198 }
200 bool
201 CairoPsOutput::textToPath(Inkscape::Extension::Print * ext)
202 {
203 return ext->get_param_bool("textToPath");
204 }
206 #include "clear-n_.h"
208 /**
209 \brief A function allocate a copy of this function.
211 This is the definition of Cairo PS out. This function just
212 calls the extension system with the memory allocated XML that
213 describes the data.
214 */
215 void
216 CairoPsOutput::init (void)
217 {
218 Inkscape::Extension::build_from_mem(
219 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
220 "<name>" N_("Cairo PS Output") "</name>\n"
221 "<id>" SP_MODULE_KEY_PRINT_CAIRO_PS "</id>\n"
222 "<param name=\"PSlevel\" gui-text=\"" N_("Restrict to PS level") "\" type=\"enum\" >\n"
223 "<_item value='PS3'>" N_("PostScript level 3") "</_item>\n"
224 #if (CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 5, 2))
225 "<_item value='PS2'>" N_("PostScript level 2") "</_item>\n"
226 #endif
227 "</param>\n"
228 "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">false</param>\n"
229 "<param name=\"blurToBitmap\" gui-text=\"" N_("Convert blur effects to bitmaps") "\" type=\"boolean\">false</param>\n"
230 "<param name=\"resolution\" gui-text=\"" N_("Preferred resolution (DPI) of bitmaps") "\" type=\"int\" min=\"72\" max=\"2400\">90</param>\n"
231 "<output>\n"
232 "<extension>.ps</extension>\n"
233 "<mimetype>application/ps</mimetype>\n"
234 "<filetypename>" N_("PostScript via Cairo (*.ps)") "</filetypename>\n"
235 "<filetypetooltip>" N_("PostScript File") "</filetypetooltip>\n"
236 "</output>\n"
237 "</inkscape-extension>", new CairoPsOutput());
239 return;
240 }
242 } } } /* namespace Inkscape, Extension, Implementation */
244 #endif /* HAVE_CAIRO_PDF */