Code

8d062fa3ab35b5103fda41724c2a3b38ab0385bc
[inkscape.git] / src / extension / internal / cairo-ps-out.cpp
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         return TRUE;
51 }
53 static bool
54 ps_print_document_to_file(SPDocument *doc, gchar const *filename)
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);
67     
68     /* Create renderer and context */
69     renderer = new CairoRenderer();
70     ctx = renderer->createContext();
71     bool ret = ctx->setPsTarget(filename);
72     if(ret) {
73         /* Render document */
74         ret = renderer->setupDocument(ctx, doc);
75         if (ret) {
76             renderer->renderItem(ctx, base);
77             ret = ctx->finish();
78         }
79     }
80     renderer->destroyContext(ctx);
82     /* Release arena */
83     sp_item_invoke_hide(base, dkey);
84     nr_arena_item_unref(root);
85     nr_object_unref((NRObject *) arena);
86 /* end */
87     delete renderer;
89     return ret;
90 }
93 /**
94     \brief  This function calls the output module with the filename
95         \param  mod   unused
96         \param  doc   Document to be saved
97     \param  uri   Filename to save to (probably will end in .ps)
98 */
99 void
100 CairoPsOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
102     Inkscape::Extension::Extension * ext;
103     unsigned int ret;
105     ext = Inkscape::Extension::db.get(SP_MODULE_KEY_PRINT_CAIRO_PS);
106     if (ext == NULL)
107         return;
109         gchar * final_name;
110         final_name = g_strdup_printf("> %s", uri);
111         ret = ps_print_document_to_file(doc, final_name);
112         g_free(final_name);
113         
114         if (!ret)
115             throw Inkscape::Extension::Output::save_failed();
117         return;
121 /**
122         \brief   A function allocate a copy of this function.
124         This is the definition of Cairo PS out.  This function just
125         calls the extension system with the memory allocated XML that
126         describes the data.
127 */
128 void
129 CairoPsOutput::init (void)
131         Inkscape::Extension::build_from_mem(
132                 "<inkscape-extension>\n"
133                         "<name>Cairo PS Output</name>\n"
134                         "<id>org.inkscape.print.ps.cairo</id>\n"
135                         "<output>\n"
136                                 "<extension>.ps</extension>\n"
137                 "<mimetype>application/ps</mimetype>\n"
138                                 "<filetypename>Cairo PS (*.ps)</filetypename>\n"
139                                 "<filetypetooltip>PS File</filetypetooltip>\n"
140                         "</output>\n"
141                 "</inkscape-extension>", new CairoPsOutput());
143         return;
146 } } }  /* namespace Inkscape, Extension, Implementation */
148 #endif /* HAVE_CAIRO_PDF */