forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharfbuzztext.cpp
More file actions
460 lines (349 loc) · 14.1 KB
/
Copy pathharfbuzztext.cpp
File metadata and controls
460 lines (349 loc) · 14.1 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* Copyright (C) 2003-2015 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"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <hb.h>
#include "hb-sk.h"
#include <hb-icu.h>
#include <SkTypeface.h>
#include <SkTypeface_android.h>
#include <SkPoint.h>
#include <SkTypes.h>
#include "foundation-unicode.h"
#include <unicode/uscript.h>
#define HB_SCALE_FACTOR 64
////////////////////////////////////////////////////////////////////////////////
extern bool MCCStringFromUnicodeSubstring(const unichar_t * p_text, uindex_t p_length, char*& r_text);
extern void MCCStringFree(char *p_string);
////////////////////////////////////////////////////////////////////////////////
static void skia_get_replacement_glyph(uint16_t p_size, SkTypeface *p_typeface, uint16_t& glyph, uindex_t& advance)
{
SkPaint paint;
paint . setTextEncoding(SkPaint::kUTF16_TextEncoding);
paint . setTextSize(p_size);
paint . setTypeface(p_typeface);
codepoint_t t_replacement = 0xFFFD;
SkScalar skWidth;
SkRect skBounds;
paint . textToGlyphs(&t_replacement, 2, &glyph);
paint . getTextWidths(&t_replacement, 2, &skWidth, &skBounds);
advance = (uint16_t)skWidth;
}
struct MCGlyphRun
{
uindex_t count;
SkPoint *positions;
uint16_t *glyphs;
SkTypeface *typeface;
};
void MCGlyphRunMake(hb_glyph_info_t *p_infos, hb_glyph_position_t *p_positions, MCGPoint* x_location, uindex_t p_start, uindex_t p_end, const MCGFont &p_font, MCGlyphRun& r_run)
{
uindex_t t_count = p_end - p_start;
MCMemoryAllocate(sizeof(uint16_t) * t_count, r_run . glyphs);
MCMemoryAllocate(sizeof(MCGPoint) * t_count, r_run . positions);
// IM-2016-03-31: [[ Bug 17281 ]] positions may have negative values, so we need to use signed offsets.
index_t x_offset, y_offset;
uindex_t run_index = 0;
uindex_t advance_y = 0;
uindex_t advance_x = 0;
for (uindex_t i = p_start; i < p_end; i++)
{
x_offset = x_location -> x + p_positions[i] . x_offset / HB_SCALE_FACTOR + advance_x;
y_offset = x_location -> y + p_positions[i] . y_offset / HB_SCALE_FACTOR + advance_y;
r_run . positions[run_index] = SkPoint::Make(x_offset, y_offset);
uint16_t t_glyph = p_infos[i] . codepoint;
if (t_glyph)
{
r_run . glyphs[run_index] = t_glyph;
advance_x += p_positions[i] . x_advance / HB_SCALE_FACTOR;
advance_y += p_positions[i] . y_advance / HB_SCALE_FACTOR;
}
else
{
uindex_t advance;
skia_get_replacement_glyph(p_font . size, (SkTypeface *)p_font . fid, r_run . glyphs[run_index], advance);
advance_x += advance;
}
run_index++;
}
x_location -> x += advance_x;
x_location -> y += advance_y;
r_run . typeface = (SkTypeface *)p_font . fid;
r_run . typeface -> ref();
r_run . count = t_count;
}
void MCGlyphRunDestroy(MCGlyphRun& p_run)
{
MCMemoryDeallocate(p_run . glyphs);
MCMemoryDeallocate(p_run . positions);
p_run . typeface -> unref();
}
////////////////////////////////////////////////////////////////////////////////
// AL-2014-10-08: [[ Bug 13542 ]] Cache harfbuzz face structures
static MCGCacheTableRef s_hb_face_cache = nil;
#define kMCHarfbuzzFaceCacheTableSize 32
#define kMCHarfbuzzFaceCacheByteSize kMCHarfbuzzFaceCacheTableSize * 256
#define kMCHarfbuzzFaceCacheMaxOccupancy kMCHarfbuzzFaceCacheTableSize * 0.5
void MCGPlatformInitialize(void)
{
s_hb_face_cache = nil;
/* UNCHECKED */ MCGCacheTableCreate(kMCHarfbuzzFaceCacheTableSize, kMCHarfbuzzFaceCacheMaxOccupancy, kMCHarfbuzzFaceCacheByteSize, s_hb_face_cache);
}
void MCGPlatformFinalize(void)
{
MCGCacheTableDestroy(s_hb_face_cache);
s_hb_face_cache = nil;
}
////////////////////////////////////////////////////////////////////////////////
static hb_font_t *HBSkiaFaceToHBFont(MCHarfbuzzSkiaFace *p_typeface)
{
return hb_sk_font_create(p_typeface, nil);
}
static hb_script_t HBScriptFromText(const unichar_t *p_text, uindex_t p_count)
{
UScriptCode t_script;
t_script = USCRIPT_COMMON;
while (p_count && MCUnicodeIsWhitespace(*p_text))
{
p_text++;
p_count--;
}
if (p_count)
{
UErrorCode t_error = U_ZERO_ERROR;
t_script = uscript_getScript((uint32_t)(*p_text), &t_error);
//MCLog("script: %s", uscript_getShortName(t_script));
}
return hb_icu_script_to_script(t_script);
}
MCHarfbuzzSkiaFace *MCHarfbuzzGetFaceForSkiaTypeface(SkTypeface *p_typeface, uint32_t p_size)
{
bool t_success;
t_success = true;
if (t_success)
t_success = s_hb_face_cache != nil;
void *t_key;
t_key = nil;
uint32_t t_id;
t_id = p_typeface -> uniqueID();
// AL-2014-10-08: [[ Bug 13542 ]] Retrieve harfbuzz face from cache if possible
if (t_success)
{
t_success = MCMemoryNew(sizeof(t_id), t_key);
MCMemoryCopy(t_key, &t_id, sizeof(t_id));
}
if (t_success)
{
MCHarfbuzzSkiaFace *t_hb_sk_face;
MCHarfbuzzSkiaFace **t_hb_sk_face_ptr;
// IM-2015-10-02: [[ Bug 14786 ]] Make sure we store & dereference the *pointer* to MCHarfbuzzSkiaFace
// instead of its contents.
t_hb_sk_face_ptr = (MCHarfbuzzSkiaFace **)MCGCacheTableGet(s_hb_face_cache, t_key, sizeof(t_id));
if (t_hb_sk_face_ptr != nil)
{
t_hb_sk_face = *t_hb_sk_face_ptr;
// AL-2014-10-27: [[ Bug 13802 ]] Make sure to set the size when we retrieve the cached face
t_hb_sk_face -> skia_face -> size = p_size;
MCMemoryDelete(t_key);
return t_hb_sk_face;
}
hb_skia_face_t *t_face;
t_face = new hb_skia_face_t;
t_face -> typeface = p_typeface;
t_face -> size = p_size;
t_hb_sk_face = new MCHarfbuzzSkiaFace;
t_hb_sk_face -> face = nil;
t_hb_sk_face -> skia_face = nil;
hb_sk_set_face(t_hb_sk_face, t_face);
t_hb_sk_face -> skia_face = t_face;
p_typeface -> ref();
// IM-2015-10-02: [[ Bug 14786 ]] Make sure we store & dereference the *pointer* to MCHarfbuzzSkiaFace
// instead of its contents.
MCGCacheTableSet(s_hb_face_cache, t_key, sizeof(t_id), &t_hb_sk_face, sizeof(MCHarfbuzzSkiaFace*));
return t_hb_sk_face;
}
if (!t_success)
MCMemoryDelete(t_key);
return nil;
}
void shape(const unichar_t* p_text, uindex_t p_char_count, MCGPoint p_location, bool p_rtl, const MCGFont &p_font, MCGlyphRun*& r_runs, uindex_t& r_run_count)
{
MCAutoArray<MCGlyphRun> t_runs;
if (p_font . fid == nil)
{
MCLog("%s: Found null font!", __FUNCTION__);
r_run_count = 0;
r_runs = nil;
return;
}
SkTypeface *t_typeface = (SkTypeface *)p_font . fid;
//MCLog("typeface name %d", t_typeface -> uniqueID());
MCHarfbuzzSkiaFace *t_hb_sk_face;
t_hb_sk_face = MCHarfbuzzGetFaceForSkiaTypeface(t_typeface, p_font . size);
if (t_hb_sk_face == nil)
{
r_run_count = 0;
r_runs = nil;
return;
}
// Set up the HarfBuzz buffer
hb_buffer_t *buffer = hb_buffer_create();
hb_buffer_set_unicode_funcs(buffer, hb_icu_get_unicode_funcs());
hb_buffer_set_direction(buffer, p_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
hb_buffer_set_script(buffer, HBScriptFromText(p_text, p_char_count));
hb_buffer_add_utf16(buffer, p_text, p_char_count, 0, p_char_count);
hb_shape(HBSkiaFaceToHBFont(t_hb_sk_face), buffer, NULL, 0);
int glyph_count = hb_buffer_get_length(buffer);
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buffer, 0);
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buffer, 0);
uindex_t t_cur_glyph = 0;
uindex_t t_start, t_end;
uindex_t t_char_index = 0;
// deal with runs of supported and unsupported glyphs
while (t_cur_glyph < glyph_count)
{
MCGlyphRun t_run;
t_start = t_end = t_cur_glyph;
// Run of successfully shaped glyphs
while (t_cur_glyph < glyph_count && glyph_info[t_cur_glyph] . codepoint != 0)
{
t_char_index++;
t_end++;
t_cur_glyph++;
}
if (t_start != t_end)
{
MCGlyphRunMake(glyph_info, glyph_pos, &p_location, t_start, t_end, p_font, t_run);
t_runs . Push(t_run);
t_start = t_cur_glyph;
t_end = t_cur_glyph;
}
// Deal with run of unsupported characters for this font.
while (t_cur_glyph < glyph_count && glyph_info[t_cur_glyph] . codepoint == 0)
{
t_end++;
t_cur_glyph++;
}
if (t_start != t_end)
{
// Need to get a fallback font here.
SkTypeface *t_fallback = nil;
#if defined(__ANDROID__)
SkCreateFallbackTypefaceForChar(*(p_text + t_char_index), SkTypeface::kNormal);
#endif /* __ANDROID__ */
// TODO: This currently seems to return nil all the time.
if (t_fallback != nil)
{
MCGFont t_font = MCGFontMake(t_fallback, p_font . size, p_font . fixed_advance, p_font . m_ascent, p_font . m_descent, p_font . m_leading, p_font . ideal);
MCGlyphRun *t_fallback_runs;
uindex_t t_fallback_run_count;
shape(p_text + t_char_index, t_end - t_start, p_location, p_rtl, t_font, t_fallback_runs, t_fallback_run_count);
for (uindex_t i = 0; i < t_fallback_run_count; i++)
t_runs . Push(t_fallback_runs[i]);
t_fallback -> unref();
MCMemoryDeleteArray(t_fallback_runs);
}
else
{
// For now, just fail to display the glyphs. Should display 'missing character' glyph.
MCGlyphRunMake(glyph_info, glyph_pos, &p_location, t_start, t_end, p_font, t_run);
t_runs . Push(t_run);
}
t_start = t_cur_glyph;
t_end = t_cur_glyph;
}
t_char_index += (t_end - t_start);
}
hb_buffer_destroy(buffer);
t_runs . Take(r_runs, r_run_count);
}
////////////////////////////////////////////////////////////////////////////////
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;
SkPaint t_paint;
t_paint . setStyle(SkPaint::kFill_Style);
t_paint . setAntiAlias(true);
t_paint . setColor(MCGColorToSkColor(self -> state -> fill_color));
t_paint . setTextSize(p_font . size);
t_paint . setTextEncoding(SkPaint::kGlyphID_TextEncoding);
SkXfermode *t_blend_mode;
t_blend_mode = MCGBlendModeToSkXfermode(self -> state -> blend_mode);
t_paint . setXfermode(t_blend_mode);
if (t_blend_mode != NULL)
t_blend_mode -> unref();
MCGlyphRun *t_glyph_runs;
uindex_t t_count;
shape(p_text, p_length/2, p_location, p_rtl, p_font, t_glyph_runs, t_count);
for (uindex_t i = 0; i < t_count; i++)
{
t_paint . setTypeface(t_glyph_runs[i] . typeface);
self -> layer -> canvas -> drawPosText(&(t_glyph_runs[i] . glyphs[0]), t_glyph_runs[i] . count * 2, &(t_glyph_runs[i] . positions[0]), t_paint);
MCGlyphRunDestroy(t_glyph_runs[i]);
}
MCMemoryDeleteArray(t_glyph_runs);
self -> is_valid = true;
}
MCGFloat __MCGContextMeasurePlatformText(MCGContextRef self, const unichar_t *p_text, uindex_t p_length, const MCGFont &p_font, const MCGAffineTransform &p_transform)
{
//if (!MCGContextIsValid(self))
// return 0.0;
MCGFloat t_width;
t_width = 0.0;
SkPaint t_paint;
t_paint . setTextSize(p_font . size);
t_paint . setTextEncoding(SkPaint::kGlyphID_TextEncoding);
MCGlyphRun *t_glyph_runs;
uindex_t t_count;
MCGPoint t_dummy = MCGPointMake(0,0);
shape(p_text, p_length/2, t_dummy, false, p_font, t_glyph_runs, t_count);
for (uindex_t i = 0; i < t_count; i++)
{
t_paint . setTypeface(t_glyph_runs[i] . typeface);
t_width += (MCGFloat) t_paint . measureText(&(t_glyph_runs[i] . glyphs[0]), t_glyph_runs[i] . count * 2);
MCGlyphRunDestroy(t_glyph_runs[i]);
}
MCMemoryDeleteArray(t_glyph_runs);
//self -> is_valid = t_success;
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)
{
//if (!MCGContextIsValid(self))
// return 0.0;
SkRect t_bounds;
t_bounds = SkRect::MakeEmpty();
SkPaint t_paint;
t_paint . setTextSize(p_font . size);
t_paint . setTextEncoding(SkPaint::kGlyphID_TextEncoding);
MCGlyphRun *t_glyph_runs;
uindex_t t_count;
MCGPoint t_dummy = MCGPointMake(0,0);
shape(p_text, p_length/2, t_dummy, false, p_font, t_glyph_runs, t_count);
for (uindex_t i = 0; i < t_count; i++)
{
SkRect t_glyph_bounds;
t_paint . setTypeface(t_glyph_runs[i] . typeface);
t_paint . measureText(&(t_glyph_runs[i] . glyphs[0]), t_glyph_runs[i] . count * 2, &t_glyph_bounds);
if (!t_bounds.isEmpty())
t_glyph_bounds.offsetTo(t_bounds.right(), t_glyph_bounds.y());
t_bounds.join(t_glyph_bounds);
MCGlyphRunDestroy(t_glyph_runs[i]);
}
MCMemoryDeleteArray(t_glyph_runs);
r_bounds = MCGRectangleFromSkRect(t_bounds);
return true;
}