Code

r19076@shi: ted | 2008-04-21 15:42:45 -0700
[inkscape.git] / src / extension / internal / cairo-png-out.cpp
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 <libnr/n-art-bpath.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     sp_document_ensure_up_to_date(doc);
60 /* Start */
61     /* Create new arena */
62     SPItem *base = SP_ITEM(sp_document_root(doc));
63     NRArena *arena = NRArena::create();
64     unsigned dkey = sp_item_display_key_new(1);
65     NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
66     
67     /* Create renderer and context */
68     renderer = new CairoRenderer();
69     ctx = renderer->createContext();
71     /* Render document */
72     bool ret = renderer->setupDocument(ctx, doc);
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     sp_item_invoke_hide(base, dkey);
82     nr_arena_item_unref(root);
83     nr_object_unref((NRObject *) arena);
84 /* end */
85     delete renderer;
87     return ret;
88 }
91 /**
92     \brief  This function calls the output module with the filename
93         \param  mod   unused
94         \param  doc   Document to be saved
95     \param  uri   Filename to save to (probably will end in .png)
96 */
97 void
98 CairoRendererOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
99 {
100     if (!png_render_document_to_file(doc, uri))
101         throw Inkscape::Extension::Output::save_failed();
103         return;
106 /**
107         \brief   A function allocate a copy of this function.
109         This is the definition of Cairo PNG out.  This function just
110         calls the extension system with the memory allocated XML that
111         describes the data.
112 */
113 void
114 CairoRendererOutput::init (void)
116         Inkscape::Extension::build_from_mem(
117                 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
118                         "<name>Cairo PNG Output</name>\n"
119                         "<id>org.inkscape.output.png.cairo</id>\n"
120                         "<output>\n"
121                                 "<extension>.png</extension>\n"
122                 "<mimetype>image/png</mimetype>\n"
123                                 "<filetypename>Cairo PNG (*.png)</filetypename>\n"
124                                 "<filetypetooltip>PNG File</filetypetooltip>\n"
125                         "</output>\n"
126                 "</inkscape-extension>", new CairoRendererOutput());
128         return;
131 } } }  /* namespace Inkscape, Extension, Implementation */
133 #endif /* HAVE_CAIRO_PDF */