blob: 8885cfe73043742e65f8f2c14e54f5fecad5f42c [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 Esfahbodc77ae402018-08-25 22:36:36 -070029#include "hb.hh"
Behdad Esfahbodaa3450c2019-06-17 22:41:49 -070030
31#ifdef HAVE_CORETEXT
32
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070033#include "hb-shaper-impl.hh"
Jonathan Kewaa6d8492012-07-24 15:52:32 -040034
Jonathan Kewaa6d8492012-07-24 15:52:32 -040035#include "hb-coretext.h"
Behdad Esfahbode0c5e0d2018-10-13 18:37:14 -040036#include "hb-aat-layout.hh"
Behdad Esfahbod6a2cbc62017-10-12 10:46:09 +020037#include <math.h>
Jonathan Kewaa6d8492012-07-24 15:52:32 -040038
Behdad Esfahbod04981ee2018-10-27 04:40:43 -070039
40/**
41 * SECTION:hb-coretext
42 * @title: hb-coretext
43 * @short_description: CoreText integration
44 * @include: hb-coretext.h
45 *
46 * Functions for using HarfBuzz with the CoreText fonts.
47 **/
48
Behdad Esfahbod06c14222017-10-11 15:29:53 +020049/* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020050#define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020051
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070052static void
53release_table_data (void *user_data)
54{
55 CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data);
56 CFRelease(cf_data);
57}
58
59static hb_blob_t *
Ebrahim Byagowi9fea6b42019-07-05 18:46:41 +043060_hb_cg_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070061{
62 CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data);
63 CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag);
64 if (unlikely (!cf_data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020065 return nullptr;
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070066
67 const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data));
68 const size_t length = CFDataGetLength (cf_data);
69 if (!data || !length)
Bruce Mitchener0c660432018-01-31 20:24:27 +070070 {
71 CFRelease (cf_data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020072 return nullptr;
Bruce Mitchener0c660432018-01-31 20:24:27 +070073 }
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070074
75 return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY,
76 reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)),
77 release_table_data);
78}
79
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020080static void
81_hb_cg_font_release (void *data)
82{
83 CGFontRelease ((CGFontRef) data);
84}
85
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070086
Behdad Esfahbod90194ef2016-02-22 15:42:53 +090087static CTFontDescriptorRef
Ebrahim Byagowie4120082018-12-17 21:31:01 +033088get_last_resort_font_desc ()
Behdad Esfahbod90194ef2016-02-22 15:42:53 +090089{
90 // TODO Handle allocation failures?
91 CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0);
92 CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault,
93 (const void **) &last_resort,
94 1,
95 &kCFTypeArrayCallBacks);
96 CFRelease (last_resort);
97 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
98 (const void **) &kCTFontCascadeListAttribute,
99 (const void **) &cascade_list,
100 1,
101 &kCFTypeDictionaryKeyCallBacks,
102 &kCFTypeDictionaryValueCallBacks);
103 CFRelease (cascade_list);
104
105 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
106 CFRelease (attributes);
107 return font_desc;
108}
109
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900110static void
111release_data (void *info, const void *data, size_t size)
112{
113 assert (hb_blob_get_length ((hb_blob_t *) info) == size &&
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430114 hb_blob_get_data ((hb_blob_t *) info, nullptr) == data);
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900115
116 hb_blob_destroy ((hb_blob_t *) info);
117}
118
119static CGFontRef
120create_cg_font (hb_face_t *face)
121{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200122 CGFontRef cg_font = nullptr;
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200123 if (face->destroy == _hb_cg_font_release)
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900124 {
125 cg_font = CGFontRetain ((CGFontRef) face->user_data);
126 }
127 else
128 {
129 hb_blob_t *blob = hb_face_reference_blob (face);
130 unsigned int blob_length;
131 const char *blob_data = hb_blob_get_data (blob, &blob_length);
132 if (unlikely (!blob_length))
133 DEBUG_MSG (CORETEXT, face, "Face has empty blob");
134
135 CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data);
136 if (likely (provider))
137 {
138 cg_font = CGFontCreateWithDataProvider (provider);
139 if (unlikely (!cg_font))
140 DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed");
141 CGDataProviderRelease (provider);
142 }
143 }
144 return cg_font;
145}
146
Behdad Esfahbode5611222016-02-22 15:28:37 +0900147static CTFontRef
148create_ct_font (CGFontRef cg_font, CGFloat font_size)
149{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200150 CTFontRef ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200151
152 /* CoreText does not enable trak table usage / tracking when creating a CTFont
153 * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems
154 * to be through the CTFontCreateUIFontForLanguage call. */
155 CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font);
156 if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) ||
157 CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay")))
158 {
Kenfdfa3d22019-02-10 23:46:05 -0800159#if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1080
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500160# define kCTFontUIFontSystem kCTFontSystemFontType
161# define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType
162#endif
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200163 CTFontUIFontType font_type = kCTFontUIFontSystem;
164 if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold")))
165 font_type = kCTFontUIFontEmphasizedSystem;
166
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200167 ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200168 CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font);
169 if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo)
170 {
171 CFRelease(ct_font);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200172 ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200173 }
174 CFRelease (ct_result_name);
175 }
176 CFRelease (cg_postscript_name);
177
178 if (!ct_font)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200179 ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200180
Behdad Esfahbode5611222016-02-22 15:28:37 +0900181 if (unlikely (!ct_font)) {
182 DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200183 return nullptr;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900184 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700185
186 /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter
187 * bug indicate that the cascade list reconfiguration occasionally causes
188 * crashes in CoreText on OS X 10.9, thus let's skip this step on older
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300189 * operating system versions. Except for the emoji font, where _not_
190 * reconfiguring the cascade list causes CoreText crashes. For details, see
191 * crbug.com/549610 */
Ebrahim Byagowifc4e6712016-09-09 23:28:28 +0430192 // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200193 if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) {
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300194 CFStringRef fontName = CTFontCopyPostScriptName (ct_font);
195 bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo;
196 CFRelease (fontName);
197 if (!isEmojiFont)
198 return ct_font;
199 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700200
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430201 CFURLRef original_url = nullptr;
Kenfdfa3d22019-02-10 23:46:05 -0800202#if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500203 ATSFontRef atsFont;
204 FSRef fsref;
205 OSStatus status;
206 atsFont = CTFontGetPlatformFont (ct_font, NULL);
207 status = ATSFontGetFileReference (atsFont, &fsref);
208 if (status == noErr)
209 original_url = CFURLCreateFromFSRef (NULL, &fsref);
210#else
211 original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute);
212#endif
Behdad Esfahbode5611222016-02-22 15:28:37 +0900213
214 /* Create font copy with cascade list that has LastResort first; this speeds up CoreText
215 * font fallback which we don't need anyway. */
216 {
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900217 CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc ();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200218 CTFontRef new_ct_font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, last_resort_font_desc);
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900219 CFRelease (last_resort_font_desc);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900220 if (new_ct_font)
221 {
Behdad Esfahbodfc9de442016-06-30 09:46:52 -0700222 /* The CTFontCreateCopyWithAttributes call fails to stay on the same font
223 * when reconfiguring the cascade list and may switch to a different font
224 * when there are fonts that go by the same name, since the descriptor is
225 * just name and size.
226 *
227 * Avoid reconfiguring the cascade lists if the new font is outside the
228 * system locations that we cannot access from the sandboxed renderer
229 * process in Blink. This can be detected by the new file URL location
230 * that the newly found font points to. */
Ebrahim Byagowi8b0d6422018-04-23 18:37:35 +0430231 CFURLRef new_url = nullptr;
Kenfdfa3d22019-02-10 23:46:05 -0800232#if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500233 atsFont = CTFontGetPlatformFont (new_ct_font, NULL);
234 status = ATSFontGetFileReference (atsFont, &fsref);
235 if (status == noErr)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430236 new_url = CFURLCreateFromFSRef (NULL, &fsref);
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500237#else
238 new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute);
239#endif
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430240 // Keep reconfigured font if URL cannot be retrieved (seems to be the case
241 // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606
242 if (!original_url || !new_url || CFEqual (original_url, new_url)) {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430243 CFRelease (ct_font);
244 ct_font = new_ct_font;
Dominik Röttschesa0223272016-06-16 14:19:39 +0200245 } else {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430246 CFRelease (new_ct_font);
247 DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed.");
Dominik Röttschesa0223272016-06-16 14:19:39 +0200248 }
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430249 if (new_url)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430250 CFRelease (new_url);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900251 }
252 else
253 DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed");
254 }
255
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430256 if (original_url)
257 CFRelease (original_url);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200258 return ct_font;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900259}
260
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700261hb_coretext_face_data_t *
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400262_hb_coretext_shaper_face_data_create (hb_face_t *face)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400263{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200264 CGFontRef cg_font = create_cg_font (face);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400265
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200266 if (unlikely (!cg_font))
Behdad Esfahboda9e25e92014-03-14 19:55:46 -0700267 {
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900268 DEBUG_MSG (CORETEXT, face, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200269 return nullptr;
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900270 }
271
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700272 return (hb_coretext_face_data_t *) cg_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400273}
274
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400275void
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700276_hb_coretext_shaper_face_data_destroy (hb_coretext_face_data_t *data)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400277{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200278 CFRelease ((CGFontRef) data);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400279}
280
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800281hb_face_t *
282hb_coretext_face_create (CGFontRef cg_font)
283{
Ebrahim Byagowi9fea6b42019-07-05 18:46:41 +0430284 return hb_face_create_for_tables (_hb_cg_reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800285}
286
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430287/*
288 * Since: 0.9.10
289 */
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500290CGFontRef
Behdad Esfahbode923e642012-12-09 19:39:40 -0500291hb_coretext_face_get_cg_font (hb_face_t *face)
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500292{
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500293 return (CGFontRef) (const void *) face->data.coretext;
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500294}
295
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400296
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700297hb_coretext_font_data_t *
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200298_hb_coretext_shaper_font_data_create (hb_font_t *font)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400299{
Dominik Röttschesdb7a73c2017-10-11 13:24:39 +0200300 hb_face_t *face = font->face;
Behdad Esfahbod0809b762018-11-16 08:29:47 -0500301 const hb_coretext_face_data_t *face_data = face->data.coretext;
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500302 if (unlikely (!face_data)) return nullptr;
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500303 CGFontRef cg_font = (CGFontRef) (const void *) face->data.coretext;
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200304
Ebrahim Byagowif3214df2019-09-14 10:56:00 +0430305 CGFloat font_size = (CGFloat) (font->ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : font->ptem);
Tor Arne Vestbøf401f852018-12-17 00:48:35 +0100306 CTFontRef ct_font = create_ct_font (cg_font, font_size);
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200307
308 if (unlikely (!ct_font))
309 {
310 DEBUG_MSG (CORETEXT, font, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200311 return nullptr;
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200312 }
313
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700314 return (hb_coretext_font_data_t *) ct_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400315}
316
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400317void
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700318_hb_coretext_shaper_font_data_destroy (hb_coretext_font_data_t *data)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400319{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200320 CFRelease ((CTFontRef) data);
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400321}
322
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500323static const hb_coretext_font_data_t *
324hb_coretext_font_data_sync (hb_font_t *font)
325{
326retry:
Behdad Esfahbod0809b762018-11-16 08:29:47 -0500327 const hb_coretext_font_data_t *data = font->data.coretext;
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500328 if (unlikely (!data)) return nullptr;
329
Ebrahim Byagowif3214df2019-09-14 10:56:00 +0430330 if (fabs (CTFontGetSize ((CTFontRef) data) - (CGFloat) font->ptem) > .5)
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500331 {
332 /* XXX-MT-bug
333 * Note that evaluating condition above can be dangerous if another thread
334 * got here first and destructed data. That's, as always, bad use pattern.
335 * If you modify the font (change font size), other threads must not be
336 * using it at the same time. However, since this check is delayed to
337 * when one actually tries to shape something, this is a XXX race condition
338 * (and the only one we have that I know of) right now. Ie. you modify the
339 * font size in one thread, then (supposedly safely) try to use it from two
340 * or more threads and BOOM! I'm not sure how to fix this. We want RCU.
341 */
342
343 /* Drop and recreate. */
344 /* If someone dropped it in the mean time, throw it away and don't touch it.
345 * Otherwise, destruct it. */
Behdad Esfahbod50d1a412018-11-16 08:52:57 -0500346 if (likely (font->data.coretext.cmpexch (const_cast<hb_coretext_font_data_t *> (data), nullptr)))
Behdad Esfahbod0aab8612018-11-16 08:43:25 -0500347 _hb_coretext_shaper_font_data_destroy (const_cast<hb_coretext_font_data_t *> (data));
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500348 else
349 goto retry;
350 }
351 return font->data.coretext;
352}
353
354
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800355/*
356 * Since: 1.7.2
357 */
358hb_font_t *
359hb_coretext_font_create (CTFontRef ct_font)
360{
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +0700361 CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr);
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800362 hb_face_t *face = hb_coretext_face_create (cg_font);
363 CFRelease (cg_font);
364 hb_font_t *font = hb_font_create (face);
365 hb_face_destroy (face);
366
Behdad Esfahbod93ef20a2018-11-03 15:03:06 -0400367 if (unlikely (hb_object_is_immutable (font)))
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800368 return font;
369
Tor Arne Vestbøf401f852018-12-17 00:48:35 +0100370 hb_font_set_ptem (font, CTFontGetSize (ct_font));
Behdad Esfahbod12453952017-12-17 12:32:33 -0500371
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800372 /* Let there be dragons here... */
Behdad Esfahbod0aab8612018-11-16 08:43:25 -0500373 font->data.coretext.cmpexch (nullptr, (hb_coretext_font_data_t *) CFRetain (ct_font));
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800374
375 return font;
376}
377
378CTFontRef
379hb_coretext_font_get_ct_font (hb_font_t *font)
380{
Behdad Esfahbod0aab8612018-11-16 08:43:25 -0500381 const hb_coretext_font_data_t *data = hb_coretext_font_data_sync (font);
Behdad Esfahbodcfb97712018-11-16 03:24:22 -0500382 return data ? (CTFontRef) data : nullptr;
Behdad Esfahbodd5e29302017-11-28 23:11:34 -0800383}
384
385
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400386/*
387 * shaper
388 */
389
Behdad Esfahbod36136962013-08-12 00:33:28 -0400390struct feature_record_t {
391 unsigned int feature;
392 unsigned int setting;
393};
394
395struct active_feature_t {
396 feature_record_t rec;
397 unsigned int order;
398
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400399 HB_INTERNAL static int cmp (const void *pa, const void *pb) {
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600400 const active_feature_t *a = (const active_feature_t *) pa;
401 const active_feature_t *b = (const active_feature_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400402 return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 :
403 a->order < b->order ? -1 : a->order > b->order ? 1 :
404 a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 :
405 0;
406 }
407 bool operator== (const active_feature_t *f) {
408 return cmp (this, f) == 0;
409 }
410};
411
412struct feature_event_t {
413 unsigned int index;
414 bool start;
415 active_feature_t feature;
416
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400417 HB_INTERNAL static int cmp (const void *pa, const void *pb) {
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600418 const feature_event_t *a = (const feature_event_t *) pa;
419 const feature_event_t *b = (const feature_event_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400420 return a->index < b->index ? -1 : a->index > b->index ? 1 :
421 a->start < b->start ? -1 : a->start > b->start ? 1 :
422 active_feature_t::cmp (&a->feature, &b->feature);
423 }
424};
425
426struct range_record_t {
427 CTFontRef font;
428 unsigned int index_first; /* == start */
429 unsigned int index_last; /* == end - 1 */
430};
431
432
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400433hb_bool_t
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400434_hb_coretext_shape (hb_shape_plan_t *shape_plan,
435 hb_font_t *font,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430436 hb_buffer_t *buffer,
437 const hb_feature_t *features,
438 unsigned int num_features)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400439{
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400440 hb_face_t *face = font->face;
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500441 CGFontRef cg_font = (CGFontRef) (const void *) face->data.coretext;
Behdad Esfahbod0809b762018-11-16 08:29:47 -0500442 CTFontRef ct_font = (CTFontRef) hb_coretext_font_data_sync (font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400443
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200444 CGFloat ct_font_size = CTFontGetSize (ct_font);
Behdad Esfahbod061105e2016-02-22 14:59:39 +0900445 CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
446 CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
447
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400448 /* Attach marks to their bases, to match the 'ot' shaper.
Behdad Esfahbod03fb6dd2018-10-03 21:02:16 +0200449 * Adapted from a very old version of hb-ot-shape:hb_form_clusters().
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400450 * Note that this only makes us be closer to the 'ot' shaper,
451 * but by no means the same. For example, if there's
452 * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will
453 * continue pointing to B2 even though B2 was merged into B1's
454 * cluster... */
Behdad Esfahbod62c27112016-02-22 15:07:20 +0900455 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400456 {
457 hb_unicode_funcs_t *unicode = buffer->unicode;
458 unsigned int count = buffer->len;
459 hb_glyph_info_t *info = buffer->info;
460 for (unsigned int i = 1; i < count; i++)
461 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint)))
462 buffer->merge_clusters (i - 1, i + 1);
463 }
464
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700465 hb_vector_t<feature_record_t> feature_records;
466 hb_vector_t<range_record_t> range_records;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400467
Behdad Esfahbod36136962013-08-12 00:33:28 -0400468 /*
469 * Set up features.
470 * (copied + modified from code from hb-uniscribe.cc)
471 */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400472 if (num_features)
473 {
474 /* Sort features by start/end events. */
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700475 hb_vector_t<feature_event_t> feature_events;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400476 for (unsigned int i = 0; i < num_features; i++)
477 {
Behdad Esfahbodcb057742018-10-13 17:03:32 -0400478 const hb_aat_feature_mapping_t * mapping = hb_aat_layout_find_feature_mapping (features[i].tag);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400479 if (!mapping)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430480 continue;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400481
482 active_feature_t feature;
483 feature.rec.feature = mapping->aatFeatureType;
484 feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable;
485 feature.order = i;
486
487 feature_event_t *event;
488
489 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400490 event->index = features[i].start;
491 event->start = true;
492 event->feature = feature;
493
494 event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400495 event->index = features[i].end;
496 event->start = false;
497 event->feature = feature;
498 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400499 feature_events.qsort ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400500 /* Add a strategic final event. */
501 {
502 active_feature_t feature;
503 feature.rec.feature = HB_TAG_NONE;
504 feature.rec.setting = 0;
505 feature.order = num_features + 1;
506
507 feature_event_t *event = feature_events.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400508 event->index = 0; /* This value does magic. */
509 event->start = false;
510 event->feature = feature;
511 }
512
513 /* Scan events and save features for each range. */
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700514 hb_vector_t<active_feature_t> active_features;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400515 unsigned int last_index = 0;
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500516 for (unsigned int i = 0; i < feature_events.length; i++)
Behdad Esfahbod36136962013-08-12 00:33:28 -0400517 {
518 feature_event_t *event = &feature_events[i];
519
520 if (event->index != last_index)
521 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430522 /* Save a snapshot of active features and the range. */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400523 range_record_t *range = range_records.push ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400524
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500525 if (active_features.length)
Behdad Esfahbod36136962013-08-12 00:33:28 -0400526 {
527 CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
528
529 /* TODO sort and resolve conflicting features? */
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400530 /* active_features.qsort (); */
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500531 for (unsigned int j = 0; j < active_features.length; j++)
Behdad Esfahbod36136962013-08-12 00:33:28 -0400532 {
Cosimo Lupo9813be32017-07-14 17:11:46 +0100533 CFStringRef keys[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400534 kCTFontFeatureTypeIdentifierKey,
535 kCTFontFeatureSelectorIdentifierKey
536 };
Cosimo Lupo9813be32017-07-14 17:11:46 +0100537 CFNumberRef values[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400538 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature),
539 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting)
540 };
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200541 static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), "");
Behdad Esfahbod36136962013-08-12 00:33:28 -0400542 CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault,
543 (const void **) keys,
544 (const void **) values,
Cosimo Lupo9813be32017-07-14 17:11:46 +0100545 ARRAY_LENGTH (keys),
Behdad Esfahbod36136962013-08-12 00:33:28 -0400546 &kCFTypeDictionaryKeyCallBacks,
547 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100548 for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++)
549 CFRelease (values[i]);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400550
551 CFArrayAppendValue (features_array, dict);
552 CFRelease (dict);
553
554 }
555
556 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
557 (const void **) &kCTFontFeatureSettingsAttribute,
558 (const void **) &features_array,
559 1,
560 &kCFTypeDictionaryKeyCallBacks,
561 &kCFTypeDictionaryValueCallBacks);
562 CFRelease (features_array);
563
564 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
565 CFRelease (attributes);
566
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200567 range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400568 CFRelease (font_desc);
569 }
570 else
571 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200572 range->font = nullptr;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400573 }
574
575 range->index_first = last_index;
576 range->index_last = event->index - 1;
577
578 last_index = event->index;
579 }
580
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700581 if (event->start)
582 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430583 active_features.push (event->feature);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400584 } else {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430585 active_feature_t *feature = active_features.find (&event->feature);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400586 if (feature)
Behdad Esfahbod2fb3a832019-05-10 18:40:29 -0700587 active_features.remove (feature - active_features.arrayZ);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400588 }
589 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400590 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400591
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400592 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500593 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500594
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400595#define ALLOCATE_ARRAY(Type, name, len, on_no_room) \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500596 Type *name = (Type *) scratch; \
Behdad Esfahbod68e12e62019-05-13 17:28:59 -0700597 do { \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500598 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400599 if (unlikely (_consumed > scratch_size)) \
600 { \
601 on_no_room; \
602 assert (0); \
603 } \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500604 scratch += _consumed; \
605 scratch_size -= _consumed; \
Behdad Esfahbod68e12e62019-05-13 17:28:59 -0700606 } while (0)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400607
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400608 ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400609 unsigned int chars_len = 0;
610 for (unsigned int i = 0; i < buffer->len; i++) {
611 hb_codepoint_t c = buffer->info[i].codepoint;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400612 if (likely (c <= 0xFFFFu))
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400613 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400614 else if (unlikely (c > 0x10FFFFu))
615 pchars[chars_len++] = 0xFFFDu;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400616 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400617 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700618 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400619 }
620 }
621
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400622 ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/);
Behdad Esfahbod9b3c60c2014-08-11 13:25:43 -0400623 chars_len = 0;
624 for (unsigned int i = 0; i < buffer->len; i++)
625 {
626 hb_codepoint_t c = buffer->info[i].codepoint;
627 unsigned int cluster = buffer->info[i].cluster;
628 log_clusters[chars_len++] = cluster;
629 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
630 log_clusters[chars_len++] = cluster; /* Surrogates. */
631 }
632
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400633#define FAIL(...) \
634 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200635 DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400636 ret = false; \
637 goto fail; \
Behdad Esfahbod10bac212019-06-18 15:15:06 -0700638 } HB_STMT_END
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400639
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400640 bool ret = true;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200641 CFStringRef string_ref = nullptr;
642 CTLineRef line = nullptr;
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400643
Bruce Mitchener8d1e4792018-10-18 22:18:42 +0700644 if (false)
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400645 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400646resize_and_retry:
Behdad Esfahbod1b550772014-08-11 20:45:12 -0400647 DEBUG_MSG (CORETEXT, buffer, "Buffer resize");
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400648 /* string_ref uses the scratch-buffer for backing store, and line references
649 * string_ref (via attr_string). We must release those before resizing buffer. */
650 assert (string_ref);
651 assert (line);
652 CFRelease (string_ref);
653 CFRelease (line);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200654 string_ref = nullptr;
655 line = nullptr;
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400656
657 /* Get previous start-of-scratch-area, that we use later for readjusting
658 * our existing scratch arrays. */
659 unsigned int old_scratch_used;
660 hb_buffer_t::scratch_buffer_t *old_scratch;
661 old_scratch = buffer->get_scratch_buffer (&old_scratch_used);
662 old_scratch_used = scratch - old_scratch;
663
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400664 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbodb9993d82014-08-10 17:40:24 -0400665 FAIL ("Buffer resize failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400666
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400667 /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the
668 * cleanest way to do without completely restructuring the rest of this shaper. */
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500669 scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400670 pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch)));
671 log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch)));
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400672 scratch += old_scratch_used;
673 scratch_size -= old_scratch_used;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400674 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400675 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200676 string_ref = CFStringCreateWithCharactersNoCopy (nullptr,
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400677 pchars, chars_len,
678 kCFAllocatorNull);
679 if (unlikely (!string_ref))
680 FAIL ("CFStringCreateWithCharactersNoCopy failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400681
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400682 /* Create an attributed string, populate it, and create a line from it, then release attributed string. */
683 {
Behdad Esfahbodfd1a6aa2014-08-11 20:01:37 -0400684 CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault,
685 chars_len);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400686 if (unlikely (!attr_string))
687 FAIL ("CFAttributedStringCreateMutable failed");
688 CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref);
Behdad Esfahbod3eb6a4d2014-08-12 19:10:33 -0400689 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
690 {
691 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
692 kCTVerticalFormsAttributeName, kCFBooleanTrue);
693 }
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400694
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400695 if (buffer->props.language)
696 {
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400697/* What's the iOS equivalent of this check?
698 * The symbols was introduced in iOS 7.0.
699 * At any rate, our fallback is safe and works fine. */
Kenfdfa3d22019-02-10 23:46:05 -0800700#if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1090
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400701# define kCTLanguageAttributeName CFSTR ("NSLanguage")
702#endif
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430703 CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault,
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400704 hb_language_to_string (buffer->props.language),
705 kCFStringEncodingUTF8,
706 kCFAllocatorNull);
707 if (unlikely (!lang))
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430708 {
Bruce Mitchener0c660432018-01-31 20:24:27 +0700709 CFRelease (attr_string);
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400710 FAIL ("CFStringCreateWithCStringNoCopy failed");
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430711 }
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400712 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
713 kCTLanguageAttributeName, lang);
714 CFRelease (lang);
715 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400716 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200717 kCTFontAttributeName, ct_font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400718
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500719 if (num_features && range_records.length)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400720 {
721 unsigned int start = 0;
722 range_record_t *last_range = &range_records[0];
723 for (unsigned int k = 0; k < chars_len; k++)
724 {
725 range_record_t *range = last_range;
726 while (log_clusters[k] < range->index_first)
727 range--;
728 while (log_clusters[k] > range->index_last)
729 range++;
730 if (range != last_range)
731 {
732 if (last_range->font)
733 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start),
734 kCTFontAttributeName, last_range->font);
735
736 start = k;
737 }
738
739 last_range = range;
740 }
741 if (start != chars_len && last_range->font)
742 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start),
743 kCTFontAttributeName, last_range->font);
744 }
Cosimo Lupo9813be32017-07-14 17:11:46 +0100745 /* Enable/disable kern if requested.
746 *
747 * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText.
748 */
749 if (num_features)
750 {
751 unsigned int zeroint = 0;
752 CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint);
753 for (unsigned int i = 0; i < num_features; i++)
754 {
755 const hb_feature_t &feature = features[i];
756 if (feature.tag == HB_TAG('k','e','r','n') &&
757 feature.start < chars_len && feature.start < feature.end)
758 {
759 CFRange feature_range = CFRangeMake (feature.start,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430760 hb_min (feature.end, chars_len) - feature.start);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100761 if (feature.value)
762 CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName);
763 else
764 CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero);
765 }
766 }
767 CFRelease (zero);
768 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400769
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400770 int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
771 CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level);
Kenfdfa3d22019-02-10 23:46:05 -0800772#if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060
Ryan Schmidt58e569e2018-04-05 17:03:36 -0500773 extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel;
774#endif
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400775 CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault,
776 (const void **) &kCTTypesetterOptionForcedEmbeddingLevel,
777 (const void **) &level_number,
778 1,
779 &kCFTypeDictionaryKeyCallBacks,
780 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100781 CFRelease (level_number);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400782 if (unlikely (!options))
Bruce Mitchener0c660432018-01-31 20:24:27 +0700783 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430784 CFRelease (attr_string);
785 FAIL ("CFDictionaryCreate failed");
Bruce Mitchener0c660432018-01-31 20:24:27 +0700786 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400787
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400788 CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
789 CFRelease (options);
790 CFRelease (attr_string);
791 if (unlikely (!typesetter))
792 FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed");
793
794 line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0));
795 CFRelease (typesetter);
796 if (unlikely (!line))
797 FAIL ("CTTypesetterCreateLine failed");
798 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400799
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400800 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line);
801 unsigned int num_runs = CFArrayGetCount (glyph_runs);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200802 DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400803
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400804 buffer->len = 0;
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400805 uint32_t status_and = ~0, status_or = 0;
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800806 double advances_so_far = 0;
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700807 /* For right-to-left runs, CoreText returns the glyphs positioned such that
808 * any trailing whitespace is to the left of (0,0). Adjust coordinate system
809 * to fix for that. Test with any RTL string with trailing spaces.
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430810 * https://crbug.com/469028
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700811 */
812 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
813 {
814 advances_so_far -= CTLineGetTrailingWhitespaceWidth (line);
815 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
816 advances_so_far = -advances_so_far;
817 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400818
819 const CFRange range_all = CFRangeMake (0, 0);
820
821 for (unsigned int i = 0; i < num_runs; i++)
822 {
823 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i));
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400824 CTRunStatus run_status = CTRunGetStatus (run);
825 status_or |= run_status;
826 status_and &= run_status;
827 DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200828 double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr);
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800829 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
830 run_advance = -run_advance;
831 DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400832
833 /* CoreText does automatic font fallback (AKA "cascading") for characters
834 * not supported by the requested font, and provides no way to turn it off,
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400835 * so we must detect if the returned run uses a font other than the requested
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400836 * one and fill in the buffer with .notdef glyphs instead of random glyph
837 * indices from a different font.
838 */
839 CFDictionaryRef attributes = CTRunGetAttributes (run);
840 CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200841 if (!CFEqual (run_ct_font, ct_font))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400842 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400843 /* The run doesn't use our main font instance. We have to figure out
844 * whether font fallback happened, or this is just CoreText giving us
845 * another CTFont using the same underlying CGFont. CoreText seems
846 * to do that in a variety of situations, one of which being vertical
847 * text, but also perhaps for caching reasons.
848 *
849 * First, see if it uses any of our subfonts created to set font features...
850 *
851 * Next, compare the CGFont to the one we used to create our fonts.
852 * Even this doesn't work all the time.
853 *
854 * Finally, we compare PS names, which I don't think are unique...
855 *
856 * Looks like if we really want to be sure here we have to modify the
857 * font to change the name table, similar to what we do in the uniscribe
858 * backend.
859 *
860 * However, even that wouldn't work if we were passed in the CGFont to
Behdad Esfahbod59089622016-04-04 14:54:32 -0700861 * construct a hb_face to begin with.
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400862 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430863 * See: https://github.com/harfbuzz/harfbuzz/pull/36
Behdad Esfahbod59089622016-04-04 14:54:32 -0700864 *
865 * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400866 */
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400867 bool matched = false;
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500868 for (unsigned int i = 0; i < range_records.length; i++)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400869 if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font))
870 {
871 matched = true;
872 break;
873 }
874 if (!matched)
875 {
Bruce Mitchenerdae20fb2018-01-31 20:16:08 +0700876 CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400877 if (run_cg_font)
878 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200879 matched = CFEqual (run_cg_font, cg_font);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400880 CFRelease (run_cg_font);
881 }
882 }
883 if (!matched)
884 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200885 CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400886 CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey);
887 CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0);
888 CFRelease (run_ps_name);
889 CFRelease (font_ps_name);
890 if (result == kCFCompareEqualTo)
891 matched = true;
892 }
893 if (!matched)
894 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400895 CFRange range = CTRunGetStringRange (run);
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430896 DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld",
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400897 range.location, range.location + range.length);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400898 if (!buffer->ensure_inplace (buffer->len + range.length))
899 goto resize_and_retry;
900 hb_glyph_info_t *info = buffer->info + buffer->len;
901
Behdad Esfahbodb0b38bb2015-01-21 19:19:33 -0800902 hb_codepoint_t notdef = 0;
903 hb_direction_t dir = buffer->props.direction;
904 hb_position_t x_advance, y_advance, x_offset, y_offset;
905 hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance);
906 hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset);
907 hb_position_t advance = x_advance + y_advance;
908 x_offset = -x_offset;
909 y_offset = -y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400910
Behdad Esfahbod08acfe02014-08-12 18:57:08 -0400911 unsigned int old_len = buffer->len;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400912 for (CFIndex j = range.location; j < range.location + range.length; j++)
913 {
914 UniChar ch = CFStringGetCharacterAtIndex (string_ref, j);
915 if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j)
916 {
917 ch = CFStringGetCharacterAtIndex (string_ref, j - 1);
918 if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu))
919 /* This is the second of a surrogate pair. Don't need .notdef
920 * for this one. */
921 continue;
922 }
Behdad Esfahbod982d94e2015-01-28 10:51:33 -0800923 if (buffer->unicode->is_default_ignorable (ch))
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430924 continue;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400925
926 info->codepoint = notdef;
Behdad Esfahbod3c41ccb2014-08-11 15:11:59 -0400927 info->cluster = log_clusters[j];
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400928
929 info->mask = advance;
Behdad Esfahboded6962c2015-08-20 15:39:53 +0100930 info->var1.i32 = x_offset;
931 info->var2.i32 = y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400932
933 info++;
934 buffer->len++;
935 }
Behdad Esfahbod08acfe02014-08-12 18:57:08 -0400936 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
937 buffer->reverse_range (old_len, buffer->len);
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800938 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400939 continue;
940 }
941 }
942
943 unsigned int num_glyphs = CTRunGetGlyphCount (run);
944 if (num_glyphs == 0)
945 continue;
946
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400947 if (!buffer->ensure_inplace (buffer->len + num_glyphs))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400948 goto resize_and_retry;
949
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400950 hb_glyph_info_t *run_info = buffer->info + buffer->len;
951
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400952 /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always
953 * succeed, and so copying data to our own buffer will be rare. Reports
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200954 * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400955 * frequently. At any rate, we can test that codepath by setting USE_PTR
956 * to false. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -0400957
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400958#define USE_PTR true
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -0400959
960#define SCRATCH_SAVE() \
961 unsigned int scratch_size_saved = scratch_size; \
962 hb_buffer_t::scratch_buffer_t *scratch_saved = scratch
963
964#define SCRATCH_RESTORE() \
965 scratch_size = scratch_size_saved; \
Misty De Meoe7108882019-06-18 15:20:38 -0700966 scratch = scratch_saved
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -0400967
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800968 { /* Setup glyphs */
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430969 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200970 const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400971 if (!glyphs) {
972 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry);
973 CTRunGetGlyphs (run, range_all, glyph_buf);
974 glyphs = glyph_buf;
975 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200976 const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400977 if (!string_indices) {
978 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry);
979 CTRunGetStringIndices (run, range_all, index_buf);
980 string_indices = index_buf;
981 }
982 hb_glyph_info_t *info = run_info;
983 for (unsigned int j = 0; j < num_glyphs; j++)
984 {
985 info->codepoint = glyphs[j];
986 info->cluster = log_clusters[string_indices[j]];
987 info++;
988 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -0400989 SCRATCH_RESTORE();
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400990 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400991 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430992 /* Setup positions.
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800993 * Note that CoreText does not return advances for glyphs. As such,
994 * for all but last glyph, we use the delta position to next glyph as
995 * advance (in the advance direction only), and for last glyph we set
996 * whatever is needed to make the whole run's advance add up. */
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430997 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200998 const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400999 if (!positions) {
1000 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry);
1001 CTRunGetPositions (run, range_all, position_buf);
1002 positions = position_buf;
1003 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001004 hb_glyph_info_t *info = run_info;
1005 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1006 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001007 hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001008 for (unsigned int j = 0; j < num_glyphs; j++)
1009 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001010 double advance;
1011 if (likely (j + 1 < num_glyphs))
1012 advance = positions[j + 1].x - positions[j].x;
1013 else /* last glyph */
1014 advance = run_advance - (positions[j].x - positions[0].x);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001015 info->mask = advance * x_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001016 info->var1.i32 = x_offset;
1017 info->var2.i32 = positions[j].y * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001018 info++;
1019 }
1020 }
1021 else
1022 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001023 hb_position_t y_offset = (positions[0].y - advances_so_far) * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001024 for (unsigned int j = 0; j < num_glyphs; j++)
1025 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001026 double advance;
1027 if (likely (j + 1 < num_glyphs))
1028 advance = positions[j + 1].y - positions[j].y;
1029 else /* last glyph */
1030 advance = run_advance - (positions[j].y - positions[0].y);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001031 info->mask = advance * y_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001032 info->var1.i32 = positions[j].x * x_mult;
1033 info->var2.i32 = y_offset;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001034 info++;
1035 }
1036 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001037 SCRATCH_RESTORE();
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001038 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001039 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001040#undef SCRATCH_RESTORE
1041#undef SCRATCH_SAVE
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001042#undef USE_PTR
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001043#undef ALLOCATE_ARRAY
1044
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001045 buffer->len += num_glyphs;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001046 }
1047
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001048 /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
Bruce Mitchener90218fa2018-01-31 20:44:45 +07001049 * or if it does, it doesn't respect it. So we get runs with wrong
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001050 * directions. As such, disable the assert... It wouldn't crash, but
1051 * cursoring will be off...
1052 *
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301053 * https://crbug.com/419769
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001054 */
Bruce Mitchener8d1e4792018-10-18 22:18:42 +07001055 if (false)
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001056 {
1057 /* Make sure all runs had the expected direction. */
Behdad Esfahbodc5509be2019-04-08 14:50:58 -04001058 HB_UNUSED bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001059 assert (bool (status_and & kCTRunStatusRightToLeft) == backward);
1060 assert (bool (status_or & kCTRunStatusRightToLeft) == backward);
1061 }
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001062
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001063 buffer->clear_positions ();
1064
1065 unsigned int count = buffer->len;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001066 hb_glyph_info_t *info = buffer->info;
1067 hb_glyph_position_t *pos = buffer->pos;
1068 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1069 for (unsigned int i = 0; i < count; i++)
1070 {
1071 pos->x_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001072 pos->x_offset = info->var1.i32;
1073 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001074
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001075 info++, pos++;
1076 }
1077 else
1078 for (unsigned int i = 0; i < count; i++)
1079 {
1080 pos->y_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001081 pos->x_offset = info->var1.i32;
1082 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001083
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001084 info++, pos++;
1085 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001086
1087 /* Fix up clusters so that we never return out-of-order indices;
1088 * if core text has reordered glyphs, we'll merge them to the
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001089 * beginning of the reordered cluster. CoreText is nice enough
1090 * to tell us whenever it has produced nonmonotonic results...
1091 * Note that we assume the input clusters were nonmonotonic to
1092 * begin with.
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001093 *
1094 * This does *not* mean we'll form the same clusters as Uniscribe
1095 * or the native OT backend, only that the cluster indices will be
1096 * monotonic in the output buffer. */
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001097 if (count > 1 && (status_or & kCTRunStatusNonMonotonic))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001098 {
1099 hb_glyph_info_t *info = buffer->info;
1100 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
1101 {
1102 unsigned int cluster = info[count - 1].cluster;
1103 for (unsigned int i = count - 1; i > 0; i--)
1104 {
Behdad Esfahbod41248cc2019-05-07 20:54:31 -07001105 cluster = hb_min (cluster, info[i - 1].cluster);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001106 info[i - 1].cluster = cluster;
1107 }
1108 }
1109 else
1110 {
1111 unsigned int cluster = info[0].cluster;
1112 for (unsigned int i = 1; i < count; i++)
1113 {
Behdad Esfahbod41248cc2019-05-07 20:54:31 -07001114 cluster = hb_min (cluster, info[i].cluster);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001115 info[i].cluster = cluster;
1116 }
1117 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001118 }
1119 }
1120
Behdad Esfahbode4da3802017-11-10 17:14:27 -08001121 buffer->unsafe_to_break_all ();
1122
Behdad Esfahbod4acce772014-08-11 17:46:50 -04001123#undef FAIL
1124
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001125fail:
1126 if (string_ref)
1127 CFRelease (string_ref);
1128 if (line)
1129 CFRelease (line);
1130
Behdad Esfahbod474a1202018-12-21 18:46:51 -05001131 for (unsigned int i = 0; i < range_records.length; i++)
Behdad Esfahbod25f4fb92014-08-10 19:05:25 -04001132 if (range_records[i].font)
1133 CFRelease (range_records[i].font);
1134
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001135 return ret;
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001136}
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001137
1138
Behdad Esfahbodaa3450c2019-06-17 22:41:49 -07001139#endif