Code

PNG output for Cairo renderer
[inkscape.git] / src / extension / internal / cairo-renderer-pdf-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...' PDF.\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-renderer-pdf-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 "sp-root.h"\r
37 \r
38 namespace Inkscape {\r
39 namespace Extension {\r
40 namespace Internal {\r
41 \r
42 bool\r
43 CairoRendererPdfOutput::check (Inkscape::Extension::Extension * module)\r
44 {\r
45         return TRUE;\r
46 }\r
47 \r
48 static bool\r
49 pdf_render_document_to_file(SPDocument *doc, gchar const *filename)\r
50 {\r
51     sp_document_ensure_up_to_date(doc);\r
52 \r
53 /* Start */\r
54     /* Create new arena */\r
55     SPItem *base = SP_ITEM(sp_document_root(doc));\r
56     NRArena *arena = NRArena::create();\r
57     unsigned dkey = sp_item_display_key_new(1);\r
58     NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);\r
59     \r
60     /* Create renderer and context */\r
61     CairoRenderer *renderer = new CairoRenderer();\r
62     CairoRenderContext *ctx = renderer->createContext();\r
63     ctx->setPdfTarget (filename);\r
64     bool ret = renderer->setupDocument(ctx, doc);\r
65     if (ret) {\r
66         renderer->renderItem(ctx, base);\r
67         ret = ctx->finish();\r
68     }\r
69 \r
70     /* Release arena */\r
71     sp_item_invoke_hide(base, dkey);\r
72     nr_arena_item_unref(root);\r
73     nr_object_unref((NRObject *) arena);\r
74 /* end */\r
75     renderer->destroyContext(ctx);\r
76     delete renderer;\r
77 \r
78     return ret;\r
79 }\r
80 \r
81 \r
82 /**\r
83     \brief  This function calls the output module with the filename\r
84         \param  mod   unused\r
85         \param  doc   Document to be saved\r
86     \param  uri   Filename to save to (probably will end in .png)\r
87 \r
88         The most interesting thing that this function does is just attach\r
89         an '>' on the front of the filename.  This is the syntax used to\r
90         tell the printing system to save to file.\r
91 */\r
92 void\r
93 CairoRendererPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)\r
94 {\r
95     gchar * final_name;\r
96     final_name = g_strdup_printf("> %s", uri);\r
97     bool ret = pdf_render_document_to_file(doc, final_name);\r
98     g_free(final_name);\r
99 \r
100     if (!ret)\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 PDF 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 CairoRendererPdfOutput::init (void)\r
115 {\r
116         Inkscape::Extension::build_from_mem(\r
117                 "<inkscape-extension>\n"\r
118             "<name>Cairo PDF Output (experimental)</name>\n"\r
119                         "<id>org.inkscape.output.pdf.cairorenderer</id>\n"\r
120                         "<output>\n"\r
121                                 "<extension>.pdf</extension>\n"\r
122                                 "<mimetype>application/pdf</mimetype>\n"\r
123                                 "<filetypename>Cairo PDF experimental (*.pdf)</filetypename>\n"\r
124                                 "<filetypetooltip>PDF File</filetypetooltip>\n"\r
125                         "</output>\n"\r
126                 "</inkscape-extension>", new CairoRendererPdfOutput());\r
127 \r
128         return;\r
129 }\r
130 \r
131 } } }  /* namespace Inkscape, Extension, Internal */\r
132 \r
133 #endif /* HAVE_CAIRO_PDF */\r