blob: 837b3fb7eed1bd7ee2ff36dcfbbe427c3d6c5a6f [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 ();
673 if (unlikely (!event))
674 goto fail_features;
675 event->index = features[i].start;
676 event->start = true;
677 event->feature = feature;
678
679 event = feature_events.push ();
680 if (unlikely (!event))
681 goto fail_features;
682 event->index = features[i].end;
683 event->start = false;
684 event->feature = feature;
685 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400686 feature_events.qsort ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400687 /* Add a strategic final event. */
688 {
689 active_feature_t feature;
690 feature.rec.feature = HB_TAG_NONE;
691 feature.rec.setting = 0;
692 feature.order = num_features + 1;
693
694 feature_event_t *event = feature_events.push ();
695 if (unlikely (!event))
696 goto fail_features;
697 event->index = 0; /* This value does magic. */
698 event->start = false;
699 event->feature = feature;
700 }
701
702 /* Scan events and save features for each range. */
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400703 hb_auto_t<hb_vector_t<active_feature_t> > active_features;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400704 unsigned int last_index = 0;
705 for (unsigned int i = 0; i < feature_events.len; i++)
706 {
707 feature_event_t *event = &feature_events[i];
708
709 if (event->index != last_index)
710 {
711 /* Save a snapshot of active features and the range. */
712 range_record_t *range = range_records.push ();
713 if (unlikely (!range))
714 goto fail_features;
715
Behdad Esfahbod36136962013-08-12 00:33:28 -0400716 if (active_features.len)
717 {
718 CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
719
720 /* TODO sort and resolve conflicting features? */
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400721 /* active_features.qsort (); */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400722 for (unsigned int j = 0; j < active_features.len; j++)
723 {
Cosimo Lupo9813be32017-07-14 17:11:46 +0100724 CFStringRef keys[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400725 kCTFontFeatureTypeIdentifierKey,
726 kCTFontFeatureSelectorIdentifierKey
727 };
Cosimo Lupo9813be32017-07-14 17:11:46 +0100728 CFNumberRef values[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400729 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature),
730 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting)
731 };
Behdad Esfahbod80cc0a72017-10-17 11:14:48 -0700732 static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), "");
Behdad Esfahbod36136962013-08-12 00:33:28 -0400733 CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault,
734 (const void **) keys,
735 (const void **) values,
Cosimo Lupo9813be32017-07-14 17:11:46 +0100736 ARRAY_LENGTH (keys),
Behdad Esfahbod36136962013-08-12 00:33:28 -0400737 &kCFTypeDictionaryKeyCallBacks,
738 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100739 for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++)
740 CFRelease (values[i]);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400741
742 CFArrayAppendValue (features_array, dict);
743 CFRelease (dict);
744
745 }
746
747 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
748 (const void **) &kCTFontFeatureSettingsAttribute,
749 (const void **) &features_array,
750 1,
751 &kCFTypeDictionaryKeyCallBacks,
752 &kCFTypeDictionaryValueCallBacks);
753 CFRelease (features_array);
754
755 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
756 CFRelease (attributes);
757
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200758 range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400759 CFRelease (font_desc);
760 }
761 else
762 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200763 range->font = nullptr;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400764 }
765
766 range->index_first = last_index;
767 range->index_last = event->index - 1;
768
769 last_index = event->index;
770 }
771
772 if (event->start) {
773 active_feature_t *feature = active_features.push ();
774 if (unlikely (!feature))
775 goto fail_features;
776 *feature = event->feature;
777 } else {
778 active_feature_t *feature = active_features.find (&event->feature);
779 if (feature)
780 active_features.remove (feature - active_features.array);
781 }
782 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400783 }
784 else
785 {
786 fail_features:
787 num_features = 0;
788 }
789
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400790 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500791 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500792
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400793#define ALLOCATE_ARRAY(Type, name, len, on_no_room) \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500794 Type *name = (Type *) scratch; \
795 { \
796 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400797 if (unlikely (_consumed > scratch_size)) \
798 { \
799 on_no_room; \
800 assert (0); \
801 } \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500802 scratch += _consumed; \
803 scratch_size -= _consumed; \
804 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400805
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400806 ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400807 unsigned int chars_len = 0;
808 for (unsigned int i = 0; i < buffer->len; i++) {
809 hb_codepoint_t c = buffer->info[i].codepoint;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400810 if (likely (c <= 0xFFFFu))
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400811 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400812 else if (unlikely (c > 0x10FFFFu))
813 pchars[chars_len++] = 0xFFFDu;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400814 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400815 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700816 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400817 }
818 }
819
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400820 ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/);
Behdad Esfahbod9b3c60c2014-08-11 13:25:43 -0400821 chars_len = 0;
822 for (unsigned int i = 0; i < buffer->len; i++)
823 {
824 hb_codepoint_t c = buffer->info[i].codepoint;
825 unsigned int cluster = buffer->info[i].cluster;
826 log_clusters[chars_len++] = cluster;
827 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
828 log_clusters[chars_len++] = cluster; /* Surrogates. */
829 }
830
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400831#define FAIL(...) \
832 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200833 DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400834 ret = false; \
835 goto fail; \
836 } HB_STMT_END;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400837
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400838 bool ret = true;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200839 CFStringRef string_ref = nullptr;
840 CTLineRef line = nullptr;
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400841
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400842 if (0)
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400843 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400844resize_and_retry:
Behdad Esfahbod1b550772014-08-11 20:45:12 -0400845 DEBUG_MSG (CORETEXT, buffer, "Buffer resize");
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400846 /* string_ref uses the scratch-buffer for backing store, and line references
847 * string_ref (via attr_string). We must release those before resizing buffer. */
848 assert (string_ref);
849 assert (line);
850 CFRelease (string_ref);
851 CFRelease (line);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200852 string_ref = nullptr;
853 line = nullptr;
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400854
855 /* Get previous start-of-scratch-area, that we use later for readjusting
856 * our existing scratch arrays. */
857 unsigned int old_scratch_used;
858 hb_buffer_t::scratch_buffer_t *old_scratch;
859 old_scratch = buffer->get_scratch_buffer (&old_scratch_used);
860 old_scratch_used = scratch - old_scratch;
861
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400862 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbodb9993d82014-08-10 17:40:24 -0400863 FAIL ("Buffer resize failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400864
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400865 /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the
866 * cleanest way to do without completely restructuring the rest of this shaper. */
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500867 scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400868 pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch)));
869 log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch)));
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400870 scratch += old_scratch_used;
871 scratch_size -= old_scratch_used;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400872 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400873 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200874 string_ref = CFStringCreateWithCharactersNoCopy (nullptr,
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400875 pchars, chars_len,
876 kCFAllocatorNull);
877 if (unlikely (!string_ref))
878 FAIL ("CFStringCreateWithCharactersNoCopy failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400879
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400880 /* Create an attributed string, populate it, and create a line from it, then release attributed string. */
881 {
Behdad Esfahbodfd1a6aa2014-08-11 20:01:37 -0400882 CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault,
883 chars_len);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400884 if (unlikely (!attr_string))
885 FAIL ("CFAttributedStringCreateMutable failed");
886 CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref);
Behdad Esfahbod3eb6a4d2014-08-12 19:10:33 -0400887 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
888 {
889 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
890 kCTVerticalFormsAttributeName, kCFBooleanTrue);
891 }
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400892
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400893 if (buffer->props.language)
894 {
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400895/* What's the iOS equivalent of this check?
896 * The symbols was introduced in iOS 7.0.
897 * At any rate, our fallback is safe and works fine. */
898#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
899# define kCTLanguageAttributeName CFSTR ("NSLanguage")
900#endif
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400901 CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault,
902 hb_language_to_string (buffer->props.language),
903 kCFStringEncodingUTF8,
904 kCFAllocatorNull);
905 if (unlikely (!lang))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700906 {
907 CFRelease (attr_string);
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400908 FAIL ("CFStringCreateWithCStringNoCopy failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700909 }
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400910 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
911 kCTLanguageAttributeName, lang);
912 CFRelease (lang);
913 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400914 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200915 kCTFontAttributeName, ct_font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400916
Cosimo Lupo9813be32017-07-14 17:11:46 +0100917 if (num_features && range_records.len)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400918 {
919 unsigned int start = 0;
920 range_record_t *last_range = &range_records[0];
921 for (unsigned int k = 0; k < chars_len; k++)
922 {
923 range_record_t *range = last_range;
924 while (log_clusters[k] < range->index_first)
925 range--;
926 while (log_clusters[k] > range->index_last)
927 range++;
928 if (range != last_range)
929 {
930 if (last_range->font)
931 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start),
932 kCTFontAttributeName, last_range->font);
933
934 start = k;
935 }
936
937 last_range = range;
938 }
939 if (start != chars_len && last_range->font)
940 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start),
941 kCTFontAttributeName, last_range->font);
942 }
Cosimo Lupo9813be32017-07-14 17:11:46 +0100943 /* Enable/disable kern if requested.
944 *
945 * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText.
946 */
947 if (num_features)
948 {
949 unsigned int zeroint = 0;
950 CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint);
951 for (unsigned int i = 0; i < num_features; i++)
952 {
953 const hb_feature_t &feature = features[i];
954 if (feature.tag == HB_TAG('k','e','r','n') &&
955 feature.start < chars_len && feature.start < feature.end)
956 {
957 CFRange feature_range = CFRangeMake (feature.start,
958 MIN (feature.end, chars_len) - feature.start);
959 if (feature.value)
960 CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName);
961 else
962 CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero);
963 }
964 }
965 CFRelease (zero);
966 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400967
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400968 int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
969 CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level);
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500970#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
971 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
972#endif
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400973 CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault,
974 (const void **) &kCTTypesetterOptionForcedEmbeddingLevel,
975 (const void **) &level_number,
976 1,
977 &kCFTypeDictionaryKeyCallBacks,
978 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100979 CFRelease (level_number);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400980 if (unlikely (!options))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700981 {
982 CFRelease (attr_string);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400983 FAIL ("CFDictionaryCreate failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700984 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400985
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400986 CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
987 CFRelease (options);
988 CFRelease (attr_string);
989 if (unlikely (!typesetter))
990 FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed");
991
992 line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0));
993 CFRelease (typesetter);
994 if (unlikely (!line))
995 FAIL ("CTTypesetterCreateLine failed");
996 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400997
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400998 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line);
999 unsigned int num_runs = CFArrayGetCount (glyph_runs);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001000 DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs);
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001001
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001002 buffer->len = 0;
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001003 uint32_t status_and = ~0, status_or = 0;
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001004 double advances_so_far = 0;
Behdad Esfahbod24f17af2015-04-21 19:21:32 -07001005 /* For right-to-left runs, CoreText returns the glyphs positioned such that
1006 * any trailing whitespace is to the left of (0,0). Adjust coordinate system
1007 * to fix for that. Test with any RTL string with trailing spaces.
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301008 * https://crbug.com/469028
Behdad Esfahbod24f17af2015-04-21 19:21:32 -07001009 */
1010 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
1011 {
1012 advances_so_far -= CTLineGetTrailingWhitespaceWidth (line);
1013 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
1014 advances_so_far = -advances_so_far;
1015 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001016
1017 const CFRange range_all = CFRangeMake (0, 0);
1018
1019 for (unsigned int i = 0; i < num_runs; i++)
1020 {
1021 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i));
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001022 CTRunStatus run_status = CTRunGetStatus (run);
1023 status_or |= run_status;
1024 status_and &= run_status;
1025 DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001026 double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001027 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
1028 run_advance = -run_advance;
1029 DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001030
1031 /* CoreText does automatic font fallback (AKA "cascading") for characters
1032 * not supported by the requested font, and provides no way to turn it off,
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001033 * so we must detect if the returned run uses a font other than the requested
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001034 * one and fill in the buffer with .notdef glyphs instead of random glyph
1035 * indices from a different font.
1036 */
1037 CFDictionaryRef attributes = CTRunGetAttributes (run);
1038 CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001039 if (!CFEqual (run_ct_font, ct_font))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001040 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001041 /* The run doesn't use our main font instance. We have to figure out
1042 * whether font fallback happened, or this is just CoreText giving us
1043 * another CTFont using the same underlying CGFont. CoreText seems
1044 * to do that in a variety of situations, one of which being vertical
1045 * text, but also perhaps for caching reasons.
1046 *
1047 * First, see if it uses any of our subfonts created to set font features...
1048 *
1049 * Next, compare the CGFont to the one we used to create our fonts.
1050 * Even this doesn't work all the time.
1051 *
1052 * Finally, we compare PS names, which I don't think are unique...
1053 *
1054 * Looks like if we really want to be sure here we have to modify the
1055 * font to change the name table, similar to what we do in the uniscribe
1056 * backend.
1057 *
1058 * However, even that wouldn't work if we were passed in the CGFont to
Behdad Esfahbod59089622016-04-04 14:54:32 -07001059 * construct a hb_face to begin with.
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001060 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301061 * See: https://github.com/harfbuzz/harfbuzz/pull/36
Behdad Esfahbod59089622016-04-04 14:54:32 -07001062 *
1063 * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001064 */
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001065 bool matched = false;
1066 for (unsigned int i = 0; i < range_records.len; i++)
1067 if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font))
1068 {
1069 matched = true;
1070 break;
1071 }
1072 if (!matched)
1073 {
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +07001074 CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001075 if (run_cg_font)
1076 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001077 matched = CFEqual (run_cg_font, cg_font);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001078 CFRelease (run_cg_font);
1079 }
1080 }
1081 if (!matched)
1082 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001083 CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001084 CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey);
1085 CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0);
1086 CFRelease (run_ps_name);
1087 CFRelease (font_ps_name);
1088 if (result == kCFCompareEqualTo)
1089 matched = true;
1090 }
1091 if (!matched)
1092 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001093 CFRange range = CTRunGetStringRange (run);
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001094 DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld",
1095 range.location, range.location + range.length);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001096 if (!buffer->ensure_inplace (buffer->len + range.length))
1097 goto resize_and_retry;
1098 hb_glyph_info_t *info = buffer->info + buffer->len;
1099
Behdad Esfahbodb0b38bb2015-01-21 19:19:33 -08001100 hb_codepoint_t notdef = 0;
1101 hb_direction_t dir = buffer->props.direction;
1102 hb_position_t x_advance, y_advance, x_offset, y_offset;
1103 hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance);
1104 hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset);
1105 hb_position_t advance = x_advance + y_advance;
1106 x_offset = -x_offset;
1107 y_offset = -y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001108
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001109 unsigned int old_len = buffer->len;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001110 for (CFIndex j = range.location; j < range.location + range.length; j++)
1111 {
1112 UniChar ch = CFStringGetCharacterAtIndex (string_ref, j);
1113 if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j)
1114 {
1115 ch = CFStringGetCharacterAtIndex (string_ref, j - 1);
1116 if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu))
1117 /* This is the second of a surrogate pair. Don't need .notdef
1118 * for this one. */
1119 continue;
1120 }
Behdad Esfahbod982d94e2015-01-28 10:51:33 -08001121 if (buffer->unicode->is_default_ignorable (ch))
1122 continue;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001123
1124 info->codepoint = notdef;
Behdad Esfahbod3c41ccb2014-08-11 15:11:59 -04001125 info->cluster = log_clusters[j];
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001126
1127 info->mask = advance;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001128 info->var1.i32 = x_offset;
1129 info->var2.i32 = y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001130
1131 info++;
1132 buffer->len++;
1133 }
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001134 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
1135 buffer->reverse_range (old_len, buffer->len);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001136 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001137 continue;
1138 }
1139 }
1140
1141 unsigned int num_glyphs = CTRunGetGlyphCount (run);
1142 if (num_glyphs == 0)
1143 continue;
1144
Behdad Esfahbod81b8d972014-08-12 15:49:47 -04001145 if (!buffer->ensure_inplace (buffer->len + num_glyphs))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001146 goto resize_and_retry;
1147
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001148 hb_glyph_info_t *run_info = buffer->info + buffer->len;
1149
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001150 /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always
1151 * succeed, and so copying data to our own buffer will be rare. Reports
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001152 * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001153 * frequently. At any rate, we can test that codepath by setting USE_PTR
1154 * to false. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001155
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001156#define USE_PTR true
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001157
1158#define SCRATCH_SAVE() \
1159 unsigned int scratch_size_saved = scratch_size; \
1160 hb_buffer_t::scratch_buffer_t *scratch_saved = scratch
1161
1162#define SCRATCH_RESTORE() \
1163 scratch_size = scratch_size_saved; \
1164 scratch = scratch_saved;
1165
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001166 { /* Setup glyphs */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001167 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001168 const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001169 if (!glyphs) {
1170 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry);
1171 CTRunGetGlyphs (run, range_all, glyph_buf);
1172 glyphs = glyph_buf;
1173 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001174 const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001175 if (!string_indices) {
1176 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry);
1177 CTRunGetStringIndices (run, range_all, index_buf);
1178 string_indices = index_buf;
1179 }
1180 hb_glyph_info_t *info = run_info;
1181 for (unsigned int j = 0; j < num_glyphs; j++)
1182 {
1183 info->codepoint = glyphs[j];
1184 info->cluster = log_clusters[string_indices[j]];
1185 info++;
1186 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001187 SCRATCH_RESTORE();
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001188 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001189 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001190 /* Setup positions.
1191 * Note that CoreText does not return advances for glyphs. As such,
1192 * for all but last glyph, we use the delta position to next glyph as
1193 * advance (in the advance direction only), and for last glyph we set
1194 * whatever is needed to make the whole run's advance add up. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001195 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001196 const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001197 if (!positions) {
1198 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry);
1199 CTRunGetPositions (run, range_all, position_buf);
1200 positions = position_buf;
1201 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001202 hb_glyph_info_t *info = run_info;
1203 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1204 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001205 hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001206 for (unsigned int j = 0; j < num_glyphs; j++)
1207 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001208 double advance;
1209 if (likely (j + 1 < num_glyphs))
1210 advance = positions[j + 1].x - positions[j].x;
1211 else /* last glyph */
1212 advance = run_advance - (positions[j].x - positions[0].x);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001213 info->mask = advance * x_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001214 info->var1.i32 = x_offset;
1215 info->var2.i32 = positions[j].y * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001216 info++;
1217 }
1218 }
1219 else
1220 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001221 hb_position_t y_offset = (positions[0].y - advances_so_far) * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001222 for (unsigned int j = 0; j < num_glyphs; j++)
1223 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001224 double advance;
1225 if (likely (j + 1 < num_glyphs))
1226 advance = positions[j + 1].y - positions[j].y;
1227 else /* last glyph */
1228 advance = run_advance - (positions[j].y - positions[0].y);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001229 info->mask = advance * y_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001230 info->var1.i32 = positions[j].x * x_mult;
1231 info->var2.i32 = y_offset;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001232 info++;
1233 }
1234 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001235 SCRATCH_RESTORE();
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001236 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001237 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001238#undef SCRATCH_RESTORE
1239#undef SCRATCH_SAVE
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001240#undef USE_PTR
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001241#undef ALLOCATE_ARRAY
1242
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001243 buffer->len += num_glyphs;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001244 }
1245
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001246 /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
Bruce Mitchener90218fa2018-01-31 20:44:45 +07001247 * or if it does, it doesn't respect it. So we get runs with wrong
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001248 * directions. As such, disable the assert... It wouldn't crash, but
1249 * cursoring will be off...
1250 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301251 * https://crbug.com/419769
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001252 */
1253 if (0)
1254 {
1255 /* Make sure all runs had the expected direction. */
1256 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1257 assert (bool (status_and & kCTRunStatusRightToLeft) == backward);
1258 assert (bool (status_or & kCTRunStatusRightToLeft) == backward);
1259 }
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001260
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001261 buffer->clear_positions ();
1262
1263 unsigned int count = buffer->len;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001264 hb_glyph_info_t *info = buffer->info;
1265 hb_glyph_position_t *pos = buffer->pos;
1266 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1267 for (unsigned int i = 0; i < count; i++)
1268 {
1269 pos->x_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001270 pos->x_offset = info->var1.i32;
1271 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001272
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001273 info++, pos++;
1274 }
1275 else
1276 for (unsigned int i = 0; i < count; i++)
1277 {
1278 pos->y_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001279 pos->x_offset = info->var1.i32;
1280 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001281
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001282 info++, pos++;
1283 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001284
1285 /* Fix up clusters so that we never return out-of-order indices;
1286 * if core text has reordered glyphs, we'll merge them to the
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001287 * beginning of the reordered cluster. CoreText is nice enough
1288 * to tell us whenever it has produced nonmonotonic results...
1289 * Note that we assume the input clusters were nonmonotonic to
1290 * begin with.
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001291 *
1292 * This does *not* mean we'll form the same clusters as Uniscribe
1293 * or the native OT backend, only that the cluster indices will be
1294 * monotonic in the output buffer. */
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001295 if (count > 1 && (status_or & kCTRunStatusNonMonotonic))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001296 {
1297 hb_glyph_info_t *info = buffer->info;
1298 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
1299 {
1300 unsigned int cluster = info[count - 1].cluster;
1301 for (unsigned int i = count - 1; i > 0; i--)
1302 {
1303 cluster = MIN (cluster, info[i - 1].cluster);
1304 info[i - 1].cluster = cluster;
1305 }
1306 }
1307 else
1308 {
1309 unsigned int cluster = info[0].cluster;
1310 for (unsigned int i = 1; i < count; i++)
1311 {
1312 cluster = MIN (cluster, info[i].cluster);
1313 info[i].cluster = cluster;
1314 }
1315 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001316 }
1317 }
1318
Behdad Esfahbode4da3802017-11-10 17:14:27 -08001319 buffer->unsafe_to_break_all ();
1320
Behdad Esfahbod4acce772014-08-11 17:46:50 -04001321#undef FAIL
1322
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001323fail:
1324 if (string_ref)
1325 CFRelease (string_ref);
1326 if (line)
1327 CFRelease (line);
1328
Behdad Esfahbod25f4fb92014-08-10 19:05:25 -04001329 for (unsigned int i = 0; i < range_records.len; i++)
1330 if (range_records[i].font)
1331 CFRelease (range_records[i].font);
1332
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001333 return ret;
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001334}
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001335
1336
1337/*
1338 * AAT shaper
1339 */
1340
Behdad Esfahbodd4bb52b2017-02-09 14:13:25 -08001341HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, face)
1342HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, font)
1343
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001344/*
1345 * shaper face data
1346 */
1347
1348struct hb_coretext_aat_shaper_face_data_t {};
1349
1350hb_coretext_aat_shaper_face_data_t *
1351_hb_coretext_aat_shaper_face_data_create (hb_face_t *face)
1352{
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001353 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 -04001354
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001355 for (unsigned int i = 0; i < ARRAY_LENGTH (tags); i++)
1356 {
1357 hb_blob_t *blob = face->reference_table (tags[i]);
1358 if (hb_blob_get_length (blob))
1359 {
1360 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001361 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 +02001362 }
1363 hb_blob_destroy (blob);
1364 }
1365
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001366 return nullptr;
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001367}
1368
1369void
1370_hb_coretext_aat_shaper_face_data_destroy (hb_coretext_aat_shaper_face_data_t *data HB_UNUSED)
1371{
1372}
1373
1374
1375/*
1376 * shaper font data
1377 */
1378
1379struct hb_coretext_aat_shaper_font_data_t {};
1380
1381hb_coretext_aat_shaper_font_data_t *
1382_hb_coretext_aat_shaper_font_data_create (hb_font_t *font)
1383{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001384 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 -04001385}
1386
1387void
1388_hb_coretext_aat_shaper_font_data_destroy (hb_coretext_aat_shaper_font_data_t *data HB_UNUSED)
1389{
1390}
1391
1392
1393/*
1394 * shaper shape_plan data
1395 */
1396
1397struct hb_coretext_aat_shaper_shape_plan_data_t {};
1398
1399hb_coretext_aat_shaper_shape_plan_data_t *
1400_hb_coretext_aat_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
1401 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -07001402 unsigned int num_user_features HB_UNUSED,
1403 const int *coords HB_UNUSED,
1404 unsigned int num_coords HB_UNUSED)
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001405{
1406 return (hb_coretext_aat_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
1407}
1408
1409void
1410_hb_coretext_aat_shaper_shape_plan_data_destroy (hb_coretext_aat_shaper_shape_plan_data_t *data HB_UNUSED)
1411{
1412}
1413
1414
1415/*
1416 * shaper
1417 */
1418
1419hb_bool_t
1420_hb_coretext_aat_shape (hb_shape_plan_t *shape_plan,
1421 hb_font_t *font,
1422 hb_buffer_t *buffer,
1423 const hb_feature_t *features,
1424 unsigned int num_features)
1425{
1426 return _hb_coretext_shape (shape_plan, font, buffer, features, num_features);
1427}