Graphviz 13.0.0~dev.20250607.1528
Loading...
Searching...
No Matches
gvrender_quartz.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 <stdlib.h>
14#include <string.h>
15#include <TargetConditionals.h>
16#include <util/gv_math.h>
17
18#if TARGET_OS_IPHONE
19#include <mach/mach_host.h>
20#include <sys/mman.h>
21#include <ImageIO/ImageIO.h>
22#endif
23
24#include <gvc/gvplugin_device.h>
25#include <gvc/gvplugin_render.h>
26#include <cgraph/cgraph.h>
27
28#include "gvplugin_quartz.h"
29
30static CGFloat dashed[] = { 6.0 };
31static CGFloat dotted[] = { 2.0, 6.0 };
32
33static void quartzgen_begin_job(GVJ_t * job)
34{
35 switch (job->device.id) {
36 case FORMAT_CGIMAGE:
37 /* save the passed-in context in the window field, so we can create a CGContext in the context field later on */
38 job->window = job->context;
39 *((CGImageRef *) job->window) = NULL;
40 }
41
42 job->context = NULL;
43}
44
45static void quartzgen_end_job(GVJ_t * job)
46{
47 CGContextRef context = job->context;
48
49#if TARGET_OS_IPHONE
50 void* context_data;
51 size_t context_datalen;
52
53 switch (job->device.id) {
54
55 case FORMAT_PDF:
56 context_data = NULL;
57 context_datalen = 0;
58 break;
59
60 default:
61 context_data = CGBitmapContextGetData(context);
62 context_datalen = CGBitmapContextGetBytesPerRow(context) * CGBitmapContextGetHeight(context);
63 break;
64 }
65#endif
66
67 switch (job->device.id) {
68
69 case FORMAT_PDF:
70 /* save the PDF */
71 CGPDFContextClose(context);
72 break;
73
74 case FORMAT_CGIMAGE:
75 /* create an image and save it where the window field is, which was set to the passed-in context at begin job */
76 *((CGImageRef *) job->window) = CGBitmapContextCreateImage(context);
77 break;
78
79 default: /* bitmap formats */
80 {
81 /* create an image destination */
82 CGDataConsumerRef data_consumer =
83 CGDataConsumerCreate(job,
85 CGImageDestinationRef image_destination =
86 CGImageDestinationCreateWithDataConsumer(data_consumer,
88
89 /* add the bitmap image to the destination and save it */
90 CGImageRef image = CGBitmapContextCreateImage(context);
91 CGImageDestinationAddImage(image_destination, image, NULL);
92 CGImageDestinationFinalize(image_destination);
93
94 /* clean up */
95 if (image_destination)
96 CFRelease(image_destination);
97 CGImageRelease(image);
98 CGDataConsumerRelease(data_consumer);
99 }
100 break;
101 }
102
103 CGContextRelease(context);
104
105#if TARGET_OS_IPHONE
106 if (context_data && context_datalen)
107 munmap(context_data, context_datalen);
108#endif
109}
110
111static void quartzgen_begin_page(GVJ_t * job)
112{
113 CGRect bounds = CGRectMake(0.0, 0.0, job->width, job->height);
114
115 if (!job->context) {
116
117 switch (job->device.id) {
118
119 case FORMAT_PDF:
120 {
121 /* create the auxiliary info for PDF content, author and title */
122 CFStringRef auxiliaryKeys[] = {
123 kCGPDFContextCreator,
124 kCGPDFContextTitle
125 };
126 CFStringRef auxiliaryValues[] = {
127 CFStringCreateWithFormat(NULL, NULL,
128 CFSTR("%s %s"),
129 job->common->info[0],
130 job->common->info[1]),
131 job->obj->type ==
133 CFStringCreateWithBytesNoCopy(NULL,
134 (const UInt8 *) agnameof(job->obj->u.g),
135 strlen(agnameof(job->obj->u.g)),
136 kCFStringEncodingUTF8,
137 false,
138 kCFAllocatorNull)
139 : CFSTR("")
140 };
141 CFDictionaryRef auxiliaryInfo =
142 CFDictionaryCreate(NULL,
143 (const void **) &auxiliaryKeys,
144 (const void **) &auxiliaryValues,
145 sizeof(auxiliaryValues) /
146 sizeof(auxiliaryValues[0]),
147 &kCFTypeDictionaryKeyCallBacks,
148 &kCFTypeDictionaryValueCallBacks);
149
150 /* create a PDF for drawing into */
151 CGDataConsumerRef data_consumer =
152 CGDataConsumerCreate(job,
154 job->context =
155 CGPDFContextCreate(data_consumer, &bounds,
156 auxiliaryInfo);
157
158 /* clean up */
159 CGDataConsumerRelease(data_consumer);
160 CFRelease(auxiliaryInfo);
161 for (size_t i = 0; i < sizeof(auxiliaryValues) / sizeof(auxiliaryValues[0]);
162 ++i)
163 CFRelease(auxiliaryValues[i]);
164 }
165 break;
166
167 default: /* bitmap formats */
168 {
169 size_t bytes_per_row =
170 (job->width * BYTES_PER_PIXEL +
172
173 void *buffer = NULL;
174
175#if TARGET_OS_IPHONE
176 /* iPhoneOS has no swap files for memory, so if we're short of memory we need to make our own temp scratch file to back it */
177
178 size_t buffer_size = job->height * bytes_per_row;
179 mach_msg_type_number_t vm_info_size = HOST_VM_INFO_COUNT;
180 vm_statistics_data_t vm_info;
181
182 if (host_statistics
183 (mach_host_self(), HOST_VM_INFO,
184 (host_info_t) & vm_info,
185 &vm_info_size) != KERN_SUCCESS
186 || buffer_size * 2 >
187 vm_info.free_count * vm_page_size) {
188 FILE *temp_file = tmpfile();
189 if (temp_file) {
190 int temp_file_descriptor = fileno(temp_file);
191 if (temp_file_descriptor >= 0
192 && ftruncate(temp_file_descriptor,
193 buffer_size) == 0) {
194 buffer = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE,
195 MAP_FILE | MAP_PRIVATE, temp_file_descriptor, 0);
196 if (buffer == MAP_FAILED)
197 buffer = NULL;
198 }
199 fclose(temp_file);
200 }
201 }
202 if (buffer == NULL) {
203 buffer = mmap(NULL, buffer_size, PROT_READ | PROT_WRITE,
204 MAP_ANON | MAP_PRIVATE, -1, 0);
205 if (buffer == MAP_FAILED) {
206 buffer = NULL;
207 }
208 }
209#endif
210
211 /* create a true color bitmap for drawing into */
212 CGColorSpaceRef color_space =
213 CGColorSpaceCreateDeviceRGB();
214 job->context = CGBitmapContextCreate(buffer, /* data: MacOSX lets system allocate, iPhoneOS use manual memory mapping */
215 job->width, /* width in pixels */
216 job->height, /* height in pixels */
217 BITS_PER_COMPONENT, /* bits per component */
218 bytes_per_row, /* bytes per row: align to 16 byte boundary */
219 color_space, /* color space: device RGB */
220 kCGImageAlphaPremultipliedFirst /* bitmap info: premul ARGB has best support in OS X */
221 );
222 job->imagedata = CGBitmapContextGetData(job->context);
223
224 /* clean up */
225 CGColorSpaceRelease(color_space);
226 }
227 break;
228 }
229
230 }
231
232 /* start the page (if this is a paged context) and graphics state */
233 CGContextRef context = job->context;
234 CGContextBeginPage(context, &bounds);
235 CGContextSaveGState(context);
236 /* CGContextSetMiterLimit(context, 1.0); */
237 /* CGContextSetLineJoin(context, kCGLineJoinBevel); */
238
239 /* set up the context transformation */
240 CGContextScaleCTM(context, job->scale.x, job->scale.y);
241 CGContextRotateCTM(context, job->rotation * M_PI / 180.0);
242 CGContextTranslateCTM(context, job->translation.x, job->translation.y);
243}
244
245static void quartzgen_end_page(GVJ_t * job)
246{
247 /* end the page (if this is a paged context) and graphics state */
248 CGContextRef context = job->context;
249 CGContextRestoreGState(context);
250 CGContextEndPage(context);
251}
252
253static void quartzgen_begin_anchor(GVJ_t * job, char *url, char *tooltip,
254 char *target, char *id)
255{
256 (void)tooltip;
257 (void)target;
258 (void)id;
259
260 pointf *url_map = job->obj->url_map_p;
261 if (url && url_map) {
262 /* set up the hyperlink to the given url */
263 CGContextRef context = job->context;
264 CFURLRef uri = CFURLCreateWithBytes(NULL, (const UInt8 *)url,
265 strlen(url), kCFStringEncodingUTF8, NULL);
266 CGPDFContextSetURLForRect(context, uri,
267 /* need to reverse the CTM on the area to get it to work */
268 CGRectApplyAffineTransform(CGRectMake
269 (url_map[0].x,
270 url_map[0].y,
271 url_map[1].
272 x -
273 url_map[0].x,
274 url_map[1].
275 y -
276 url_map[0].
277 y),
278 CGContextGetCTM
279 (context))
280 );
281
282 /* clean up */
283 CFRelease(uri);
284 }
285}
286
287static void quartzgen_path(GVJ_t * job, int filled)
288{
289 CGContextRef context = job->context;
290
291 /* set up colors */
292 if (filled)
293 CGContextSetRGBFillColor(context, job->obj->fillcolor.u.RGBA[0],
294 job->obj->fillcolor.u.RGBA[1],
295 job->obj->fillcolor.u.RGBA[2],
296 job->obj->fillcolor.u.RGBA[3]);
297 CGContextSetRGBStrokeColor(context, job->obj->pencolor.u.RGBA[0],
298 job->obj->pencolor.u.RGBA[1],
299 job->obj->pencolor.u.RGBA[2],
300 job->obj->pencolor.u.RGBA[3]);
301
302 /* set up line style */
303 const CGFloat *segments;
304 size_t segment_count;
305 switch (job->obj->pen) {
306 case PEN_DASHED:
307 segments = dashed;
308 segment_count = sizeof(dashed) / sizeof(CGFloat);
309 break;
310 case PEN_DOTTED:
311 segments = dotted;
312 segment_count = sizeof(dotted) / sizeof(CGFloat);
313 break;
314 default:
315 segments = NULL;
316 segment_count = 0;
317 break;
318 }
319 CGContextSetLineDash(context, 0.0, segments, segment_count);
320
321 /* set up line width */
322 CGContextSetLineWidth(context, job->obj->penwidth); // *job->scale.x);
323
324 /* draw the path */
325 CGContextDrawPath(context, filled ? kCGPathFillStroke : kCGPathStroke);
326}
327
328static void quartzgen_textspan(GVJ_t * job, pointf p, textspan_t * span)
329{
330 CGContextRef context = job->context;
331
332 /* adjust text position */
333 switch (span->just) {
334 case 'r':
335 p.x -= span->size.x;
336 break;
337 case 'l':
338 p.x -= 0.0;
339 break;
340 case 'n':
341 default:
342 p.x -= span->size.x / 2.0;
343 break;
344 }
345 p.y += span->yoffset_centerline;
346
347 void *layout;
348 if (span->free_layout == &quartz_free_layout)
349 layout = span->layout;
350 else
351 layout =
352 quartz_new_layout(span->font->name, span->font->size, span->str);
353
354 CGContextSetRGBFillColor(context, job->obj->pencolor.u.RGBA[0],
355 job->obj->pencolor.u.RGBA[1],
356 job->obj->pencolor.u.RGBA[2],
357 job->obj->pencolor.u.RGBA[3]);
358 quartz_draw_layout(layout, context, CGPointMake(p.x, p.y));
359
360 if (span->free_layout != &quartz_free_layout)
362}
363
364static void quartzgen_ellipse(GVJ_t * job, pointf * A, int filled)
365{
366 /* convert ellipse into the current path */
367 CGContextRef context = job->context;
368 double dx = A[1].x - A[0].x;
369 double dy = A[1].y - A[0].y;
370 CGContextAddEllipseInRect(context,
371 CGRectMake(A[0].x - dx, A[0].y - dy,
372 dx * 2.0, dy * 2.0));
373
374 /* draw the ellipse */
375 quartzgen_path(job, filled);
376}
377
378static void quartzgen_polygon(GVJ_t *job, pointf *A, size_t n, int filled) {
379 /* convert polygon into the current path */
380 CGContextRef context = job->context;
381 CGContextMoveToPoint(context, A[0].x, A[0].y);
382 for (size_t i = 1; i < n; ++i)
383 CGContextAddLineToPoint(context, A[i].x, A[i].y);
384 CGContextClosePath(context);
385
386 /* draw the ellipse */
387 quartzgen_path(job, filled);
388}
389
390static void quartzgen_bezier(GVJ_t *job, pointf *A, size_t n, int filled) {
391 /* convert bezier into the current path */
392 CGContextRef context = job->context;
393 CGContextMoveToPoint(context, A[0].x, A[0].y);
394 for (size_t i = 1; i < n; i += 3)
395 CGContextAddCurveToPoint(context, A[i].x, A[i].y, A[i + 1].x,
396 A[i + 1].y, A[i + 2].x, A[i + 2].y);
397
398 /* draw the ellipse */
399 quartzgen_path(job, filled);
400}
401
402static void quartzgen_polyline(GVJ_t *job, pointf *A, size_t n) {
403 /* convert polyline into the current path */
404 CGContextRef context = job->context;
405 CGContextMoveToPoint(context, A[0].x, A[0].y);
406 for (size_t i = 1; i < n; ++i)
407 CGContextAddLineToPoint(context, A[i].x, A[i].y);
408
409 /* draw the ellipse */
410 quartzgen_path(job, 0);
411}
412
416 0, /* quartzgen_begin_graph */
417 0, /* quartzgen_end_graph */
418 0, /* quartzgen_begin_layer */
419 0, /* quartzgen_end_layer */
422 0, /* quartzgen_begin_cluster */
423 0, /* quartzgen_end_cluster */
424 0, /* quartzgen_begin_nodes */
425 0, /* quartzgen_end_nodes */
426 0, /* quartzgen_begin_edges */
427 0, /* quartzgen_end_edges */
428 0, /* quartzgen_begin_node */
429 0, /* quartzgen_end_node */
430 0, /* quartzgen_begin_edge */
431 0, /* quartzgen_end_edge */
433 0, /* quartzgen_end_anchor */
434 0, /* quartzgen_begin_label */
435 0, /* quartzgen_end_label */
437 0,
442 0, /* quartzgen_comment */
443 0, /* quartzgen_library_shape */
444};
445
448 4., /* default pad - graph units */
449 NULL, /* knowncolors */
450 0, /* sizeof knowncolors */
451 RGBA_DOUBLE /* color_type */
452};
453
456 {0., 0.}, /* default margin - points */
457 {0., 0.}, /* default page width, height - points */
458 {72., 72.} /* dpi */
459};
460
463 {36., 36.}, /* default margin - points */
464 {0., 0.}, /* default page width, height - points */
465 {72., 72.} /* dpi */
466};
467
472
474 {FORMAT_PDF, "pdf:quartz", 8, NULL, &device_features_quartz_paged},
475 {FORMAT_CGIMAGE, "cgimage:quartz", 8, NULL, &device_features_quartz},
476 {FORMAT_BMP, "bmp:quartz", 8, NULL, &device_features_quartz},
477 {FORMAT_GIF, "gif:quartz", 8, NULL, &device_features_quartz},
478 {FORMAT_ICO, "ico:quartz", 8, NULL, &device_features_quartz},
479 {FORMAT_JPEG, "jpe:quartz", 8, NULL, &device_features_quartz},
480 {FORMAT_JPEG, "jpeg:quartz", 8, NULL, &device_features_quartz},
481 {FORMAT_JPEG, "jpg:quartz", 8, NULL, &device_features_quartz},
482 {FORMAT_JPEG2000, "jp2:quartz", 8, NULL, &device_features_quartz},
483 {FORMAT_PNG, "png:quartz", 8, NULL, &device_features_quartz},
484 {FORMAT_TIFF, "tif:quartz", 8, NULL, &device_features_quartz},
485 {FORMAT_TIFF, "tiff:quartz", 8, NULL, &device_features_quartz},
486 {FORMAT_TGA, "tga:quartz", 8, NULL, &device_features_quartz},
487#if !TARGET_OS_IPHONE
488 {FORMAT_EXR, "exr:quartz", 8, NULL, &device_features_quartz},
489 {FORMAT_ICNS, "icns:quartz", 8, NULL, &device_features_quartz},
490 {FORMAT_PICT, "pct:quartz", 8, NULL, &device_features_quartz},
491 {FORMAT_PICT, "pict:quartz", 8, NULL, &device_features_quartz},
492 {FORMAT_PSD, "psd:quartz", 8, NULL, &device_features_quartz},
493 {FORMAT_SGI, "sgi:quartz", 8, NULL, &device_features_quartz},
494#endif
495 {0, NULL, 0, NULL, NULL}
496};
#define M_PI
Definition arith.h:41
abstract graph C library, Cgraph API
@ RGBA_DOUBLE
Definition color.h:27
static float dy
Definition draw.c:38
static float dx
Definition draw.c:37
#define A(n, t)
Definition expr.h:76
node NULL
Definition grammar.y:180
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
Arithmetic helper functions.
@ BYTES_PER_PIXEL
Definition gv_math.h:88
@ PEN_DOTTED
Definition gvcjob.h:35
@ PEN_DASHED
Definition gvcjob.h:35
#define GVRENDER_NO_WHITE_BG
Definition gvcjob.h:106
#define GVDEVICE_DOES_PAGES
Definition gvcjob.h:87
#define GVDEVICE_DOES_TRUECOLOR
Definition gvcjob.h:90
#define GVDEVICE_BINARY_FORMAT
Definition gvcjob.h:91
#define GVRENDER_DOES_MAPS
Definition gvcjob.h:97
@ ROOTGRAPH_OBJTYPE
Definition gvcjob.h:168
#define GVRENDER_DOES_TRANSFORM
Definition gvcjob.h:95
#define GVRENDER_DOES_MAP_RECTANGLE
Definition gvcjob.h:98
@ FORMAT_ICO
@ FORMAT_TIFF
@ FORMAT_BMP
CGDataConsumerCallbacks device_data_consumer_callbacks
CFStringRef format_to_uti(format_type format)
void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
void * quartz_new_layout(char *fontname, double fontsize, char *text)
static const int BYTE_ALIGN
static const int BITS_PER_COMPONENT
@ FORMAT_PICT
@ FORMAT_CGIMAGE
@ FORMAT_EXR
@ FORMAT_JPEG2000
@ FORMAT_TGA
@ FORMAT_ICNS
@ FORMAT_PSD
@ FORMAT_SGI
void quartz_free_layout(void *layout)
format_type
@ FORMAT_JPEG
Definition gvrender_gd.c:37
@ FORMAT_GIF
Definition gvrender_gd.c:36
@ FORMAT_PNG
Definition gvrender_gd.c:38
@ FORMAT_PDF
static void quartzgen_bezier(GVJ_t *job, pointf *A, size_t n, int filled)
static CGFloat dashed[]
static gvdevice_features_t device_features_quartz_paged
static CGFloat dotted[]
static gvrender_engine_t quartzgen_engine
static void quartzgen_end_page(GVJ_t *job)
static void quartzgen_polygon(GVJ_t *job, pointf *A, size_t n, int filled)
static void quartzgen_path(GVJ_t *job, int filled)
static void quartzgen_begin_job(GVJ_t *job)
static gvrender_features_t render_features_quartz
static void quartzgen_end_job(GVJ_t *job)
gvplugin_installed_t gvrender_quartz_types[]
static gvdevice_features_t device_features_quartz
static void quartzgen_ellipse(GVJ_t *job, pointf *A, int filled)
static void quartzgen_polyline(GVJ_t *job, pointf *A, size_t n)
static void quartzgen_begin_page(GVJ_t *job)
static void quartzgen_begin_anchor(GVJ_t *job, char *url, char *tooltip, char *target, char *id)
gvplugin_installed_t gvdevice_quartz_types[]
static void quartzgen_textspan(GVJ_t *job, pointf p, textspan_t *span)
T_cell image
Definition htmlparse.y:340
static int layout(graph_t *g, layout_info *infop)
Definition layout.c:814
char ** info
Definition gvcommon.h:20
int rotation
Definition gvcjob.h:319
obj_state_t * obj
Definition gvcjob.h:269
gvplugin_active_device_t device
Definition gvcjob.h:286
void * context
Definition gvcjob.h:295
unsigned char * imagedata
location of imagedata
Definition gvcjob.h:297
GVCOMMON_t * common
Definition gvcjob.h:267
pointf scale
Definition gvcjob.h:332
unsigned int width
Definition gvcjob.h:327
void * window
Definition gvcjob.h:353
pointf translation
Definition gvcjob.h:333
unsigned int height
Definition gvcjob.h:328
double RGBA[4]
Definition color.h:32
union color_s::@74 u
ingroup plugin_api
Definition gvplugin.h:35
graph_t * g
Definition gvcjob.h:186
pointf * url_map_p
Definition gvcjob.h:240
gvcolor_t fillcolor
Definition gvcjob.h:194
obj_type type
Definition gvcjob.h:184
pen_type pen
Definition gvcjob.h:197
gvcolor_t pencolor
Definition gvcjob.h:194
union obj_state_s::@94 u
double penwidth
Definition gvcjob.h:199
double x
Definition geom.h:29
double y
Definition geom.h:29
char * name
Definition textspan.h:54
double size
Definition textspan.h:57
char * str
Definition textspan.h:65
char just
'l' 'n' 'r'
Definition textspan.h:71
void * layout
Definition textspan.h:67
pointf size
Definition textspan.h:70
textfont_t * font
Definition textspan.h:66
double yoffset_centerline
Definition textspan.h:69
void(* free_layout)(void *layout)
Definition textspan.h:68