Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvtextlayout_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 <stdbool.h>
14#include <stdlib.h>
15#include <string.h>
16
18#include "gvplugin_quartz.h"
19
20#ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
21#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 30200
22#include <CoreText/CoreText.h>
23#endif
24#endif
25
26#if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
27 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050) || \
28 (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && \
29 __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 30200)
30
31#ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
32#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1060
33/* symbol defined in 10.5.x dylib but not in headers */
34extern const CFStringRef kCTForegroundColorFromContextAttributeName;
35#endif
36#endif
37
38void *quartz_new_layout(char* fontname, double fontsize, char* text)
39{
40 CFStringRef fontnameref = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)fontname, strlen(fontname), kCFStringEncodingUTF8, FALSE);
41 CFStringRef textref = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)text, strlen(text), kCFStringEncodingUTF8, FALSE);
42 CTLineRef line = NULL;
43
44 if (fontnameref && textref) {
45 /* set up the Core Text line */
46 CTFontRef font = CTFontCreateWithName(fontnameref, fontsize, NULL);
47 CFTypeRef attributeNames[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName };
48 CFTypeRef attributeValues[] = { font, kCFBooleanTrue };
49 CFDictionaryRef attributes = CFDictionaryCreate(
50 kCFAllocatorDefault,
51 (const void**)attributeNames,
52 (const void**)attributeValues,
53 2,
54 &kCFTypeDictionaryKeyCallBacks,
55 &kCFTypeDictionaryValueCallBacks);
56 CFAttributedStringRef attributed = CFAttributedStringCreate(kCFAllocatorDefault, textref, attributes);
57 line = CTLineCreateWithAttributedString(attributed);
58
59 CFRelease(attributed);
60 CFRelease(attributes);
61 CFRelease(font);
62 }
63
64 if (textref)
65 CFRelease(textref);
66 if (fontnameref)
67 CFRelease(fontnameref);
68 return (void *)line;
69}
70
71void quartz_size_layout(void *layout, double* width, double* height, double* yoffset_layout)
72{
73 /* get the typographic bounds */
74 CGFloat ascent = 0.0;
75 CGFloat descent = 0.0;
76 CGFloat leading = 0.0;
77
78 *width = CTLineGetTypographicBounds(layout, &ascent, &descent, &leading);
79 *height = ascent + descent + leading;
80 *yoffset_layout = ascent;
81}
82
83void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
84{
85 CGContextSetTextPosition(context, position.x, position.y);
86 CTLineDraw(layout, context);
87}
88
89void quartz_free_layout(void *layout)
90{
91 if (layout)
92 CFRelease(layout);
93};
94
95#endif
96
97static bool quartz_textlayout(textspan_t *para, char **fontpath)
98{
99 (void)fontpath;
100
101 void *line = quartz_new_layout(para->font->name, para->font->size, para->str);
102 if (line)
103 {
104 /* report the layout */
105 para->layout = line;
107 quartz_size_layout(line, &para->size.x, &para->size.y, &para->yoffset_layout);
108 para->yoffset_centerline = 0.2 * para->font->size;
109 return true;
110 }
111 else
112 return false;
113};
114
118
120 {0, "textlayout", 8, &quartz_textlayout_engine, NULL},
121 {0, NULL, 0, NULL, NULL}
122};
node NULL
Definition grammar.y:163
void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
void * quartz_new_layout(char *fontname, double fontsize, char *text)
void quartz_size_layout(void *layout, double *width, double *height, double *yoffset_layout)
void quartz_free_layout(void *layout)
static gvtextlayout_engine_t quartz_textlayout_engine
gvplugin_installed_t gvtextlayout_quartz_types[]
static bool quartz_textlayout(textspan_t *para, char **fontpath)
$2 font
Definition htmlparse.y:300
static int layout(graph_t *g, layout_info *infop)
Definition layout.c:811
ingroup plugin_api
Definition gvplugin.h:35
double x
Definition geom.h:29
double y
Definition geom.h:29
float x
Definition simple.h:22
float y
Definition simple.h:22
char * name
Definition textspan.h:54
double size
Definition textspan.h:57
double yoffset_layout
Definition textspan.h:69
char * str
Definition textspan.h:65
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