Graphviz 13.0.0~dev.20241220.2304
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#include <stdbool.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <util/agxbuf.h>
16
18#include <gvc/gvio.h>
19
20#ifdef HAVE_PANGOCAIRO
21#include <cairo.h>
22#include <gdk-pixbuf/gdk-pixbuf.h>
23#include <gdk/gdk.h>
24
25#ifdef _MSC_VER //*dependencies
26 #pragma comment( lib, "gvc.lib" )
27 #pragma comment( lib, "glib-2.0.lib" )
28 #pragma comment( lib, "cairo.lib" )
29 #pragma comment( lib, "gobject-2.0.lib" )
30 #pragma comment( lib, "graph.lib" )
31 #pragma comment( lib, "gdk-pixbuf.lib" )
32#endif
33
34typedef enum {
35 FORMAT_BMP_CAIRO,
36 FORMAT_JPEG_CAIRO,
38 FORMAT_ICO_CAIRO,
40
41static void gdk_set_mimedata_from_file (cairo_surface_t *image, const char *mime_type, const char *file)
42{
43 FILE *fp;
44 unsigned char *data = NULL;
45 long len;
46 const char *id_prefix = "gvloadimage_gdk-";
47
48 fp = fopen (file, "rb");
49 if (fp == NULL)
50 return;
51 fseek (fp, 0, SEEK_END);
52 len = ftell(fp);
53 rewind(fp);
54 if (len > 0)
55 data = malloc ((size_t)len);
56 if (data) {
57 if (fread(data, (size_t)len, 1, fp) != 1) {
58 free (data);
59 data = NULL;
60 }
61 }
62 fclose(fp);
63
64 if (data) {
65 cairo_surface_set_mime_data (image, mime_type, data, (unsigned long)len, free, data);
66 agxbuf id = {0};
67 agxbprint(&id, "%s%s", id_prefix, file);
68 char *unique_id = agxbdisown(&id);
69 cairo_surface_set_mime_data(image, CAIRO_MIME_TYPE_UNIQUE_ID,
70 (unsigned char*)unique_id,
71 strlen(unique_id), free, unique_id);
72 }
73}
74
75static void gdk_set_mimedata(cairo_surface_t *image, usershape_t *us)
76{
77 switch (us->type) {
78 case FT_PNG:
79 gdk_set_mimedata_from_file (image, CAIRO_MIME_TYPE_PNG, us->name);
80 break;
81 case FT_JPEG:
82 gdk_set_mimedata_from_file (image, CAIRO_MIME_TYPE_JPEG, us->name);
83 break;
84 default:
85 break;
86 }
87}
88
89static void gdk_freeimage(usershape_t *us)
90{
91 cairo_surface_destroy(us->data);
92}
93
94static cairo_surface_t* gdk_loadimage(GVJ_t * job, usershape_t *us)
95{
96 cairo_t *cr = job->context; /* target context */
97 GdkPixbuf *image = NULL;
98 cairo_surface_t *cairo_image = NULL;
99 cairo_pattern_t *pattern;
100
101 assert(job);
102 assert(us);
103 assert(us->name);
104
105 if (us->data) {
106 if (us->datafree == gdk_freeimage) {
107 cairo_image = cairo_surface_reference(us->data); /* use cached data */
108 } else {
109 us->datafree(us); /* free incompatible cache data */
110 us->datafree = NULL;
111 us->data = NULL;
112 }
113 }
114 if (!cairo_image) { /* read file into cache */
116 return NULL;
117 switch (us->type) {
118 case FT_PNG:
119 case FT_JPEG:
120 case FT_BMP:
121 case FT_ICO:
122 case FT_TIFF:
123 // FIXME - should be using a stream reader
124 image = gdk_pixbuf_new_from_file(us->name, NULL);
125 break;
126 default:
127 image = NULL;
128 }
129 if (image) {
130 cairo_save (cr);
131 gdk_cairo_set_source_pixbuf (cr, image, 0, 0);
132 pattern = cairo_get_source (cr);
133 assert(cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SURFACE);
134 cairo_pattern_get_surface (pattern, &cairo_image);
135 cairo_image = cairo_surface_reference (cairo_image);
136 cairo_restore (cr);
137 gdk_set_mimedata (cairo_image, us);
138 us->data = cairo_surface_reference(cairo_image);
139 us->datafree = gdk_freeimage;
140 }
142 }
143 return cairo_image;
144}
145
146static void gdk_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
147{
148 (void)filled;
149
150 cairo_t *cr = job->context; /* target context */
151 cairo_surface_t *image;
152
153 image = gdk_loadimage(job, us);
154 if (image) {
155 cairo_save(cr);
156 cairo_translate(cr, b.LL.x, -b.UR.y);
157 cairo_scale(cr, (b.UR.x - b.LL.x) / us->w, (b.UR.y - b.LL.y) / us->h);
158 cairo_set_source_surface (cr, image, 0, 0);
159 cairo_paint (cr);
160 cairo_restore(cr);
161 cairo_surface_destroy (image);
162 }
163}
164
165static gvloadimage_engine_t engine_gdk = {
166 gdk_loadimage_cairo
167};
168
169#endif
170
172#ifdef HAVE_PANGOCAIRO
173 {FORMAT_BMP_CAIRO, "bmp:cairo", 1, &engine_gdk, NULL},
174 {FORMAT_JPEG_CAIRO, "jpe:cairo", 2, &engine_gdk, NULL},
175 {FORMAT_JPEG_CAIRO, "jpg:cairo", 2, &engine_gdk, NULL},
176 {FORMAT_JPEG_CAIRO, "jpeg:cairo", 2, &engine_gdk, NULL},
177 {FORMAT_PNG_CAIRO, "png:cairo", -1, &engine_gdk, NULL},
178 {FORMAT_ICO_CAIRO, "ico:cairo", 1, &engine_gdk, NULL},
179#endif
180 {0, NULL, 0, NULL, NULL}
181};
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
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)
format_type
@ FORMAT_PNG_CAIRO
gvplugin_installed_t gvloadimage_gdk_types[]
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
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