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