blob: 61f9c35a1104affe35179c7f3038dcc172a76564 [file] [log] [blame]
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001/*
Behdad Esfahbod36136962013-08-12 00:33:28 -04002 * Copyright © 2012,2013 Mozilla Foundation.
3 * Copyright © 2012,2013 Google, Inc.
Jonathan Kewaa6d8492012-07-24 15:52:32 -04004 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Mozilla Author(s): Jonathan Kew
Behdad Esfahbod301168d2012-07-30 17:48:04 -040026 * Google Author(s): Behdad Esfahbod
Jonathan Kewaa6d8492012-07-24 15:52:32 -040027 */
28
Behdad Esfahbod027857d2012-07-26 17:34:25 -040029#define HB_SHAPER coretext
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040030
31#include "hb-private.hh"
32#include "hb-debug.hh"
Behdad Esfahbod301168d2012-07-30 17:48:04 -040033#include "hb-shaper-impl-private.hh"
Jonathan Kewaa6d8492012-07-24 15:52:32 -040034
Jonathan Kewaa6d8492012-07-24 15:52:32 -040035#include "hb-coretext.h"
Behdad Esfahbod6a2cbc62017-10-12 10:46:09 +020036#include <math.h>
Jonathan Kewaa6d8492012-07-24 15:52:32 -040037
Behdad Esfahbod06c14222017-10-11 15:29:53 +020038/* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020039#define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020040
41static CGFloat
Behdad Esfahbod12453952017-12-17 12:32:33 -050042coretext_font_size_from_ptem (float ptem)
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020043{
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020044 /* CoreText points are CSS pixels (96 per inch),
45 * NOT typographic points (72 per inch).
46 *
47 * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
48 */
49 ptem *= 96.f / 72.f;
50 return ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : ptem;
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020051}
Behdad Esfahbod12453952017-12-17 12:32:33 -050052static float
53coretext_font_size_to_ptem (CGFloat size)
54{
55 size *= 72.f / 96.f;
56 return size <= 0.f ? 0 : size;
57}
Jonathan Kewaa6d8492012-07-24 15:52:32 -040058
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070059static void
60release_table_data (void *user_data)
61{
62 CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data);
63 CFRelease(cf_data);
64}
65
66static hb_blob_t *
67reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
68{
69 CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data);
70 CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag);
71 if (unlikely (!cf_data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020072 return nullptr;
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070073
74 const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data));
75 const size_t length = CFDataGetLength (cf_data);
76 if (!data || !length)
Bruce Mitchener0c660432018-01-31 20:24:27 +070077 {
78 CFRelease (cf_data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020079 return nullptr;
Bruce Mitchener0c660432018-01-31 20:24:27 +070080 }
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070081
82 return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY,
83 reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)),
84 release_table_data);
85}
86
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020087static void
88_hb_cg_font_release (void *data)
89{
90 CGFontRelease ((CGFontRef) data);
91}
92
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070093
Behdad Esfahbod466b3e52017-02-03 16:43:25 -080094HB_SHAPER_DATA_ENSURE_DEFINE(coretext, face)
Dominik Röttschesa5ebe1d2017-10-11 13:32:38 +020095HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(coretext, font,
Behdad Esfahbod12453952017-12-17 12:32:33 -050096 fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size_from_ptem (font->ptem)) <= .5
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020097)
Jonathan Kewaa6d8492012-07-24 15:52:32 -040098
Behdad Esfahbod90194ef2016-02-22 15:42:53 +090099static CTFontDescriptorRef
100get_last_resort_font_desc (void)
101{
102 // TODO Handle allocation failures?
103 CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0);
104 CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault,
105 (const void **) &last_resort,
106 1,
107 &kCFTypeArrayCallBacks);
108 CFRelease (last_resort);
109 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
110 (const void **) &kCTFontCascadeListAttribute,
111 (const void **) &cascade_list,
112 1,
113 &kCFTypeDictionaryKeyCallBacks,
114 &kCFTypeDictionaryValueCallBacks);
115 CFRelease (cascade_list);
116
117 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
118 CFRelease (attributes);
119 return font_desc;
120}
121
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900122static void
123release_data (void *info, const void *data, size_t size)
124{
125 assert (hb_blob_get_length ((hb_blob_t *) info) == size &&
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200126 hb_blob_get_data ((hb_blob_t *) info, nullptr) == data);
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900127
128 hb_blob_destroy ((hb_blob_t *) info);
129}
130
131static CGFontRef
132create_cg_font (hb_face_t *face)
133{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200134 CGFontRef cg_font = nullptr;
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200135 if (face->destroy == _hb_cg_font_release)
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900136 {
137 cg_font = CGFontRetain ((CGFontRef) face->user_data);
138 }
139 else
140 {
141 hb_blob_t *blob = hb_face_reference_blob (face);
142 unsigned int blob_length;
143 const char *blob_data = hb_blob_get_data (blob, &blob_length);
144 if (unlikely (!blob_length))
145 DEBUG_MSG (CORETEXT, face, "Face has empty blob");
146
147 CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data);
148 if (likely (provider))
149 {
150 cg_font = CGFontCreateWithDataProvider (provider);
151 if (unlikely (!cg_font))
152 DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed");
153 CGDataProviderRelease (provider);
154 }
155 }
156 return cg_font;
157}
158
Behdad Esfahbode5611222016-02-22 15:28:37 +0900159static CTFontRef
160create_ct_font (CGFontRef cg_font, CGFloat font_size)
161{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200162 CTFontRef ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200163
164 /* CoreText does not enable trak table usage / tracking when creating a CTFont
165 * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems
166 * to be through the CTFontCreateUIFontForLanguage call. */
167 CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font);
168 if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) ||
169 CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay")))
170 {
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500171#if MAC_OS_X_VERSION_MIN_REQUIRED < 1080
172# define kCTFontUIFontSystem kCTFontSystemFontType
173# define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType
174#endif
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200175 CTFontUIFontType font_type = kCTFontUIFontSystem;
176 if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold")))
177 font_type = kCTFontUIFontEmphasizedSystem;
178
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200179 ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200180 CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font);
181 if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo)
182 {
183 CFRelease(ct_font);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200184 ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200185 }
186 CFRelease (ct_result_name);
187 }
188 CFRelease (cg_postscript_name);
189
190 if (!ct_font)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200191 ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200192
Behdad Esfahbode5611222016-02-22 15:28:37 +0900193 if (unlikely (!ct_font)) {
194 DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200195 return nullptr;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900196 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700197
198 /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter
199 * bug indicate that the cascade list reconfiguration occasionally causes
200 * crashes in CoreText on OS X 10.9, thus let's skip this step on older
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300201 * operating system versions. Except for the emoji font, where _not_
202 * reconfiguring the cascade list causes CoreText crashes. For details, see
203 * crbug.com/549610 */
Ebrahim Byagowifc4e6712016-09-09 23:28:28 +0430204 // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200205 if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) {
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300206 CFStringRef fontName = CTFontCopyPostScriptName (ct_font);
207 bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo;
208 CFRelease (fontName);
209 if (!isEmojiFont)
210 return ct_font;
211 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700212
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430213 CFURLRef original_url = nullptr;
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500214#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
215 ATSFontRef atsFont;
216 FSRef fsref;
217 OSStatus status;
218 atsFont = CTFontGetPlatformFont (ct_font, NULL);
219 status = ATSFontGetFileReference (atsFont, &fsref);
220 if (status == noErr)
221 original_url = CFURLCreateFromFSRef (NULL, &fsref);
222#else
223 original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute);
224#endif
Behdad Esfahbode5611222016-02-22 15:28:37 +0900225
226 /* Create font copy with cascade list that has LastResort first; this speeds up CoreText
227 * font fallback which we don't need anyway. */
228 {
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900229 CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc ();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200230 CTFontRef new_ct_font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, last_resort_font_desc);
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900231 CFRelease (last_resort_font_desc);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900232 if (new_ct_font)
233 {
Behdad Esfahbodfc9de442016-06-30 09:46:52 -0700234 /* The CTFontCreateCopyWithAttributes call fails to stay on the same font
235 * when reconfiguring the cascade list and may switch to a different font
236 * when there are fonts that go by the same name, since the descriptor is
237 * just name and size.
238 *
239 * Avoid reconfiguring the cascade lists if the new font is outside the
240 * system locations that we cannot access from the sandboxed renderer
241 * process in Blink. This can be detected by the new file URL location
242 * that the newly found font points to. */
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430243 CFURLRef new_url = nullptr;
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500244#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
245 atsFont = CTFontGetPlatformFont (new_ct_font, NULL);
246 status = ATSFontGetFileReference (atsFont, &fsref);
247 if (status == noErr)
248 new_url = CFURLCreateFromFSRef (NULL, &fsref);
249#else
250 new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute);
251#endif
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430252 // Keep reconfigured font if URL cannot be retrieved (seems to be the case
253 // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606
254 if (!original_url || !new_url || CFEqual (original_url, new_url)) {
Dominik Röttschesa0223272016-06-16 14:19:39 +0200255 CFRelease (ct_font);
256 ct_font = new_ct_font;
257 } else {
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430258 CFRelease (new_ct_font);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200259 DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed.");
260 }
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430261 if (new_url)
262 CFRelease (new_url);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900263 }
264 else
265 DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed");
266 }
267
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430268 if (original_url)
269 CFRelease (original_url);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200270 return ct_font;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900271}
272
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400273hb_coretext_shaper_face_data_t *
274_hb_coretext_shaper_face_data_create (hb_face_t *face)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400275{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200276 CGFontRef cg_font = create_cg_font (face);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400277
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200278 if (unlikely (!cg_font))
Behdad Esfahboda9e25e92014-03-14 19:55:46 -0700279 {
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900280 DEBUG_MSG (CORETEXT, face, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200281 return nullptr;
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900282 }
283
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200284 return (hb_coretext_shaper_face_data_t *) cg_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400285}
286
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400287void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200288_hb_coretext_shaper_face_data_destroy (hb_coretext_shaper_face_data_t *data)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400289{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200290 CFRelease ((CGFontRef) data);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400291}
292
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800293hb_face_t *
294hb_coretext_face_create (CGFontRef cg_font)
295{
296 return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
297}
298
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430299/*
300 * Since: 0.9.10
301 */
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500302CGFontRef
Behdad Esfahbode923e642012-12-09 19:39:40 -0500303hb_coretext_face_get_cg_font (hb_face_t *face)
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500304{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200305 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200306 return (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500307}
308
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400309
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400310hb_coretext_shaper_font_data_t *
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200311_hb_coretext_shaper_font_data_create (hb_font_t *font)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400312{
Dominik Röttschesdb7a73c2017-10-11 13:24:39 +0200313 hb_face_t *face = font->face;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200314 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200315 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200316
Behdad Esfahbod12453952017-12-17 12:32:33 -0500317 CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size_from_ptem (font->ptem));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200318
319 if (unlikely (!ct_font))
320 {
321 DEBUG_MSG (CORETEXT, font, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200322 return nullptr;
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200323 }
324
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200325 return (hb_coretext_shaper_font_data_t *) ct_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400326}
327
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400328void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200329_hb_coretext_shaper_font_data_destroy (hb_coretext_shaper_font_data_t *data)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400330{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200331 CFRelease ((CTFontRef) data);
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400332}
333
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800334/*
335 * Since: 1.7.2
336 */
337hb_font_t *
338hb_coretext_font_create (CTFontRef ct_font)
339{
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +0700340 CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr);
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800341 hb_face_t *face = hb_coretext_face_create (cg_font);
342 CFRelease (cg_font);
343 hb_font_t *font = hb_font_create (face);
344 hb_face_destroy (face);
345
346 if (unlikely (hb_object_is_inert (font)))
347 return font;
348
Behdad Esfahbod12453952017-12-17 12:32:33 -0500349 hb_font_set_ptem (font, coretext_font_size_to_ptem (CTFontGetSize(ct_font)));
350
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800351 /* Let there be dragons here... */
352 HB_SHAPER_DATA_GET (font) = (hb_coretext_shaper_font_data_t *) CFRetain (ct_font);
353
354 return font;
355}
356
357CTFontRef
358hb_coretext_font_get_ct_font (hb_font_t *font)
359{
360 if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr;
361 return (CTFontRef) HB_SHAPER_DATA_GET (font);
362}
363
364
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400365
366/*
367 * shaper shape_plan data
368 */
369
370struct hb_coretext_shaper_shape_plan_data_t {};
371
372hb_coretext_shaper_shape_plan_data_t *
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400373_hb_coretext_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
374 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700375 unsigned int num_user_features HB_UNUSED,
376 const int *coords HB_UNUSED,
377 unsigned int num_coords HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400378{
379 return (hb_coretext_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
380}
381
382void
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400383_hb_coretext_shaper_shape_plan_data_destroy (hb_coretext_shaper_shape_plan_data_t *data HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400384{
385}
386
387
388/*
389 * shaper
390 */
391
Behdad Esfahbod36136962013-08-12 00:33:28 -0400392struct feature_record_t {
393 unsigned int feature;
394 unsigned int setting;
395};
396
397struct active_feature_t {
398 feature_record_t rec;
399 unsigned int order;
400
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600401 static int cmp (const void *pa, const void *pb) {
402 const active_feature_t *a = (const active_feature_t *) pa;
403 const active_feature_t *b = (const active_feature_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400404 return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 :
405 a->order < b->order ? -1 : a->order > b->order ? 1 :
406 a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 :
407 0;
408 }
409 bool operator== (const active_feature_t *f) {
410 return cmp (this, f) == 0;
411 }
412};
413
414struct feature_event_t {
415 unsigned int index;
416 bool start;
417 active_feature_t feature;
418
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600419 static int cmp (const void *pa, const void *pb) {
420 const feature_event_t *a = (const feature_event_t *) pa;
421 const feature_event_t *b = (const feature_event_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400422 return a->index < b->index ? -1 : a->index > b->index ? 1 :
423 a->start < b->start ? -1 : a->start > b->start ? 1 :
424 active_feature_t::cmp (&a->feature, &b->feature);
425 }
426};
427
428struct range_record_t {
429 CTFontRef font;
430 unsigned int index_first; /* == start */
431 unsigned int index_last; /* == end - 1 */
432};
433
434
435/* The following enum members are added in OS X 10.8. */
436#define kAltHalfWidthTextSelector 6
437#define kAltProportionalTextSelector 5
438#define kAlternateHorizKanaOffSelector 1
439#define kAlternateHorizKanaOnSelector 0
440#define kAlternateKanaType 34
441#define kAlternateVertKanaOffSelector 3
442#define kAlternateVertKanaOnSelector 2
443#define kCaseSensitiveLayoutOffSelector 1
444#define kCaseSensitiveLayoutOnSelector 0
445#define kCaseSensitiveLayoutType 33
446#define kCaseSensitiveSpacingOffSelector 3
447#define kCaseSensitiveSpacingOnSelector 2
448#define kContextualAlternatesOffSelector 1
449#define kContextualAlternatesOnSelector 0
450#define kContextualAlternatesType 36
451#define kContextualLigaturesOffSelector 19
452#define kContextualLigaturesOnSelector 18
453#define kContextualSwashAlternatesOffSelector 5
454#define kContextualSwashAlternatesOnSelector 4
455#define kDefaultLowerCaseSelector 0
456#define kDefaultUpperCaseSelector 0
457#define kHistoricalLigaturesOffSelector 21
458#define kHistoricalLigaturesOnSelector 20
459#define kHojoCharactersSelector 12
460#define kJIS2004CharactersSelector 11
461#define kLowerCasePetiteCapsSelector 2
462#define kLowerCaseSmallCapsSelector 1
463#define kLowerCaseType 37
464#define kMathematicalGreekOffSelector 11
465#define kMathematicalGreekOnSelector 10
466#define kNLCCharactersSelector 13
467#define kQuarterWidthTextSelector 4
468#define kScientificInferiorsSelector 4
469#define kStylisticAltEightOffSelector 17
470#define kStylisticAltEightOnSelector 16
471#define kStylisticAltEighteenOffSelector 37
472#define kStylisticAltEighteenOnSelector 36
473#define kStylisticAltElevenOffSelector 23
474#define kStylisticAltElevenOnSelector 22
475#define kStylisticAltFifteenOffSelector 31
476#define kStylisticAltFifteenOnSelector 30
477#define kStylisticAltFiveOffSelector 11
478#define kStylisticAltFiveOnSelector 10
479#define kStylisticAltFourOffSelector 9
480#define kStylisticAltFourOnSelector 8
481#define kStylisticAltFourteenOffSelector 29
482#define kStylisticAltFourteenOnSelector 28
483#define kStylisticAltNineOffSelector 19
484#define kStylisticAltNineOnSelector 18
485#define kStylisticAltNineteenOffSelector 39
486#define kStylisticAltNineteenOnSelector 38
487#define kStylisticAltOneOffSelector 3
488#define kStylisticAltOneOnSelector 2
489#define kStylisticAltSevenOffSelector 15
490#define kStylisticAltSevenOnSelector 14
491#define kStylisticAltSeventeenOffSelector 35
492#define kStylisticAltSeventeenOnSelector 34
493#define kStylisticAltSixOffSelector 13
494#define kStylisticAltSixOnSelector 12
495#define kStylisticAltSixteenOffSelector 33
496#define kStylisticAltSixteenOnSelector 32
497#define kStylisticAltTenOffSelector 21
498#define kStylisticAltTenOnSelector 20
499#define kStylisticAltThirteenOffSelector 27
500#define kStylisticAltThirteenOnSelector 26
501#define kStylisticAltThreeOffSelector 7
502#define kStylisticAltThreeOnSelector 6
503#define kStylisticAltTwelveOffSelector 25
504#define kStylisticAltTwelveOnSelector 24
505#define kStylisticAltTwentyOffSelector 41
506#define kStylisticAltTwentyOnSelector 40
507#define kStylisticAltTwoOffSelector 5
508#define kStylisticAltTwoOnSelector 4
509#define kStylisticAlternativesType 35
510#define kSwashAlternatesOffSelector 3
511#define kSwashAlternatesOnSelector 2
512#define kThirdWidthTextSelector 3
513#define kTraditionalNamesCharactersSelector 14
514#define kUpperCasePetiteCapsSelector 2
515#define kUpperCaseSmallCapsSelector 1
516#define kUpperCaseType 38
517
518/* Table data courtesy of Apple. */
Behdad Esfahbod522b1cc2014-08-14 13:29:30 -0400519static const struct feature_mapping_t {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400520 FourCharCode otFeatureTag;
521 uint16_t aatFeatureType;
522 uint16_t selectorToEnable;
523 uint16_t selectorToDisable;
524} feature_mappings[] = {
525 { 'c2pc', kUpperCaseType, kUpperCasePetiteCapsSelector, kDefaultUpperCaseSelector },
526 { 'c2sc', kUpperCaseType, kUpperCaseSmallCapsSelector, kDefaultUpperCaseSelector },
527 { 'calt', kContextualAlternatesType, kContextualAlternatesOnSelector, kContextualAlternatesOffSelector },
528 { 'case', kCaseSensitiveLayoutType, kCaseSensitiveLayoutOnSelector, kCaseSensitiveLayoutOffSelector },
529 { 'clig', kLigaturesType, kContextualLigaturesOnSelector, kContextualLigaturesOffSelector },
530 { 'cpsp', kCaseSensitiveLayoutType, kCaseSensitiveSpacingOnSelector, kCaseSensitiveSpacingOffSelector },
531 { 'cswh', kContextualAlternatesType, kContextualSwashAlternatesOnSelector, kContextualSwashAlternatesOffSelector },
532 { 'dlig', kLigaturesType, kRareLigaturesOnSelector, kRareLigaturesOffSelector },
533 { 'expt', kCharacterShapeType, kExpertCharactersSelector, 16 },
534 { 'frac', kFractionsType, kDiagonalFractionsSelector, kNoFractionsSelector },
535 { 'fwid', kTextSpacingType, kMonospacedTextSelector, 7 },
536 { 'halt', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
537 { 'hist', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
538 { 'hkna', kAlternateKanaType, kAlternateHorizKanaOnSelector, kAlternateHorizKanaOffSelector, },
539 { 'hlig', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
540 { 'hngl', kTransliterationType, kHanjaToHangulSelector, kNoTransliterationSelector },
541 { 'hojo', kCharacterShapeType, kHojoCharactersSelector, 16 },
542 { 'hwid', kTextSpacingType, kHalfWidthTextSelector, 7 },
543 { 'ital', kItalicCJKRomanType, kCJKItalicRomanOnSelector, kCJKItalicRomanOffSelector },
544 { 'jp04', kCharacterShapeType, kJIS2004CharactersSelector, 16 },
545 { 'jp78', kCharacterShapeType, kJIS1978CharactersSelector, 16 },
546 { 'jp83', kCharacterShapeType, kJIS1983CharactersSelector, 16 },
547 { 'jp90', kCharacterShapeType, kJIS1990CharactersSelector, 16 },
548 { 'liga', kLigaturesType, kCommonLigaturesOnSelector, kCommonLigaturesOffSelector },
549 { 'lnum', kNumberCaseType, kUpperCaseNumbersSelector, 2 },
550 { 'mgrk', kMathematicalExtrasType, kMathematicalGreekOnSelector, kMathematicalGreekOffSelector },
551 { 'nlck', kCharacterShapeType, kNLCCharactersSelector, 16 },
552 { 'onum', kNumberCaseType, kLowerCaseNumbersSelector, 2 },
553 { 'ordn', kVerticalPositionType, kOrdinalsSelector, kNormalPositionSelector },
554 { 'palt', kTextSpacingType, kAltProportionalTextSelector, 7 },
555 { 'pcap', kLowerCaseType, kLowerCasePetiteCapsSelector, kDefaultLowerCaseSelector },
556 { 'pkna', kTextSpacingType, kProportionalTextSelector, 7 },
557 { 'pnum', kNumberSpacingType, kProportionalNumbersSelector, 4 },
558 { 'pwid', kTextSpacingType, kProportionalTextSelector, 7 },
559 { 'qwid', kTextSpacingType, kQuarterWidthTextSelector, 7 },
560 { 'ruby', kRubyKanaType, kRubyKanaOnSelector, kRubyKanaOffSelector },
561 { 'sinf', kVerticalPositionType, kScientificInferiorsSelector, kNormalPositionSelector },
562 { 'smcp', kLowerCaseType, kLowerCaseSmallCapsSelector, kDefaultLowerCaseSelector },
563 { 'smpl', kCharacterShapeType, kSimplifiedCharactersSelector, 16 },
564 { 'ss01', kStylisticAlternativesType, kStylisticAltOneOnSelector, kStylisticAltOneOffSelector },
565 { 'ss02', kStylisticAlternativesType, kStylisticAltTwoOnSelector, kStylisticAltTwoOffSelector },
566 { 'ss03', kStylisticAlternativesType, kStylisticAltThreeOnSelector, kStylisticAltThreeOffSelector },
567 { 'ss04', kStylisticAlternativesType, kStylisticAltFourOnSelector, kStylisticAltFourOffSelector },
568 { 'ss05', kStylisticAlternativesType, kStylisticAltFiveOnSelector, kStylisticAltFiveOffSelector },
569 { 'ss06', kStylisticAlternativesType, kStylisticAltSixOnSelector, kStylisticAltSixOffSelector },
570 { 'ss07', kStylisticAlternativesType, kStylisticAltSevenOnSelector, kStylisticAltSevenOffSelector },
571 { 'ss08', kStylisticAlternativesType, kStylisticAltEightOnSelector, kStylisticAltEightOffSelector },
572 { 'ss09', kStylisticAlternativesType, kStylisticAltNineOnSelector, kStylisticAltNineOffSelector },
573 { 'ss10', kStylisticAlternativesType, kStylisticAltTenOnSelector, kStylisticAltTenOffSelector },
574 { 'ss11', kStylisticAlternativesType, kStylisticAltElevenOnSelector, kStylisticAltElevenOffSelector },
575 { 'ss12', kStylisticAlternativesType, kStylisticAltTwelveOnSelector, kStylisticAltTwelveOffSelector },
576 { 'ss13', kStylisticAlternativesType, kStylisticAltThirteenOnSelector, kStylisticAltThirteenOffSelector },
577 { 'ss14', kStylisticAlternativesType, kStylisticAltFourteenOnSelector, kStylisticAltFourteenOffSelector },
578 { 'ss15', kStylisticAlternativesType, kStylisticAltFifteenOnSelector, kStylisticAltFifteenOffSelector },
579 { 'ss16', kStylisticAlternativesType, kStylisticAltSixteenOnSelector, kStylisticAltSixteenOffSelector },
580 { 'ss17', kStylisticAlternativesType, kStylisticAltSeventeenOnSelector, kStylisticAltSeventeenOffSelector },
581 { 'ss18', kStylisticAlternativesType, kStylisticAltEighteenOnSelector, kStylisticAltEighteenOffSelector },
582 { 'ss19', kStylisticAlternativesType, kStylisticAltNineteenOnSelector, kStylisticAltNineteenOffSelector },
583 { 'ss20', kStylisticAlternativesType, kStylisticAltTwentyOnSelector, kStylisticAltTwentyOffSelector },
584 { 'subs', kVerticalPositionType, kInferiorsSelector, kNormalPositionSelector },
585 { 'sups', kVerticalPositionType, kSuperiorsSelector, kNormalPositionSelector },
586 { 'swsh', kContextualAlternatesType, kSwashAlternatesOnSelector, kSwashAlternatesOffSelector },
587 { 'titl', kStyleOptionsType, kTitlingCapsSelector, kNoStyleOptionsSelector },
588 { 'tnam', kCharacterShapeType, kTraditionalNamesCharactersSelector, 16 },
589 { 'tnum', kNumberSpacingType, kMonospacedNumbersSelector, 4 },
590 { 'trad', kCharacterShapeType, kTraditionalCharactersSelector, 16 },
591 { 'twid', kTextSpacingType, kThirdWidthTextSelector, 7 },
592 { 'unic', kLetterCaseType, 14, 15 },
593 { 'valt', kTextSpacingType, kAltProportionalTextSelector, 7 },
594 { 'vert', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
595 { 'vhal', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
596 { 'vkna', kAlternateKanaType, kAlternateVertKanaOnSelector, kAlternateVertKanaOffSelector },
597 { 'vpal', kTextSpacingType, kAltProportionalTextSelector, 7 },
598 { 'vrt2', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
599 { 'zero', kTypographicExtrasType, kSlashedZeroOnSelector, kSlashedZeroOffSelector },
600};
601
602static int
603_hb_feature_mapping_cmp (const void *key_, const void *entry_)
604{
605 unsigned int key = * (unsigned int *) key_;
606 const feature_mapping_t * entry = (const feature_mapping_t *) entry_;
607 return key < entry->otFeatureTag ? -1 :
608 key > entry->otFeatureTag ? 1 :
609 0;
610}
611
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400612hb_bool_t
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400613_hb_coretext_shape (hb_shape_plan_t *shape_plan,
614 hb_font_t *font,
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400615 hb_buffer_t *buffer,
616 const hb_feature_t *features,
617 unsigned int num_features)
618{
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400619 hb_face_t *face = font->face;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200620 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
621 CTFontRef ct_font = (CTFontRef) HB_SHAPER_DATA_GET (font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400622
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200623 CGFloat ct_font_size = CTFontGetSize (ct_font);
Behdad Esfahbod061105e2016-02-22 14:59:39 +0900624 CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
625 CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
626
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400627 /* Attach marks to their bases, to match the 'ot' shaper.
628 * Adapted from hb-ot-shape:hb_form_clusters().
629 * Note that this only makes us be closer to the 'ot' shaper,
630 * but by no means the same. For example, if there's
631 * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will
632 * continue pointing to B2 even though B2 was merged into B1's
633 * cluster... */
Behdad Esfahbod62c27112016-02-22 15:07:20 +0900634 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400635 {
636 hb_unicode_funcs_t *unicode = buffer->unicode;
637 unsigned int count = buffer->len;
638 hb_glyph_info_t *info = buffer->info;
639 for (unsigned int i = 1; i < count; i++)
640 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint)))
641 buffer->merge_clusters (i - 1, i + 1);
642 }
643
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400644 hb_auto_t<hb_vector_t<feature_record_t> > feature_records;
645 hb_auto_t<hb_vector_t<range_record_t> > range_records;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400646
Behdad Esfahbod36136962013-08-12 00:33:28 -0400647 /*
648 * Set up features.
649 * (copied + modified from code from hb-uniscribe.cc)
650 */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400651 if (num_features)
652 {
653 /* Sort features by start/end events. */
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400654 hb_auto_t<hb_vector_t<feature_event_t> > feature_events;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400655 for (unsigned int i = 0; i < num_features; i++)
656 {
657 const feature_mapping_t * mapping = (const feature_mapping_t *) bsearch (&features[i].tag,
658 feature_mappings,
659 ARRAY_LENGTH (feature_mappings),
660 sizeof (feature_mappings[0]),
661 _hb_feature_mapping_cmp);
662 if (!mapping)
663 continue;
664
665 active_feature_t feature;
666 feature.rec.feature = mapping->aatFeatureType;
667 feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable;
668 feature.order = i;
669
670 feature_event_t *event;
671
672 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400673 event->index = features[i].start;
674 event->start = true;
675 event->feature = feature;
676
677 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400678 event->index = features[i].end;
679 event->start = false;
680 event->feature = feature;
681 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400682 feature_events.qsort ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400683 /* Add a strategic final event. */
684 {
685 active_feature_t feature;
686 feature.rec.feature = HB_TAG_NONE;
687 feature.rec.setting = 0;
688 feature.order = num_features + 1;
689
690 feature_event_t *event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400691 event->index = 0; /* This value does magic. */
692 event->start = false;
693 event->feature = feature;
694 }
695
696 /* Scan events and save features for each range. */
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400697 hb_auto_t<hb_vector_t<active_feature_t> > active_features;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400698 unsigned int last_index = 0;
699 for (unsigned int i = 0; i < feature_events.len; i++)
700 {
701 feature_event_t *event = &feature_events[i];
702
703 if (event->index != last_index)
704 {
705 /* Save a snapshot of active features and the range. */
706 range_record_t *range = range_records.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400707
Behdad Esfahbod36136962013-08-12 00:33:28 -0400708 if (active_features.len)
709 {
710 CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
711
712 /* TODO sort and resolve conflicting features? */
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400713 /* active_features.qsort (); */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400714 for (unsigned int j = 0; j < active_features.len; j++)
715 {
Cosimo Lupo9813be32017-07-14 17:11:46 +0100716 CFStringRef keys[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400717 kCTFontFeatureTypeIdentifierKey,
718 kCTFontFeatureSelectorIdentifierKey
719 };
Cosimo Lupo9813be32017-07-14 17:11:46 +0100720 CFNumberRef values[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400721 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature),
722 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting)
723 };
Behdad Esfahbod80cc0a72017-10-17 11:14:48 -0700724 static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), "");
Behdad Esfahbod36136962013-08-12 00:33:28 -0400725 CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault,
726 (const void **) keys,
727 (const void **) values,
Cosimo Lupo9813be32017-07-14 17:11:46 +0100728 ARRAY_LENGTH (keys),
Behdad Esfahbod36136962013-08-12 00:33:28 -0400729 &kCFTypeDictionaryKeyCallBacks,
730 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100731 for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++)
732 CFRelease (values[i]);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400733
734 CFArrayAppendValue (features_array, dict);
735 CFRelease (dict);
736
737 }
738
739 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
740 (const void **) &kCTFontFeatureSettingsAttribute,
741 (const void **) &features_array,
742 1,
743 &kCFTypeDictionaryKeyCallBacks,
744 &kCFTypeDictionaryValueCallBacks);
745 CFRelease (features_array);
746
747 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
748 CFRelease (attributes);
749
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200750 range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400751 CFRelease (font_desc);
752 }
753 else
754 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200755 range->font = nullptr;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400756 }
757
758 range->index_first = last_index;
759 range->index_last = event->index - 1;
760
761 last_index = event->index;
762 }
763
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700764 if (event->start)
765 {
766 active_features.push (event->feature);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400767 } else {
768 active_feature_t *feature = active_features.find (&event->feature);
769 if (feature)
Ebrahim Byagowi93bdf9b2018-05-09 23:24:17 +0430770 active_features.remove (feature - active_features.arrayZ);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400771 }
772 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400773 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400774
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400775 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500776 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500777
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400778#define ALLOCATE_ARRAY(Type, name, len, on_no_room) \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500779 Type *name = (Type *) scratch; \
780 { \
781 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400782 if (unlikely (_consumed > scratch_size)) \
783 { \
784 on_no_room; \
785 assert (0); \
786 } \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500787 scratch += _consumed; \
788 scratch_size -= _consumed; \
789 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400790
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400791 ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400792 unsigned int chars_len = 0;
793 for (unsigned int i = 0; i < buffer->len; i++) {
794 hb_codepoint_t c = buffer->info[i].codepoint;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400795 if (likely (c <= 0xFFFFu))
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400796 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400797 else if (unlikely (c > 0x10FFFFu))
798 pchars[chars_len++] = 0xFFFDu;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400799 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400800 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700801 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400802 }
803 }
804
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400805 ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/);
Behdad Esfahbod9b3c60c2014-08-11 13:25:43 -0400806 chars_len = 0;
807 for (unsigned int i = 0; i < buffer->len; i++)
808 {
809 hb_codepoint_t c = buffer->info[i].codepoint;
810 unsigned int cluster = buffer->info[i].cluster;
811 log_clusters[chars_len++] = cluster;
812 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
813 log_clusters[chars_len++] = cluster; /* Surrogates. */
814 }
815
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400816#define FAIL(...) \
817 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200818 DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400819 ret = false; \
820 goto fail; \
821 } HB_STMT_END;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400822
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400823 bool ret = true;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200824 CFStringRef string_ref = nullptr;
825 CTLineRef line = nullptr;
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400826
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400827 if (0)
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400828 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400829resize_and_retry:
Behdad Esfahbod1b550772014-08-11 20:45:12 -0400830 DEBUG_MSG (CORETEXT, buffer, "Buffer resize");
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400831 /* string_ref uses the scratch-buffer for backing store, and line references
832 * string_ref (via attr_string). We must release those before resizing buffer. */
833 assert (string_ref);
834 assert (line);
835 CFRelease (string_ref);
836 CFRelease (line);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200837 string_ref = nullptr;
838 line = nullptr;
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400839
840 /* Get previous start-of-scratch-area, that we use later for readjusting
841 * our existing scratch arrays. */
842 unsigned int old_scratch_used;
843 hb_buffer_t::scratch_buffer_t *old_scratch;
844 old_scratch = buffer->get_scratch_buffer (&old_scratch_used);
845 old_scratch_used = scratch - old_scratch;
846
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400847 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbodb9993d82014-08-10 17:40:24 -0400848 FAIL ("Buffer resize failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400849
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400850 /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the
851 * cleanest way to do without completely restructuring the rest of this shaper. */
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500852 scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400853 pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch)));
854 log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch)));
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400855 scratch += old_scratch_used;
856 scratch_size -= old_scratch_used;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400857 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400858 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200859 string_ref = CFStringCreateWithCharactersNoCopy (nullptr,
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400860 pchars, chars_len,
861 kCFAllocatorNull);
862 if (unlikely (!string_ref))
863 FAIL ("CFStringCreateWithCharactersNoCopy failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400864
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400865 /* Create an attributed string, populate it, and create a line from it, then release attributed string. */
866 {
Behdad Esfahbodfd1a6aa2014-08-11 20:01:37 -0400867 CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault,
868 chars_len);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400869 if (unlikely (!attr_string))
870 FAIL ("CFAttributedStringCreateMutable failed");
871 CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref);
Behdad Esfahbod3eb6a4d2014-08-12 19:10:33 -0400872 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
873 {
874 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
875 kCTVerticalFormsAttributeName, kCFBooleanTrue);
876 }
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400877
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400878 if (buffer->props.language)
879 {
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400880/* What's the iOS equivalent of this check?
881 * The symbols was introduced in iOS 7.0.
882 * At any rate, our fallback is safe and works fine. */
883#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
884# define kCTLanguageAttributeName CFSTR ("NSLanguage")
885#endif
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400886 CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault,
887 hb_language_to_string (buffer->props.language),
888 kCFStringEncodingUTF8,
889 kCFAllocatorNull);
890 if (unlikely (!lang))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700891 {
892 CFRelease (attr_string);
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400893 FAIL ("CFStringCreateWithCStringNoCopy failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700894 }
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400895 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
896 kCTLanguageAttributeName, lang);
897 CFRelease (lang);
898 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400899 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200900 kCTFontAttributeName, ct_font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400901
Cosimo Lupo9813be32017-07-14 17:11:46 +0100902 if (num_features && range_records.len)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400903 {
904 unsigned int start = 0;
905 range_record_t *last_range = &range_records[0];
906 for (unsigned int k = 0; k < chars_len; k++)
907 {
908 range_record_t *range = last_range;
909 while (log_clusters[k] < range->index_first)
910 range--;
911 while (log_clusters[k] > range->index_last)
912 range++;
913 if (range != last_range)
914 {
915 if (last_range->font)
916 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start),
917 kCTFontAttributeName, last_range->font);
918
919 start = k;
920 }
921
922 last_range = range;
923 }
924 if (start != chars_len && last_range->font)
925 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start),
926 kCTFontAttributeName, last_range->font);
927 }
Cosimo Lupo9813be32017-07-14 17:11:46 +0100928 /* Enable/disable kern if requested.
929 *
930 * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText.
931 */
932 if (num_features)
933 {
934 unsigned int zeroint = 0;
935 CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint);
936 for (unsigned int i = 0; i < num_features; i++)
937 {
938 const hb_feature_t &feature = features[i];
939 if (feature.tag == HB_TAG('k','e','r','n') &&
940 feature.start < chars_len && feature.start < feature.end)
941 {
942 CFRange feature_range = CFRangeMake (feature.start,
943 MIN (feature.end, chars_len) - feature.start);
944 if (feature.value)
945 CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName);
946 else
947 CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero);
948 }
949 }
950 CFRelease (zero);
951 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400952
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400953 int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
954 CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level);
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500955#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
956 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
957#endif
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400958 CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault,
959 (const void **) &kCTTypesetterOptionForcedEmbeddingLevel,
960 (const void **) &level_number,
961 1,
962 &kCFTypeDictionaryKeyCallBacks,
963 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100964 CFRelease (level_number);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400965 if (unlikely (!options))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700966 {
967 CFRelease (attr_string);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400968 FAIL ("CFDictionaryCreate failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700969 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400970
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400971 CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
972 CFRelease (options);
973 CFRelease (attr_string);
974 if (unlikely (!typesetter))
975 FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed");
976
977 line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0));
978 CFRelease (typesetter);
979 if (unlikely (!line))
980 FAIL ("CTTypesetterCreateLine failed");
981 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400982
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400983 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line);
984 unsigned int num_runs = CFArrayGetCount (glyph_runs);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200985 DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400986
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400987 buffer->len = 0;
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400988 uint32_t status_and = ~0, status_or = 0;
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800989 double advances_so_far = 0;
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700990 /* For right-to-left runs, CoreText returns the glyphs positioned such that
991 * any trailing whitespace is to the left of (0,0). Adjust coordinate system
992 * to fix for that. Test with any RTL string with trailing spaces.
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430993 * https://crbug.com/469028
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700994 */
995 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
996 {
997 advances_so_far -= CTLineGetTrailingWhitespaceWidth (line);
998 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
999 advances_so_far = -advances_so_far;
1000 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001001
1002 const CFRange range_all = CFRangeMake (0, 0);
1003
1004 for (unsigned int i = 0; i < num_runs; i++)
1005 {
1006 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i));
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001007 CTRunStatus run_status = CTRunGetStatus (run);
1008 status_or |= run_status;
1009 status_and &= run_status;
1010 DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001011 double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001012 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
1013 run_advance = -run_advance;
1014 DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001015
1016 /* CoreText does automatic font fallback (AKA "cascading") for characters
1017 * not supported by the requested font, and provides no way to turn it off,
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001018 * so we must detect if the returned run uses a font other than the requested
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001019 * one and fill in the buffer with .notdef glyphs instead of random glyph
1020 * indices from a different font.
1021 */
1022 CFDictionaryRef attributes = CTRunGetAttributes (run);
1023 CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001024 if (!CFEqual (run_ct_font, ct_font))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001025 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001026 /* The run doesn't use our main font instance. We have to figure out
1027 * whether font fallback happened, or this is just CoreText giving us
1028 * another CTFont using the same underlying CGFont. CoreText seems
1029 * to do that in a variety of situations, one of which being vertical
1030 * text, but also perhaps for caching reasons.
1031 *
1032 * First, see if it uses any of our subfonts created to set font features...
1033 *
1034 * Next, compare the CGFont to the one we used to create our fonts.
1035 * Even this doesn't work all the time.
1036 *
1037 * Finally, we compare PS names, which I don't think are unique...
1038 *
1039 * Looks like if we really want to be sure here we have to modify the
1040 * font to change the name table, similar to what we do in the uniscribe
1041 * backend.
1042 *
1043 * However, even that wouldn't work if we were passed in the CGFont to
Behdad Esfahbod59089622016-04-04 14:54:32 -07001044 * construct a hb_face to begin with.
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001045 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301046 * See: https://github.com/harfbuzz/harfbuzz/pull/36
Behdad Esfahbod59089622016-04-04 14:54:32 -07001047 *
1048 * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001049 */
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001050 bool matched = false;
1051 for (unsigned int i = 0; i < range_records.len; i++)
1052 if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font))
1053 {
1054 matched = true;
1055 break;
1056 }
1057 if (!matched)
1058 {
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +07001059 CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001060 if (run_cg_font)
1061 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001062 matched = CFEqual (run_cg_font, cg_font);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001063 CFRelease (run_cg_font);
1064 }
1065 }
1066 if (!matched)
1067 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001068 CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001069 CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey);
1070 CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0);
1071 CFRelease (run_ps_name);
1072 CFRelease (font_ps_name);
1073 if (result == kCFCompareEqualTo)
1074 matched = true;
1075 }
1076 if (!matched)
1077 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001078 CFRange range = CTRunGetStringRange (run);
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001079 DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld",
1080 range.location, range.location + range.length);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001081 if (!buffer->ensure_inplace (buffer->len + range.length))
1082 goto resize_and_retry;
1083 hb_glyph_info_t *info = buffer->info + buffer->len;
1084
Behdad Esfahbodb0b38bb2015-01-21 19:19:33 -08001085 hb_codepoint_t notdef = 0;
1086 hb_direction_t dir = buffer->props.direction;
1087 hb_position_t x_advance, y_advance, x_offset, y_offset;
1088 hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance);
1089 hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset);
1090 hb_position_t advance = x_advance + y_advance;
1091 x_offset = -x_offset;
1092 y_offset = -y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001093
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001094 unsigned int old_len = buffer->len;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001095 for (CFIndex j = range.location; j < range.location + range.length; j++)
1096 {
1097 UniChar ch = CFStringGetCharacterAtIndex (string_ref, j);
1098 if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j)
1099 {
1100 ch = CFStringGetCharacterAtIndex (string_ref, j - 1);
1101 if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu))
1102 /* This is the second of a surrogate pair. Don't need .notdef
1103 * for this one. */
1104 continue;
1105 }
Behdad Esfahbod982d94e2015-01-28 10:51:33 -08001106 if (buffer->unicode->is_default_ignorable (ch))
1107 continue;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001108
1109 info->codepoint = notdef;
Behdad Esfahbod3c41ccb2014-08-11 15:11:59 -04001110 info->cluster = log_clusters[j];
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001111
1112 info->mask = advance;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001113 info->var1.i32 = x_offset;
1114 info->var2.i32 = y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001115
1116 info++;
1117 buffer->len++;
1118 }
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001119 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
1120 buffer->reverse_range (old_len, buffer->len);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001121 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001122 continue;
1123 }
1124 }
1125
1126 unsigned int num_glyphs = CTRunGetGlyphCount (run);
1127 if (num_glyphs == 0)
1128 continue;
1129
Behdad Esfahbod81b8d972014-08-12 15:49:47 -04001130 if (!buffer->ensure_inplace (buffer->len + num_glyphs))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001131 goto resize_and_retry;
1132
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001133 hb_glyph_info_t *run_info = buffer->info + buffer->len;
1134
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001135 /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always
1136 * succeed, and so copying data to our own buffer will be rare. Reports
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001137 * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001138 * frequently. At any rate, we can test that codepath by setting USE_PTR
1139 * to false. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001140
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001141#define USE_PTR true
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001142
1143#define SCRATCH_SAVE() \
1144 unsigned int scratch_size_saved = scratch_size; \
1145 hb_buffer_t::scratch_buffer_t *scratch_saved = scratch
1146
1147#define SCRATCH_RESTORE() \
1148 scratch_size = scratch_size_saved; \
1149 scratch = scratch_saved;
1150
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001151 { /* Setup glyphs */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001152 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001153 const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001154 if (!glyphs) {
1155 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry);
1156 CTRunGetGlyphs (run, range_all, glyph_buf);
1157 glyphs = glyph_buf;
1158 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001159 const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001160 if (!string_indices) {
1161 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry);
1162 CTRunGetStringIndices (run, range_all, index_buf);
1163 string_indices = index_buf;
1164 }
1165 hb_glyph_info_t *info = run_info;
1166 for (unsigned int j = 0; j < num_glyphs; j++)
1167 {
1168 info->codepoint = glyphs[j];
1169 info->cluster = log_clusters[string_indices[j]];
1170 info++;
1171 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001172 SCRATCH_RESTORE();
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001173 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001174 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001175 /* Setup positions.
1176 * Note that CoreText does not return advances for glyphs. As such,
1177 * for all but last glyph, we use the delta position to next glyph as
1178 * advance (in the advance direction only), and for last glyph we set
1179 * whatever is needed to make the whole run's advance add up. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001180 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001181 const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001182 if (!positions) {
1183 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry);
1184 CTRunGetPositions (run, range_all, position_buf);
1185 positions = position_buf;
1186 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001187 hb_glyph_info_t *info = run_info;
1188 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1189 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001190 hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001191 for (unsigned int j = 0; j < num_glyphs; j++)
1192 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001193 double advance;
1194 if (likely (j + 1 < num_glyphs))
1195 advance = positions[j + 1].x - positions[j].x;
1196 else /* last glyph */
1197 advance = run_advance - (positions[j].x - positions[0].x);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001198 info->mask = advance * x_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001199 info->var1.i32 = x_offset;
1200 info->var2.i32 = positions[j].y * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001201 info++;
1202 }
1203 }
1204 else
1205 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001206 hb_position_t y_offset = (positions[0].y - advances_so_far) * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001207 for (unsigned int j = 0; j < num_glyphs; j++)
1208 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001209 double advance;
1210 if (likely (j + 1 < num_glyphs))
1211 advance = positions[j + 1].y - positions[j].y;
1212 else /* last glyph */
1213 advance = run_advance - (positions[j].y - positions[0].y);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001214 info->mask = advance * y_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001215 info->var1.i32 = positions[j].x * x_mult;
1216 info->var2.i32 = y_offset;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001217 info++;
1218 }
1219 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001220 SCRATCH_RESTORE();
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001221 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001222 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001223#undef SCRATCH_RESTORE
1224#undef SCRATCH_SAVE
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001225#undef USE_PTR
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001226#undef ALLOCATE_ARRAY
1227
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001228 buffer->len += num_glyphs;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001229 }
1230
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001231 /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
Bruce Mitchener90218fa2018-01-31 20:44:45 +07001232 * or if it does, it doesn't respect it. So we get runs with wrong
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001233 * directions. As such, disable the assert... It wouldn't crash, but
1234 * cursoring will be off...
1235 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301236 * https://crbug.com/419769
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001237 */
1238 if (0)
1239 {
1240 /* Make sure all runs had the expected direction. */
1241 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1242 assert (bool (status_and & kCTRunStatusRightToLeft) == backward);
1243 assert (bool (status_or & kCTRunStatusRightToLeft) == backward);
1244 }
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001245
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001246 buffer->clear_positions ();
1247
1248 unsigned int count = buffer->len;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001249 hb_glyph_info_t *info = buffer->info;
1250 hb_glyph_position_t *pos = buffer->pos;
1251 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1252 for (unsigned int i = 0; i < count; i++)
1253 {
1254 pos->x_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001255 pos->x_offset = info->var1.i32;
1256 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001257
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001258 info++, pos++;
1259 }
1260 else
1261 for (unsigned int i = 0; i < count; i++)
1262 {
1263 pos->y_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001264 pos->x_offset = info->var1.i32;
1265 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001266
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001267 info++, pos++;
1268 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001269
1270 /* Fix up clusters so that we never return out-of-order indices;
1271 * if core text has reordered glyphs, we'll merge them to the
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001272 * beginning of the reordered cluster. CoreText is nice enough
1273 * to tell us whenever it has produced nonmonotonic results...
1274 * Note that we assume the input clusters were nonmonotonic to
1275 * begin with.
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001276 *
1277 * This does *not* mean we'll form the same clusters as Uniscribe
1278 * or the native OT backend, only that the cluster indices will be
1279 * monotonic in the output buffer. */
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001280 if (count > 1 && (status_or & kCTRunStatusNonMonotonic))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001281 {
1282 hb_glyph_info_t *info = buffer->info;
1283 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
1284 {
1285 unsigned int cluster = info[count - 1].cluster;
1286 for (unsigned int i = count - 1; i > 0; i--)
1287 {
1288 cluster = MIN (cluster, info[i - 1].cluster);
1289 info[i - 1].cluster = cluster;
1290 }
1291 }
1292 else
1293 {
1294 unsigned int cluster = info[0].cluster;
1295 for (unsigned int i = 1; i < count; i++)
1296 {
1297 cluster = MIN (cluster, info[i].cluster);
1298 info[i].cluster = cluster;
1299 }
1300 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001301 }
1302 }
1303
Behdad Esfahbode4da3802017-11-10 17:14:27 -08001304 buffer->unsafe_to_break_all ();
1305
Behdad Esfahbod4acce772014-08-11 17:46:50 -04001306#undef FAIL
1307
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001308fail:
1309 if (string_ref)
1310 CFRelease (string_ref);
1311 if (line)
1312 CFRelease (line);
1313
Behdad Esfahbod25f4fb92014-08-10 19:05:25 -04001314 for (unsigned int i = 0; i < range_records.len; i++)
1315 if (range_records[i].font)
1316 CFRelease (range_records[i].font);
1317
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001318 return ret;
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001319}
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001320
1321
1322/*
1323 * AAT shaper
1324 */
1325
Behdad Esfahbodd4bb52b2017-02-09 14:13:25 -08001326HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, face)
1327HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, font)
1328
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001329/*
1330 * shaper face data
1331 */
1332
1333struct hb_coretext_aat_shaper_face_data_t {};
1334
1335hb_coretext_aat_shaper_face_data_t *
1336_hb_coretext_aat_shaper_face_data_create (hb_face_t *face)
1337{
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001338 static const hb_tag_t tags[] = {HB_CORETEXT_TAG_MORX, HB_CORETEXT_TAG_MORT, HB_CORETEXT_TAG_KERX};
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001339
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001340 for (unsigned int i = 0; i < ARRAY_LENGTH (tags); i++)
1341 {
1342 hb_blob_t *blob = face->reference_table (tags[i]);
1343 if (hb_blob_get_length (blob))
1344 {
1345 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001346 return hb_coretext_shaper_face_data_ensure (face) ? (hb_coretext_aat_shaper_face_data_t *) HB_SHAPER_DATA_SUCCEEDED : nullptr;
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001347 }
1348 hb_blob_destroy (blob);
1349 }
1350
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001351 return nullptr;
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001352}
1353
1354void
1355_hb_coretext_aat_shaper_face_data_destroy (hb_coretext_aat_shaper_face_data_t *data HB_UNUSED)
1356{
1357}
1358
1359
1360/*
1361 * shaper font data
1362 */
1363
1364struct hb_coretext_aat_shaper_font_data_t {};
1365
1366hb_coretext_aat_shaper_font_data_t *
1367_hb_coretext_aat_shaper_font_data_create (hb_font_t *font)
1368{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001369 return hb_coretext_shaper_font_data_ensure (font) ? (hb_coretext_aat_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED : nullptr;
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001370}
1371
1372void
1373_hb_coretext_aat_shaper_font_data_destroy (hb_coretext_aat_shaper_font_data_t *data HB_UNUSED)
1374{
1375}
1376
1377
1378/*
1379 * shaper shape_plan data
1380 */
1381
1382struct hb_coretext_aat_shaper_shape_plan_data_t {};
1383
1384hb_coretext_aat_shaper_shape_plan_data_t *
1385_hb_coretext_aat_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
1386 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -07001387 unsigned int num_user_features HB_UNUSED,
1388 const int *coords HB_UNUSED,
1389 unsigned int num_coords HB_UNUSED)
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001390{
1391 return (hb_coretext_aat_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
1392}
1393
1394void
1395_hb_coretext_aat_shaper_shape_plan_data_destroy (hb_coretext_aat_shaper_shape_plan_data_t *data HB_UNUSED)
1396{
1397}
1398
1399
1400/*
1401 * shaper
1402 */
1403
1404hb_bool_t
1405_hb_coretext_aat_shape (hb_shape_plan_t *shape_plan,
1406 hb_font_t *font,
1407 hb_buffer_t *buffer,
1408 const hb_feature_t *features,
1409 unsigned int num_features)
1410{
1411 return _hb_coretext_shape (shape_plan, font, buffer, features, num_features);
1412}