Graphviz 13.1.0~dev.20250626.0830
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 <TargetConditionals.h>
16
18#include "gvplugin_quartz.h"
19
20#if TARGET_OS_IPHONE
21#include <CoreText/CoreText.h>
22#endif
23
24void *quartz_new_layout(char* fontname, double fontsize, char* text)
25{
26 CFStringRef fontnameref = CFStringCreateWithCString(NULL, fontname, kCFStringEncodingUTF8);
27 CFStringRef textref = CFStringCreateWithCString(NULL, text, kCFStringEncodingUTF8);
28 CTLineRef line = NULL;
29
30 if (fontnameref && textref) {
31 /* set up the Core Text line */
32 CTFontRef font = CTFontCreateWithName(fontnameref, fontsize, NULL);
33 const void *attributeNames[] = {
34 kCTFontAttributeName, kCTForegroundColorFromContextAttributeName};
35 const void *attributeValues[] = {font, kCFBooleanTrue};
36 CFDictionaryRef attributes = CFDictionaryCreate(
37 NULL,
38 attributeNames,
39 attributeValues,
40 sizeof(attributeNames) / sizeof(attributeNames[0]),
41 &kCFTypeDictionaryKeyCallBacks,
42 &kCFTypeDictionaryValueCallBacks);
43 CFAttributedStringRef attributed = CFAttributedStringCreate(NULL, textref, attributes);
44 line = CTLineCreateWithAttributedString(attributed);
45
46 CFRelease(attributed);
47 CFRelease(attributes);
48 CFRelease(font);
49 }
50
51 if (textref)
52 CFRelease(textref);
53 if (fontnameref)
54 CFRelease(fontnameref);
55// Suppress Clang/GCC -Wcast-qual warning. Casting away const here is acceptable
56// as we do not intend to modify the target through the resulting non-const
57// pointer. We need a non-const pointer simply to satisfy the type of
58// `textspan_t.layout`.
59#ifdef __GNUC__
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Wcast-qual"
62#endif
63 return (void *)line;
64#ifdef __GNUC__
65#pragma GCC diagnostic pop
66#endif
67}
68
69void quartz_size_layout(void *layout, double* width, double* height, double* yoffset_layout)
70{
71 /* get the typographic bounds */
72 CGFloat ascent = 0.0;
73 CGFloat descent = 0.0;
74 CGFloat leading = 0.0;
75
76 *width = CTLineGetTypographicBounds(layout, &ascent, &descent, &leading);
77 *height = ascent + descent + leading;
78 *yoffset_layout = ascent;
79}
80
81void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
82{
83 CGContextSetTextPosition(context, position.x, position.y);
84 CTLineDraw(layout, context);
85}
86
88{
89 if (layout)
90 CFRelease(layout);
91}
92
93static bool quartz_textlayout(textspan_t *para, char **fontpath)
94{
95 (void)fontpath;
96
97 void *line = quartz_new_layout(para->font->name, para->font->size, para->str);
98 if (line)
99 {
100 /* report the layout */
101 para->layout = line;
103 quartz_size_layout(line, &para->size.x, &para->size.y, &para->yoffset_layout);
104 para->yoffset_centerline = 0.2 * para->font->size;
105 return true;
106 }
107 else
108 return false;
109};
110
114
116 {0, "textlayout", 8, &quartz_textlayout_engine, NULL},
117 {0, NULL, 0, NULL, NULL}
118};
node NULL
Definition grammar.y:181
void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
void * quartz_new_layout(char *fontname, double fontsize, char *text)
static gvtextlayout_engine_t quartz_textlayout_engine
void quartz_size_layout(void *layout, double *width, double *height, double *yoffset_layout)
gvplugin_installed_t gvtextlayout_quartz_types[]
void quartz_free_layout(void *layout)
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:814
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