Code

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