1 /*
2 * A quick hack to use the Cairo renderer to write out a file. This
3 * then makes 'save as...' PNG.
4 *
5 * Authors:
6 * Ted Gould <ted@gould.cx>
7 * Ulf Erikson <ulferikson@users.sf.net>
8 *
9 * Copyright (C) 2004-2006 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #ifdef HAVE_CAIRO_PDF
20 #include "cairo-png-out.h"
21 #include "cairo-render-context.h"
22 #include "cairo-renderer.h"
23 #include <print.h>
24 #include "extension/system.h"
25 #include "extension/print.h"
26 #include "extension/db.h"
27 #include "extension/output.h"
28 #include "display/nr-arena.h"
29 #include "display/nr-arena-item.h"
31 #include "display/curve.h"
32 #include "display/canvas-bpath.h"
33 #include "sp-item.h"
34 #include "style.h"
35 #include "sp-root.h"
36 #include "sp-shape.h"
38 #include "io/sys.h"
40 namespace Inkscape {
41 namespace Extension {
42 namespace Internal {
44 bool
45 CairoRendererOutput::check (Inkscape::Extension::Extension * module)
46 {
47 return TRUE;
48 }
50 static bool
51 png_render_document_to_file(SPDocument *doc, gchar const *filename)
52 {
53 CairoRenderer *renderer;
54 CairoRenderContext *ctx;
56 sp_document_ensure_up_to_date(doc);
58 /* Start */
59 /* Create new arena */
60 SPItem *base = SP_ITEM(sp_document_root(doc));
61 NRArena *arena = NRArena::create();
62 unsigned dkey = sp_item_display_key_new(1);
63 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
65 /* Create renderer and context */
66 renderer = new CairoRenderer();
67 ctx = renderer->createContext();
69 /* Render document */
70 bool ret = renderer->setupDocument(ctx, doc, TRUE, NULL);
71 if (ret) {
72 renderer->renderItem(ctx, base);
73 ctx->saveAsPng(filename);
74 ret = ctx->finish();
75 }
76 renderer->destroyContext(ctx);
78 /* Release arena */
79 sp_item_invoke_hide(base, dkey);
80 nr_object_unref((NRObject *) arena);
81 /* end */
82 delete renderer;
84 return ret;
85 }
88 /**
89 \brief This function calls the output module with the filename
90 \param mod unused
91 \param doc Document to be saved
92 \param uri Filename to save to (probably will end in .png)
93 */
94 void
95 CairoRendererOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *filename)
96 {
97 if (!png_render_document_to_file(doc, filename))
98 throw Inkscape::Extension::Output::save_failed();
100 return;
101 }
103 /**
104 \brief A function allocate a copy of this function.
106 This is the definition of Cairo PNG out. This function just
107 calls the extension system with the memory allocated XML that
108 describes the data.
109 */
110 void
111 CairoRendererOutput::init (void)
112 {
113 Inkscape::Extension::build_from_mem(
114 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
115 "<name>Cairo PNG Output</name>\n"
116 "<id>org.inkscape.output.png.cairo</id>\n"
117 "<output>\n"
118 "<extension>.png</extension>\n"
119 "<mimetype>image/png</mimetype>\n"
120 "<filetypename>Cairo PNG (*.png)</filetypename>\n"
121 "<filetypetooltip>PNG File</filetypetooltip>\n"
122 "</output>\n"
123 "</inkscape-extension>", new CairoRendererOutput());
125 return;
126 }
128 } } } /* namespace Inkscape, Extension, Implementation */
130 #endif /* HAVE_CAIRO_PDF */