Graphviz 13.1.0~dev.20250626.0830
Loading...
Searching...
No Matches
gvloadimage_pango.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 <stdbool.h>
14#include <stdint.h>
15#include <stdlib.h>
16#include <string.h>
17
19#include <gvc/gvio.h>
20
21#include <cairo.h>
22
23enum {
25};
26
27static cairo_status_t
28reader (void *closure, unsigned char *data, unsigned int length)
29{
30 assert(closure);
31 if (length == fread(data, 1, length, (FILE *)closure)
32 || feof((FILE *)closure))
33 return CAIRO_STATUS_SUCCESS;
34 return CAIRO_STATUS_READ_ERROR;
35}
36
38{
39 cairo_surface_destroy(us->data);
40}
41
42static cairo_surface_t *cairo_loadimage(usershape_t *us) {
43 cairo_surface_t *surface = NULL; /* source surface */
44
45 assert(us);
46 assert(us->name);
47 assert(us->name[0]);
48
49 if (us->data) {
50 if (us->datafree == cairo_freeimage)
51 surface = us->data; /* use cached data */
52 else {
53 us->datafree(us); /* free incompatible cache data */
54 us->datafree = NULL;
55 us->data = NULL;
56 }
57 }
58 if (!surface) { /* read file into cache */
60 return NULL;
61 assert(us->f);
62 switch (us->type) {
63#ifdef CAIRO_HAS_PNG_FUNCTIONS
64 case FT_PNG:
65 surface = cairo_image_surface_create_from_png_stream(reader, us->f);
66 cairo_surface_reference(surface);
67 break;
68#endif
69 default:
70 surface = NULL;
71 }
72 if (surface) {
73 us->data = surface;
75 }
77 }
78 return surface;
79}
80
81static void pango_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
82{
83 cairo_t *cr = job->context; /* target context */
84 cairo_surface_t *surface; /* source surface */
85
86 assert(job);
87 assert(us);
88 assert(us->name);
89 assert(us->name[0]);
90
91 // suppress unused parameter warning
92 (void)filled;
93
94 surface = cairo_loadimage(us);
95 if (surface) {
96 cairo_save(cr);
97 cairo_translate(cr, b.LL.x, -b.UR.y);
98 cairo_scale(cr, (b.UR.x - b.LL.x)/(us->w), (b.UR.y - b.LL.y)/(us->h));
99 cairo_set_source_surface (cr, surface, 0, 0);
100 cairo_paint (cr);
101 cairo_restore(cr);
102 }
103}
104
105static void pango_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, bool filled)
106{
107 // suppress unused parameter warning
108 (void)filled;
109
110 cairo_surface_t *const surface = cairo_loadimage(us); // source surface
111 if (surface) {
112 const cairo_format_t format = cairo_image_surface_get_format(surface);
113 if ((format != CAIRO_FORMAT_ARGB32) && (format != CAIRO_FORMAT_RGB24))
114 return;
115
116 const int X = cairo_image_surface_get_width(surface);
117 const int Y = cairo_image_surface_get_height(surface);
118 const int stride = cairo_image_surface_get_stride(surface);
119 const unsigned char *data = cairo_image_surface_get_data(surface);
120
121 gvputs(job, "save\n");
122
123 /* define image data as string array (one per raster line) */
124 /* see parallel code in gd_loadimage_ps(). FIXME: refactor... */
125 gvputs(job, "/myctr 0 def\n");
126 gvputs(job, "/myarray [\n");
127 for (int y = 0; y < Y; y++) {
128 gvputs(job, "<");
129 const unsigned char *ix = data + y * stride;
130 for (int x = 0; x < X; x++) {
131 uint32_t rgba;
132 memcpy(&rgba, ix, sizeof(rgba));
133 ix += sizeof(rgba);
134 const unsigned blue = rgba & 0xff;
135 const unsigned green = (rgba >> 8) & 0xff;
136 const unsigned red = (rgba >> 16) & 0xff;
137 const unsigned alpha = (rgba >> 24) & 0xff;
138 if (alpha < 0x7f)
139 gvputs(job, "ffffff");
140 else
141 gvprintf(job, "%02x%02x%02x", red, green, blue);
142 }
143 gvputs(job, ">\n");
144 }
145 gvputs(job, "] def\n");
146 gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n");
147
148 /* this sets the position of the image */
149 gvprintf(job, "%g %g translate\n",
150 (b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.),
151 (b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.));
152
153 /* this sets the rendered size to fit the box */
154 gvprintf(job,"%g %g scale\n",
155 ((b.UR.x - b.LL.x) * 72. / 96.),
156 ((b.UR.y - b.LL.y) * 72. / 96.));
157
158 /* xsize ysize bits-per-sample [matrix] */
159 gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y);
160
161 gvputs(job, "{myproc} false 3 colorimage\n");
162
163 gvputs(job, "restore\n");
164 }
165}
166
170
174
176 {FORMAT_PNG_CAIRO, "png:cairo", 1, &engine_cairo, NULL},
177 {FORMAT_PNG_PS, "png:lasi", 2, &engine_ps, NULL},
178 {FORMAT_PNG_PS, "png:ps", 2, &engine_ps, NULL},
179 {0, NULL, 0, NULL, NULL}
180};
static Extype_t length(Exid_t *rhs, Exdisc_t *disc)
Definition compile.c:1588
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
node NULL
Definition grammar.y:181
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
int gvputs(GVJ_t *job, const char *s)
Definition gvdevice.c:266
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:402
GVIO_API const char * format
Definition gvio.h:51
static gvloadimage_engine_t engine_ps
static gvloadimage_engine_t engine_cairo
gvplugin_installed_t gvloadimage_pango_types[]
static void pango_loadimage_cairo(GVJ_t *job, usershape_t *us, boxf b, bool filled)
static void cairo_freeimage(usershape_t *us)
@ FORMAT_PNG_PS
@ FORMAT_PNG_CAIRO
static void pango_loadimage_ps(GVJ_t *job, usershape_t *us, boxf b, bool filled)
static cairo_surface_t * cairo_loadimage(usershape_t *us)
static cairo_status_t reader(void *closure, unsigned char *data, unsigned int length)
#define alpha
Definition shapes.c:4058
pointf dpi
Definition gvcjob.h:325
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
FILE * f
Definition usershape.h:58
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_PNG
Definition usershape.h:25