Graphviz 13.0.0~dev.20250424.1043
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(kCFAllocatorDefault, NULL,
128 CFSTR("%s %s"),
129 job->common->info[0],
130 job->common->info[1]),
131 job->obj->type ==
133 CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
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(kCFAllocatorDefault,
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 =
265 CFURLCreateWithBytes(kCFAllocatorDefault, (const UInt8 *) url,
266 strlen(url), kCFStringEncodingUTF8, NULL);
267 CGPDFContextSetURLForRect(context, uri,
268 /* need to reverse the CTM on the area to get it to work */
269 CGRectApplyAffineTransform(CGRectMake
270 (url_map[0].x,
271 url_map[0].y,
272 url_map[1].
273 x -
274 url_map[0].x,
275 url_map[1].
276 y -
277 url_map[0].
278 y),
279 CGContextGetCTM
280 (context))
281 );
282
283 /* clean up */
284 CFRelease(uri);
285 }
286}
287
288static void quartzgen_path(GVJ_t * job, int filled)
289{
290 CGContextRef context = job->context;
291
292 /* set up colors */
293 if (filled)
294 CGContextSetRGBFillColor(context, job->obj->fillcolor.u.RGBA[0],
295 job->obj->fillcolor.u.RGBA[1],
296 job->obj->fillcolor.u.RGBA[2],
297 job->obj->fillcolor.u.RGBA[3]);
298 CGContextSetRGBStrokeColor(context, job->obj->pencolor.u.RGBA[0],
299 job->obj->pencolor.u.RGBA[1],
300 job->obj->pencolor.u.RGBA[2],
301 job->obj->pencolor.u.RGBA[3]);
302
303 /* set up line style */
304 const CGFloat *segments;
305 size_t segment_count;
306 switch (job->obj->pen) {
307 case PEN_DASHED:
308 segments = dashed;
309 segment_count = sizeof(dashed) / sizeof(CGFloat);
310 break;
311 case PEN_DOTTED:
312 segments = dotted;
313 segment_count = sizeof(dotted) / sizeof(CGFloat);
314 break;
315 default:
316 segments = NULL;
317 segment_count = 0;
318 break;
319 }
320 CGContextSetLineDash(context, 0.0, segments, segment_count);
321
322 /* set up line width */
323 CGContextSetLineWidth(context, job->obj->penwidth); // *job->scale.x);
324
325 /* draw the path */
326 CGContextDrawPath(context, filled ? kCGPathFillStroke : kCGPathStroke);
327}
328
329static void quartzgen_textspan(GVJ_t * job, pointf p, textspan_t * span)
330{
331 CGContextRef context = job->context;
332
333 /* adjust text position */
334 switch (span->just) {
335 case 'r':
336 p.x -= span->size.x;
337 break;
338 case 'l':
339 p.x -= 0.0;
340 break;
341 case 'n':
342 default:
343 p.x -= span->size.x / 2.0;
344 break;
345 }
346 p.y += span->yoffset_centerline;
347
348 void *layout;
349 if (span->free_layout == &quartz_free_layout)
350 layout = span->layout;
351 else
352 layout =
353 quartz_new_layout(span->font->name, span->font->size, span->str);
354
355 CGContextSetRGBFillColor(context, job->obj->pencolor.u.RGBA[0],
356 job->obj->pencolor.u.RGBA[1],
357 job->obj->pencolor.u.RGBA[2],
358 job->obj->pencolor.u.RGBA[3]);
359 quartz_draw_layout(layout, context, CGPointMake(p.x, p.y));
360
361 if (span->free_layout != &quartz_free_layout)
363}
364
365static void quartzgen_ellipse(GVJ_t * job, pointf * A, int filled)
366{
367 /* convert ellipse into the current path */
368 CGContextRef context = job->context;
369 double dx = A[1].x - A[0].x;
370 double dy = A[1].y - A[0].y;
371 CGContextAddEllipseInRect(context,
372 CGRectMake(A[0].x - dx, A[0].y - dy,
373 dx * 2.0, dy * 2.0));
374
375 /* draw the ellipse */
376 quartzgen_path(job, filled);
377}
378
379static void quartzgen_polygon(GVJ_t *job, pointf *A, size_t n, int filled) {
380 /* convert polygon into the current path */
381 CGContextRef context = job->context;
382 CGContextMoveToPoint(context, A[0].x, A[0].y);
383 for (size_t i = 1; i < n; ++i)
384 CGContextAddLineToPoint(context, A[i].x, A[i].y);
385 CGContextClosePath(context);
386
387 /* draw the ellipse */
388 quartzgen_path(job, filled);
389}
390
391static void quartzgen_bezier(GVJ_t *job, pointf *A, size_t n, int filled) {
392 /* convert bezier into the current path */
393 CGContextRef context = job->context;
394 CGContextMoveToPoint(context, A[0].x, A[0].y);
395 for (size_t i = 1; i < n; i += 3)
396 CGContextAddCurveToPoint(context, A[i].x, A[i].y, A[i + 1].x,
397 A[i + 1].y, A[i + 2].x, A[i + 2].y);
398
399 /* draw the ellipse */
400 quartzgen_path(job, filled);
401}
402
403static void quartzgen_polyline(GVJ_t *job, pointf *A, size_t n) {
404 /* convert polyline into the current path */
405 CGContextRef context = job->context;
406 CGContextMoveToPoint(context, A[0].x, A[0].y);
407 for (size_t i = 1; i < n; ++i)
408 CGContextAddLineToPoint(context, A[i].x, A[i].y);
409
410 /* draw the ellipse */
411 quartzgen_path(job, 0);
412}
413
417 0, /* quartzgen_begin_graph */
418 0, /* quartzgen_end_graph */
419 0, /* quartzgen_begin_layer */
420 0, /* quartzgen_end_layer */
423 0, /* quartzgen_begin_cluster */
424 0, /* quartzgen_end_cluster */
425 0, /* quartzgen_begin_nodes */
426 0, /* quartzgen_end_nodes */
427 0, /* quartzgen_begin_edges */
428 0, /* quartzgen_end_edges */
429 0, /* quartzgen_begin_node */
430 0, /* quartzgen_end_node */
431 0, /* quartzgen_begin_edge */
432 0, /* quartzgen_end_edge */
434 0, /* quartzgen_end_anchor */
435 0, /* quartzgen_begin_label */
436 0, /* quartzgen_end_label */
438 0,
443 0, /* quartzgen_comment */
444 0, /* quartzgen_library_shape */
445};
446
449 4., /* default pad - graph units */
450 NULL, /* knowncolors */
451 0, /* sizeof knowncolors */
452 RGBA_DOUBLE /* color_type */
453};
454
457 {0., 0.}, /* default margin - points */
458 {0., 0.}, /* default page width, height - points */
459 {72., 72.} /* dpi */
460};
461
464 {36., 36.}, /* default margin - points */
465 {0., 0.}, /* default page width, height - points */
466 {72., 72.} /* dpi */
467};
468
473
475 {FORMAT_PDF, "pdf:quartz", 8, NULL, &device_features_quartz_paged},
476 {FORMAT_CGIMAGE, "cgimage:quartz", 8, NULL, &device_features_quartz},
477 {FORMAT_BMP, "bmp:quartz", 8, NULL, &device_features_quartz},
478 {FORMAT_GIF, "gif:quartz", 8, NULL, &device_features_quartz},
479 {FORMAT_ICO, "ico:quartz", 8, NULL, &device_features_quartz},
480 {FORMAT_JPEG, "jpe:quartz", 8, NULL, &device_features_quartz},
481 {FORMAT_JPEG, "jpeg:quartz", 8, NULL, &device_features_quartz},
482 {FORMAT_JPEG, "jpg:quartz", 8, NULL, &device_features_quartz},
483 {FORMAT_JPEG2000, "jp2:quartz", 8, NULL, &device_features_quartz},
484 {FORMAT_PNG, "png:quartz", 8, NULL, &device_features_quartz},
485 {FORMAT_TIFF, "tif:quartz", 8, NULL, &device_features_quartz},
486 {FORMAT_TIFF, "tiff:quartz", 8, NULL, &device_features_quartz},
487 {FORMAT_TGA, "tga:quartz", 8, NULL, &device_features_quartz},
488#if !TARGET_OS_IPHONE
489 {FORMAT_EXR, "exr:quartz", 8, NULL, &device_features_quartz},
490 {FORMAT_ICNS, "icns:quartz", 8, NULL, &device_features_quartz},
491 {FORMAT_PICT, "pct:quartz", 8, NULL, &device_features_quartz},
492 {FORMAT_PICT, "pict:quartz", 8, NULL, &device_features_quartz},
493 {FORMAT_PSD, "psd:quartz", 8, NULL, &device_features_quartz},
494 {FORMAT_SGI, "sgi:quartz", 8, NULL, &device_features_quartz},
495#endif
496 {0, NULL, 0, NULL, NULL}
497};
#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:37
static float dx
Definition draw.c:36
#define A(n, t)
Definition expr.h:76
node NULL
Definition grammar.y:163
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
Arithmetic helper functions.
@ BYTES_PER_PIXEL
Definition gv_math.h:87
@ 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_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_ICO
@ 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::@72 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::@90 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