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_arena_item_unref(root);
81 nr_object_unref((NRObject *) arena);
82 /* end */
83 delete renderer;
85 return ret;
86 }
89 /**
90 \brief This function calls the output module with the filename
91 \param mod unused
92 \param doc Document to be saved
93 \param uri Filename to save to (probably will end in .png)
94 */
95 void
96 CairoRendererOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
97 {
98 if (!png_render_document_to_file(doc, uri))
99 throw Inkscape::Extension::Output::save_failed();
101 return;
102 }
104 /**
105 \brief A function allocate a copy of this function.
107 This is the definition of Cairo PNG out. This function just
108 calls the extension system with the memory allocated XML that
109 describes the data.
110 */
111 void
112 CairoRendererOutput::init (void)
113 {
114 Inkscape::Extension::build_from_mem(
115 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
116 "<name>Cairo PNG Output</name>\n"
117 "<id>org.inkscape.output.png.cairo</id>\n"
118 "<output>\n"
119 "<extension>.png</extension>\n"
120 "<mimetype>image/png</mimetype>\n"
121 "<filetypename>Cairo PNG (*.png)</filetypename>\n"
122 "<filetypetooltip>PNG File</filetypetooltip>\n"
123 "</output>\n"
124 "</inkscape-extension>", new CairoRendererOutput());
126 return;
127 }
129 } } } /* namespace Inkscape, Extension, Implementation */
131 #endif /* HAVE_CAIRO_PDF */