1 /*
2 * A quick hack to use the Cairo renderer to write out a file. This
3 * then makes 'save as...' PDF.
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-renderer-pdf-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 "sp-root.h"
38 namespace Inkscape {
39 namespace Extension {
40 namespace Internal {
42 bool
43 CairoRendererPdfOutput::check (Inkscape::Extension::Extension * module)
44 {
45 return TRUE;
46 }
48 static bool
49 pdf_render_document_to_file(SPDocument *doc, gchar const *filename)
50 {
51 sp_document_ensure_up_to_date(doc);
53 /* Start */
54 /* Create new arena */
55 SPItem *base = SP_ITEM(sp_document_root(doc));
56 NRArena *arena = NRArena::create();
57 unsigned dkey = sp_item_display_key_new(1);
58 NRArenaItem *root = sp_item_invoke_show(base, arena, dkey, SP_ITEM_SHOW_DISPLAY);
60 /* Create renderer and context */
61 CairoRenderer *renderer = new CairoRenderer();
62 CairoRenderContext *ctx = renderer->createContext();
63 ctx->setPdfTarget (filename);
64 bool ret = renderer->setupDocument(ctx, doc);
65 if (ret) {
66 renderer->renderItem(ctx, base);
67 ret = ctx->finish();
68 }
70 /* Release arena */
71 sp_item_invoke_hide(base, dkey);
72 nr_arena_item_unref(root);
73 nr_object_unref((NRObject *) arena);
74 /* end */
75 renderer->destroyContext(ctx);
76 delete renderer;
78 return ret;
79 }
82 /**
83 \brief This function calls the output module with the filename
84 \param mod unused
85 \param doc Document to be saved
86 \param uri Filename to save to (probably will end in .png)
88 The most interesting thing that this function does is just attach
89 an '>' on the front of the filename. This is the syntax used to
90 tell the printing system to save to file.
91 */
92 void
93 CairoRendererPdfOutput::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
94 {
95 gchar * final_name;
96 final_name = g_strdup_printf("> %s", uri);
97 bool ret = pdf_render_document_to_file(doc, final_name);
98 g_free(final_name);
100 if (!ret)
101 throw Inkscape::Extension::Output::save_failed();
103 return;
104 }
106 /**
107 \brief A function allocate a copy of this function.
109 This is the definition of Cairo PDF out. This function just
110 calls the extension system with the memory allocated XML that
111 describes the data.
112 */
113 void
114 CairoRendererPdfOutput::init (void)
115 {
116 Inkscape::Extension::build_from_mem(
117 "<inkscape-extension>\n"
118 "<name>Cairo PDF Output (experimental)</name>\n"
119 "<id>org.inkscape.output.pdf.cairorenderer</id>\n"
120 "<output>\n"
121 "<extension>.pdf</extension>\n"
122 "<mimetype>application/pdf</mimetype>\n"
123 "<filetypename>Cairo PDF experimental (*.pdf)</filetypename>\n"
124 "<filetypetooltip>PDF File</filetypetooltip>\n"
125 "</output>\n"
126 "</inkscape-extension>", new CairoRendererPdfOutput());
128 return;
129 }
131 } } } /* namespace Inkscape, Extension, Internal */
133 #endif /* HAVE_CAIRO_PDF */