Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvloadimage_rsvg.c
Go to the documentation of this file.
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 <assert.h>
12#include <common/render.h>
13#include <common/utils.h>
14#include <stdbool.h>
15#include <stddef.h>
16
18
19#include <librsvg/rsvg.h>
20#ifndef RSVG_CAIRO_H
21#include <librsvg/rsvg-cairo.h>
22#endif
23#include <cairo-svg.h>
24
25typedef enum {
28
29
31{
32 g_object_unref(us->data);
33}
34
35static RsvgHandle* gvloadimage_rsvg_load(GVJ_t * job, usershape_t *us)
36{
37 RsvgHandle* rsvgh = NULL;
38 GError *err = NULL;
39
40 assert(job);
41 assert(us);
42 assert(us->name);
43
44 if (us->data) {
46 rsvgh = us->data; /* use cached data */
47 else {
48 us->datafree(us); /* free incompatible cache data */
49 us->data = NULL;
50 }
51
52 }
53
54 if (!rsvgh) { /* read file into cache */
56 return NULL;
57 switch (us->type) {
58 case FT_SVG: {
59 const char *const safe_path = safefile(us->name);
60 assert(safe_path != NULL &&
61 "gvusershape_file_access did not validate file path");
62 rsvgh = rsvg_handle_new_from_file(safe_path, &err);
63
64 if (rsvgh == NULL) {
65 fprintf(stderr, "rsvg_handle_new_from_file returned an error: %s\n", err->message);
66 g_error_free(err);
67 return NULL;
68 }
69
70 rsvg_handle_set_dpi(rsvgh, POINTS_PER_INCH);
71
72 break;
73 }
74 default:
75 rsvgh = NULL;
76 }
77
78 if (rsvgh) {
79 us->data = rsvgh;
81 }
82
84 }
85
86 return rsvgh;
87}
88
89static void gvloadimage_rsvg_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
90{
91 (void)filled;
92
93 RsvgHandle* rsvgh = gvloadimage_rsvg_load(job, us);
94
95 cairo_t *cr = job->context; /* target context */
96 cairo_surface_t *surface; /* source surface */
97
98 if (rsvgh) {
99 cairo_save(cr);
100
101 surface = cairo_svg_surface_create(NULL, us->w, us->h);
102
103 cairo_surface_reference(surface);
104
105 cairo_set_source_surface(cr, surface, 0, 0);
106 cairo_translate(cr, b.LL.x, -b.UR.y);
107 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
108#if LIBRSVG_MAJOR_VERSION > 2 || (LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 46)
109 const RsvgRectangle viewport = {.width = b.UR.x - b.LL.x,
110 .height = b.UR.y - b.LL.y};
111 rsvg_handle_render_document(rsvgh, cr, &viewport, NULL);
112#else
113 rsvg_handle_render_cairo(rsvgh, cr);
114#endif
115
116 cairo_paint (cr);
117 cairo_restore(cr);
118 }
119}
120
124
126 {FORMAT_SVG_CAIRO, "svg:cairo", 1, &engine_cairo, NULL},
127 {0, NULL, 0, NULL, NULL}
128};
const char * safefile(const char *filename)
Definition utils.c:258
static char * err
Definition delaunay.c:708
#define POINTS_PER_INCH
Definition geom.h:64
node NULL
Definition grammar.y:163
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
static gvloadimage_engine_t engine_cairo
static RsvgHandle * gvloadimage_rsvg_load(GVJ_t *job, usershape_t *us)
static void gvloadimage_rsvg_free(usershape_t *us)
static void gvloadimage_rsvg_cairo(GVJ_t *job, usershape_t *us, boxf b, bool filled)
gvplugin_installed_t gvloadimage_rsvg_types[]
format_type
@ FORMAT_SVG_CAIRO
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
ingroup plugin_api
Definition gvplugin.h:35
double x
Definition geom.h:29
double y
Definition geom.h:29
const char * name
Definition usershape.h:54
void(* datafree)(usershape_t *us)
Definition usershape.h:65
void * data
Definition usershape.h:63
imagetype_t type
Definition usershape.h:59
double h
Definition usershape.h:61
double w
Definition usershape.h:61
@ FT_SVG
Definition usershape.h:26