Graphviz 12.0.1~dev.20240716.0800
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 <stdlib.h>
15
17#include <gvc/gvio.h>
18
19#include <cairo.h>
20
24
25static cairo_status_t
26reader (void *closure, unsigned char *data, unsigned int length)
27{
28 assert(closure);
29 if (length == fread(data, 1, length, (FILE *)closure)
30 || feof((FILE *)closure))
31 return CAIRO_STATUS_SUCCESS;
32 return CAIRO_STATUS_READ_ERROR;
33}
34
36{
37 cairo_surface_destroy(us->data);
38}
39
40static cairo_surface_t* cairo_loadimage(GVJ_t * job, usershape_t *us)
41{
42 cairo_surface_t *surface = NULL; /* source surface */
43
44 assert(job);
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(job, 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 cairo_surface_t *surface; /* source surface */
108 cairo_format_t format;
109 int X, Y, x, y, stride;
110 unsigned char *data, *ix, alpha, red, green, blue;
111
112 // suppress unused parameter warning
113 (void)filled;
114
115 surface = cairo_loadimage(job, us);
116 if (surface) {
117 format = cairo_image_surface_get_format(surface);
118 if ((format != CAIRO_FORMAT_ARGB32) && (format != CAIRO_FORMAT_RGB24))
119 return;
120
121 X = cairo_image_surface_get_width(surface);
122 Y = cairo_image_surface_get_height(surface);
123 stride = cairo_image_surface_get_stride(surface);
124 data = cairo_image_surface_get_data(surface);
125
126 gvputs(job, "save\n");
127
128 /* define image data as string array (one per raster line) */
129 /* see parallel code in gd_loadimage_ps(). FIXME: refactor... */
130 gvputs(job, "/myctr 0 def\n");
131 gvputs(job, "/myarray [\n");
132 for (y = 0; y < Y; y++) {
133 gvputs(job, "<");
134 ix = data + y * stride;
135 for (x = 0; x < X; x++) {
136 /* FIXME - this code may have endian problems */
137 blue = *ix++;
138 green = *ix++;
139 red = *ix++;
140 alpha = *ix++;
141 if (alpha < 0x7f)
142 gvputs(job, "ffffff");
143 else
144 gvprintf(job, "%02x%02x%02x", red, green, blue);
145 }
146 gvputs(job, ">\n");
147 }
148 gvputs(job, "] def\n");
149 gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n");
150
151 /* this sets the position of the image */
152 gvprintf(job, "%g %g translate\n",
153 (b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.),
154 (b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.));
155
156 /* this sets the rendered size to fit the box */
157 gvprintf(job,"%g %g scale\n",
158 ((b.UR.x - b.LL.x) * 72. / 96.),
159 ((b.UR.y - b.LL.y) * 72. / 96.));
160
161 /* xsize ysize bits-per-sample [matrix] */
162 gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y);
163
164 gvputs(job, "{myproc} false 3 colorimage\n");
165
166 gvputs(job, "restore\n");
167 }
168}
169
173
177
179 {FORMAT_PNG_CAIRO, "png:cairo", 1, &engine_cairo, NULL},
180 {FORMAT_PNG_PS, "png:lasi", 2, &engine_ps, NULL},
181 {FORMAT_PNG_PS, "png:ps", 2, &engine_ps, NULL},
182 {0, NULL, 0, NULL, NULL}
183};
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
node NULL
Definition grammar.y:149
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:263
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:394
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)
static void pango_loadimage_ps(GVJ_t *job, usershape_t *us, boxf b, bool filled)
format_type
@ FORMAT_PNG_PS
@ FORMAT_PNG_CAIRO
static cairo_surface_t * cairo_loadimage(GVJ_t *job, usershape_t *us)
static cairo_status_t reader(void *closure, unsigned char *data, unsigned int length)
#define alpha
Definition shapes.c:4068
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