Graphviz 13.0.0~dev.20250511.0440
Loading...
Searching...
No Matches
gvloadimage_gdk.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 <stdbool.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <util/alloc.h>
16#include <util/agxbuf.h>
17#include <util/gv_ftell.h>
18
20#include <gvc/gvio.h>
21
22#include <cairo.h>
23#include <gdk-pixbuf/gdk-pixbuf.h>
24#include <gdk/gdk.h>
25
26enum {
31};
32
33static void gdk_set_mimedata_from_file (cairo_surface_t *image, const char *mime_type, const char *file)
34{
35 unsigned char *data = NULL;
36
37 FILE *const fp = fopen(file, "rb");
38 if (fp == NULL)
39 return;
40 fseek (fp, 0, SEEK_END);
41 const size_t len = gv_ftell(fp);
42 rewind(fp);
43 if (len > 0)
44 data = malloc(len);
45 if (data) {
46 if (fread(data, len, 1, fp) != 1) {
47 free (data);
48 data = NULL;
49 }
50 }
51 fclose(fp);
52
53 if (data) {
54 if (cairo_surface_set_mime_data(image, mime_type, data,
55 (unsigned long)len, free, data) !=
56 CAIRO_STATUS_SUCCESS) {
57 free(data);
58 return;
59 }
60 agxbuf id = {0};
61 agxbprint(&id, "gvloadimage_gdk-%s", file);
62 char *const unique_id = agxbdisown(&id);
63 if (cairo_surface_set_mime_data(image, CAIRO_MIME_TYPE_UNIQUE_ID,
64 (unsigned char *)unique_id,
65 strlen(unique_id), free, unique_id) !=
66 CAIRO_STATUS_SUCCESS) {
67 free(unique_id);
68 }
69 }
70}
71
72static void gdk_set_mimedata(cairo_surface_t *image, usershape_t *us)
73{
74 switch (us->type) {
75 case FT_PNG:
76 gdk_set_mimedata_from_file (image, CAIRO_MIME_TYPE_PNG, us->name);
77 break;
78 case FT_JPEG:
79 gdk_set_mimedata_from_file (image, CAIRO_MIME_TYPE_JPEG, us->name);
80 break;
81 default:
82 break;
83 }
84}
85
87typedef struct {
88 cairo_surface_t *surface;
89 GdkPixbuf *origin;
90} data_t;
91
92static void gdk_freeimage(usershape_t *us)
93{
94 assert(us->data != NULL);
95 data_t *const data = us->data;
96 const bool is_last = cairo_surface_get_reference_count(data->surface) == 1;
97 cairo_surface_destroy(data->surface);
98 if (is_last) {
99 free(data->origin);
100 free(data);
101 }
102}
103
104static cairo_surface_t* gdk_loadimage(GVJ_t * job, usershape_t *us)
105{
106 cairo_t *cr = job->context; /* target context */
107 GdkPixbuf *image = NULL;
108 cairo_surface_t *cairo_image = NULL;
109
110 assert(job);
111 assert(us);
112 assert(us->name);
113
114 if (us->data) {
115 if (us->datafree == gdk_freeimage) {
116 // use cached data
117 data_t *const data = us->data;
118 cairo_image = cairo_surface_reference(data->surface);
119 } else {
120 us->datafree(us); /* free incompatible cache data */
121 us->datafree = NULL;
122 us->data = NULL;
123 }
124 }
125 if (!cairo_image) { /* read file into cache */
127 return NULL;
128 switch (us->type) {
129 case FT_PNG:
130 case FT_JPEG:
131 case FT_BMP:
132 case FT_ICO:
133 case FT_TIFF:
134 // FIXME - should be using a stream reader
135 image = gdk_pixbuf_new_from_file(us->name, NULL);
136 break;
137 default:
138 image = NULL;
139 }
140 if (image) {
141 cairo_save (cr);
142 gdk_cairo_set_source_pixbuf (cr, image, 0, 0);
143 cairo_pattern_t *const pattern = cairo_get_source(cr);
144 assert(cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SURFACE);
145 cairo_pattern_get_surface (pattern, &cairo_image);
146 cairo_image = cairo_surface_reference (cairo_image);
147 cairo_restore (cr);
148 gdk_set_mimedata (cairo_image, us);
149 data_t *const data = gv_alloc(sizeof(data_t));
150 *data = (data_t){.surface = cairo_surface_reference(cairo_image),
151 .origin = image};
152 us->data = data;
154 }
156 }
157 return cairo_image;
158}
159
160static void gdk_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
161{
162 (void)filled;
163
164 cairo_t *cr = job->context; /* target context */
165 cairo_surface_t *const image = gdk_loadimage(job, us);
166 if (image) {
167 cairo_save(cr);
168 cairo_translate(cr, b.LL.x, -b.UR.y);
169 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
170 cairo_set_source_surface (cr, image, 0, 0);
171 cairo_paint (cr);
172 cairo_restore(cr);
173 cairo_surface_destroy (image);
174 }
175}
176
180
182 {FORMAT_BMP_CAIRO, "bmp:cairo", 1, &engine_gdk, NULL},
183 {FORMAT_JPEG_CAIRO, "jpe:cairo", 2, &engine_gdk, NULL},
184 {FORMAT_JPEG_CAIRO, "jpg:cairo", 2, &engine_gdk, NULL},
185 {FORMAT_JPEG_CAIRO, "jpeg:cairo", 2, &engine_gdk, NULL},
186 {FORMAT_PNG_CAIRO, "png:cairo", -1, &engine_gdk, NULL},
187 {FORMAT_ICO_CAIRO, "ico:cairo", 1, &engine_gdk, NULL},
188 {0, NULL, 0, NULL, NULL}
189};
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static char * agxbdisown(agxbuf *xb)
Definition agxbuf.h:327
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
static double len(glCompPoint p)
Definition glutils.c:150
void * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:163
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
Abstraction over ftell
static size_t gv_ftell(FILE *stream)
ftell, accounting for platform limitations
Definition gv_ftell.h:11
@ FORMAT_BMP_CAIRO
@ FORMAT_ICO_CAIRO
@ FORMAT_PNG_CAIRO
@ FORMAT_JPEG_CAIRO
gvplugin_installed_t gvloadimage_gdk_types[]
static void gdk_loadimage_cairo(GVJ_t *job, usershape_t *us, boxf b, bool filled)
static void gdk_set_mimedata_from_file(cairo_surface_t *image, const char *mime_type, const char *file)
static gvloadimage_engine_t engine_gdk
static void gdk_freeimage(usershape_t *us)
static cairo_surface_t * gdk_loadimage(GVJ_t *job, usershape_t *us)
static void gdk_set_mimedata(cairo_surface_t *image, usershape_t *us)
T_cell image
Definition htmlparse.y:340
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
resources allocated for loaded images
GdkPixbuf * origin
cairo_surface_t * surface
Definition legal.c:50
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_BMP
Definition usershape.h:25
@ FT_ICO
Definition usershape.h:27
@ FT_TIFF
Definition usershape.h:27
@ FT_JPEG
Definition usershape.h:25
@ FT_PNG
Definition usershape.h:25