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