blob: e27c2bade58e9be5397ba5b59deff8a174087145 [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 Esfahbod301168d2012-07-30 17:48:04 -040030#include "hb-shaper-impl-private.hh"
Jonathan Kewaa6d8492012-07-24 15:52:32 -040031
Jonathan Kewaa6d8492012-07-24 15:52:32 -040032#include "hb-coretext.h"
Behdad Esfahbod6a2cbc62017-10-12 10:46:09 +020033#include <math.h>
Jonathan Kewaa6d8492012-07-24 15:52:32 -040034
Jonathan Kewaa6d8492012-07-24 15:52:32 -040035
36#ifndef HB_DEBUG_CORETEXT
37#define HB_DEBUG_CORETEXT (HB_DEBUG+0)
38#endif
39
Behdad Esfahbod06c14222017-10-11 15:29:53 +020040/* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020041#define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020042
43static CGFloat
44coretext_font_size (float ptem)
45{
Behdad Esfahbodc2cf68d2017-10-13 10:30:19 +020046 /* CoreText points are CSS pixels (96 per inch),
47 * NOT typographic points (72 per inch).
48 *
49 * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
50 */
51 ptem *= 96.f / 72.f;
52 return ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : ptem;
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020053}
Jonathan Kewaa6d8492012-07-24 15:52:32 -040054
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070055static void
56release_table_data (void *user_data)
57{
58 CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data);
59 CFRelease(cf_data);
60}
61
62static hb_blob_t *
63reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
64{
65 CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data);
66 CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag);
67 if (unlikely (!cf_data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020068 return nullptr;
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070069
70 const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data));
71 const size_t length = CFDataGetLength (cf_data);
72 if (!data || !length)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +020073 return nullptr;
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 -070086hb_face_t *
87hb_coretext_face_create (CGFontRef cg_font)
88{
Behdad Esfahbode1b6d922017-10-11 15:51:31 +020089 return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
Behdad Esfahboda9e25e92014-03-14 19:55:46 -070090}
91
Behdad Esfahbod466b3e52017-02-03 16:43:25 -080092HB_SHAPER_DATA_ENSURE_DEFINE(coretext, face)
Dominik Röttschesa5ebe1d2017-10-11 13:32:38 +020093HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(coretext, font,
Behdad Esfahbod95883fc2017-10-13 10:21:07 +020094 fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size (font->ptem)) <= .5
95)
Jonathan Kewaa6d8492012-07-24 15:52:32 -040096
Behdad Esfahbod301168d2012-07-30 17:48:04 -040097/*
98 * shaper face data
99 */
100
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900101static CTFontDescriptorRef
102get_last_resort_font_desc (void)
103{
104 // TODO Handle allocation failures?
105 CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0);
106 CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault,
107 (const void **) &last_resort,
108 1,
109 &kCFTypeArrayCallBacks);
110 CFRelease (last_resort);
111 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
112 (const void **) &kCTFontCascadeListAttribute,
113 (const void **) &cascade_list,
114 1,
115 &kCFTypeDictionaryKeyCallBacks,
116 &kCFTypeDictionaryValueCallBacks);
117 CFRelease (cascade_list);
118
119 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
120 CFRelease (attributes);
121 return font_desc;
122}
123
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900124static void
125release_data (void *info, const void *data, size_t size)
126{
127 assert (hb_blob_get_length ((hb_blob_t *) info) == size &&
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200128 hb_blob_get_data ((hb_blob_t *) info, nullptr) == data);
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900129
130 hb_blob_destroy ((hb_blob_t *) info);
131}
132
133static CGFontRef
134create_cg_font (hb_face_t *face)
135{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200136 CGFontRef cg_font = nullptr;
Behdad Esfahbode1b6d922017-10-11 15:51:31 +0200137 if (face->destroy == _hb_cg_font_release)
Behdad Esfahbodba3d49d2016-02-22 15:50:12 +0900138 {
139 cg_font = CGFontRetain ((CGFontRef) face->user_data);
140 }
141 else
142 {
143 hb_blob_t *blob = hb_face_reference_blob (face);
144 unsigned int blob_length;
145 const char *blob_data = hb_blob_get_data (blob, &blob_length);
146 if (unlikely (!blob_length))
147 DEBUG_MSG (CORETEXT, face, "Face has empty blob");
148
149 CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data);
150 if (likely (provider))
151 {
152 cg_font = CGFontCreateWithDataProvider (provider);
153 if (unlikely (!cg_font))
154 DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed");
155 CGDataProviderRelease (provider);
156 }
157 }
158 return cg_font;
159}
160
Behdad Esfahbode5611222016-02-22 15:28:37 +0900161static CTFontRef
162create_ct_font (CGFontRef cg_font, CGFloat font_size)
163{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200164 CTFontRef ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200165
166 /* CoreText does not enable trak table usage / tracking when creating a CTFont
167 * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems
168 * to be through the CTFontCreateUIFontForLanguage call. */
169 CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font);
170 if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) ||
171 CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay")))
172 {
173 CTFontUIFontType font_type = kCTFontUIFontSystem;
174 if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold")))
175 font_type = kCTFontUIFontEmphasizedSystem;
176
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200177 ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200178 CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font);
179 if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo)
180 {
181 CFRelease(ct_font);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200182 ct_font = nullptr;
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200183 }
184 CFRelease (ct_result_name);
185 }
186 CFRelease (cg_postscript_name);
187
188 if (!ct_font)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200189 ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr);
Dominik Röttschesdd4b3212017-10-12 11:49:37 +0200190
Behdad Esfahbode5611222016-02-22 15:28:37 +0900191 if (unlikely (!ct_font)) {
192 DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200193 return nullptr;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900194 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700195
196 /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter
197 * bug indicate that the cascade list reconfiguration occasionally causes
198 * crashes in CoreText on OS X 10.9, thus let's skip this step on older
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300199 * operating system versions. Except for the emoji font, where _not_
200 * reconfiguring the cascade list causes CoreText crashes. For details, see
201 * crbug.com/549610 */
Ebrahim Byagowifc4e6712016-09-09 23:28:28 +0430202 // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200203 if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) {
Dominik Röttschesb717cd72016-09-07 23:56:57 +0300204 CFStringRef fontName = CTFontCopyPostScriptName (ct_font);
205 bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo;
206 CFRelease (fontName);
207 if (!isEmojiFont)
208 return ct_font;
209 }
Behdad Esfahbod489acf62016-07-22 17:41:43 -0700210
Dominik Röttschesa0223272016-06-16 14:19:39 +0200211 CFURLRef original_url = (CFURLRef)CTFontCopyAttribute(ct_font, kCTFontURLAttribute);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900212
213 /* Create font copy with cascade list that has LastResort first; this speeds up CoreText
214 * font fallback which we don't need anyway. */
215 {
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900216 CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc ();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200217 CTFontRef new_ct_font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, last_resort_font_desc);
Behdad Esfahbod90194ef2016-02-22 15:42:53 +0900218 CFRelease (last_resort_font_desc);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900219 if (new_ct_font)
220 {
Behdad Esfahbodfc9de442016-06-30 09:46:52 -0700221 /* The CTFontCreateCopyWithAttributes call fails to stay on the same font
222 * when reconfiguring the cascade list and may switch to a different font
223 * when there are fonts that go by the same name, since the descriptor is
224 * just name and size.
225 *
226 * Avoid reconfiguring the cascade lists if the new font is outside the
227 * system locations that we cannot access from the sandboxed renderer
228 * process in Blink. This can be detected by the new file URL location
229 * that the newly found font points to. */
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430230 CFURLRef new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute);
231 // Keep reconfigured font if URL cannot be retrieved (seems to be the case
232 // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606
233 if (!original_url || !new_url || CFEqual (original_url, new_url)) {
Dominik Röttschesa0223272016-06-16 14:19:39 +0200234 CFRelease (ct_font);
235 ct_font = new_ct_font;
236 } else {
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430237 CFRelease (new_ct_font);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200238 DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed.");
239 }
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430240 if (new_url)
241 CFRelease (new_url);
Behdad Esfahbode5611222016-02-22 15:28:37 +0900242 }
243 else
244 DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed");
245 }
246
Ebrahim Byagowi87442122016-07-12 03:49:21 +0430247 if (original_url)
248 CFRelease (original_url);
Dominik Röttschesa0223272016-06-16 14:19:39 +0200249 return ct_font;
Behdad Esfahbode5611222016-02-22 15:28:37 +0900250}
251
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400252hb_coretext_shaper_face_data_t *
253_hb_coretext_shaper_face_data_create (hb_face_t *face)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400254{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200255 CGFontRef cg_font = create_cg_font (face);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400256
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200257 if (unlikely (!cg_font))
Behdad Esfahboda9e25e92014-03-14 19:55:46 -0700258 {
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900259 DEBUG_MSG (CORETEXT, face, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200260 return nullptr;
Behdad Esfahbod15063b12016-02-22 15:56:29 +0900261 }
262
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200263 return (hb_coretext_shaper_face_data_t *) cg_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400264}
265
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400266void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200267_hb_coretext_shaper_face_data_destroy (hb_coretext_shaper_face_data_t *data)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400268{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200269 CFRelease ((CGFontRef) data);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400270}
271
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430272/*
273 * Since: 0.9.10
274 */
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500275CGFontRef
Behdad Esfahbode923e642012-12-09 19:39:40 -0500276hb_coretext_face_get_cg_font (hb_face_t *face)
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500277{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200278 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200279 return (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500280}
281
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400282
283/*
284 * shaper font data
285 */
286
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400287hb_coretext_shaper_font_data_t *
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200288_hb_coretext_shaper_font_data_create (hb_font_t *font)
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400289{
Dominik Röttschesdb7a73c2017-10-11 13:24:39 +0200290 hb_face_t *face = font->face;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200291 if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200292 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200293
Behdad Esfahbod95883fc2017-10-13 10:21:07 +0200294 CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size (font->ptem));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200295
296 if (unlikely (!ct_font))
297 {
298 DEBUG_MSG (CORETEXT, font, "CGFont creation failed..");
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200299 return nullptr;
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200300 }
301
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200302 return (hb_coretext_shaper_font_data_t *) ct_font;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400303}
304
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400305void
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200306_hb_coretext_shaper_font_data_destroy (hb_coretext_shaper_font_data_t *data)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400307{
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200308 CFRelease ((CTFontRef) data);
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400309}
310
311
312/*
313 * shaper shape_plan data
314 */
315
316struct hb_coretext_shaper_shape_plan_data_t {};
317
318hb_coretext_shaper_shape_plan_data_t *
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400319_hb_coretext_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
320 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700321 unsigned int num_user_features HB_UNUSED,
322 const int *coords HB_UNUSED,
323 unsigned int num_coords HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400324{
325 return (hb_coretext_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
326}
327
328void
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400329_hb_coretext_shaper_shape_plan_data_destroy (hb_coretext_shaper_shape_plan_data_t *data HB_UNUSED)
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400330{
331}
332
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500333CTFontRef
334hb_coretext_font_get_ct_font (hb_font_t *font)
335{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200336 if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr;
Dominik Röttschesdb7a73c2017-10-11 13:24:39 +0200337 return (CTFontRef)HB_SHAPER_DATA_GET (font);
Behdad Esfahbod9a839582012-12-09 18:47:36 -0500338}
339
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400340
341/*
342 * shaper
343 */
344
Behdad Esfahbod36136962013-08-12 00:33:28 -0400345struct feature_record_t {
346 unsigned int feature;
347 unsigned int setting;
348};
349
350struct active_feature_t {
351 feature_record_t rec;
352 unsigned int order;
353
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600354 static int cmp (const void *pa, const void *pb) {
355 const active_feature_t *a = (const active_feature_t *) pa;
356 const active_feature_t *b = (const active_feature_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400357 return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 :
358 a->order < b->order ? -1 : a->order > b->order ? 1 :
359 a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 :
360 0;
361 }
362 bool operator== (const active_feature_t *f) {
363 return cmp (this, f) == 0;
364 }
365};
366
367struct feature_event_t {
368 unsigned int index;
369 bool start;
370 active_feature_t feature;
371
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600372 static int cmp (const void *pa, const void *pb) {
373 const feature_event_t *a = (const feature_event_t *) pa;
374 const feature_event_t *b = (const feature_event_t *) pb;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400375 return a->index < b->index ? -1 : a->index > b->index ? 1 :
376 a->start < b->start ? -1 : a->start > b->start ? 1 :
377 active_feature_t::cmp (&a->feature, &b->feature);
378 }
379};
380
381struct range_record_t {
382 CTFontRef font;
383 unsigned int index_first; /* == start */
384 unsigned int index_last; /* == end - 1 */
385};
386
387
388/* The following enum members are added in OS X 10.8. */
389#define kAltHalfWidthTextSelector 6
390#define kAltProportionalTextSelector 5
391#define kAlternateHorizKanaOffSelector 1
392#define kAlternateHorizKanaOnSelector 0
393#define kAlternateKanaType 34
394#define kAlternateVertKanaOffSelector 3
395#define kAlternateVertKanaOnSelector 2
396#define kCaseSensitiveLayoutOffSelector 1
397#define kCaseSensitiveLayoutOnSelector 0
398#define kCaseSensitiveLayoutType 33
399#define kCaseSensitiveSpacingOffSelector 3
400#define kCaseSensitiveSpacingOnSelector 2
401#define kContextualAlternatesOffSelector 1
402#define kContextualAlternatesOnSelector 0
403#define kContextualAlternatesType 36
404#define kContextualLigaturesOffSelector 19
405#define kContextualLigaturesOnSelector 18
406#define kContextualSwashAlternatesOffSelector 5
407#define kContextualSwashAlternatesOnSelector 4
408#define kDefaultLowerCaseSelector 0
409#define kDefaultUpperCaseSelector 0
410#define kHistoricalLigaturesOffSelector 21
411#define kHistoricalLigaturesOnSelector 20
412#define kHojoCharactersSelector 12
413#define kJIS2004CharactersSelector 11
414#define kLowerCasePetiteCapsSelector 2
415#define kLowerCaseSmallCapsSelector 1
416#define kLowerCaseType 37
417#define kMathematicalGreekOffSelector 11
418#define kMathematicalGreekOnSelector 10
419#define kNLCCharactersSelector 13
420#define kQuarterWidthTextSelector 4
421#define kScientificInferiorsSelector 4
422#define kStylisticAltEightOffSelector 17
423#define kStylisticAltEightOnSelector 16
424#define kStylisticAltEighteenOffSelector 37
425#define kStylisticAltEighteenOnSelector 36
426#define kStylisticAltElevenOffSelector 23
427#define kStylisticAltElevenOnSelector 22
428#define kStylisticAltFifteenOffSelector 31
429#define kStylisticAltFifteenOnSelector 30
430#define kStylisticAltFiveOffSelector 11
431#define kStylisticAltFiveOnSelector 10
432#define kStylisticAltFourOffSelector 9
433#define kStylisticAltFourOnSelector 8
434#define kStylisticAltFourteenOffSelector 29
435#define kStylisticAltFourteenOnSelector 28
436#define kStylisticAltNineOffSelector 19
437#define kStylisticAltNineOnSelector 18
438#define kStylisticAltNineteenOffSelector 39
439#define kStylisticAltNineteenOnSelector 38
440#define kStylisticAltOneOffSelector 3
441#define kStylisticAltOneOnSelector 2
442#define kStylisticAltSevenOffSelector 15
443#define kStylisticAltSevenOnSelector 14
444#define kStylisticAltSeventeenOffSelector 35
445#define kStylisticAltSeventeenOnSelector 34
446#define kStylisticAltSixOffSelector 13
447#define kStylisticAltSixOnSelector 12
448#define kStylisticAltSixteenOffSelector 33
449#define kStylisticAltSixteenOnSelector 32
450#define kStylisticAltTenOffSelector 21
451#define kStylisticAltTenOnSelector 20
452#define kStylisticAltThirteenOffSelector 27
453#define kStylisticAltThirteenOnSelector 26
454#define kStylisticAltThreeOffSelector 7
455#define kStylisticAltThreeOnSelector 6
456#define kStylisticAltTwelveOffSelector 25
457#define kStylisticAltTwelveOnSelector 24
458#define kStylisticAltTwentyOffSelector 41
459#define kStylisticAltTwentyOnSelector 40
460#define kStylisticAltTwoOffSelector 5
461#define kStylisticAltTwoOnSelector 4
462#define kStylisticAlternativesType 35
463#define kSwashAlternatesOffSelector 3
464#define kSwashAlternatesOnSelector 2
465#define kThirdWidthTextSelector 3
466#define kTraditionalNamesCharactersSelector 14
467#define kUpperCasePetiteCapsSelector 2
468#define kUpperCaseSmallCapsSelector 1
469#define kUpperCaseType 38
470
471/* Table data courtesy of Apple. */
Behdad Esfahbod522b1cc2014-08-14 13:29:30 -0400472static const struct feature_mapping_t {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400473 FourCharCode otFeatureTag;
474 uint16_t aatFeatureType;
475 uint16_t selectorToEnable;
476 uint16_t selectorToDisable;
477} feature_mappings[] = {
478 { 'c2pc', kUpperCaseType, kUpperCasePetiteCapsSelector, kDefaultUpperCaseSelector },
479 { 'c2sc', kUpperCaseType, kUpperCaseSmallCapsSelector, kDefaultUpperCaseSelector },
480 { 'calt', kContextualAlternatesType, kContextualAlternatesOnSelector, kContextualAlternatesOffSelector },
481 { 'case', kCaseSensitiveLayoutType, kCaseSensitiveLayoutOnSelector, kCaseSensitiveLayoutOffSelector },
482 { 'clig', kLigaturesType, kContextualLigaturesOnSelector, kContextualLigaturesOffSelector },
483 { 'cpsp', kCaseSensitiveLayoutType, kCaseSensitiveSpacingOnSelector, kCaseSensitiveSpacingOffSelector },
484 { 'cswh', kContextualAlternatesType, kContextualSwashAlternatesOnSelector, kContextualSwashAlternatesOffSelector },
485 { 'dlig', kLigaturesType, kRareLigaturesOnSelector, kRareLigaturesOffSelector },
486 { 'expt', kCharacterShapeType, kExpertCharactersSelector, 16 },
487 { 'frac', kFractionsType, kDiagonalFractionsSelector, kNoFractionsSelector },
488 { 'fwid', kTextSpacingType, kMonospacedTextSelector, 7 },
489 { 'halt', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
490 { 'hist', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
491 { 'hkna', kAlternateKanaType, kAlternateHorizKanaOnSelector, kAlternateHorizKanaOffSelector, },
492 { 'hlig', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector },
493 { 'hngl', kTransliterationType, kHanjaToHangulSelector, kNoTransliterationSelector },
494 { 'hojo', kCharacterShapeType, kHojoCharactersSelector, 16 },
495 { 'hwid', kTextSpacingType, kHalfWidthTextSelector, 7 },
496 { 'ital', kItalicCJKRomanType, kCJKItalicRomanOnSelector, kCJKItalicRomanOffSelector },
497 { 'jp04', kCharacterShapeType, kJIS2004CharactersSelector, 16 },
498 { 'jp78', kCharacterShapeType, kJIS1978CharactersSelector, 16 },
499 { 'jp83', kCharacterShapeType, kJIS1983CharactersSelector, 16 },
500 { 'jp90', kCharacterShapeType, kJIS1990CharactersSelector, 16 },
501 { 'liga', kLigaturesType, kCommonLigaturesOnSelector, kCommonLigaturesOffSelector },
502 { 'lnum', kNumberCaseType, kUpperCaseNumbersSelector, 2 },
503 { 'mgrk', kMathematicalExtrasType, kMathematicalGreekOnSelector, kMathematicalGreekOffSelector },
504 { 'nlck', kCharacterShapeType, kNLCCharactersSelector, 16 },
505 { 'onum', kNumberCaseType, kLowerCaseNumbersSelector, 2 },
506 { 'ordn', kVerticalPositionType, kOrdinalsSelector, kNormalPositionSelector },
507 { 'palt', kTextSpacingType, kAltProportionalTextSelector, 7 },
508 { 'pcap', kLowerCaseType, kLowerCasePetiteCapsSelector, kDefaultLowerCaseSelector },
509 { 'pkna', kTextSpacingType, kProportionalTextSelector, 7 },
510 { 'pnum', kNumberSpacingType, kProportionalNumbersSelector, 4 },
511 { 'pwid', kTextSpacingType, kProportionalTextSelector, 7 },
512 { 'qwid', kTextSpacingType, kQuarterWidthTextSelector, 7 },
513 { 'ruby', kRubyKanaType, kRubyKanaOnSelector, kRubyKanaOffSelector },
514 { 'sinf', kVerticalPositionType, kScientificInferiorsSelector, kNormalPositionSelector },
515 { 'smcp', kLowerCaseType, kLowerCaseSmallCapsSelector, kDefaultLowerCaseSelector },
516 { 'smpl', kCharacterShapeType, kSimplifiedCharactersSelector, 16 },
517 { 'ss01', kStylisticAlternativesType, kStylisticAltOneOnSelector, kStylisticAltOneOffSelector },
518 { 'ss02', kStylisticAlternativesType, kStylisticAltTwoOnSelector, kStylisticAltTwoOffSelector },
519 { 'ss03', kStylisticAlternativesType, kStylisticAltThreeOnSelector, kStylisticAltThreeOffSelector },
520 { 'ss04', kStylisticAlternativesType, kStylisticAltFourOnSelector, kStylisticAltFourOffSelector },
521 { 'ss05', kStylisticAlternativesType, kStylisticAltFiveOnSelector, kStylisticAltFiveOffSelector },
522 { 'ss06', kStylisticAlternativesType, kStylisticAltSixOnSelector, kStylisticAltSixOffSelector },
523 { 'ss07', kStylisticAlternativesType, kStylisticAltSevenOnSelector, kStylisticAltSevenOffSelector },
524 { 'ss08', kStylisticAlternativesType, kStylisticAltEightOnSelector, kStylisticAltEightOffSelector },
525 { 'ss09', kStylisticAlternativesType, kStylisticAltNineOnSelector, kStylisticAltNineOffSelector },
526 { 'ss10', kStylisticAlternativesType, kStylisticAltTenOnSelector, kStylisticAltTenOffSelector },
527 { 'ss11', kStylisticAlternativesType, kStylisticAltElevenOnSelector, kStylisticAltElevenOffSelector },
528 { 'ss12', kStylisticAlternativesType, kStylisticAltTwelveOnSelector, kStylisticAltTwelveOffSelector },
529 { 'ss13', kStylisticAlternativesType, kStylisticAltThirteenOnSelector, kStylisticAltThirteenOffSelector },
530 { 'ss14', kStylisticAlternativesType, kStylisticAltFourteenOnSelector, kStylisticAltFourteenOffSelector },
531 { 'ss15', kStylisticAlternativesType, kStylisticAltFifteenOnSelector, kStylisticAltFifteenOffSelector },
532 { 'ss16', kStylisticAlternativesType, kStylisticAltSixteenOnSelector, kStylisticAltSixteenOffSelector },
533 { 'ss17', kStylisticAlternativesType, kStylisticAltSeventeenOnSelector, kStylisticAltSeventeenOffSelector },
534 { 'ss18', kStylisticAlternativesType, kStylisticAltEighteenOnSelector, kStylisticAltEighteenOffSelector },
535 { 'ss19', kStylisticAlternativesType, kStylisticAltNineteenOnSelector, kStylisticAltNineteenOffSelector },
536 { 'ss20', kStylisticAlternativesType, kStylisticAltTwentyOnSelector, kStylisticAltTwentyOffSelector },
537 { 'subs', kVerticalPositionType, kInferiorsSelector, kNormalPositionSelector },
538 { 'sups', kVerticalPositionType, kSuperiorsSelector, kNormalPositionSelector },
539 { 'swsh', kContextualAlternatesType, kSwashAlternatesOnSelector, kSwashAlternatesOffSelector },
540 { 'titl', kStyleOptionsType, kTitlingCapsSelector, kNoStyleOptionsSelector },
541 { 'tnam', kCharacterShapeType, kTraditionalNamesCharactersSelector, 16 },
542 { 'tnum', kNumberSpacingType, kMonospacedNumbersSelector, 4 },
543 { 'trad', kCharacterShapeType, kTraditionalCharactersSelector, 16 },
544 { 'twid', kTextSpacingType, kThirdWidthTextSelector, 7 },
545 { 'unic', kLetterCaseType, 14, 15 },
546 { 'valt', kTextSpacingType, kAltProportionalTextSelector, 7 },
547 { 'vert', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
548 { 'vhal', kTextSpacingType, kAltHalfWidthTextSelector, 7 },
549 { 'vkna', kAlternateKanaType, kAlternateVertKanaOnSelector, kAlternateVertKanaOffSelector },
550 { 'vpal', kTextSpacingType, kAltProportionalTextSelector, 7 },
551 { 'vrt2', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector },
552 { 'zero', kTypographicExtrasType, kSlashedZeroOnSelector, kSlashedZeroOffSelector },
553};
554
555static int
556_hb_feature_mapping_cmp (const void *key_, const void *entry_)
557{
558 unsigned int key = * (unsigned int *) key_;
559 const feature_mapping_t * entry = (const feature_mapping_t *) entry_;
560 return key < entry->otFeatureTag ? -1 :
561 key > entry->otFeatureTag ? 1 :
562 0;
563}
564
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400565hb_bool_t
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400566_hb_coretext_shape (hb_shape_plan_t *shape_plan,
567 hb_font_t *font,
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400568 hb_buffer_t *buffer,
569 const hb_feature_t *features,
570 unsigned int num_features)
571{
Behdad Esfahbod301168d2012-07-30 17:48:04 -0400572 hb_face_t *face = font->face;
Behdad Esfahbodf3341302017-10-11 13:17:46 +0200573 CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
574 CTFontRef ct_font = (CTFontRef) HB_SHAPER_DATA_GET (font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400575
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200576 CGFloat ct_font_size = CTFontGetSize (ct_font);
Behdad Esfahbod061105e2016-02-22 14:59:39 +0900577 CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
578 CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
579
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400580 /* Attach marks to their bases, to match the 'ot' shaper.
581 * Adapted from hb-ot-shape:hb_form_clusters().
582 * Note that this only makes us be closer to the 'ot' shaper,
583 * but by no means the same. For example, if there's
584 * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will
585 * continue pointing to B2 even though B2 was merged into B1's
586 * cluster... */
Behdad Esfahbod62c27112016-02-22 15:07:20 +0900587 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Behdad Esfahbod624a2992014-08-11 15:29:18 -0400588 {
589 hb_unicode_funcs_t *unicode = buffer->unicode;
590 unsigned int count = buffer->len;
591 hb_glyph_info_t *info = buffer->info;
592 for (unsigned int i = 1; i < count; i++)
593 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint)))
594 buffer->merge_clusters (i - 1, i + 1);
595 }
596
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400597 hb_auto_array_t<feature_record_t> feature_records;
598 hb_auto_array_t<range_record_t> range_records;
599
Behdad Esfahbod36136962013-08-12 00:33:28 -0400600 /*
601 * Set up features.
602 * (copied + modified from code from hb-uniscribe.cc)
603 */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400604 if (num_features)
605 {
606 /* Sort features by start/end events. */
607 hb_auto_array_t<feature_event_t> feature_events;
608 for (unsigned int i = 0; i < num_features; i++)
609 {
610 const feature_mapping_t * mapping = (const feature_mapping_t *) bsearch (&features[i].tag,
611 feature_mappings,
612 ARRAY_LENGTH (feature_mappings),
613 sizeof (feature_mappings[0]),
614 _hb_feature_mapping_cmp);
615 if (!mapping)
616 continue;
617
618 active_feature_t feature;
619 feature.rec.feature = mapping->aatFeatureType;
620 feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable;
621 feature.order = i;
622
623 feature_event_t *event;
624
625 event = feature_events.push ();
626 if (unlikely (!event))
627 goto fail_features;
628 event->index = features[i].start;
629 event->start = true;
630 event->feature = feature;
631
632 event = feature_events.push ();
633 if (unlikely (!event))
634 goto fail_features;
635 event->index = features[i].end;
636 event->start = false;
637 event->feature = feature;
638 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400639 feature_events.qsort ();
Behdad Esfahbod36136962013-08-12 00:33:28 -0400640 /* Add a strategic final event. */
641 {
642 active_feature_t feature;
643 feature.rec.feature = HB_TAG_NONE;
644 feature.rec.setting = 0;
645 feature.order = num_features + 1;
646
647 feature_event_t *event = feature_events.push ();
648 if (unlikely (!event))
649 goto fail_features;
650 event->index = 0; /* This value does magic. */
651 event->start = false;
652 event->feature = feature;
653 }
654
655 /* Scan events and save features for each range. */
656 hb_auto_array_t<active_feature_t> active_features;
657 unsigned int last_index = 0;
658 for (unsigned int i = 0; i < feature_events.len; i++)
659 {
660 feature_event_t *event = &feature_events[i];
661
662 if (event->index != last_index)
663 {
664 /* Save a snapshot of active features and the range. */
665 range_record_t *range = range_records.push ();
666 if (unlikely (!range))
667 goto fail_features;
668
Behdad Esfahbod36136962013-08-12 00:33:28 -0400669 if (active_features.len)
670 {
671 CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
672
673 /* TODO sort and resolve conflicting features? */
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400674 /* active_features.qsort (); */
Behdad Esfahbod36136962013-08-12 00:33:28 -0400675 for (unsigned int j = 0; j < active_features.len; j++)
676 {
Cosimo Lupo9813be32017-07-14 17:11:46 +0100677 CFStringRef keys[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400678 kCTFontFeatureTypeIdentifierKey,
679 kCTFontFeatureSelectorIdentifierKey
680 };
Cosimo Lupo9813be32017-07-14 17:11:46 +0100681 CFNumberRef values[] = {
Behdad Esfahbod36136962013-08-12 00:33:28 -0400682 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature),
683 CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting)
684 };
Behdad Esfahbod80cc0a72017-10-17 11:14:48 -0700685 static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), "");
Behdad Esfahbod36136962013-08-12 00:33:28 -0400686 CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault,
687 (const void **) keys,
688 (const void **) values,
Cosimo Lupo9813be32017-07-14 17:11:46 +0100689 ARRAY_LENGTH (keys),
Behdad Esfahbod36136962013-08-12 00:33:28 -0400690 &kCFTypeDictionaryKeyCallBacks,
691 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100692 for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++)
693 CFRelease (values[i]);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400694
695 CFArrayAppendValue (features_array, dict);
696 CFRelease (dict);
697
698 }
699
700 CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault,
701 (const void **) &kCTFontFeatureSettingsAttribute,
702 (const void **) &features_array,
703 1,
704 &kCFTypeDictionaryKeyCallBacks,
705 &kCFTypeDictionaryValueCallBacks);
706 CFRelease (features_array);
707
708 CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes);
709 CFRelease (attributes);
710
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200711 range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc);
Behdad Esfahbod36136962013-08-12 00:33:28 -0400712 CFRelease (font_desc);
713 }
714 else
715 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200716 range->font = nullptr;
Behdad Esfahbod36136962013-08-12 00:33:28 -0400717 }
718
719 range->index_first = last_index;
720 range->index_last = event->index - 1;
721
722 last_index = event->index;
723 }
724
725 if (event->start) {
726 active_feature_t *feature = active_features.push ();
727 if (unlikely (!feature))
728 goto fail_features;
729 *feature = event->feature;
730 } else {
731 active_feature_t *feature = active_features.find (&event->feature);
732 if (feature)
733 active_features.remove (feature - active_features.array);
734 }
735 }
Behdad Esfahbod36136962013-08-12 00:33:28 -0400736 }
737 else
738 {
739 fail_features:
740 num_features = 0;
741 }
742
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400743 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500744 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500745
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400746#define ALLOCATE_ARRAY(Type, name, len, on_no_room) \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500747 Type *name = (Type *) scratch; \
748 { \
749 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400750 if (unlikely (_consumed > scratch_size)) \
751 { \
752 on_no_room; \
753 assert (0); \
754 } \
Behdad Esfahbod8fcadb92013-11-13 14:33:57 -0500755 scratch += _consumed; \
756 scratch_size -= _consumed; \
757 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400758
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400759 ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400760 unsigned int chars_len = 0;
761 for (unsigned int i = 0; i < buffer->len; i++) {
762 hb_codepoint_t c = buffer->info[i].codepoint;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400763 if (likely (c <= 0xFFFFu))
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400764 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400765 else if (unlikely (c > 0x10FFFFu))
766 pchars[chars_len++] = 0xFFFDu;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400767 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400768 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700769 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400770 }
771 }
772
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400773 ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/);
Behdad Esfahbod9b3c60c2014-08-11 13:25:43 -0400774 chars_len = 0;
775 for (unsigned int i = 0; i < buffer->len; i++)
776 {
777 hb_codepoint_t c = buffer->info[i].codepoint;
778 unsigned int cluster = buffer->info[i].cluster;
779 log_clusters[chars_len++] = cluster;
780 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
781 log_clusters[chars_len++] = cluster; /* Surrogates. */
782 }
783
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400784#define FAIL(...) \
785 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200786 DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400787 ret = false; \
788 goto fail; \
789 } HB_STMT_END;
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400790
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400791 bool ret = true;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200792 CFStringRef string_ref = nullptr;
793 CTLineRef line = nullptr;
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400794
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400795 if (0)
Behdad Esfahboda782a5e2013-08-07 21:08:54 -0400796 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400797resize_and_retry:
Behdad Esfahbod1b550772014-08-11 20:45:12 -0400798 DEBUG_MSG (CORETEXT, buffer, "Buffer resize");
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400799 /* string_ref uses the scratch-buffer for backing store, and line references
800 * string_ref (via attr_string). We must release those before resizing buffer. */
801 assert (string_ref);
802 assert (line);
803 CFRelease (string_ref);
804 CFRelease (line);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200805 string_ref = nullptr;
806 line = nullptr;
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400807
808 /* Get previous start-of-scratch-area, that we use later for readjusting
809 * our existing scratch arrays. */
810 unsigned int old_scratch_used;
811 hb_buffer_t::scratch_buffer_t *old_scratch;
812 old_scratch = buffer->get_scratch_buffer (&old_scratch_used);
813 old_scratch_used = scratch - old_scratch;
814
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400815 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbodb9993d82014-08-10 17:40:24 -0400816 FAIL ("Buffer resize failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400817
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400818 /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the
819 * cleanest way to do without completely restructuring the rest of this shaper. */
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500820 scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400821 pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch)));
822 log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch)));
Behdad Esfahbod81b8d972014-08-12 15:49:47 -0400823 scratch += old_scratch_used;
824 scratch_size -= old_scratch_used;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400825 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400826 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200827 string_ref = CFStringCreateWithCharactersNoCopy (nullptr,
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400828 pchars, chars_len,
829 kCFAllocatorNull);
830 if (unlikely (!string_ref))
831 FAIL ("CFStringCreateWithCharactersNoCopy failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400832
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400833 /* Create an attributed string, populate it, and create a line from it, then release attributed string. */
834 {
Behdad Esfahbodfd1a6aa2014-08-11 20:01:37 -0400835 CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault,
836 chars_len);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400837 if (unlikely (!attr_string))
838 FAIL ("CFAttributedStringCreateMutable failed");
839 CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref);
Behdad Esfahbod3eb6a4d2014-08-12 19:10:33 -0400840 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
841 {
842 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
843 kCTVerticalFormsAttributeName, kCFBooleanTrue);
844 }
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400845
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400846 if (buffer->props.language)
847 {
Behdad Esfahbod20076cc2014-08-12 19:26:35 -0400848/* What's the iOS equivalent of this check?
849 * The symbols was introduced in iOS 7.0.
850 * At any rate, our fallback is safe and works fine. */
851#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
852# define kCTLanguageAttributeName CFSTR ("NSLanguage")
853#endif
Behdad Esfahbod1b3011c2014-08-12 19:17:19 -0400854 CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault,
855 hb_language_to_string (buffer->props.language),
856 kCFStringEncodingUTF8,
857 kCFAllocatorNull);
858 if (unlikely (!lang))
859 FAIL ("CFStringCreateWithCStringNoCopy failed");
860 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
861 kCTLanguageAttributeName, lang);
862 CFRelease (lang);
863 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -0400864 CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200865 kCTFontAttributeName, ct_font);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400866
Cosimo Lupo9813be32017-07-14 17:11:46 +0100867 if (num_features && range_records.len)
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400868 {
869 unsigned int start = 0;
870 range_record_t *last_range = &range_records[0];
871 for (unsigned int k = 0; k < chars_len; k++)
872 {
873 range_record_t *range = last_range;
874 while (log_clusters[k] < range->index_first)
875 range--;
876 while (log_clusters[k] > range->index_last)
877 range++;
878 if (range != last_range)
879 {
880 if (last_range->font)
881 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start),
882 kCTFontAttributeName, last_range->font);
883
884 start = k;
885 }
886
887 last_range = range;
888 }
889 if (start != chars_len && last_range->font)
890 CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start),
891 kCTFontAttributeName, last_range->font);
892 }
Cosimo Lupo9813be32017-07-14 17:11:46 +0100893 /* Enable/disable kern if requested.
894 *
895 * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText.
896 */
897 if (num_features)
898 {
899 unsigned int zeroint = 0;
900 CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint);
901 for (unsigned int i = 0; i < num_features; i++)
902 {
903 const hb_feature_t &feature = features[i];
904 if (feature.tag == HB_TAG('k','e','r','n') &&
905 feature.start < chars_len && feature.start < feature.end)
906 {
907 CFRange feature_range = CFRangeMake (feature.start,
908 MIN (feature.end, chars_len) - feature.start);
909 if (feature.value)
910 CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName);
911 else
912 CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero);
913 }
914 }
915 CFRelease (zero);
916 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400917
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400918 int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
919 CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level);
920 CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault,
921 (const void **) &kCTTypesetterOptionForcedEmbeddingLevel,
922 (const void **) &level_number,
923 1,
924 &kCFTypeDictionaryKeyCallBacks,
925 &kCFTypeDictionaryValueCallBacks);
Cosimo Lupo9813be32017-07-14 17:11:46 +0100926 CFRelease (level_number);
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400927 if (unlikely (!options))
928 FAIL ("CFDictionaryCreate failed");
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400929
Behdad Esfahbod4acce772014-08-11 17:46:50 -0400930 CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
931 CFRelease (options);
932 CFRelease (attr_string);
933 if (unlikely (!typesetter))
934 FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed");
935
936 line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0));
937 CFRelease (typesetter);
938 if (unlikely (!line))
939 FAIL ("CTTypesetterCreateLine failed");
940 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400941
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400942 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line);
943 unsigned int num_runs = CFArrayGetCount (glyph_runs);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200944 DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs);
Jonathan Kewaa6d8492012-07-24 15:52:32 -0400945
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400946 buffer->len = 0;
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400947 uint32_t status_and = ~0, status_or = 0;
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800948 double advances_so_far = 0;
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700949 /* For right-to-left runs, CoreText returns the glyphs positioned such that
950 * any trailing whitespace is to the left of (0,0). Adjust coordinate system
951 * to fix for that. Test with any RTL string with trailing spaces.
Behdad Esfahbod39851ce2015-04-21 19:23:27 -0700952 * https://code.google.com/p/chromium/issues/detail?id=469028
Behdad Esfahbod24f17af2015-04-21 19:21:32 -0700953 */
954 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
955 {
956 advances_so_far -= CTLineGetTrailingWhitespaceWidth (line);
957 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
958 advances_so_far = -advances_so_far;
959 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400960
961 const CFRange range_all = CFRangeMake (0, 0);
962
963 for (unsigned int i = 0; i < num_runs; i++)
964 {
965 CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i));
Behdad Esfahbod10b11042014-08-11 20:02:45 -0400966 CTRunStatus run_status = CTRunGetStatus (run);
967 status_or |= run_status;
968 status_and &= run_status;
969 DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200970 double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr);
Behdad Esfahbod6917a042015-01-28 10:43:32 -0800971 if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction))
972 run_advance = -run_advance;
973 DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400974
975 /* CoreText does automatic font fallback (AKA "cascading") for characters
976 * not supported by the requested font, and provides no way to turn it off,
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400977 * so we must detect if the returned run uses a font other than the requested
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400978 * one and fill in the buffer with .notdef glyphs instead of random glyph
979 * indices from a different font.
980 */
981 CFDictionaryRef attributes = CTRunGetAttributes (run);
982 CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName));
Behdad Esfahboda8e466c2017-10-11 13:05:59 +0200983 if (!CFEqual (run_ct_font, ct_font))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -0400984 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -0400985 /* The run doesn't use our main font instance. We have to figure out
986 * whether font fallback happened, or this is just CoreText giving us
987 * another CTFont using the same underlying CGFont. CoreText seems
988 * to do that in a variety of situations, one of which being vertical
989 * text, but also perhaps for caching reasons.
990 *
991 * First, see if it uses any of our subfonts created to set font features...
992 *
993 * Next, compare the CGFont to the one we used to create our fonts.
994 * Even this doesn't work all the time.
995 *
996 * Finally, we compare PS names, which I don't think are unique...
997 *
998 * Looks like if we really want to be sure here we have to modify the
999 * font to change the name table, similar to what we do in the uniscribe
1000 * backend.
1001 *
1002 * However, even that wouldn't work if we were passed in the CGFont to
Behdad Esfahbod59089622016-04-04 14:54:32 -07001003 * construct a hb_face to begin with.
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001004 *
1005 * See: http://github.com/behdad/harfbuzz/pull/36
Behdad Esfahbod59089622016-04-04 14:54:32 -07001006 *
1007 * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001008 */
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001009 bool matched = false;
1010 for (unsigned int i = 0; i < range_records.len; i++)
1011 if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font))
1012 {
1013 matched = true;
1014 break;
1015 }
1016 if (!matched)
1017 {
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001018 CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, 0);
1019 if (run_cg_font)
1020 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001021 matched = CFEqual (run_cg_font, cg_font);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001022 CFRelease (run_cg_font);
1023 }
1024 }
1025 if (!matched)
1026 {
Behdad Esfahboda8e466c2017-10-11 13:05:59 +02001027 CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey);
Behdad Esfahbodfd0001d2014-08-12 10:32:41 -04001028 CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey);
1029 CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0);
1030 CFRelease (run_ps_name);
1031 CFRelease (font_ps_name);
1032 if (result == kCFCompareEqualTo)
1033 matched = true;
1034 }
1035 if (!matched)
1036 {
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001037 CFRange range = CTRunGetStringRange (run);
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001038 DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld",
1039 range.location, range.location + range.length);
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001040 if (!buffer->ensure_inplace (buffer->len + range.length))
1041 goto resize_and_retry;
1042 hb_glyph_info_t *info = buffer->info + buffer->len;
1043
Behdad Esfahbodb0b38bb2015-01-21 19:19:33 -08001044 hb_codepoint_t notdef = 0;
1045 hb_direction_t dir = buffer->props.direction;
1046 hb_position_t x_advance, y_advance, x_offset, y_offset;
1047 hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance);
1048 hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset);
1049 hb_position_t advance = x_advance + y_advance;
1050 x_offset = -x_offset;
1051 y_offset = -y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001052
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001053 unsigned int old_len = buffer->len;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001054 for (CFIndex j = range.location; j < range.location + range.length; j++)
1055 {
1056 UniChar ch = CFStringGetCharacterAtIndex (string_ref, j);
1057 if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j)
1058 {
1059 ch = CFStringGetCharacterAtIndex (string_ref, j - 1);
1060 if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu))
1061 /* This is the second of a surrogate pair. Don't need .notdef
1062 * for this one. */
1063 continue;
1064 }
Behdad Esfahbod982d94e2015-01-28 10:51:33 -08001065 if (buffer->unicode->is_default_ignorable (ch))
1066 continue;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001067
1068 info->codepoint = notdef;
Behdad Esfahbod3c41ccb2014-08-11 15:11:59 -04001069 info->cluster = log_clusters[j];
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001070
1071 info->mask = advance;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001072 info->var1.i32 = x_offset;
1073 info->var2.i32 = y_offset;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001074
1075 info++;
1076 buffer->len++;
1077 }
Behdad Esfahbod08acfe02014-08-12 18:57:08 -04001078 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
1079 buffer->reverse_range (old_len, buffer->len);
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001080 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001081 continue;
1082 }
1083 }
1084
1085 unsigned int num_glyphs = CTRunGetGlyphCount (run);
1086 if (num_glyphs == 0)
1087 continue;
1088
Behdad Esfahbod81b8d972014-08-12 15:49:47 -04001089 if (!buffer->ensure_inplace (buffer->len + num_glyphs))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001090 goto resize_and_retry;
1091
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001092 hb_glyph_info_t *run_info = buffer->info + buffer->len;
1093
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001094 /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always
1095 * succeed, and so copying data to our own buffer will be rare. Reports
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001096 * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001097 * frequently. At any rate, we can test that codepath by setting USE_PTR
1098 * to false. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001099
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001100#define USE_PTR true
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001101
1102#define SCRATCH_SAVE() \
1103 unsigned int scratch_size_saved = scratch_size; \
1104 hb_buffer_t::scratch_buffer_t *scratch_saved = scratch
1105
1106#define SCRATCH_RESTORE() \
1107 scratch_size = scratch_size_saved; \
1108 scratch = scratch_saved;
1109
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001110 { /* Setup glyphs */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001111 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001112 const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001113 if (!glyphs) {
1114 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry);
1115 CTRunGetGlyphs (run, range_all, glyph_buf);
1116 glyphs = glyph_buf;
1117 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001118 const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001119 if (!string_indices) {
1120 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry);
1121 CTRunGetStringIndices (run, range_all, index_buf);
1122 string_indices = index_buf;
1123 }
1124 hb_glyph_info_t *info = run_info;
1125 for (unsigned int j = 0; j < num_glyphs; j++)
1126 {
1127 info->codepoint = glyphs[j];
1128 info->cluster = log_clusters[string_indices[j]];
1129 info++;
1130 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001131 SCRATCH_RESTORE();
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001132 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001133 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001134 /* Setup positions.
1135 * Note that CoreText does not return advances for glyphs. As such,
1136 * for all but last glyph, we use the delta position to next glyph as
1137 * advance (in the advance direction only), and for last glyph we set
1138 * whatever is needed to make the whole run's advance add up. */
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001139 SCRATCH_SAVE();
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001140 const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001141 if (!positions) {
1142 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry);
1143 CTRunGetPositions (run, range_all, position_buf);
1144 positions = position_buf;
1145 }
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001146 hb_glyph_info_t *info = run_info;
1147 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1148 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001149 hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001150 for (unsigned int j = 0; j < num_glyphs; j++)
1151 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001152 double advance;
1153 if (likely (j + 1 < num_glyphs))
1154 advance = positions[j + 1].x - positions[j].x;
1155 else /* last glyph */
1156 advance = run_advance - (positions[j].x - positions[0].x);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001157 info->mask = advance * x_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001158 info->var1.i32 = x_offset;
1159 info->var2.i32 = positions[j].y * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001160 info++;
1161 }
1162 }
1163 else
1164 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001165 hb_position_t y_offset = (positions[0].y - advances_so_far) * y_mult;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001166 for (unsigned int j = 0; j < num_glyphs; j++)
1167 {
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001168 double advance;
1169 if (likely (j + 1 < num_glyphs))
1170 advance = positions[j + 1].y - positions[j].y;
1171 else /* last glyph */
1172 advance = run_advance - (positions[j].y - positions[0].y);
Behdad Esfahbod70622e52015-01-21 18:50:57 -08001173 info->mask = advance * y_mult;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001174 info->var1.i32 = positions[j].x * x_mult;
1175 info->var2.i32 = y_offset;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001176 info++;
1177 }
1178 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001179 SCRATCH_RESTORE();
Behdad Esfahbod6917a042015-01-28 10:43:32 -08001180 advances_so_far += run_advance;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001181 }
Behdad Esfahbodc3e924f2014-08-12 14:25:11 -04001182#undef SCRATCH_RESTORE
1183#undef SCRATCH_SAVE
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001184#undef USE_PTR
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001185#undef ALLOCATE_ARRAY
1186
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001187 buffer->len += num_glyphs;
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001188 }
1189
Behdad Esfahbod50ad7782015-08-18 10:22:16 +01001190 /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
1191 * or if it does, it doesn't resepct it. So we get runs with wrong
1192 * directions. As such, disable the assert... It wouldn't crash, but
1193 * cursoring will be off...
1194 *
1195 * http://crbug.com/419769
1196 */
1197 if (0)
1198 {
1199 /* Make sure all runs had the expected direction. */
1200 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1201 assert (bool (status_and & kCTRunStatusRightToLeft) == backward);
1202 assert (bool (status_or & kCTRunStatusRightToLeft) == backward);
1203 }
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001204
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001205 buffer->clear_positions ();
1206
1207 unsigned int count = buffer->len;
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001208 hb_glyph_info_t *info = buffer->info;
1209 hb_glyph_position_t *pos = buffer->pos;
1210 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
1211 for (unsigned int i = 0; i < count; i++)
1212 {
1213 pos->x_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001214 pos->x_offset = info->var1.i32;
1215 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001216
1217 info->mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1218
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001219 info++, pos++;
1220 }
1221 else
1222 for (unsigned int i = 0; i < count; i++)
1223 {
1224 pos->y_advance = info->mask;
Behdad Esfahboded6962c2015-08-20 15:39:53 +01001225 pos->x_offset = info->var1.i32;
1226 pos->y_offset = info->var2.i32;
Behdad Esfahbod239119a2017-08-13 15:08:34 -07001227
1228 info->mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1229
Behdad Esfahbod5a0eed32014-08-11 23:47:16 -04001230 info++, pos++;
1231 }
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001232
1233 /* Fix up clusters so that we never return out-of-order indices;
1234 * if core text has reordered glyphs, we'll merge them to the
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001235 * beginning of the reordered cluster. CoreText is nice enough
1236 * to tell us whenever it has produced nonmonotonic results...
1237 * Note that we assume the input clusters were nonmonotonic to
1238 * begin with.
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001239 *
1240 * This does *not* mean we'll form the same clusters as Uniscribe
1241 * or the native OT backend, only that the cluster indices will be
1242 * monotonic in the output buffer. */
Behdad Esfahbod10b11042014-08-11 20:02:45 -04001243 if (count > 1 && (status_or & kCTRunStatusNonMonotonic))
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001244 {
1245 hb_glyph_info_t *info = buffer->info;
1246 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
1247 {
1248 unsigned int cluster = info[count - 1].cluster;
1249 for (unsigned int i = count - 1; i > 0; i--)
1250 {
1251 cluster = MIN (cluster, info[i - 1].cluster);
1252 info[i - 1].cluster = cluster;
1253 }
1254 }
1255 else
1256 {
1257 unsigned int cluster = info[0].cluster;
1258 for (unsigned int i = 1; i < count; i++)
1259 {
1260 cluster = MIN (cluster, info[i].cluster);
1261 info[i].cluster = cluster;
1262 }
1263 }
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001264 }
1265 }
1266
Behdad Esfahbod4acce772014-08-11 17:46:50 -04001267#undef FAIL
1268
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001269fail:
1270 if (string_ref)
1271 CFRelease (string_ref);
1272 if (line)
1273 CFRelease (line);
1274
Behdad Esfahbod25f4fb92014-08-10 19:05:25 -04001275 for (unsigned int i = 0; i < range_records.len; i++)
1276 if (range_records[i].font)
1277 CFRelease (range_records[i].font);
1278
Behdad Esfahboda6b8dc82014-08-11 15:08:19 -04001279 return ret;
Jonathan Kewaa6d8492012-07-24 15:52:32 -04001280}
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001281
1282
1283/*
1284 * AAT shaper
1285 */
1286
Behdad Esfahbodd4bb52b2017-02-09 14:13:25 -08001287HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, face)
1288HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, font)
1289
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001290/*
1291 * shaper face data
1292 */
1293
1294struct hb_coretext_aat_shaper_face_data_t {};
1295
1296hb_coretext_aat_shaper_face_data_t *
1297_hb_coretext_aat_shaper_face_data_create (hb_face_t *face)
1298{
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001299 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 -04001300
Behdad Esfahbod84686bf2017-10-11 15:02:48 +02001301 for (unsigned int i = 0; i < ARRAY_LENGTH (tags); i++)
1302 {
1303 hb_blob_t *blob = face->reference_table (tags[i]);
1304 if (hb_blob_get_length (blob))
1305 {
1306 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001307 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 +02001308 }
1309 hb_blob_destroy (blob);
1310 }
1311
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001312 return nullptr;
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001313}
1314
1315void
1316_hb_coretext_aat_shaper_face_data_destroy (hb_coretext_aat_shaper_face_data_t *data HB_UNUSED)
1317{
1318}
1319
1320
1321/*
1322 * shaper font data
1323 */
1324
1325struct hb_coretext_aat_shaper_font_data_t {};
1326
1327hb_coretext_aat_shaper_font_data_t *
1328_hb_coretext_aat_shaper_font_data_create (hb_font_t *font)
1329{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001330 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 -04001331}
1332
1333void
1334_hb_coretext_aat_shaper_font_data_destroy (hb_coretext_aat_shaper_font_data_t *data HB_UNUSED)
1335{
1336}
1337
1338
1339/*
1340 * shaper shape_plan data
1341 */
1342
1343struct hb_coretext_aat_shaper_shape_plan_data_t {};
1344
1345hb_coretext_aat_shaper_shape_plan_data_t *
1346_hb_coretext_aat_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
1347 const hb_feature_t *user_features HB_UNUSED,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -07001348 unsigned int num_user_features HB_UNUSED,
1349 const int *coords HB_UNUSED,
1350 unsigned int num_coords HB_UNUSED)
Behdad Esfahbodc79865f2014-03-14 19:37:55 -04001351{
1352 return (hb_coretext_aat_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
1353}
1354
1355void
1356_hb_coretext_aat_shaper_shape_plan_data_destroy (hb_coretext_aat_shaper_shape_plan_data_t *data HB_UNUSED)
1357{
1358}
1359
1360
1361/*
1362 * shaper
1363 */
1364
1365hb_bool_t
1366_hb_coretext_aat_shape (hb_shape_plan_t *shape_plan,
1367 hb_font_t *font,
1368 hb_buffer_t *buffer,
1369 const hb_feature_t *features,
1370 unsigned int num_features)
1371{
1372 return _hb_coretext_shape (shape_plan, font, buffer, features, num_features);
1373}