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 #include "cairo-png-out.h"\r
18 #include "cairo-render-context.h"\r
19 #include "cairo-renderer.h"\r
20 #include <print.h>\r
21 #include "extension/system.h"\r
22 #include "extension/print.h"\r
23 #include "extension/db.h"\r
24 #include "extension/output.h"\r
25 #include "display/nr-arena.h"\r
26 #include "display/nr-arena-item.h"\r
27 \r
28 #include <libnr/n-art-bpath.h>\r
29 \r
30 #include "display/curve.h"\r
31 #include "display/canvas-bpath.h"\r
32 #include "sp-item.h"\r
33 #include "style.h"\r
34 #include "sp-root.h"\r
35 #include "sp-shape.h"\r
36 \r
37 #include "io/sys.h"\r
38 \r
39 namespace Inkscape {\r
40 namespace Extension {\r
41 namespace Internal {\r
42 \r
43 bool\r
44 CairoRendererOutput::check (Inkscape::Extension::Extension * module)\r
45 {\r
46 return TRUE;\r
47 }\r
48 \r
49 static bool\r
50 png_render_document_to_file(SPDocument *doc, gchar const *filename)\r
51 {\r
52 CairoRenderer *renderer;\r
53 CairoRenderContext *ctx;\r
54 \r
55 sp_document_ensure_up_to_date(doc);\r
56 \r
57 /* Start */\r
58 /* Create new arena */\r
59 SPItem *base = SP_ITEM(sp_document_root(doc));\r
60 NRArena *arena = NRArena::create();\r
61 unsigned dkey = sp_item_display_key_new(1);\r
62 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);\r
63 \r
64 /* Create renderer and context */\r
65 renderer = new CairoRenderer();\r
66 ctx = renderer->createContext();\r
67 \r
68 /* Render document */\r
69 bool ret = renderer->setupDocument(ctx, doc);\r
70 if (ret) {\r
71 renderer->renderItem(ctx, base);\r
72 ctx->saveAsPng(filename);\r
73 ret = ctx->finish();\r
74 }\r
75 renderer->destroyContext(ctx);\r
76 \r
77 /* Release arena */\r
78 sp_item_invoke_hide(base, dkey);\r
79 nr_arena_item_unref(root);\r
80 nr_object_unref((NRObject *) arena);\r
81 /* end */\r
82 delete renderer;\r
83 \r
84 return ret;\r
85 }\r
86 \r
87 \r
88 /**\r
89 \brief This function calls the output module with the filename\r
90 \param mod unused\r
91 \param doc Document to be saved\r
92 \param uri Filename to save to (probably will end in .png)\r
93 */\r
94 void\r
95 CairoRendererOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)\r
96 {\r
97 if (!png_render_document_to_file(doc, uri))\r
98 throw Inkscape::Extension::Output::save_failed();\r
99 \r
100 return;\r
101 }\r
102 \r
103 /**\r
104 \brief A function allocate a copy of this function.\r
105 \r
106 This is the definition of Cairo PNG out. This function just\r
107 calls the extension system with the memory allocated XML that\r
108 describes the data.\r
109 */\r
110 void\r
111 CairoRendererOutput::init (void)\r
112 {\r
113 Inkscape::Extension::build_from_mem(\r
114 "<inkscape-extension>\n"\r
115 "<name>Cairo PNG Output</name>\n"\r
116 "<id>org.inkscape.output.png.cairo</id>\n"\r
117 "<output>\n"\r
118 "<extension>.png</extension>\n"\r
119 "<mimetype>image/png</mimetype>\n"\r
120 "<filetypename>Cairo PNG (*.png)</filetypename>\n"\r
121 "<filetypetooltip>PNG File</filetypetooltip>\n"\r
122 "</output>\n"\r
123 "</inkscape-extension>", new CairoRendererOutput());\r
124 \r
125 return;\r
126 }\r
127 \r
128 } } } /* namespace Inkscape, Extension, Implementation */\r