032592bf5b796ad71c51c53ebb35e923b4d7eeb1
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 <libnr/n-art-bpath.h>
34 #include "display/curve.h"
35 #include "display/canvas-bpath.h"
36 #include "sp-item.h"
37 #include "style.h"
38 #include "sp-root.h"
39 #include "sp-shape.h"
41 #include "io/sys.h"
43 namespace Inkscape {
44 namespace Extension {
45 namespace Internal {
47 bool
48 CairoPsOutput::check (Inkscape::Extension::Extension * module)
49 {
50 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PS))
51 return FALSE;
53 return TRUE;}
55 static bool
56 ps_print_document_to_file(SPDocument *doc, gchar const *filename, unsigned int level, bool texttopath, bool filtertobitmap)
57 {
58 CairoRenderer *renderer;
59 CairoRenderContext *ctx;
61 sp_document_ensure_up_to_date(doc);
63 /* Start */
64 /* Create new arena */
65 SPItem *base = SP_ITEM(sp_document_root(doc));
66 NRArena *arena = NRArena::create();
67 unsigned dkey = sp_item_display_key_new(1);
68 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
70 /* Create renderer and context */
71 renderer = new CairoRenderer();
72 ctx = renderer->createContext();
73 ctx->setPSLevel(level);
74 ctx->setTextToPath(texttopath);
75 ctx->setFilterToBitmap(filtertobitmap);
77 bool ret = ctx->setPsTarget(filename);
78 if(ret) {
79 /* Render document */
80 ret = renderer->setupDocument(ctx, doc);
81 if (ret) {
82 renderer->renderItem(ctx, base);
83 ret = ctx->finish();
84 }
85 }
86 renderer->destroyContext(ctx);
88 /* Release arena */
89 sp_item_invoke_hide(base, dkey);
90 nr_arena_item_unref(root);
91 nr_object_unref((NRObject *) arena);
92 /* end */
93 delete renderer;
95 return ret;
97 }
100 /**
101 \brief This function calls the output module with the filename
102 \param mod unused
103 \param doc Document to be saved
104 \param uri Filename to save to (probably will end in .ps)
105 */
106 void
107 CairoPsOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
108 {
109 Inkscape::Extension::Extension * ext;
110 unsigned int ret;
112 ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PS);
113 if (ext == NULL)
114 return;
116 const gchar *old_level = NULL;
117 const gchar *new_level = NULL;
118 int level = 1;
119 try {
120 old_level = ext->get_param_enum("PSlevel");
121 new_level = mod->get_param_enum("PSlevel");
122 if((new_level != NULL) && (g_ascii_strcasecmp("PS2", new_level) == 0))
123 level = 0;
124 // ext->set_param_enum("PSlevel", new_level);
125 }
126 catch(...) {
127 g_warning("Parameter <PSlevel> might not exists");
128 }
130 bool old_textToPath = FALSE;
131 bool new_textToPath = FALSE;
132 try {
133 old_textToPath = ext->get_param_bool("textToPath");
134 new_textToPath = mod->get_param_bool("textToPath");
135 ext->set_param_bool("textToPath", new_textToPath);
136 }
137 catch(...) {
138 g_warning("Parameter <textToPath> might not exists");
139 }
141 bool old_blurToBitmap = FALSE;
142 bool new_blurToBitmap = FALSE;
143 try {
144 old_blurToBitmap = ext->get_param_bool("blurToBitmap");
145 new_blurToBitmap = mod->get_param_bool("blurToBitmap");
146 ext->set_param_bool("blurToBitmap", new_blurToBitmap);
147 }
148 catch(...) {
149 g_warning("Parameter <blurToBitmap> might not exists");
150 }
152 gchar * final_name;
153 final_name = g_strdup_printf("> %s", uri);
154 ret = ps_print_document_to_file(doc, final_name, level, new_textToPath, new_blurToBitmap);
155 g_free(final_name);
157 try {
158 ext->set_param_bool("blurToBitmap", old_blurToBitmap);
159 }
160 catch(...) {
161 g_warning("Parameter <blurToBitmap> might not exists");
162 }
163 try {
164 ext->set_param_bool("textToPath", old_textToPath);
165 }
166 catch(...) {
167 g_warning("Parameter <textToPath> might not exists");
168 }
169 try {
170 // ext->set_param_enum("PSlevel", old_level);
171 }
172 catch(...) {
173 g_warning("Parameter <PSlevel> might not exists");
174 }
177 if (!ret)
178 throw Inkscape::Extension::Output::save_failed();
180 return;
182 }
184 bool
185 CairoPsOutput::textToPath(Inkscape::Extension::Print * ext)
186 {
187 return ext->get_param_bool("textToPath");
188 }
190 #include "clear-n_.h"
192 /**
193 \brief A function allocate a copy of this function.
195 This is the definition of Cairo PS out. This function just
196 calls the extension system with the memory allocated XML that
197 describes the data.
198 */
199 void
200 CairoPsOutput::init (void)
201 {
202 Inkscape::Extension::build_from_mem(
203 "<inkscape-extension>\n"
204 "<name>Cairo PS Output</name>\n"
205 "<id>" SP_MODULE_KEY_PRINT_CAIRO_PS "</id>\n"
206 "<param name=\"PSlevel\" gui-text=\"" N_("Restrict to PS level") "\" type=\"enum\" >\n"
207 "<item value='PS3'>" N_("PostScript 3") "</item>\n"
208 #if (CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 5, 2))
209 "<item value='PS2'>" N_("PostScript level 2") "</item>\n"
210 #endif
211 "</param>\n"
212 "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">false</param>\n"
213 "<param name=\"blurToBitmap\" gui-text=\"" N_("Convert blur effects to bitmaps") "\" type=\"boolean\">false</param>\n"
214 "<output>\n"
215 "<extension>.ps</extension>\n"
216 "<mimetype>application/ps</mimetype>\n"
217 "<filetypename>PostScript via Cairo (*.ps)</filetypename>\n"
218 "<filetypetooltip>PostScript File</filetypetooltip>\n"
219 "</output>\n"
220 "</inkscape-extension>", new CairoPsOutput());
222 return;
223 }
225 } } } /* namespace Inkscape, Extension, Implementation */
227 #endif /* HAVE_CAIRO_PDF */