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 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2004-2006 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #ifdef HAVE_CAIRO_PDF
22 #include "cairo-png-out.h"
23 #include "cairo-render-context.h"
24 #include "cairo-renderer.h"
25 #include <print.h>
26 #include "extension/system.h"
27 #include "extension/print.h"
28 #include "extension/db.h"
29 #include "extension/output.h"
30 #include "display/nr-arena.h"
31 #include "display/nr-arena-item.h"
33 #include "display/curve.h"
34 #include "display/canvas-bpath.h"
35 #include "sp-item.h"
36 #include "style.h"
37 #include "sp-root.h"
38 #include "sp-shape.h"
40 #include "io/sys.h"
42 namespace Inkscape {
43 namespace Extension {
44 namespace Internal {
46 bool
47 CairoRendererOutput::check (Inkscape::Extension::Extension * module)
48 {
49 return TRUE;
50 }
52 static bool
53 png_render_document_to_file(SPDocument *doc, gchar const *filename)
54 {
55 CairoRenderer *renderer;
56 CairoRenderContext *ctx;
58 doc->ensureUpToDate();
60 /* Start */
61 /* Create new arena */
62 SPItem *base = SP_ITEM(doc->getRoot());
63 NRArena *arena = NRArena::create();
64 unsigned dkey = SPItem::display_key_new(1);
65 NRArenaItem *root = base->invoke_show(arena, dkey, SP_ITEM_SHOW_DISPLAY);
67 /* Create renderer and context */
68 renderer = new CairoRenderer();
69 ctx = renderer->createContext();
71 /* Render document */
72 bool ret = renderer->setupDocument(ctx, doc, TRUE, NULL);
73 if (ret) {
74 renderer->renderItem(ctx, base);
75 ctx->saveAsPng(filename);
76 ret = ctx->finish();
77 }
78 renderer->destroyContext(ctx);
80 /* Release arena */
81 base->invoke_hide(dkey);
82 nr_object_unref((NRObject *) arena);
83 /* end */
84 delete renderer;
86 return ret;
87 }
90 /**
91 \brief This function calls the output module with the filename
92 \param mod unused
93 \param doc Document to be saved
94 \param uri Filename to save to (probably will end in .png)
95 */
96 void
97 CairoRendererOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *filename)
98 {
99 if (!png_render_document_to_file(doc, filename))
100 throw Inkscape::Extension::Output::save_failed();
102 return;
103 }
105 /**
106 \brief A function allocate a copy of this function.
108 This is the definition of Cairo PNG out. This function just
109 calls the extension system with the memory allocated XML that
110 describes the data.
111 */
112 void
113 CairoRendererOutput::init (void)
114 {
115 Inkscape::Extension::build_from_mem(
116 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
117 "<name>Cairo PNG Output</name>\n"
118 "<id>org.inkscape.output.png.cairo</id>\n"
119 "<output>\n"
120 "<extension>.png</extension>\n"
121 "<mimetype>image/png</mimetype>\n"
122 "<filetypename>Cairo PNG (*.png)</filetypename>\n"
123 "<filetypetooltip>PNG File</filetypetooltip>\n"
124 "</output>\n"
125 "</inkscape-extension>", new CairoRendererOutput());
127 return;
128 }
130 } } } /* namespace Inkscape, Extension, Implementation */
132 #endif /* HAVE_CAIRO_PDF */