forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoretext-skia.cpp
More file actions
369 lines (288 loc) · 10.9 KB
/
Copy pathcoretext-skia.cpp
File metadata and controls
369 lines (288 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* Copyright (C) 2003-2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "graphics.h"
#include "graphics-internal.h"
#import <CoreFoundation/CoreFoundation.h>
#ifdef TARGET_SUBPLATFORM_IPHONE
#import <CoreText/CoreText.h>
#import <CoreGraphics/CoreGraphics.h>
#else
#import <ApplicationServices/ApplicationServices.h>
#endif
#include <SkFont.h>
#include <SkTypeface.h>
#include <SkTypeface_mac.h>
////////////////////////////////////////////////////////////////////////////////
static CGColorSpaceRef s_colour_space = NULL;
static CGColorRef s_text_colour = NULL;
static CGContextRef s_measure_context;
static void *s_measure_data = NULL;
void MCGPlatformInitialize(void)
{
s_colour_space = NULL;
s_text_colour = NULL;
s_measure_context = NULL;
s_measure_data = NULL;
const CGFloat t_colour_components[] = {1.0, 1.0};
#ifdef TARGET_SUBPLATFORM_IPHONE
// iOS doesn't support device-independent or generic color spaces
s_colour_space = CGColorSpaceCreateDeviceGray();
#else
s_colour_space = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
#endif
s_text_colour = CGColorCreate(s_colour_space, t_colour_components);
/* UNCHECKED */ MCMemoryAllocate(1, s_measure_data);
s_measure_context = CGBitmapContextCreate(s_measure_data, 1, 1, 8, 1, NULL, kCGImageAlphaOnly);
}
void MCGPlatformFinalize(void)
{
if (s_text_colour != NULL)
CGColorRelease(s_text_colour);
if (s_colour_space != NULL)
CGColorSpaceRelease(s_colour_space);
if (s_measure_context != NULL)
CGContextRelease(s_measure_context);
MCMemoryDelete(s_measure_data);
s_colour_space = NULL;
s_text_colour = NULL;
s_measure_context = NULL;
s_measure_data = NULL;
}
////////////////////////////////////////////////////////////////////////////////
// Creates an SkTypeface from a CTFontRef
static inline sk_sp<SkTypeface> MCGSkTypefaceFromCTFontRef(CTFontRef p_ref)
{
return sk_sp<SkTypeface>(SkCreateTypefaceFromCTFont(p_ref, nil));
}
////////////////////////////////////////////////////////////////////////////////
static CTLineRef MCGCreateCTLineFromText(const unichar_t *p_text, uindex_t p_length, const MCGFont &p_font)
{
bool t_success = true;
CFStringRef t_cftext = nil;
if (t_success)
{
t_cftext = CFStringCreateWithCharacters(kCFAllocatorDefault, p_text, p_length / sizeof(unichar_t));
t_success = t_cftext != nil;
}
CFDictionaryRef t_attributes = nil;
if (t_success)
{
CTFontRef t_font = static_cast<CTFontRef>(p_font.fid);
CFStringRef t_keys[] =
{
kCTFontAttributeName,
kCTForegroundColorAttributeName,
};
CFTypeRef t_values[] =
{
t_font,
s_text_colour,
};
t_attributes = CFDictionaryCreate(NULL,
(const void **)&t_keys,
(const void **)&t_values,
sizeof(t_keys) / sizeof(t_keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
t_success = t_attributes != nil;
}
CFAttributedStringRef t_attributed_text = nil;
if (t_success)
{
t_attributed_text = CFAttributedStringCreate(nil, t_cftext, t_attributes);
t_success = t_attributed_text != nil;
}
CTLineRef t_line = nil;
if (t_success)
t_line = CTLineCreateWithAttributedString(t_attributed_text);
if (t_attributed_text != NULL)
CFRelease(t_attributed_text);
if (t_attributes != NULL)
CFRelease(t_attributes);
if (t_cftext != NULL)
CFRelease(t_cftext);
return t_line;
}
////////////////////////////////////////////////////////////////////////////////
// Wrapper used for managing glyph/position arrays
template <class T>
class MCGMaybeOwnedArray
{
public:
MCGMaybeOwnedArray(uindex_t t_length) :
m_length(t_length)
{
}
MCGMaybeOwnedArray(const MCGMaybeOwnedArray&) = delete;
~MCGMaybeOwnedArray()
{
if (m_owned)
MCMemoryDeleteArray(m_array);
}
bool TryBorrow(const T* p_array)
{
MCAssert(m_array == nil);
if (p_array != nil)
m_array = const_cast<T*>(p_array);
return p_array != nil;
}
bool Allocate()
{
MCAssert(m_array == nil);
m_owned = true;
return MCMemoryNewArray(m_length, m_array);
}
operator T* ()
{
return m_array;
}
private:
T* m_array = nil;
uindex_t m_length = 0;
bool m_owned = false;
};
////////////////////////////////////////////////////////////////////////////////
template <class Callback>
static void MCGForEachRunInLine(CTLineRef p_line, Callback p_callback)
{
// Get the array containing the runs
CFArrayRef t_runs = CTLineGetGlyphRuns(p_line);
for (CFIndex i = 0; i < CFArrayGetCount(t_runs); i++)
{
// Get this run
CTRunRef t_run = static_cast<CTRunRef>(CFArrayGetValueAtIndex(t_runs, i));
// Execute the callback
p_callback(t_run);
}
}
template <class Callback>
static void MCGForEachGlyphInRun(CTRunRef p_run, Callback p_callback)
{
// Number of glyphs in the run
CFIndex t_length = CTRunGetGlyphCount(p_run);
// Get a copy of the glyph array
MCGMaybeOwnedArray<CGGlyph> t_glyphs(t_length);
if (!t_glyphs.TryBorrow(CTRunGetGlyphsPtr(p_run)))
{
if (t_glyphs.Allocate())
CTRunGetGlyphs(p_run, CFRangeMake(0, t_length), t_glyphs);
}
// Get a copy of the advances array
MCGMaybeOwnedArray<CGPoint> t_points(t_length);
if (!t_points.TryBorrow(CTRunGetPositionsPtr(p_run)))
{
if (t_points.Allocate())
CTRunGetPositions(p_run, CFRangeMake(0, t_length), t_points);
}
// Iterate over the contents of the glyph array
if (t_glyphs != nil && t_points != nil)
{
for (CFIndex i = 0; i < t_length; i++)
{
// Execute the callback
p_callback(t_points[i], t_glyphs[i]);
}
}
}
////////////////////////////////////////////////////////////////////////////////
void MCGContextDrawPlatformText(MCGContextRef self, const unichar_t *p_text, uindex_t p_length, MCGPoint p_location, const MCGFont &p_font, bool p_rtl)
{
if (!MCGContextIsValid(self))
return;
// The paint containing the fill settings
SkPaint t_paint;
if (!MCGContextSetupFill(self, t_paint))
{
self->is_valid = false;
return;
}
// Only use LCD-style anti-aliasing if the layer is opaque.
t_paint.setLCDRenderText(self != nullptr ? MCGContextIsLayerOpaque(self) : true);
// Enable emoji style fonts
t_paint.setEmbeddedBitmapText(true);
// Ensure we use anti-aliasing
t_paint.setAntiAlias(true);
// Ensure we use subpixel positioning
t_paint.setSubpixelText(true);
// We are going to draw text by glyph ID rather than by codeunit
t_paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
// Set the transform we're using
SkMatrix t_matrix;
MCGAffineTransformToSkMatrix(MCGContextGetDeviceTransform(self), t_matrix);
t_paint.setTextMatrix(&t_matrix);
// Create a fully laid-out line of text
CTLineRef t_line = MCGCreateCTLineFromText(p_text, p_length, p_font);
// Iterate over the runs of text within the line
MCGForEachRunInLine(t_line,
[=, &t_paint](CTRunRef p_run)
{
// Get the attribute dictionary for this run
CFDictionaryRef t_attributes = CTRunGetAttributes(p_run);
// Get the CTFontRef used for drawing this run
CTFontRef t_font = static_cast<CTFontRef>(CFDictionaryGetValue(t_attributes, kCTFontAttributeName));
// Calculate the origin of this run
CGAffineTransform t_text_transform = CTRunGetTextMatrix(p_run);
MCGFloat t_x = p_location.x + t_text_transform.tx;
MCGFloat t_y = p_location.y + t_text_transform.ty;
// Convert the CTFontRef into a Skia Typeface object
t_paint.setTypeface(MCGSkTypefaceFromCTFontRef(t_font));
t_paint.setTextSize(CTFontGetSize(t_font));
// Iterate through the glyphs of the run
MCGForEachGlyphInRun(p_run,
[=](CGPoint p_point, CGGlyph p_glyph)
{
// Both Skia and CG glyphs are just indices into the typeface glyph list
SkGlyphID t_glyph = p_glyph;
// Adjust the position for this glyph
MCGFloat t_glyph_x = t_x + p_point.x;
MCGFloat t_glyph_y = t_y + p_point.y;
// Finally, draw this glyph to the canvas
self->layer->canvas->drawText(&t_glyph, sizeof(t_glyph), t_glyph_x, t_glyph_y, t_paint);
});
});
// Release the typeset line
CFRelease(t_line);
}
////////////////////////////////////////////////////////////////////////////////
MCGFloat __MCGContextMeasurePlatformText(MCGContextRef self, const unichar_t *p_text, uindex_t p_length, const MCGFont &p_font, const MCGAffineTransform &p_transform)
{
// Create a fully laid-out line of text
CTLineRef t_line =
MCGCreateCTLineFromText(p_text, p_length, p_font);
if (t_line == nullptr)
{
return 0.0;
}
MCGFloat t_width =
CTLineGetTypographicBounds(t_line, NULL, NULL, NULL);
CFRelease(t_line);
return t_width;
}
bool MCGContextMeasurePlatformTextImageBounds(MCGContextRef self, const unichar_t *p_text, uindex_t p_length, const MCGFont &p_font, const MCGAffineTransform &p_transform, MCGRectangle &r_bounds)
{
// Create a fully laid-out line of text
CTLineRef t_line =
MCGCreateCTLineFromText(p_text, p_length, p_font);
if (t_line == nullptr)
{
return 0.0;
}
CGRect t_bounds =
CTLineGetImageBounds(t_line, s_measure_context);
r_bounds = MCGRectangleMake(t_bounds.origin.x,
-(t_bounds.origin.y + t_bounds.size.height),
t_bounds.size.width,
t_bounds.size.height);
CFRelease(t_line);
return true;
}
////////////////////////////////////////////////////////////////////////////////