Graphviz 14.1.3~dev.20260201.2050
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 "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
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
27enum {
29};
30
32{
33 g_object_unref(us->data);
34}
35
36static 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) {
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 */
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;
82 }
83
85 }
86
87 return rsvgh;
88}
89
90static 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
125
127 {FORMAT_SVG_CAIRO, "svg:cairo", 1, &engine_cairo, NULL},
128 {0, NULL, 0, NULL, NULL}
129};
const char * safefile(const char *filename)
Definition utils.c:265
static char * err
Definition delaunay.c:532
#define POINTS_PER_INCH
Definition geom.h:58
node NULL
Definition grammar.y:181
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_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