Debian Codegraph @

plugin/rsvg/gvloadimage_rsvg.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #include <assert.h>
14 #include <common/render.h>
15 #include <common/utils.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18
19 #include <gvc/gvplugin_loadimage.h>
20
21 #include <librsvg/rsvg.h>
22 #ifndef RSVG_CAIRO_H
23 #include <librsvg/rsvg-cairo.h>
24 #endif
25 #include <cairo-svg.h>
26
27 enum {
28 FORMAT_SVG_CAIRO,
29 };
30
31 static void gvloadimage_rsvg_free(usershape_t *us)
32 {
33 g_object_unref(us->data);
34 }
35
36 static RsvgHandle* gvloadimage_rsvg_load(GVJ_t * job, usershape_t *us)
37 {
38 RsvgHandle* rsvgh = NULL;
39 GError *err = NULL;
40
41 assert(job);
42 assert(us);
43 assert(us->name);
44
45 if (us->data) {
46 if (us->datafree == gvloadimage_rsvg_free)
47 rsvgh = us->data; /* use cached data */
48 else {
49 us->datafree(us); /* free incompatible cache data */
50 us->data = NULL;
51 }
52
53 }
54
55 if (!rsvgh) { /* read file into cache */
56 if (!gvusershape_file_access(us))
57 return NULL;
58 switch (us->type) {
59 case FT_SVG: {
60 const char *const safe_path = safefile(us->name);
61 assert(safe_path != NULL &&
62 "gvusershape_file_access did not validate file path");
63 rsvgh = rsvg_handle_new_from_file(safe_path, &err);
64
65 if (rsvgh == NULL) {
66 fprintf(stderr, "rsvg_handle_new_from_file returned an error: %s\n", err->message);
67 g_error_free(err);
68 return NULL;
69 }
70
71 rsvg_handle_set_dpi(rsvgh, POINTS_PER_INCH);
72
73 break;
74 }
75 default:
76 rsvgh = NULL;
77 }
78
79 if (rsvgh) {
80 us->data = rsvgh;
81 us->datafree = gvloadimage_rsvg_free;
82 }
83
84 gvusershape_file_release(us);
85 }
86
87 return rsvgh;
88 }
89
90 static void gvloadimage_rsvg_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
91 {
92 (void)filled;
93
94 RsvgHandle* rsvgh = gvloadimage_rsvg_load(job, us);
95
96 cairo_t *cr = job->context; /* target context */
97 cairo_surface_t *surface; /* source surface */
98
99 if (rsvgh) {
100 cairo_save(cr);
101
102 surface = cairo_svg_surface_create(NULL, us->w, us->h);
103
104 cairo_surface_reference(surface);
105
106 cairo_set_source_surface(cr, surface, 0, 0);
107 cairo_translate(cr, b.LL.x, -b.UR.y);
108 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
109 #if LIBRSVG_MAJOR_VERSION > 2 || (LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 46)
110 const RsvgRectangle viewport = {.width = b.UR.x - b.LL.x,
111 .height = b.UR.y - b.LL.y};
112 rsvg_handle_render_document(rsvgh, cr, &viewport, NULL);
113 #else
114 rsvg_handle_render_cairo(rsvgh, cr);
115 #endif
116
117 cairo_paint (cr);
118 cairo_restore(cr);
119 }
120 }
121
122 static gvloadimage_engine_t engine_cairo = {
123 gvloadimage_rsvg_cairo
124 };
125
126 gvplugin_installed_t gvloadimage_rsvg_types[] = {
127 {FORMAT_SVG_CAIRO, "svg:cairo", 1, &engine_cairo, NULL},
128 {0, NULL, 0, NULL, NULL}
129 };