Graphviz 13.0.0~dev.20250210.0415
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
25enum {
27};
28
30{
31 g_object_unref(us->data);
32}
33
34static RsvgHandle* gvloadimage_rsvg_load(GVJ_t * job, usershape_t *us)
35{
36 RsvgHandle* rsvgh = NULL;
37 GError *err = NULL;
38
39 assert(job);
40 assert(us);
41 assert(us->name);
42
43 if (us->data) {
45 rsvgh = us->data; /* use cached data */
46 else {
47 us->datafree(us); /* free incompatible cache data */
48 us->data = NULL;
49 }
50
51 }
52
53 if (!rsvgh) { /* read file into cache */
55 return NULL;
56 switch (us->type) {
57 case FT_SVG: {
58 const char *const safe_path = safefile(us->name);
59 assert(safe_path != NULL &&
60 "gvusershape_file_access did not validate file path");
61 rsvgh = rsvg_handle_new_from_file(safe_path, &err);
62
63 if (rsvgh == NULL) {
64 fprintf(stderr, "rsvg_handle_new_from_file returned an error: %s\n", err->message);
65 g_error_free(err);
66 return NULL;
67 }
68
69 rsvg_handle_set_dpi(rsvgh, POINTS_PER_INCH);
70
71 break;
72 }
73 default:
74 rsvgh = NULL;
75 }
76
77 if (rsvgh) {
78 us->data = rsvgh;
80 }
81
83 }
84
85 return rsvgh;
86}
87
88static void gvloadimage_rsvg_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
89{
90 (void)filled;
91
92 RsvgHandle* rsvgh = gvloadimage_rsvg_load(job, us);
93
94 cairo_t *cr = job->context; /* target context */
95 cairo_surface_t *surface; /* source surface */
96
97 if (rsvgh) {
98 cairo_save(cr);
99
100 surface = cairo_svg_surface_create(NULL, us->w, us->h);
101
102 cairo_surface_reference(surface);
103
104 cairo_set_source_surface(cr, surface, 0, 0);
105 cairo_translate(cr, b.LL.x, -b.UR.y);
106 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
107#if LIBRSVG_MAJOR_VERSION > 2 || (LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 46)
108 const RsvgRectangle viewport = {.width = b.UR.x - b.LL.x,
109 .height = b.UR.y - b.LL.y};
110 rsvg_handle_render_document(rsvgh, cr, &viewport, NULL);
111#else
112 rsvg_handle_render_cairo(rsvgh, cr);
113#endif
114
115 cairo_paint (cr);
116 cairo_restore(cr);
117 }
118}
119
123
125 {FORMAT_SVG_CAIRO, "svg:cairo", 1, &engine_cairo, NULL},
126 {0, NULL, 0, NULL, NULL}
127};
const char * safefile(const char *filename)
Definition utils.c:260
static char * err
Definition delaunay.c:696
#define POINTS_PER_INCH
Definition geom.h:58
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)
@ FORMAT_SVG_CAIRO
gvplugin_installed_t gvloadimage_rsvg_types[]
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