blob: 3bdc3f784cdca7c37456ecc8ff503bcb17afc490 [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"
Behdad Esfahbod301168d2012-07-30 17:48:04 -040032#include "hb-shaper-impl-private.hh"
Jonathan Kewaa6d8492012-07-24 15:52:32 -040033
Jonathan Kewaa6d8492012-07-24 15:52:32 -040034#include "hb-coretext.h"
Behdad Esfahbod6a2cbc62017-10-12 10:46:09 +020035#include <math.h>
Jonathan Kewaa6d8492012-07-24 15:52:32 -040036
Behdad Esfahbod06c14222017-10-11 15:29:53 +020037/* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020038#define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020039
40static CGFloat
Behdad Esfahbod12453952017-12-17 12:32:33 -050041coretext_font_size_from_ptem (float ptem)
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020042{
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020043 /* CoreText points are CSS pixels (96 per inch),
44 * NOT typographic points (72 per inch).
45 *
46 * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
47 */
48 ptem *= 96.f / 72.f;
49 return ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : ptem;
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020050}
Behdad Esfahbod12453952017-12-17 12:32:33 -050051static float
52coretext_font_size_to_ptem (CGFloat size)
53{
54 size *= 72.f / 96.f;
55 return size <= 0.f ? 0 : size;
56}
Jonathan Kewaa6d8492012-07-24 15:52:32 -040057
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070058static void
59release_table_data (void *user_data)
60{
61 CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data);
62 CFRelease(cf_data);
63}
64
65static hb_blob_t *
66reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
67{
68 CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data);
69 CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag);
70 if (unlikely (!cf_data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020071 return nullptr;
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070072
73 const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data));
74 const size_t length = CFDataGetLength (cf_data);
75 if (!data || !length)
Bruce Mitchener0c660432018-01-31 20:24:27 +070076 {
77 CFRelease (cf_data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020078 return nullptr;
Bruce Mitchener0c660432018-01-31 20:24:27 +070079 }
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070080
81 return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY,
82 reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)),
83 release_table_data);
84}
85
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020086static void
87_hb_cg_font_release (void *data)
88{
89 CGFontRelease ((CGFontRef) data);
90}
91
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070092
Behdad Esfahbod466b3e52017-02-03 16:43:25 -080093HB_SHAPER_DATA_ENSURE_DEFINE(coretext, face)
Dominik Röttschesa5ebe1d2017-10-11 13:32:38 +020094HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(coretext, font,
Behdad Esfahbod12453952017-12-17 12:32:33 -050095 fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size_from_ptem (font->ptem)) <= .5
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020096)
Jonathan Kewaa6d8492012-07-24 15:52:32 -040097
Behdad Esfahbod90194ef2016-02-22 15:42:53 +090098static CTFontDescriptorRef
99get_last_resort_font_desc (void)
100{
101 // TODO Handle allocation failures?
102 CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0);
103 CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault,
104 (const void **) &last_resort,
105 1,
106 &kCFTypeArrayCallBacks);
107 CFRelease (last_resort);
108 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
109 (const void **) &kCTFontCascadeListAttribute,
110 (const void **) &cascade_list,
111 1,
112 &kCFTypeDictionaryKeyCallBacks,
113 &kCFTypeDictionaryValueCallBacks);
114 CFRelease (cascade_list);
115
116 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
117 CFRelease (attributes);
118 return font_desc;
119}
120
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900121static void
122release_data (void *info, const void *data, size_t size)
123{
124 assert (hb_blob_get_length ((hb_blob_t *) info) == size &&
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200125 hb_blob_get_data ((hb_blob_t *) info, nullptr) == data);
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900126
127 hb_blob_destroy ((hb_blob_t *) info);
128}
129
130static CGFontRef
131create_cg_font (hb_face_t *face)
132{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200133 CGFontRef cg_font = nullptr;
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200134 if (face->destroy == _hb_cg_font_release)
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900135 {
136 cg_font = CGFontRetain ((CGFontRef) face->user_data);
137 }
138 else
139 {
140 hb_blob_t *blob = hb_face_reference_blob (face);
141 unsigned int blob_length;
142 const char *blob_data = hb_blob_get_data (blob, &blob_length);
143 if (unlikely (!blob_length))
144 DEBUG_MSG (CORETEXT, face, "Face has empty blob");
145
146 CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data);
147 if (likely (provider))
148 {
149 cg_font = CGFontCreateWithDataProvider (provider);
150 if (unlikely (!cg_font))
151 DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed");
152 CGDataProviderRelease (provider);
153 }
154 }
155 return cg_font;
156}
157
Behdad Esfahbode5611222016-02-22 15:28:37 +0900158static CTFontRef
159create_ct_font (CGFontRef cg_font, CGFloat font_size)
160{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200161 CTFontRef ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200162
163 /* CoreText does not enable trak table usage / tracking when creating a CTFont
164 * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems
165 * to be through the CTFontCreateUIFontForLanguage call. */
166 CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font);
167 if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) ||
168 CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay")))
169 {
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500170#if MAC_OS_X_VERSION_MIN_REQUIRED < 1080
171# define kCTFontUIFontSystem kCTFontSystemFontType
172# define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType
173#endif
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200174 CTFontUIFontType font_type = kCTFontUIFontSystem;
175 if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold")))
176 font_type = kCTFontUIFontEmphasizedSystem;
177
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200178 ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200179 CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font);
180 if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo)
181 {
182 CFRelease(ct_font);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200183 ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200184 }
185 CFRelease (ct_result_name);
186 }
187 CFRelease (cg_postscript_name);
188
189 if (!ct_font)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200190 ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200191
Behdad Esfahbode5611222016-02-22 15:28:37 +0900192 if (unlikely (!ct_font)) {
193 DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200194 return nullptr;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900195 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700196
197 /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter
198 * bug indicate that the cascade list reconfiguration occasionally causes
199 * crashes in CoreText on OS X 10.9, thus let's skip this step on older
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300200 * operating system versions. Except for the emoji font, where _not_
201 * reconfiguring the cascade list causes CoreText crashes. For details, see
202 * crbug.com/549610 */
Ebrahim Byagowifc4e6712016-09-09 23:28:28 +0430203 // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200204 if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) {
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300205 CFStringRef fontName = CTFontCopyPostScriptName (ct_font);
206 bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo;
207 CFRelease (fontName);
208 if (!isEmojiFont)
209 return ct_font;
210 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700211
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430212 CFURLRef original_url = nullptr;
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500213#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
214 ATSFontRef atsFont;
215 FSRef fsref;
216 OSStatus status;
217 atsFont = CTFontGetPlatformFont (ct_font, NULL);
218 status = ATSFontGetFileReference (atsFont, &fsref);
219 if (status == noErr)
220 original_url = CFURLCreateFromFSRef (NULL, &fsref);
221#else
222 original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute);
223#endif
Behdad Esfahbode5611222016-02-22 15:28:37 +0900224
225 /* Create font copy with cascade list that has LastResort first; this speeds up CoreText
226 * font fallback which we don't need anyway. */
227 {
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900228 CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc ();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200229 CTFontRef new_ct_font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, last_resort_font_desc);
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900230 CFRelease (last_resort_font_desc);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900231 if (new_ct_font)
232 {
Behdad Esfahbodfc9de442016-06-30 09:46:52 -0700233 /* The CTFontCreateCopyWithAttributes call fails to stay on the same font
234 * when reconfiguring the cascade list and may switch to a different font
235 * when there are fonts that go by the same name, since the descriptor is
236 * just name and size.
237 *
238 * Avoid reconfiguring the cascade lists if the new font is outside the
239 * system locations that we cannot access from the sandboxed renderer
240 * process in Blink. This can be detected by the new file URL location
241 * that the newly found font points to. */
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430242 CFURLRef new_url = nullptr;
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500243#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
244 atsFont = CTFontGetPlatformFont (new_ct_font, NULL);
245 status = ATSFontGetFileReference (atsFont, &fsref);
246 if (status == noErr)
247 new_url = CFURLCreateFromFSRef (NULL, &fsref);
248#else
249 new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute);
250#endif
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430251 // Keep reconfigured font if URL cannot be retrieved (seems to be the case
252 // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606
253 if (!original_url || !new_url || CFEqual (original_url, new_url)) {
Dominik Röttschesa0223272016-06-16 14:19:39 +0200254 CFRelease (ct_font);
255 ct_font = new_ct_font;
256 } else {
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430257 CFRelease (new_ct_font);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200258 DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed.");
259 }
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430260 if (new_url)
261 CFRelease (new_url);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900262 }
263 else
264 DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed");
265 }
266
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430267 if (original_url)
268 CFRelease (original_url);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200269 return ct_font;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900270}
271
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400272hb_coretext_shaper_face_data_t *
273_hb_coretext_shaper_face_data_create (hb_face_t *face)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400274{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200275 CGFontRef cg_font = create_cg_font (face);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400276
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200277 if (unlikely (!cg_font))
Behdad Esfahboda9e25e92014-03-14 19:55:46 -0700278 {
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900279 DEBUG_MSG (CORETEXT, face, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200280 return nullptr;
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900281 }
282
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200283 return (hb_coretext_shaper_face_data_t *) cg_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400284}
285
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400286void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200287_hb_coretext_shaper_face_data_destroy (hb_coretext_shaper_face_data_t *data)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400288{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200289 CFRelease ((CGFontRef) data);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400290}
291
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800292hb_face_t *
293hb_coretext_face_create (CGFontRef cg_font)
294{
295 return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
296}
297
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430298/*
299 * Since: 0.9.10
300 */
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500301CGFontRef
Behdad Esfahbode923e642012-12-09 19:39:40 -0500302hb_coretext_face_get_cg_font (hb_face_t *face)
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500303{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200304 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200305 return (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500306}
307
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400308
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400309hb_coretext_shaper_font_data_t *
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200310_hb_coretext_shaper_font_data_create (hb_font_t *font)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400311{
Dominik Röttschesdb7a73c2017-10-11 13:24:39 +0200312 hb_face_t *face = font->face;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200313 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200314 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200315
Behdad Esfahbod12453952017-12-17 12:32:33 -0500316 CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size_from_ptem (font->ptem));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200317
318 if (unlikely (!ct_font))
319 {
320 DEBUG_MSG (CORETEXT, font, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200321 return nullptr;
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200322 }
323
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200324 return (hb_coretext_shaper_font_data_t *) ct_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400325}
326
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400327void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200328_hb_coretext_shaper_font_data_destroy (hb_coretext_shaper_font_data_t *data)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400329{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200330 CFRelease ((CTFontRef) data);
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400331}
332
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800333/*
334 * Since: 1.7.2
335 */
336hb_font_t *
337hb_coretext_font_create (CTFontRef ct_font)
338{
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +0700339 CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr);
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800340 hb_face_t *face = hb_coretext_face_create (cg_font);
341 CFRelease (cg_font);
342 hb_font_t *font = hb_font_create (face);
343 hb_face_destroy (face);
344
345 if (unlikely (hb_object_is_inert (font)))
346 return font;
347
Behdad Esfahbod12453952017-12-17 12:32:33 -0500348 hb_font_set_ptem (font, coretext_font_size_to_ptem (CTFontGetSize(ct_font)));
349
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800350 /* Let there be dragons here... */
351 HB_SHAPER_DATA_GET (font) = (hb_coretext_shaper_font_data_t *) CFRetain (ct_font);
352
353 return font;
354}
355
356CTFontRef
357hb_coretext_font_get_ct_font (hb_font_t *font)
358{
359 if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr;
360 return (CTFontRef) HB_SHAPER_DATA_GET (font);
361}
362
363
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400364
365/*
366 * shaper shape_plan data
367 */
368
369struct hb_coretext_shaper_shape_plan_data_t {};
370
371hb_coretext_shaper_shape_plan_data_t *
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400372_hb_coretext_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
373 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700374 unsigned int num_user_features HB_UNUSED,
375 const int *coords HB_UNUSED,
376 unsigned int num_coords HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400377{
378 return (hb_coretext_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
379}
380
381void
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400382_hb_coretext_shaper_shape_plan_data_destroy (hb_coretext_shaper_shape_plan_data_t *data HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400383{
384}
385
386
387/*
388 * shaper
389 */
390
Behdad Esfahbod36136962013-08-12 00:33:28 -0400391struct feature_record_t {
392 unsigned int feature;
393 unsigned int setting;
394};
395
396struct active_feature_t {
397 feature_record_t rec;
398 unsigned int order;
399
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600400 static int cmp (const void *pa, const void *pb) {
401 const active_feature_t *a = (const active_feature_t *) pa;
402 const active_feature_t *b = (const active_feature_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400403 return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 :
404 a->order < b->order ? -1 : a->order > b->order ? 1 :
405 a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 :
406 0;
407 }
408 bool operator== (const active_feature_t *f) {
409 return cmp (this, f) == 0;
410 }
411};
412
413struct feature_event_t {
414 unsigned int index;
415 bool start;
416 active_feature_t feature;
417
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600418 static int cmp (const void *pa, const void *pb) {
419 const feature_event_t *a = (const feature_event_t *) pa;
420 const feature_event_t *b = (const feature_event_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400421 return a->index < b->index ? -1 : a->index > b->index ? 1 :
422 a->start < b->start ? -1 : a->start > b->start ? 1 :
423 active_feature_t::cmp (&a->feature, &b->feature);
424 }
425};
426
427struct range_record_t {
428 CTFontRef font;
429 unsigned int index_first; /* == start */
430 unsigned int index_last; /* == end - 1 */
431};
432
433
434/* The following enum members are added in OS X 10.8. */
435#define kAltHalfWidthTextSelector 6
436#define kAltProportionalTextSelector 5
437#define kAlternateHorizKanaOffSelector 1
438#define kAlternateHorizKanaOnSelector 0
439#define kAlternateKanaType 34
440#define kAlternateVertKanaOffSelector 3
441#define kAlternateVertKanaOnSelector 2
442#define kCaseSensitiveLayoutOffSelector 1
443#define kCaseSensitiveLayoutOnSelector 0
444#define kCaseSensitiveLayoutType 33
445#define kCaseSensitiveSpacingOffSelector 3
446#define kCaseSensitiveSpacingOnSelector 2
447#define kContextualAlternatesOffSelector 1
448#define kContextualAlternatesOnSelector 0
449#define kContextualAlternatesType 36
450#define kContextualLigaturesOffSelector 19
451#define kContextualLigaturesOnSelector 18
452#define kContextualSwashAlternatesOffSelector 5
453#define kContextualSwashAlternatesOnSelector 4
454#define kDefaultLowerCaseSelector 0
455#define kDefaultUpperCaseSelector 0
456#define kHistoricalLigaturesOffSelector 21
457#define kHistoricalLigaturesOnSelector 20
458#define kHojoCharactersSelector 12
459#define kJIS2004CharactersSelector 11
460#define kLowerCasePetiteCapsSelector 2
461#define kLowerCaseSmallCapsSelector 1
462#define kLowerCaseType 37
463#define kMathematicalGreekOffSelector 11
464#define kMathematicalGreekOnSelector 10
465#define kNLCCharactersSelector 13
466#define kQuarterWidthTextSelector 4
467#define kScientificInferiorsSelector 4
468#define kStylisticAltEightOffSelector 17
469#define kStylisticAltEightOnSelector 16
470#define kStylisticAltEighteenOffSelector 37
471#define kStylisticAltEighteenOnSelector 36
472#define kStylisticAltElevenOffSelector 23
473#define kStylisticAltElevenOnSelector 22
474#define kStylisticAltFifteenOffSelector 31
475#define kStylisticAltFifteenOnSelector 30
476#define kStylisticAltFiveOffSelector 11
477#define kStylisticAltFiveOnSelector 10
478#define kStylisticAltFourOffSelector 9
479#define kStylisticAltFourOnSelector 8
480#define kStylisticAltFourteenOffSelector 29
481#define kStylisticAltFourteenOnSelector 28
482#define kStylisticAltNineOffSelector 19
483#define kStylisticAltNineOnSelector 18
484#define kStylisticAltNineteenOffSelector 39
485#define kStylisticAltNineteenOnSelector 38
486#define kStylisticAltOneOffSelector 3
487#define kStylisticAltOneOnSelector 2
488#define kStylisticAltSevenOffSelector 15
489#define kStylisticAltSevenOnSelector 14
490#define kStylisticAltSeventeenOffSelector 35
491#define kStylisticAltSeventeenOnSelector 34
492#define kStylisticAltSixOffSelector 13
493#define kStylisticAltSixOnSelector 12
494#define kStylisticAltSixteenOffSelector 33
495#define kStylisticAltSixteenOnSelector 32
496#define kStylisticAltTenOffSelector 21
497#define kStylisticAltTenOnSelector 20
498#define kStylisticAltThirteenOffSelector 27
499#define kStylisticAltThirteenOnSelector 26
500#define kStylisticAltThreeOffSelector 7
501#define kStylisticAltThreeOnSelector 6
502#define kStylisticAltTwelveOffSelector 25
503#define kStylisticAltTwelveOnSelector 24
504#define kStylisticAltTwentyOffSelector 41
505#define kStylisticAltTwentyOnSelector 40
506#define kStylisticAltTwoOffSelector 5
507#define kStylisticAltTwoOnSelector 4
508#define kStylisticAlternativesType 35
509#define kSwashAlternatesOffSelector 3
510#define kSwashAlternatesOnSelector 2
511#define kThirdWidthTextSelector 3
512#define kTraditionalNamesCharactersSelector 14
513#define kUpperCasePetiteCapsSelector 2
514#define kUpperCaseSmallCapsSelector 1
515#define kUpperCaseType 38
516
517/* Table data courtesy of Apple. */
Behdad Esfahbod522b1cc2014-08-14 13:29:30 -0400518static const struct feature_mapping_t {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400519 FourCharCode otFeatureTag;
520 uint16_t aatFeatureType;
521 uint16_t selectorToEnable;
522 uint16_t selectorToDisable;
523} feature_mappings[] = {
524 { 'c2pc', kUpperCaseType, kUpperCasePetiteCapsSelector, kDefaultUpperCaseSelector },
525 { 'c2sc', kUpperCaseType, kUpperCaseSmallCapsSelector, kDefaultUpperCaseSelector },
526 { 'calt', kContextualAlternatesType, kContextualAlternatesOnSelector, kContextualAlternatesOffSelector },
527 { 'case', kCaseSensitiveLayoutType, kCaseSensitiveLayoutOnSelector, kCaseSensitiveLayoutOffSelector },
528 { 'clig', kLigaturesType, kContextualLigaturesOnSelector, kContextualLigaturesOffSelector },
529 { 'cpsp', kCaseSensitiveLayoutType, kCaseSensitiveSpacingOnSelector, kCaseSensitiveSpacingOffSelector },
530 { 'cswh', kContextualAlternatesType, kContextualSwashAlternatesOnSelector, kContextualSwashAlternatesOffSelector },
531 { 'dlig', kLigaturesType, kRareLigaturesOnSelector, kRareLigaturesOffSelector },
532 { 'expt', kCharacterShapeType, kExpertCharactersSelector, 16 },
533 { 'frac', kFractionsType, kDiagonalFractionsSelector, kNoFractionsSelector },
534 { 'fwid', kTextSpacingType, kMonospacedTextSelector, 7 },
535 { 'halt', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
536 { 'hist', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
537 { 'hkna', kAlternateKanaType, kAlternateHorizKanaOnSelector, kAlternateHorizKanaOffSelector, },
538 { 'hlig', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
539 { 'hngl', kTransliterationType, kHanjaToHangulSelector, kNoTransliterationSelector },
540 { 'hojo', kCharacterShapeType, kHojoCharactersSelector, 16 },
541 { 'hwid', kTextSpacingType, kHalfWidthTextSelector, 7 },
542 { 'ital', kItalicCJKRomanType, kCJKItalicRomanOnSelector, kCJKItalicRomanOffSelector },
543 { 'jp04', kCharacterShapeType, kJIS2004CharactersSelector, 16 },
544 { 'jp78', kCharacterShapeType, kJIS1978CharactersSelector, 16 },
545 { 'jp83', kCharacterShapeType, kJIS1983CharactersSelector, 16 },
546 { 'jp90', kCharacterShapeType, kJIS1990CharactersSelector, 16 },
547 { 'liga', kLigaturesType, kCommonLigaturesOnSelector, kCommonLigaturesOffSelector },
548 { 'lnum', kNumberCaseType, kUpperCaseNumbersSelector, 2 },
549 { 'mgrk', kMathematicalExtrasType, kMathematicalGreekOnSelector, kMathematicalGreekOffSelector },
550 { 'nlck', kCharacterShapeType, kNLCCharactersSelector, 16 },
551 { 'onum', kNumberCaseType, kLowerCaseNumbersSelector, 2 },
552 { 'ordn', kVerticalPositionType, kOrdinalsSelector, kNormalPositionSelector },
553 { 'palt', kTextSpacingType, kAltProportionalTextSelector, 7 },
554 { 'pcap', kLowerCaseType, kLowerCasePetiteCapsSelector, kDefaultLowerCaseSelector },
555 { 'pkna', kTextSpacingType, kProportionalTextSelector, 7 },
556 { 'pnum', kNumberSpacingType, kProportionalNumbersSelector, 4 },
557 { 'pwid', kTextSpacingType, kProportionalTextSelector, 7 },
558 { 'qwid', kTextSpacingType, kQuarterWidthTextSelector, 7 },
559 { 'ruby', kRubyKanaType, kRubyKanaOnSelector, kRubyKanaOffSelector },
560 { 'sinf', kVerticalPositionType, kScientificInferiorsSelector, kNormalPositionSelector },
561 { 'smcp', kLowerCaseType, kLowerCaseSmallCapsSelector, kDefaultLowerCaseSelector },
562 { 'smpl', kCharacterShapeType, kSimplifiedCharactersSelector, 16 },
563 { 'ss01', kStylisticAlternativesType, kStylisticAltOneOnSelector, kStylisticAltOneOffSelector },
564 { 'ss02', kStylisticAlternativesType, kStylisticAltTwoOnSelector, kStylisticAltTwoOffSelector },
565 { 'ss03', kStylisticAlternativesType, kStylisticAltThreeOnSelector, kStylisticAltThreeOffSelector },
566 { 'ss04', kStylisticAlternativesType, kStylisticAltFourOnSelector, kStylisticAltFourOffSelector },
567 { 'ss05', kStylisticAlternativesType, kStylisticAltFiveOnSelector, kStylisticAltFiveOffSelector },
568 { 'ss06', kStylisticAlternativesType, kStylisticAltSixOnSelector, kStylisticAltSixOffSelector },
569 { 'ss07', kStylisticAlternativesType, kStylisticAltSevenOnSelector, kStylisticAltSevenOffSelector },
570 { 'ss08', kStylisticAlternativesType, kStylisticAltEightOnSelector, kStylisticAltEightOffSelector },
571 { 'ss09', kStylisticAlternativesType, kStylisticAltNineOnSelector, kStylisticAltNineOffSelector },
572 { 'ss10', kStylisticAlternativesType, kStylisticAltTenOnSelector, kStylisticAltTenOffSelector },
573 { 'ss11', kStylisticAlternativesType, kStylisticAltElevenOnSelector, kStylisticAltElevenOffSelector },
574 { 'ss12', kStylisticAlternativesType, kStylisticAltTwelveOnSelector, kStylisticAltTwelveOffSelector },
575 { 'ss13', kStylisticAlternativesType, kStylisticAltThirteenOnSelector, kStylisticAltThirteenOffSelector },
576 { 'ss14', kStylisticAlternativesType, kStylisticAltFourteenOnSelector, kStylisticAltFourteenOffSelector },
577 { 'ss15', kStylisticAlternativesType, kStylisticAltFifteenOnSelector, kStylisticAltFifteenOffSelector },
578 { 'ss16', kStylisticAlternativesType, kStylisticAltSixteenOnSelector, kStylisticAltSixteenOffSelector },
579 { 'ss17', kStylisticAlternativesType, kStylisticAltSeventeenOnSelector, kStylisticAltSeventeenOffSelector },
580 { 'ss18', kStylisticAlternativesType, kStylisticAltEighteenOnSelector, kStylisticAltEighteenOffSelector },
581 { 'ss19', kStylisticAlternativesType, kStylisticAltNineteenOnSelector, kStylisticAltNineteenOffSelector },
582 { 'ss20', kStylisticAlternativesType, kStylisticAltTwentyOnSelector, kStylisticAltTwentyOffSelector },
583 { 'subs', kVerticalPositionType, kInferiorsSelector, kNormalPositionSelector },
584 { 'sups', kVerticalPositionType, kSuperiorsSelector, kNormalPositionSelector },
585 { 'swsh', kContextualAlternatesType, kSwashAlternatesOnSelector, kSwashAlternatesOffSelector },
586 { 'titl', kStyleOptionsType, kTitlingCapsSelector, kNoStyleOptionsSelector },
587 { 'tnam', kCharacterShapeType, kTraditionalNamesCharactersSelector, 16 },
588 { 'tnum', kNumberSpacingType, kMonospacedNumbersSelector, 4 },
589 { 'trad', kCharacterShapeType, kTraditionalCharactersSelector, 16 },
590 { 'twid', kTextSpacingType, kThirdWidthTextSelector, 7 },
591 { 'unic', kLetterCaseType, 14, 15 },
592 { 'valt', kTextSpacingType, kAltProportionalTextSelector, 7 },
593 { 'vert', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
594 { 'vhal', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
595 { 'vkna', kAlternateKanaType, kAlternateVertKanaOnSelector, kAlternateVertKanaOffSelector },
596 { 'vpal', kTextSpacingType, kAltProportionalTextSelector, 7 },
597 { 'vrt2', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
598 { 'zero', kTypographicExtrasType, kSlashedZeroOnSelector, kSlashedZeroOffSelector },
599};
600
601static int
602_hb_feature_mapping_cmp (const void *key_, const void *entry_)
603{
604 unsigned int key = * (unsigned int *) key_;
605 const feature_mapping_t * entry = (const feature_mapping_t *) entry_;
606 return key < entry->otFeatureTag ? -1 :
607 key > entry->otFeatureTag ? 1 :
608 0;
609}
610
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400611hb_bool_t
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400612_hb_coretext_shape (hb_shape_plan_t *shape_plan,
613 hb_font_t *font,
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400614 hb_buffer_t *buffer,
615 const hb_feature_t *features,
616 unsigned int num_features)
617{
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400618 hb_face_t *face = font->face;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200619 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
620 CTFontRef ct_font = (CTFontRef) HB_SHAPER_DATA_GET (font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400621
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200622 CGFloat ct_font_size = CTFontGetSize (ct_font);
Behdad Esfahbod061105e2016-02-22 14:59:39 +0900623 CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
624 CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
625
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400626 /* Attach marks to their bases, to match the 'ot' shaper.
627 * Adapted from hb-ot-shape:hb_form_clusters().
628 * Note that this only makes us be closer to the 'ot' shaper,
629 * but by no means the same. For example, if there's
630 * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will
631 * continue pointing to B2 even though B2 was merged into B1's
632 * cluster... */
Behdad Esfahbod62c27112016-02-22 15:07:20 +0900633 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400634 {
635 hb_unicode_funcs_t *unicode = buffer->unicode;
636 unsigned int count = buffer->len;
637 hb_glyph_info_t *info = buffer->info;
638 for (unsigned int i = 1; i < count; i++)
639 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint)))
640 buffer->merge_clusters (i - 1, i + 1);
641 }
642
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400643 hb_auto_t<hb_vector_t<feature_record_t> > feature_records;
644 hb_auto_t<hb_vector_t<range_record_t> > range_records;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400645
Behdad Esfahbod36136962013-08-12 00:33:28 -0400646 /*
647 * Set up features.
648 * (copied + modified from code from hb-uniscribe.cc)
649 */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400650 if (num_features)
651 {
652 /* Sort features by start/end events. */
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400653 hb_auto_t<hb_vector_t<feature_event_t> > feature_events;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400654 for (unsigned int i = 0; i < num_features; i++)
655 {
656 const feature_mapping_t * mapping = (const feature_mapping_t *) bsearch (&features[i].tag,
657 feature_mappings,
658 ARRAY_LENGTH (feature_mappings),
659 sizeof (feature_mappings[0]),
660 _hb_feature_mapping_cmp);
661 if (!mapping)
662 continue;
663
664 active_feature_t feature;
665 feature.rec.feature = mapping->aatFeatureType;
666 feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable;
667 feature.order = i;
668
669 feature_event_t *event;
670
671 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400672 event->index = features[i].start;
673 event->start = true;
674 event->feature = feature;
675
676 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400677 event->index = features[i].end;
678 event->start = false;
679 event->feature = feature;
680 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400681 feature_events.qsort ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400682 /* Add a strategic final event. */
683 {
684 active_feature_t feature;
685 feature.rec.feature = HB_TAG_NONE;
686 feature.rec.setting = 0;
687 feature.order = num_features + 1;
688
689 feature_event_t *event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400690 event->index = 0; /* This value does magic. */
691 event->start = false;
692 event->feature = feature;
693 }
694
695 /* Scan events and save features for each range. */
Behdad Esfahbod37b95612018-05-01 19:09:00 -0400696 hb_auto_t<hb_vector_t<active_feature_t> > active_features;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400697 unsigned int last_index = 0;
698 for (unsigned int i = 0; i < feature_events.len; i++)
699 {
700 feature_event_t *event = &feature_events[i];
701
702 if (event->index != last_index)
703 {
704 /* Save a snapshot of active features and the range. */
705 range_record_t *range = range_records.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400706
Behdad Esfahbod36136962013-08-12 00:33:28 -0400707 if (active_features.len)
708 {
709 CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
710
711 /* TODO sort and resolve conflicting features? */
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400712 /* active_features.qsort (); */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400713 for (unsigned int j = 0; j < active_features.len; j++)
714 {
Cosimo Lupo9813be32017-07-14 17:11:46 +0100715 CFStringRef keys[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400716 kCTFontFeatureTypeIdentifierKey,
717 kCTFontFeatureSelectorIdentifierKey
718 };
Cosimo Lupo9813be32017-07-14 17:11:46 +0100719 CFNumberRef values[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400720 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature),
721 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting)
722 };
Behdad Esfahbod80cc0a72017-10-17 11:14:48 -0700723 static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), "");
Behdad Esfahbod36136962013-08-12 00:33:28 -0400724 CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault,
725 (const void **) keys,
726 (const void **) values,
Cosimo Lupo9813be32017-07-14 17:11:46 +0100727 ARRAY_LENGTH (keys),
Behdad Esfahbod36136962013-08-12 00:33:28 -0400728 &kCFTypeDictionaryKeyCallBacks,
729 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100730 for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++)
731 CFRelease (values[i]);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400732
733 CFArrayAppendValue (features_array, dict);
734 CFRelease (dict);
735
736 }
737
738 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
739 (const void **) &kCTFontFeatureSettingsAttribute,
740 (const void **) &features_array,
741 1,
742 &kCFTypeDictionaryKeyCallBacks,
743 &kCFTypeDictionaryValueCallBacks);
744 CFRelease (features_array);
745
746 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
747 CFRelease (attributes);
748
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200749 range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400750 CFRelease (font_desc);
751 }
752 else
753 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200754 range->font = nullptr;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400755 }
756
757 range->index_first = last_index;
758 range->index_last = event->index - 1;
759
760 last_index = event->index;
761 }
762
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700763 if (event->start)
764 {
765 active_features.push (event->feature);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400766 } else {
767 active_feature_t *feature = active_features.find (&event->feature);
768 if (feature)
Ebrahim Byagowi93bdf9b2018-05-09 23:24:17 +0430769 active_features.remove (feature - active_features.arrayZ);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400770 }
771 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400772 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400773
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400774 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500775 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500776
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400777#define ALLOCATE_ARRAY(Type, name, len, on_no_room) \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500778 Type *name = (Type *) scratch; \
779 { \
780 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400781 if (unlikely (_consumed > scratch_size)) \
782 { \
783 on_no_room; \
784 assert (0); \
785 } \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500786 scratch += _consumed; \
787 scratch_size -= _consumed; \
788 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400789
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400790 ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400791 unsigned int chars_len = 0;
792 for (unsigned int i = 0; i < buffer->len; i++) {
793 hb_codepoint_t c = buffer->info[i].codepoint;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400794 if (likely (c <= 0xFFFFu))
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400795 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400796 else if (unlikely (c > 0x10FFFFu))
797 pchars[chars_len++] = 0xFFFDu;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400798 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400799 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700800 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400801 }
802 }
803
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400804 ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/);
Behdad Esfahbod9b3c60c2014-08-11 13:25:43 -0400805 chars_len = 0;
806 for (unsigned int i = 0; i < buffer->len; i++)
807 {
808 hb_codepoint_t c = buffer->info[i].codepoint;
809 unsigned int cluster = buffer->info[i].cluster;
810 log_clusters[chars_len++] = cluster;
811 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
812 log_clusters[chars_len++] = cluster; /* Surrogates. */
813 }
814
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400815#define FAIL(...) \
816 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200817 DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400818 ret = false; \
819 goto fail; \
820 } HB_STMT_END;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400821
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400822 bool ret = true;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200823 CFStringRef string_ref = nullptr;
824 CTLineRef line = nullptr;
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400825
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400826 if (0)
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400827 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400828resize_and_retry:
Behdad Esfahbod1b550772014-08-11 20:45:12 -0400829 DEBUG_MSG (CORETEXT, buffer, "Buffer resize");
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400830 /* string_ref uses the scratch-buffer for backing store, and line references
831 * string_ref (via attr_string). We must release those before resizing buffer. */
832 assert (string_ref);
833 assert (line);
834 CFRelease (string_ref);
835 CFRelease (line);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200836 string_ref = nullptr;
837 line = nullptr;
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400838
839 /* Get previous start-of-scratch-area, that we use later for readjusting
840 * our existing scratch arrays. */
841 unsigned int old_scratch_used;
842 hb_buffer_t::scratch_buffer_t *old_scratch;
843 old_scratch = buffer->get_scratch_buffer (&old_scratch_used);
844 old_scratch_used = scratch - old_scratch;
845
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400846 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbodb9993d82014-08-10 17:40:24 -0400847 FAIL ("Buffer resize failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400848
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400849 /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the
850 * cleanest way to do without completely restructuring the rest of this shaper. */
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500851 scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400852 pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch)));
853 log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch)));
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400854 scratch += old_scratch_used;
855 scratch_size -= old_scratch_used;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400856 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400857 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200858 string_ref = CFStringCreateWithCharactersNoCopy (nullptr,
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400859 pchars, chars_len,
860 kCFAllocatorNull);
861 if (unlikely (!string_ref))
862 FAIL ("CFStringCreateWithCharactersNoCopy failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400863
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400864 /* Create an attributed string, populate it, and create a line from it, then release attributed string. */
865 {
Behdad Esfahbodfd1a6aa2014-08-11 20:01:37 -0400866 CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault,
867 chars_len);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400868 if (unlikely (!attr_string))
869 FAIL ("CFAttributedStringCreateMutable failed");
870 CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref);
Behdad Esfahbod3eb6a4d2014-08-12 19:10:33 -0400871 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
872 {
873 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
874 kCTVerticalFormsAttributeName, kCFBooleanTrue);
875 }
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400876
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400877 if (buffer->props.language)
878 {
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400879/* What's the iOS equivalent of this check?
880 * The symbols was introduced in iOS 7.0.
881 * At any rate, our fallback is safe and works fine. */
882#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
883# define kCTLanguageAttributeName CFSTR ("NSLanguage")
884#endif
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400885 CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault,
886 hb_language_to_string (buffer->props.language),
887 kCFStringEncodingUTF8,
888 kCFAllocatorNull);
889 if (unlikely (!lang))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700890 {
891 CFRelease (attr_string);
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400892 FAIL ("CFStringCreateWithCStringNoCopy failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700893 }
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400894 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
895 kCTLanguageAttributeName, lang);
896 CFRelease (lang);
897 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400898 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200899 kCTFontAttributeName, ct_font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400900
Cosimo Lupo9813be32017-07-14 17:11:46 +0100901 if (num_features && range_records.len)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400902 {
903 unsigned int start = 0;
904 range_record_t *last_range = &range_records[0];
905 for (unsigned int k = 0; k < chars_len; k++)
906 {
907 range_record_t *range = last_range;
908 while (log_clusters[k] < range->index_first)
909 range--;
910 while (log_clusters[k] > range->index_last)
911 range++;
912 if (range != last_range)
913 {
914 if (last_range->font)
915 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start),
916 kCTFontAttributeName, last_range->font);
917
918 start = k;
919 }
920
921 last_range = range;
922 }
923 if (start != chars_len && last_range->font)
924 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start),
925 kCTFontAttributeName, last_range->font);
926 }
Cosimo Lupo9813be32017-07-14 17:11:46 +0100927 /* Enable/disable kern if requested.
928 *
929 * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText.
930 */
931 if (num_features)
932 {
933 unsigned int zeroint = 0;
934 CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint);
935 for (unsigned int i = 0; i < num_features; i++)
936 {
937 const hb_feature_t &feature = features[i];
938 if (feature.tag == HB_TAG('k','e','r','n') &&
939 feature.start < chars_len && feature.start < feature.end)
940 {
941 CFRange feature_range = CFRangeMake (feature.start,
942 MIN (feature.end, chars_len) - feature.start);
943 if (feature.value)
944 CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName);
945 else
946 CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero);
947 }
948 }
949 CFRelease (zero);
950 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400951
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400952 int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
953 CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level);
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500954#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
955 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
956#endif
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400957 CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault,
958 (const void **) &kCTTypesetterOptionForcedEmbeddingLevel,
959 (const void **) &level_number,
960 1,
961 &kCFTypeDictionaryKeyCallBacks,
962 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100963 CFRelease (level_number);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400964 if (unlikely (!options))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700965 {
966 CFRelease (attr_string);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400967 FAIL ("CFDictionaryCreate failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700968 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400969
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400970 CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
971 CFRelease (options);
972 CFRelease (attr_string);
973 if (unlikely (!typesetter))
974 FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed");
975
976 line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0));
977 CFRelease (typesetter);
978 if (unlikely (!line))
979 FAIL ("CTTypesetterCreateLine failed");
980 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400981
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400982 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line);
983 unsigned int num_runs = CFArrayGetCount (glyph_runs);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200984 DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400985
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400986 buffer->len = 0;
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400987 uint32_t status_and = ~0, status_or = 0;
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800988 double advances_so_far = 0;
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700989 /* For right-to-left runs, CoreText returns the glyphs positioned such that
990 * any trailing whitespace is to the left of (0,0). Adjust coordinate system
991 * to fix for that. Test with any RTL string with trailing spaces.
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430992 * https://crbug.com/469028
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700993 */
994 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
995 {
996 advances_so_far -= CTLineGetTrailingWhitespaceWidth (line);
997 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
998 advances_so_far = -advances_so_far;
999 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001000
1001 const CFRange range_all = CFRangeMake (0, 0);
1002
1003 for (unsigned int i = 0; i < num_runs; i++)
1004 {
1005 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i));
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001006 CTRunStatus run_status = CTRunGetStatus (run);
1007 status_or |= run_status;
1008 status_and &= run_status;
1009 DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001010 double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001011 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
1012 run_advance = -run_advance;
1013 DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001014
1015 /* CoreText does automatic font fallback (AKA "cascading") for characters
1016 * not supported by the requested font, and provides no way to turn it off,
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001017 * so we must detect if the returned run uses a font other than the requested
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001018 * one and fill in the buffer with .notdef glyphs instead of random glyph
1019 * indices from a different font.
1020 */
1021 CFDictionaryRef attributes = CTRunGetAttributes (run);
1022 CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001023 if (!CFEqual (run_ct_font, ct_font))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001024 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001025 /* The run doesn't use our main font instance. We have to figure out
1026 * whether font fallback happened, or this is just CoreText giving us
1027 * another CTFont using the same underlying CGFont. CoreText seems
1028 * to do that in a variety of situations, one of which being vertical
1029 * text, but also perhaps for caching reasons.
1030 *
1031 * First, see if it uses any of our subfonts created to set font features...
1032 *
1033 * Next, compare the CGFont to the one we used to create our fonts.
1034 * Even this doesn't work all the time.
1035 *
1036 * Finally, we compare PS names, which I don't think are unique...
1037 *
1038 * Looks like if we really want to be sure here we have to modify the
1039 * font to change the name table, similar to what we do in the uniscribe
1040 * backend.
1041 *
1042 * However, even that wouldn't work if we were passed in the CGFont to
Behdad Esfahbod59089622016-04-04 14:54:32 -07001043 * construct a hb_face to begin with.
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001044 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301045 * See: https://github.com/harfbuzz/harfbuzz/pull/36
Behdad Esfahbod59089622016-04-04 14:54:32 -07001046 *
1047 * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001048 */
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001049 bool matched = false;
1050 for (unsigned int i = 0; i < range_records.len; i++)
1051 if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font))
1052 {
1053 matched = true;
1054 break;
1055 }
1056 if (!matched)
1057 {
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +07001058 CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001059 if (run_cg_font)
1060 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001061 matched = CFEqual (run_cg_font, cg_font);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001062 CFRelease (run_cg_font);
1063 }
1064 }
1065 if (!matched)
1066 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001067 CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001068 CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey);
1069 CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0);
1070 CFRelease (run_ps_name);
1071 CFRelease (font_ps_name);
1072 if (result == kCFCompareEqualTo)
1073 matched = true;
1074 }
1075 if (!matched)
1076 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001077 CFRange range = CTRunGetStringRange (run);
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001078 DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld",
1079 range.location, range.location + range.length);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001080 if (!buffer->ensure_inplace (buffer->len + range.length))
1081 goto resize_and_retry;
1082 hb_glyph_info_t *info = buffer->info + buffer->len;
1083
Behdad Esfahbodb0b38bb2015-01-21 19:19:33 -08001084 hb_codepoint_t notdef = 0;
1085 hb_direction_t dir = buffer->props.direction;
1086 hb_position_t x_advance, y_advance, x_offset, y_offset;
1087 hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance);
1088 hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset);
1089 hb_position_t advance = x_advance + y_advance;
1090 x_offset = -x_offset;
1091 y_offset = -y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001092
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001093 unsigned int old_len = buffer->len;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001094 for (CFIndex j = range.location; j < range.location + range.length; j++)
1095 {
1096 UniChar ch = CFStringGetCharacterAtIndex (string_ref, j);
1097 if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j)
1098 {
1099 ch = CFStringGetCharacterAtIndex (string_ref, j - 1);
1100 if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu))
1101 /* This is the second of a surrogate pair. Don't need .notdef
1102 * for this one. */
1103 continue;
1104 }
Behdad Esfahbod982d94e2015-01-28 10:51:33 -08001105 if (buffer->unicode->is_default_ignorable (ch))
1106 continue;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001107
1108 info->codepoint = notdef;
Behdad Esfahbod3c41ccb2014-08-11 15:11:59 -04001109 info->cluster = log_clusters[j];
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001110
1111 info->mask = advance;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001112 info->var1.i32 = x_offset;
1113 info->var2.i32 = y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001114
1115 info++;
1116 buffer->len++;
1117 }
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001118 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
1119 buffer->reverse_range (old_len, buffer->len);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001120 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001121 continue;
1122 }
1123 }
1124
1125 unsigned int num_glyphs = CTRunGetGlyphCount (run);
1126 if (num_glyphs == 0)
1127 continue;
1128
Behdad Esfahbod81b8d972014-08-12 15:49:47 -04001129 if (!buffer->ensure_inplace (buffer->len + num_glyphs))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001130 goto resize_and_retry;
1131
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001132 hb_glyph_info_t *run_info = buffer->info + buffer->len;
1133
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001134 /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always
1135 * succeed, and so copying data to our own buffer will be rare. Reports
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001136 * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001137 * frequently. At any rate, we can test that codepath by setting USE_PTR
1138 * to false. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001139
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001140#define USE_PTR true
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001141
1142#define SCRATCH_SAVE() \
1143 unsigned int scratch_size_saved = scratch_size; \
1144 hb_buffer_t::scratch_buffer_t *scratch_saved = scratch
1145
1146#define SCRATCH_RESTORE() \
1147 scratch_size = scratch_size_saved; \
1148 scratch = scratch_saved;
1149
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001150 { /* Setup glyphs */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001151 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001152 const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001153 if (!glyphs) {
1154 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry);
1155 CTRunGetGlyphs (run, range_all, glyph_buf);
1156 glyphs = glyph_buf;
1157 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001158 const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001159 if (!string_indices) {
1160 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry);
1161 CTRunGetStringIndices (run, range_all, index_buf);
1162 string_indices = index_buf;
1163 }
1164 hb_glyph_info_t *info = run_info;
1165 for (unsigned int j = 0; j < num_glyphs; j++)
1166 {
1167 info->codepoint = glyphs[j];
1168 info->cluster = log_clusters[string_indices[j]];
1169 info++;
1170 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001171 SCRATCH_RESTORE();
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001172 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001173 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001174 /* Setup positions.
1175 * Note that CoreText does not return advances for glyphs. As such,
1176 * for all but last glyph, we use the delta position to next glyph as
1177 * advance (in the advance direction only), and for last glyph we set
1178 * whatever is needed to make the whole run's advance add up. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001179 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001180 const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001181 if (!positions) {
1182 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry);
1183 CTRunGetPositions (run, range_all, position_buf);
1184 positions = position_buf;
1185 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001186 hb_glyph_info_t *info = run_info;
1187 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1188 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001189 hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001190 for (unsigned int j = 0; j < num_glyphs; j++)
1191 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001192 double advance;
1193 if (likely (j + 1 < num_glyphs))
1194 advance = positions[j + 1].x - positions[j].x;
1195 else /* last glyph */
1196 advance = run_advance - (positions[j].x - positions[0].x);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001197 info->mask = advance * x_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001198 info->var1.i32 = x_offset;
1199 info->var2.i32 = positions[j].y * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001200 info++;
1201 }
1202 }
1203 else
1204 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001205 hb_position_t y_offset = (positions[0].y - advances_so_far) * y_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].y - positions[j].y;
1211 else /* last glyph */
1212 advance = run_advance - (positions[j].y - positions[0].y);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001213 info->mask = advance * y_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001214 info->var1.i32 = positions[j].x * x_mult;
1215 info->var2.i32 = y_offset;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001216 info++;
1217 }
1218 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001219 SCRATCH_RESTORE();
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001220 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001221 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001222#undef SCRATCH_RESTORE
1223#undef SCRATCH_SAVE
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001224#undef USE_PTR
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001225#undef ALLOCATE_ARRAY
1226
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001227 buffer->len += num_glyphs;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001228 }
1229
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001230 /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
Bruce Mitchener90218fa2018-01-31 20:44:45 +07001231 * or if it does, it doesn't respect it. So we get runs with wrong
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001232 * directions. As such, disable the assert... It wouldn't crash, but
1233 * cursoring will be off...
1234 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301235 * https://crbug.com/419769
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001236 */
1237 if (0)
1238 {
1239 /* Make sure all runs had the expected direction. */
1240 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1241 assert (bool (status_and & kCTRunStatusRightToLeft) == backward);
1242 assert (bool (status_or & kCTRunStatusRightToLeft) == backward);
1243 }
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001244
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001245 buffer->clear_positions ();
1246
1247 unsigned int count = buffer->len;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001248 hb_glyph_info_t *info = buffer->info;
1249 hb_glyph_position_t *pos = buffer->pos;
1250 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1251 for (unsigned int i = 0; i < count; i++)
1252 {
1253 pos->x_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001254 pos->x_offset = info->var1.i32;
1255 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001256
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001257 info++, pos++;
1258 }
1259 else
1260 for (unsigned int i = 0; i < count; i++)
1261 {
1262 pos->y_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001263 pos->x_offset = info->var1.i32;
1264 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001265
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001266 info++, pos++;
1267 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001268
1269 /* Fix up clusters so that we never return out-of-order indices;
1270 * if core text has reordered glyphs, we'll merge them to the
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001271 * beginning of the reordered cluster. CoreText is nice enough
1272 * to tell us whenever it has produced nonmonotonic results...
1273 * Note that we assume the input clusters were nonmonotonic to
1274 * begin with.
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001275 *
1276 * This does *not* mean we'll form the same clusters as Uniscribe
1277 * or the native OT backend, only that the cluster indices will be
1278 * monotonic in the output buffer. */
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001279 if (count > 1 && (status_or & kCTRunStatusNonMonotonic))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001280 {
1281 hb_glyph_info_t *info = buffer->info;
1282 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
1283 {
1284 unsigned int cluster = info[count - 1].cluster;
1285 for (unsigned int i = count - 1; i > 0; i--)
1286 {
1287 cluster = MIN (cluster, info[i - 1].cluster);
1288 info[i - 1].cluster = cluster;
1289 }
1290 }
1291 else
1292 {
1293 unsigned int cluster = info[0].cluster;
1294 for (unsigned int i = 1; i < count; i++)
1295 {
1296 cluster = MIN (cluster, info[i].cluster);
1297 info[i].cluster = cluster;
1298 }
1299 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001300 }
1301 }
1302
Behdad Esfahbode4da3802017-11-10 17:14:27 -08001303 buffer->unsafe_to_break_all ();
1304
Behdad Esfahbod4acce772014-08-11 17:46:50 -04001305#undef FAIL
1306
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001307fail:
1308 if (string_ref)
1309 CFRelease (string_ref);
1310 if (line)
1311 CFRelease (line);
1312
Behdad Esfahbod25f4fb92014-08-10 19:05:25 -04001313 for (unsigned int i = 0; i < range_records.len; i++)
1314 if (range_records[i].font)
1315 CFRelease (range_records[i].font);
1316
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001317 return ret;
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001318}
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001319
1320
1321/*
1322 * AAT shaper
1323 */
1324
Behdad Esfahbodd4bb52b2017-02-09 14:13:25 -08001325HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, face)
1326HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, font)
1327
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001328/*
1329 * shaper face data
1330 */
1331
1332struct hb_coretext_aat_shaper_face_data_t {};
1333
1334hb_coretext_aat_shaper_face_data_t *
1335_hb_coretext_aat_shaper_face_data_create (hb_face_t *face)
1336{
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001337 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 -04001338
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001339 for (unsigned int i = 0; i < ARRAY_LENGTH (tags); i++)
1340 {
1341 hb_blob_t *blob = face->reference_table (tags[i]);
1342 if (hb_blob_get_length (blob))
1343 {
1344 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001345 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 +02001346 }
1347 hb_blob_destroy (blob);
1348 }
1349
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001350 return nullptr;
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001351}
1352
1353void
1354_hb_coretext_aat_shaper_face_data_destroy (hb_coretext_aat_shaper_face_data_t *data HB_UNUSED)
1355{
1356}
1357
1358
1359/*
1360 * shaper font data
1361 */
1362
1363struct hb_coretext_aat_shaper_font_data_t {};
1364
1365hb_coretext_aat_shaper_font_data_t *
1366_hb_coretext_aat_shaper_font_data_create (hb_font_t *font)
1367{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001368 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 -04001369}
1370
1371void
1372_hb_coretext_aat_shaper_font_data_destroy (hb_coretext_aat_shaper_font_data_t *data HB_UNUSED)
1373{
1374}
1375
1376
1377/*
1378 * shaper shape_plan data
1379 */
1380
1381struct hb_coretext_aat_shaper_shape_plan_data_t {};
1382
1383hb_coretext_aat_shaper_shape_plan_data_t *
1384_hb_coretext_aat_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
1385 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -07001386 unsigned int num_user_features HB_UNUSED,
1387 const int *coords HB_UNUSED,
1388 unsigned int num_coords HB_UNUSED)
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001389{
1390 return (hb_coretext_aat_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
1391}
1392
1393void
1394_hb_coretext_aat_shaper_shape_plan_data_destroy (hb_coretext_aat_shaper_shape_plan_data_t *data HB_UNUSED)
1395{
1396}
1397
1398
1399/*
1400 * shaper
1401 */
1402
1403hb_bool_t
1404_hb_coretext_aat_shape (hb_shape_plan_t *shape_plan,
1405 hb_font_t *font,
1406 hb_buffer_t *buffer,
1407 const hb_feature_t *features,
1408 unsigned int num_features)
1409{
1410 return _hb_coretext_shape (shape_plan, font, buffer, features, num_features);
1411}