Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 2 | * Copyright © 2012,2013 Mozilla Foundation. |
| 3 | * Copyright © 2012,2013 Google, Inc. |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 4 | * |
| 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 Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 29 | #define HB_SHAPER coretext |
Behdad Esfahbod | 40ec3bb | 2017-11-03 16:57:30 -0400 | [diff] [blame] | 30 | |
| 31 | #include "hb-private.hh" |
| 32 | #include "hb-debug.hh" |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 33 | #include "hb-shaper-impl-private.hh" |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 34 | |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 35 | #include "hb-coretext.h" |
Behdad Esfahbod | 6a2cbc6 | 2017-10-12 10:46:09 +0200 | [diff] [blame] | 36 | #include <math.h> |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 37 | |
Behdad Esfahbod | 06c1422 | 2017-10-11 15:29:53 +0200 | [diff] [blame] | 38 | /* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */ |
Behdad Esfahbod | c2cf68d | 2017-10-13 10:30:19 +0200 | [diff] [blame] | 39 | #define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f |
Behdad Esfahbod | 95883fc | 2017-10-13 10:21:07 +0200 | [diff] [blame] | 40 | |
| 41 | static CGFloat |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 42 | coretext_font_size_from_ptem (float ptem) |
Behdad Esfahbod | 95883fc | 2017-10-13 10:21:07 +0200 | [diff] [blame] | 43 | { |
Behdad Esfahbod | c2cf68d | 2017-10-13 10:30:19 +0200 | [diff] [blame] | 44 | /* CoreText points are CSS pixels (96 per inch), |
| 45 | * NOT typographic points (72 per inch). |
| 46 | * |
| 47 | * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html |
| 48 | */ |
| 49 | ptem *= 96.f / 72.f; |
| 50 | return ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : ptem; |
Behdad Esfahbod | 95883fc | 2017-10-13 10:21:07 +0200 | [diff] [blame] | 51 | } |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 52 | static float |
| 53 | coretext_font_size_to_ptem (CGFloat size) |
| 54 | { |
| 55 | size *= 72.f / 96.f; |
| 56 | return size <= 0.f ? 0 : size; |
| 57 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 58 | |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 59 | static void |
| 60 | release_table_data (void *user_data) |
| 61 | { |
| 62 | CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data); |
| 63 | CFRelease(cf_data); |
| 64 | } |
| 65 | |
| 66 | static hb_blob_t * |
| 67 | reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
| 68 | { |
| 69 | CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data); |
| 70 | CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag); |
| 71 | if (unlikely (!cf_data)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 72 | return nullptr; |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 73 | |
| 74 | const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data)); |
| 75 | const size_t length = CFDataGetLength (cf_data); |
| 76 | if (!data || !length) |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 77 | { |
| 78 | CFRelease (cf_data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 79 | return nullptr; |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 80 | } |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 81 | |
| 82 | return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY, |
| 83 | reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)), |
| 84 | release_table_data); |
| 85 | } |
| 86 | |
Behdad Esfahbod | e1b6d92 | 2017-10-11 15:51:31 +0200 | [diff] [blame] | 87 | static void |
| 88 | _hb_cg_font_release (void *data) |
| 89 | { |
| 90 | CGFontRelease ((CGFontRef) data); |
| 91 | } |
| 92 | |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 93 | |
Behdad Esfahbod | 466b3e5 | 2017-02-03 16:43:25 -0800 | [diff] [blame] | 94 | HB_SHAPER_DATA_ENSURE_DEFINE(coretext, face) |
Dominik Röttsches | a5ebe1d | 2017-10-11 13:32:38 +0200 | [diff] [blame] | 95 | HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(coretext, font, |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 96 | fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size_from_ptem (font->ptem)) <= .5 |
Behdad Esfahbod | 95883fc | 2017-10-13 10:21:07 +0200 | [diff] [blame] | 97 | ) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 98 | |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 99 | static CTFontDescriptorRef |
| 100 | get_last_resort_font_desc (void) |
| 101 | { |
| 102 | // TODO Handle allocation failures? |
| 103 | CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0); |
| 104 | CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault, |
| 105 | (const void **) &last_resort, |
| 106 | 1, |
| 107 | &kCFTypeArrayCallBacks); |
| 108 | CFRelease (last_resort); |
| 109 | CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault, |
| 110 | (const void **) &kCTFontCascadeListAttribute, |
| 111 | (const void **) &cascade_list, |
| 112 | 1, |
| 113 | &kCFTypeDictionaryKeyCallBacks, |
| 114 | &kCFTypeDictionaryValueCallBacks); |
| 115 | CFRelease (cascade_list); |
| 116 | |
| 117 | CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes); |
| 118 | CFRelease (attributes); |
| 119 | return font_desc; |
| 120 | } |
| 121 | |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 122 | static void |
| 123 | release_data (void *info, const void *data, size_t size) |
| 124 | { |
| 125 | assert (hb_blob_get_length ((hb_blob_t *) info) == size && |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 126 | hb_blob_get_data ((hb_blob_t *) info, nullptr) == data); |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 127 | |
| 128 | hb_blob_destroy ((hb_blob_t *) info); |
| 129 | } |
| 130 | |
| 131 | static CGFontRef |
| 132 | create_cg_font (hb_face_t *face) |
| 133 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 134 | CGFontRef cg_font = nullptr; |
Behdad Esfahbod | e1b6d92 | 2017-10-11 15:51:31 +0200 | [diff] [blame] | 135 | if (face->destroy == _hb_cg_font_release) |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 136 | { |
| 137 | cg_font = CGFontRetain ((CGFontRef) face->user_data); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | hb_blob_t *blob = hb_face_reference_blob (face); |
| 142 | unsigned int blob_length; |
| 143 | const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 144 | if (unlikely (!blob_length)) |
| 145 | DEBUG_MSG (CORETEXT, face, "Face has empty blob"); |
| 146 | |
| 147 | CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); |
| 148 | if (likely (provider)) |
| 149 | { |
| 150 | cg_font = CGFontCreateWithDataProvider (provider); |
| 151 | if (unlikely (!cg_font)) |
| 152 | DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed"); |
| 153 | CGDataProviderRelease (provider); |
| 154 | } |
| 155 | } |
| 156 | return cg_font; |
| 157 | } |
| 158 | |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 159 | static CTFontRef |
| 160 | create_ct_font (CGFontRef cg_font, CGFloat font_size) |
| 161 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 162 | CTFontRef ct_font = nullptr; |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 163 | |
| 164 | /* CoreText does not enable trak table usage / tracking when creating a CTFont |
| 165 | * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems |
| 166 | * to be through the CTFontCreateUIFontForLanguage call. */ |
| 167 | CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font); |
| 168 | if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) || |
| 169 | CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay"))) |
| 170 | { |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 171 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 1080 |
| 172 | # define kCTFontUIFontSystem kCTFontSystemFontType |
| 173 | # define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType |
| 174 | #endif |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 175 | CTFontUIFontType font_type = kCTFontUIFontSystem; |
| 176 | if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold"))) |
| 177 | font_type = kCTFontUIFontEmphasizedSystem; |
| 178 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 179 | ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr); |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 180 | CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font); |
| 181 | if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo) |
| 182 | { |
| 183 | CFRelease(ct_font); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 184 | ct_font = nullptr; |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 185 | } |
| 186 | CFRelease (ct_result_name); |
| 187 | } |
| 188 | CFRelease (cg_postscript_name); |
| 189 | |
| 190 | if (!ct_font) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 191 | ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr); |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 192 | |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 193 | if (unlikely (!ct_font)) { |
| 194 | DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed"); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 195 | return nullptr; |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 196 | } |
Behdad Esfahbod | 489acf6 | 2016-07-22 17:41:43 -0700 | [diff] [blame] | 197 | |
| 198 | /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter |
| 199 | * bug indicate that the cascade list reconfiguration occasionally causes |
| 200 | * crashes in CoreText on OS X 10.9, thus let's skip this step on older |
Dominik Röttsches | b717cd7 | 2016-09-07 23:56:57 +0300 | [diff] [blame] | 201 | * operating system versions. Except for the emoji font, where _not_ |
| 202 | * reconfiguring the cascade list causes CoreText crashes. For details, see |
| 203 | * crbug.com/549610 */ |
Ebrahim Byagowi | fc4e671 | 2016-09-09 23:28:28 +0430 | [diff] [blame] | 204 | // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 205 | if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) { |
Dominik Röttsches | b717cd7 | 2016-09-07 23:56:57 +0300 | [diff] [blame] | 206 | CFStringRef fontName = CTFontCopyPostScriptName (ct_font); |
| 207 | bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo; |
| 208 | CFRelease (fontName); |
| 209 | if (!isEmojiFont) |
| 210 | return ct_font; |
| 211 | } |
Behdad Esfahbod | 489acf6 | 2016-07-22 17:41:43 -0700 | [diff] [blame] | 212 | |
Ebrahim Byagowi | 8b0d642 | 2018-04-23 18:37:35 +0430 | [diff] [blame] | 213 | CFURLRef original_url = nullptr; |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 214 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
| 215 | ATSFontRef atsFont; |
| 216 | FSRef fsref; |
| 217 | OSStatus status; |
| 218 | atsFont = CTFontGetPlatformFont (ct_font, NULL); |
| 219 | status = ATSFontGetFileReference (atsFont, &fsref); |
| 220 | if (status == noErr) |
| 221 | original_url = CFURLCreateFromFSRef (NULL, &fsref); |
| 222 | #else |
| 223 | original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute); |
| 224 | #endif |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 225 | |
| 226 | /* Create font copy with cascade list that has LastResort first; this speeds up CoreText |
| 227 | * font fallback which we don't need anyway. */ |
| 228 | { |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 229 | CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc (); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 230 | CTFontRef new_ct_font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, last_resort_font_desc); |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 231 | CFRelease (last_resort_font_desc); |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 232 | if (new_ct_font) |
| 233 | { |
Behdad Esfahbod | fc9de44 | 2016-06-30 09:46:52 -0700 | [diff] [blame] | 234 | /* The CTFontCreateCopyWithAttributes call fails to stay on the same font |
| 235 | * when reconfiguring the cascade list and may switch to a different font |
| 236 | * when there are fonts that go by the same name, since the descriptor is |
| 237 | * just name and size. |
| 238 | * |
| 239 | * Avoid reconfiguring the cascade lists if the new font is outside the |
| 240 | * system locations that we cannot access from the sandboxed renderer |
| 241 | * process in Blink. This can be detected by the new file URL location |
| 242 | * that the newly found font points to. */ |
Ebrahim Byagowi | 8b0d642 | 2018-04-23 18:37:35 +0430 | [diff] [blame] | 243 | CFURLRef new_url = nullptr; |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 244 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
| 245 | atsFont = CTFontGetPlatformFont (new_ct_font, NULL); |
| 246 | status = ATSFontGetFileReference (atsFont, &fsref); |
| 247 | if (status == noErr) |
| 248 | new_url = CFURLCreateFromFSRef (NULL, &fsref); |
| 249 | #else |
| 250 | new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute); |
| 251 | #endif |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 252 | // Keep reconfigured font if URL cannot be retrieved (seems to be the case |
| 253 | // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606 |
| 254 | if (!original_url || !new_url || CFEqual (original_url, new_url)) { |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 255 | CFRelease (ct_font); |
| 256 | ct_font = new_ct_font; |
| 257 | } else { |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 258 | CFRelease (new_ct_font); |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 259 | DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed."); |
| 260 | } |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 261 | if (new_url) |
| 262 | CFRelease (new_url); |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed"); |
| 266 | } |
| 267 | |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 268 | if (original_url) |
| 269 | CFRelease (original_url); |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 270 | return ct_font; |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 271 | } |
| 272 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 273 | hb_coretext_shaper_face_data_t * |
| 274 | _hb_coretext_shaper_face_data_create (hb_face_t *face) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 275 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 276 | CGFontRef cg_font = create_cg_font (face); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 277 | |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 278 | if (unlikely (!cg_font)) |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 279 | { |
Behdad Esfahbod | 15063b1 | 2016-02-22 15:56:29 +0900 | [diff] [blame] | 280 | DEBUG_MSG (CORETEXT, face, "CGFont creation failed.."); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 281 | return nullptr; |
Behdad Esfahbod | 15063b1 | 2016-02-22 15:56:29 +0900 | [diff] [blame] | 282 | } |
| 283 | |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 284 | return (hb_coretext_shaper_face_data_t *) cg_font; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 287 | void |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 288 | _hb_coretext_shaper_face_data_destroy (hb_coretext_shaper_face_data_t *data) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 289 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 290 | CFRelease ((CGFontRef) data); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 293 | hb_face_t * |
| 294 | hb_coretext_face_create (CGFontRef cg_font) |
| 295 | { |
| 296 | return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release); |
| 297 | } |
| 298 | |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 299 | /* |
| 300 | * Since: 0.9.10 |
| 301 | */ |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 302 | CGFontRef |
Behdad Esfahbod | e923e64 | 2012-12-09 19:39:40 -0500 | [diff] [blame] | 303 | hb_coretext_face_get_cg_font (hb_face_t *face) |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 304 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 305 | if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr; |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 306 | return (CGFontRef) HB_SHAPER_DATA_GET (face); |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 309 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 310 | hb_coretext_shaper_font_data_t * |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 311 | _hb_coretext_shaper_font_data_create (hb_font_t *font) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 312 | { |
Dominik Röttsches | db7a73c | 2017-10-11 13:24:39 +0200 | [diff] [blame] | 313 | hb_face_t *face = font->face; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 314 | if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr; |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 315 | CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face); |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 316 | |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 317 | CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size_from_ptem (font->ptem)); |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 318 | |
| 319 | if (unlikely (!ct_font)) |
| 320 | { |
| 321 | DEBUG_MSG (CORETEXT, font, "CGFont creation failed.."); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 322 | return nullptr; |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 325 | return (hb_coretext_shaper_font_data_t *) ct_font; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 328 | void |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 329 | _hb_coretext_shaper_font_data_destroy (hb_coretext_shaper_font_data_t *data) |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 330 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 331 | CFRelease ((CTFontRef) data); |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 334 | /* |
| 335 | * Since: 1.7.2 |
| 336 | */ |
| 337 | hb_font_t * |
| 338 | hb_coretext_font_create (CTFontRef ct_font) |
| 339 | { |
Bruce Mitchener | dae20fb | 2018-01-31 20:16:08 +0700 | [diff] [blame] | 340 | CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr); |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 341 | hb_face_t *face = hb_coretext_face_create (cg_font); |
| 342 | CFRelease (cg_font); |
| 343 | hb_font_t *font = hb_font_create (face); |
| 344 | hb_face_destroy (face); |
| 345 | |
| 346 | if (unlikely (hb_object_is_inert (font))) |
| 347 | return font; |
| 348 | |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 349 | hb_font_set_ptem (font, coretext_font_size_to_ptem (CTFontGetSize(ct_font))); |
| 350 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 351 | /* Let there be dragons here... */ |
| 352 | HB_SHAPER_DATA_GET (font) = (hb_coretext_shaper_font_data_t *) CFRetain (ct_font); |
| 353 | |
| 354 | return font; |
| 355 | } |
| 356 | |
| 357 | CTFontRef |
| 358 | hb_coretext_font_get_ct_font (hb_font_t *font) |
| 359 | { |
| 360 | if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr; |
| 361 | return (CTFontRef) HB_SHAPER_DATA_GET (font); |
| 362 | } |
| 363 | |
| 364 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * shaper shape_plan data |
| 368 | */ |
| 369 | |
| 370 | struct hb_coretext_shaper_shape_plan_data_t {}; |
| 371 | |
| 372 | hb_coretext_shaper_shape_plan_data_t * |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame] | 373 | _hb_coretext_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, |
| 374 | const hb_feature_t *user_features HB_UNUSED, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 375 | unsigned int num_user_features HB_UNUSED, |
| 376 | const int *coords HB_UNUSED, |
| 377 | unsigned int num_coords HB_UNUSED) |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 378 | { |
| 379 | return (hb_coretext_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; |
| 380 | } |
| 381 | |
| 382 | void |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame] | 383 | _hb_coretext_shaper_shape_plan_data_destroy (hb_coretext_shaper_shape_plan_data_t *data HB_UNUSED) |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 384 | { |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* |
| 389 | * shaper |
| 390 | */ |
| 391 | |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 392 | struct feature_record_t { |
| 393 | unsigned int feature; |
| 394 | unsigned int setting; |
| 395 | }; |
| 396 | |
| 397 | struct active_feature_t { |
| 398 | feature_record_t rec; |
| 399 | unsigned int order; |
| 400 | |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 401 | static int cmp (const void *pa, const void *pb) { |
| 402 | const active_feature_t *a = (const active_feature_t *) pa; |
| 403 | const active_feature_t *b = (const active_feature_t *) pb; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 404 | return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 : |
| 405 | a->order < b->order ? -1 : a->order > b->order ? 1 : |
| 406 | a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 : |
| 407 | 0; |
| 408 | } |
| 409 | bool operator== (const active_feature_t *f) { |
| 410 | return cmp (this, f) == 0; |
| 411 | } |
| 412 | }; |
| 413 | |
| 414 | struct feature_event_t { |
| 415 | unsigned int index; |
| 416 | bool start; |
| 417 | active_feature_t feature; |
| 418 | |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 419 | static int cmp (const void *pa, const void *pb) { |
| 420 | const feature_event_t *a = (const feature_event_t *) pa; |
| 421 | const feature_event_t *b = (const feature_event_t *) pb; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 422 | return a->index < b->index ? -1 : a->index > b->index ? 1 : |
| 423 | a->start < b->start ? -1 : a->start > b->start ? 1 : |
| 424 | active_feature_t::cmp (&a->feature, &b->feature); |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | struct range_record_t { |
| 429 | CTFontRef font; |
| 430 | unsigned int index_first; /* == start */ |
| 431 | unsigned int index_last; /* == end - 1 */ |
| 432 | }; |
| 433 | |
| 434 | |
| 435 | /* The following enum members are added in OS X 10.8. */ |
| 436 | #define kAltHalfWidthTextSelector 6 |
| 437 | #define kAltProportionalTextSelector 5 |
| 438 | #define kAlternateHorizKanaOffSelector 1 |
| 439 | #define kAlternateHorizKanaOnSelector 0 |
| 440 | #define kAlternateKanaType 34 |
| 441 | #define kAlternateVertKanaOffSelector 3 |
| 442 | #define kAlternateVertKanaOnSelector 2 |
| 443 | #define kCaseSensitiveLayoutOffSelector 1 |
| 444 | #define kCaseSensitiveLayoutOnSelector 0 |
| 445 | #define kCaseSensitiveLayoutType 33 |
| 446 | #define kCaseSensitiveSpacingOffSelector 3 |
| 447 | #define kCaseSensitiveSpacingOnSelector 2 |
| 448 | #define kContextualAlternatesOffSelector 1 |
| 449 | #define kContextualAlternatesOnSelector 0 |
| 450 | #define kContextualAlternatesType 36 |
| 451 | #define kContextualLigaturesOffSelector 19 |
| 452 | #define kContextualLigaturesOnSelector 18 |
| 453 | #define kContextualSwashAlternatesOffSelector 5 |
| 454 | #define kContextualSwashAlternatesOnSelector 4 |
| 455 | #define kDefaultLowerCaseSelector 0 |
| 456 | #define kDefaultUpperCaseSelector 0 |
| 457 | #define kHistoricalLigaturesOffSelector 21 |
| 458 | #define kHistoricalLigaturesOnSelector 20 |
| 459 | #define kHojoCharactersSelector 12 |
| 460 | #define kJIS2004CharactersSelector 11 |
| 461 | #define kLowerCasePetiteCapsSelector 2 |
| 462 | #define kLowerCaseSmallCapsSelector 1 |
| 463 | #define kLowerCaseType 37 |
| 464 | #define kMathematicalGreekOffSelector 11 |
| 465 | #define kMathematicalGreekOnSelector 10 |
| 466 | #define kNLCCharactersSelector 13 |
| 467 | #define kQuarterWidthTextSelector 4 |
| 468 | #define kScientificInferiorsSelector 4 |
| 469 | #define kStylisticAltEightOffSelector 17 |
| 470 | #define kStylisticAltEightOnSelector 16 |
| 471 | #define kStylisticAltEighteenOffSelector 37 |
| 472 | #define kStylisticAltEighteenOnSelector 36 |
| 473 | #define kStylisticAltElevenOffSelector 23 |
| 474 | #define kStylisticAltElevenOnSelector 22 |
| 475 | #define kStylisticAltFifteenOffSelector 31 |
| 476 | #define kStylisticAltFifteenOnSelector 30 |
| 477 | #define kStylisticAltFiveOffSelector 11 |
| 478 | #define kStylisticAltFiveOnSelector 10 |
| 479 | #define kStylisticAltFourOffSelector 9 |
| 480 | #define kStylisticAltFourOnSelector 8 |
| 481 | #define kStylisticAltFourteenOffSelector 29 |
| 482 | #define kStylisticAltFourteenOnSelector 28 |
| 483 | #define kStylisticAltNineOffSelector 19 |
| 484 | #define kStylisticAltNineOnSelector 18 |
| 485 | #define kStylisticAltNineteenOffSelector 39 |
| 486 | #define kStylisticAltNineteenOnSelector 38 |
| 487 | #define kStylisticAltOneOffSelector 3 |
| 488 | #define kStylisticAltOneOnSelector 2 |
| 489 | #define kStylisticAltSevenOffSelector 15 |
| 490 | #define kStylisticAltSevenOnSelector 14 |
| 491 | #define kStylisticAltSeventeenOffSelector 35 |
| 492 | #define kStylisticAltSeventeenOnSelector 34 |
| 493 | #define kStylisticAltSixOffSelector 13 |
| 494 | #define kStylisticAltSixOnSelector 12 |
| 495 | #define kStylisticAltSixteenOffSelector 33 |
| 496 | #define kStylisticAltSixteenOnSelector 32 |
| 497 | #define kStylisticAltTenOffSelector 21 |
| 498 | #define kStylisticAltTenOnSelector 20 |
| 499 | #define kStylisticAltThirteenOffSelector 27 |
| 500 | #define kStylisticAltThirteenOnSelector 26 |
| 501 | #define kStylisticAltThreeOffSelector 7 |
| 502 | #define kStylisticAltThreeOnSelector 6 |
| 503 | #define kStylisticAltTwelveOffSelector 25 |
| 504 | #define kStylisticAltTwelveOnSelector 24 |
| 505 | #define kStylisticAltTwentyOffSelector 41 |
| 506 | #define kStylisticAltTwentyOnSelector 40 |
| 507 | #define kStylisticAltTwoOffSelector 5 |
| 508 | #define kStylisticAltTwoOnSelector 4 |
| 509 | #define kStylisticAlternativesType 35 |
| 510 | #define kSwashAlternatesOffSelector 3 |
| 511 | #define kSwashAlternatesOnSelector 2 |
| 512 | #define kThirdWidthTextSelector 3 |
| 513 | #define kTraditionalNamesCharactersSelector 14 |
| 514 | #define kUpperCasePetiteCapsSelector 2 |
| 515 | #define kUpperCaseSmallCapsSelector 1 |
| 516 | #define kUpperCaseType 38 |
| 517 | |
| 518 | /* Table data courtesy of Apple. */ |
Behdad Esfahbod | 522b1cc | 2014-08-14 13:29:30 -0400 | [diff] [blame] | 519 | static const struct feature_mapping_t { |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 520 | FourCharCode otFeatureTag; |
| 521 | uint16_t aatFeatureType; |
| 522 | uint16_t selectorToEnable; |
| 523 | uint16_t selectorToDisable; |
| 524 | } feature_mappings[] = { |
| 525 | { 'c2pc', kUpperCaseType, kUpperCasePetiteCapsSelector, kDefaultUpperCaseSelector }, |
| 526 | { 'c2sc', kUpperCaseType, kUpperCaseSmallCapsSelector, kDefaultUpperCaseSelector }, |
| 527 | { 'calt', kContextualAlternatesType, kContextualAlternatesOnSelector, kContextualAlternatesOffSelector }, |
| 528 | { 'case', kCaseSensitiveLayoutType, kCaseSensitiveLayoutOnSelector, kCaseSensitiveLayoutOffSelector }, |
| 529 | { 'clig', kLigaturesType, kContextualLigaturesOnSelector, kContextualLigaturesOffSelector }, |
| 530 | { 'cpsp', kCaseSensitiveLayoutType, kCaseSensitiveSpacingOnSelector, kCaseSensitiveSpacingOffSelector }, |
| 531 | { 'cswh', kContextualAlternatesType, kContextualSwashAlternatesOnSelector, kContextualSwashAlternatesOffSelector }, |
| 532 | { 'dlig', kLigaturesType, kRareLigaturesOnSelector, kRareLigaturesOffSelector }, |
| 533 | { 'expt', kCharacterShapeType, kExpertCharactersSelector, 16 }, |
| 534 | { 'frac', kFractionsType, kDiagonalFractionsSelector, kNoFractionsSelector }, |
| 535 | { 'fwid', kTextSpacingType, kMonospacedTextSelector, 7 }, |
| 536 | { 'halt', kTextSpacingType, kAltHalfWidthTextSelector, 7 }, |
| 537 | { 'hist', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector }, |
| 538 | { 'hkna', kAlternateKanaType, kAlternateHorizKanaOnSelector, kAlternateHorizKanaOffSelector, }, |
| 539 | { 'hlig', kLigaturesType, kHistoricalLigaturesOnSelector, kHistoricalLigaturesOffSelector }, |
| 540 | { 'hngl', kTransliterationType, kHanjaToHangulSelector, kNoTransliterationSelector }, |
| 541 | { 'hojo', kCharacterShapeType, kHojoCharactersSelector, 16 }, |
| 542 | { 'hwid', kTextSpacingType, kHalfWidthTextSelector, 7 }, |
| 543 | { 'ital', kItalicCJKRomanType, kCJKItalicRomanOnSelector, kCJKItalicRomanOffSelector }, |
| 544 | { 'jp04', kCharacterShapeType, kJIS2004CharactersSelector, 16 }, |
| 545 | { 'jp78', kCharacterShapeType, kJIS1978CharactersSelector, 16 }, |
| 546 | { 'jp83', kCharacterShapeType, kJIS1983CharactersSelector, 16 }, |
| 547 | { 'jp90', kCharacterShapeType, kJIS1990CharactersSelector, 16 }, |
| 548 | { 'liga', kLigaturesType, kCommonLigaturesOnSelector, kCommonLigaturesOffSelector }, |
| 549 | { 'lnum', kNumberCaseType, kUpperCaseNumbersSelector, 2 }, |
| 550 | { 'mgrk', kMathematicalExtrasType, kMathematicalGreekOnSelector, kMathematicalGreekOffSelector }, |
| 551 | { 'nlck', kCharacterShapeType, kNLCCharactersSelector, 16 }, |
| 552 | { 'onum', kNumberCaseType, kLowerCaseNumbersSelector, 2 }, |
| 553 | { 'ordn', kVerticalPositionType, kOrdinalsSelector, kNormalPositionSelector }, |
| 554 | { 'palt', kTextSpacingType, kAltProportionalTextSelector, 7 }, |
| 555 | { 'pcap', kLowerCaseType, kLowerCasePetiteCapsSelector, kDefaultLowerCaseSelector }, |
| 556 | { 'pkna', kTextSpacingType, kProportionalTextSelector, 7 }, |
| 557 | { 'pnum', kNumberSpacingType, kProportionalNumbersSelector, 4 }, |
| 558 | { 'pwid', kTextSpacingType, kProportionalTextSelector, 7 }, |
| 559 | { 'qwid', kTextSpacingType, kQuarterWidthTextSelector, 7 }, |
| 560 | { 'ruby', kRubyKanaType, kRubyKanaOnSelector, kRubyKanaOffSelector }, |
| 561 | { 'sinf', kVerticalPositionType, kScientificInferiorsSelector, kNormalPositionSelector }, |
| 562 | { 'smcp', kLowerCaseType, kLowerCaseSmallCapsSelector, kDefaultLowerCaseSelector }, |
| 563 | { 'smpl', kCharacterShapeType, kSimplifiedCharactersSelector, 16 }, |
| 564 | { 'ss01', kStylisticAlternativesType, kStylisticAltOneOnSelector, kStylisticAltOneOffSelector }, |
| 565 | { 'ss02', kStylisticAlternativesType, kStylisticAltTwoOnSelector, kStylisticAltTwoOffSelector }, |
| 566 | { 'ss03', kStylisticAlternativesType, kStylisticAltThreeOnSelector, kStylisticAltThreeOffSelector }, |
| 567 | { 'ss04', kStylisticAlternativesType, kStylisticAltFourOnSelector, kStylisticAltFourOffSelector }, |
| 568 | { 'ss05', kStylisticAlternativesType, kStylisticAltFiveOnSelector, kStylisticAltFiveOffSelector }, |
| 569 | { 'ss06', kStylisticAlternativesType, kStylisticAltSixOnSelector, kStylisticAltSixOffSelector }, |
| 570 | { 'ss07', kStylisticAlternativesType, kStylisticAltSevenOnSelector, kStylisticAltSevenOffSelector }, |
| 571 | { 'ss08', kStylisticAlternativesType, kStylisticAltEightOnSelector, kStylisticAltEightOffSelector }, |
| 572 | { 'ss09', kStylisticAlternativesType, kStylisticAltNineOnSelector, kStylisticAltNineOffSelector }, |
| 573 | { 'ss10', kStylisticAlternativesType, kStylisticAltTenOnSelector, kStylisticAltTenOffSelector }, |
| 574 | { 'ss11', kStylisticAlternativesType, kStylisticAltElevenOnSelector, kStylisticAltElevenOffSelector }, |
| 575 | { 'ss12', kStylisticAlternativesType, kStylisticAltTwelveOnSelector, kStylisticAltTwelveOffSelector }, |
| 576 | { 'ss13', kStylisticAlternativesType, kStylisticAltThirteenOnSelector, kStylisticAltThirteenOffSelector }, |
| 577 | { 'ss14', kStylisticAlternativesType, kStylisticAltFourteenOnSelector, kStylisticAltFourteenOffSelector }, |
| 578 | { 'ss15', kStylisticAlternativesType, kStylisticAltFifteenOnSelector, kStylisticAltFifteenOffSelector }, |
| 579 | { 'ss16', kStylisticAlternativesType, kStylisticAltSixteenOnSelector, kStylisticAltSixteenOffSelector }, |
| 580 | { 'ss17', kStylisticAlternativesType, kStylisticAltSeventeenOnSelector, kStylisticAltSeventeenOffSelector }, |
| 581 | { 'ss18', kStylisticAlternativesType, kStylisticAltEighteenOnSelector, kStylisticAltEighteenOffSelector }, |
| 582 | { 'ss19', kStylisticAlternativesType, kStylisticAltNineteenOnSelector, kStylisticAltNineteenOffSelector }, |
| 583 | { 'ss20', kStylisticAlternativesType, kStylisticAltTwentyOnSelector, kStylisticAltTwentyOffSelector }, |
| 584 | { 'subs', kVerticalPositionType, kInferiorsSelector, kNormalPositionSelector }, |
| 585 | { 'sups', kVerticalPositionType, kSuperiorsSelector, kNormalPositionSelector }, |
| 586 | { 'swsh', kContextualAlternatesType, kSwashAlternatesOnSelector, kSwashAlternatesOffSelector }, |
| 587 | { 'titl', kStyleOptionsType, kTitlingCapsSelector, kNoStyleOptionsSelector }, |
| 588 | { 'tnam', kCharacterShapeType, kTraditionalNamesCharactersSelector, 16 }, |
| 589 | { 'tnum', kNumberSpacingType, kMonospacedNumbersSelector, 4 }, |
| 590 | { 'trad', kCharacterShapeType, kTraditionalCharactersSelector, 16 }, |
| 591 | { 'twid', kTextSpacingType, kThirdWidthTextSelector, 7 }, |
| 592 | { 'unic', kLetterCaseType, 14, 15 }, |
| 593 | { 'valt', kTextSpacingType, kAltProportionalTextSelector, 7 }, |
| 594 | { 'vert', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector }, |
| 595 | { 'vhal', kTextSpacingType, kAltHalfWidthTextSelector, 7 }, |
| 596 | { 'vkna', kAlternateKanaType, kAlternateVertKanaOnSelector, kAlternateVertKanaOffSelector }, |
| 597 | { 'vpal', kTextSpacingType, kAltProportionalTextSelector, 7 }, |
| 598 | { 'vrt2', kVerticalSubstitutionType, kSubstituteVerticalFormsOnSelector, kSubstituteVerticalFormsOffSelector }, |
| 599 | { 'zero', kTypographicExtrasType, kSlashedZeroOnSelector, kSlashedZeroOffSelector }, |
| 600 | }; |
| 601 | |
| 602 | static int |
| 603 | _hb_feature_mapping_cmp (const void *key_, const void *entry_) |
| 604 | { |
| 605 | unsigned int key = * (unsigned int *) key_; |
| 606 | const feature_mapping_t * entry = (const feature_mapping_t *) entry_; |
| 607 | return key < entry->otFeatureTag ? -1 : |
| 608 | key > entry->otFeatureTag ? 1 : |
| 609 | 0; |
| 610 | } |
| 611 | |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 612 | hb_bool_t |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 613 | _hb_coretext_shape (hb_shape_plan_t *shape_plan, |
| 614 | hb_font_t *font, |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 615 | hb_buffer_t *buffer, |
| 616 | const hb_feature_t *features, |
| 617 | unsigned int num_features) |
| 618 | { |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 619 | hb_face_t *face = font->face; |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 620 | CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face); |
| 621 | CTFontRef ct_font = (CTFontRef) HB_SHAPER_DATA_GET (font); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 622 | |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 623 | CGFloat ct_font_size = CTFontGetSize (ct_font); |
Behdad Esfahbod | 061105e | 2016-02-22 14:59:39 +0900 | [diff] [blame] | 624 | CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size; |
| 625 | CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size; |
| 626 | |
Behdad Esfahbod | 624a299 | 2014-08-11 15:29:18 -0400 | [diff] [blame] | 627 | /* Attach marks to their bases, to match the 'ot' shaper. |
| 628 | * Adapted from hb-ot-shape:hb_form_clusters(). |
| 629 | * Note that this only makes us be closer to the 'ot' shaper, |
| 630 | * but by no means the same. For example, if there's |
| 631 | * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will |
| 632 | * continue pointing to B2 even though B2 was merged into B1's |
| 633 | * cluster... */ |
Behdad Esfahbod | 62c2711 | 2016-02-22 15:07:20 +0900 | [diff] [blame] | 634 | if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES) |
Behdad Esfahbod | 624a299 | 2014-08-11 15:29:18 -0400 | [diff] [blame] | 635 | { |
| 636 | hb_unicode_funcs_t *unicode = buffer->unicode; |
| 637 | unsigned int count = buffer->len; |
| 638 | hb_glyph_info_t *info = buffer->info; |
| 639 | for (unsigned int i = 1; i < count; i++) |
| 640 | if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint))) |
| 641 | buffer->merge_clusters (i - 1, i + 1); |
| 642 | } |
| 643 | |
Behdad Esfahbod | 37b9561 | 2018-05-01 19:09:00 -0400 | [diff] [blame^] | 644 | hb_auto_t<hb_vector_t<feature_record_t> > feature_records; |
| 645 | hb_auto_t<hb_vector_t<range_record_t> > range_records; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 646 | |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 647 | /* |
| 648 | * Set up features. |
| 649 | * (copied + modified from code from hb-uniscribe.cc) |
| 650 | */ |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 651 | if (num_features) |
| 652 | { |
| 653 | /* Sort features by start/end events. */ |
Behdad Esfahbod | 37b9561 | 2018-05-01 19:09:00 -0400 | [diff] [blame^] | 654 | hb_auto_t<hb_vector_t<feature_event_t> > feature_events; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 655 | for (unsigned int i = 0; i < num_features; i++) |
| 656 | { |
| 657 | const feature_mapping_t * mapping = (const feature_mapping_t *) bsearch (&features[i].tag, |
| 658 | feature_mappings, |
| 659 | ARRAY_LENGTH (feature_mappings), |
| 660 | sizeof (feature_mappings[0]), |
| 661 | _hb_feature_mapping_cmp); |
| 662 | if (!mapping) |
| 663 | continue; |
| 664 | |
| 665 | active_feature_t feature; |
| 666 | feature.rec.feature = mapping->aatFeatureType; |
| 667 | feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable; |
| 668 | feature.order = i; |
| 669 | |
| 670 | feature_event_t *event; |
| 671 | |
| 672 | event = feature_events.push (); |
| 673 | if (unlikely (!event)) |
| 674 | goto fail_features; |
| 675 | event->index = features[i].start; |
| 676 | event->start = true; |
| 677 | event->feature = feature; |
| 678 | |
| 679 | event = feature_events.push (); |
| 680 | if (unlikely (!event)) |
| 681 | goto fail_features; |
| 682 | event->index = features[i].end; |
| 683 | event->start = false; |
| 684 | event->feature = feature; |
| 685 | } |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 686 | feature_events.qsort (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 687 | /* Add a strategic final event. */ |
| 688 | { |
| 689 | active_feature_t feature; |
| 690 | feature.rec.feature = HB_TAG_NONE; |
| 691 | feature.rec.setting = 0; |
| 692 | feature.order = num_features + 1; |
| 693 | |
| 694 | feature_event_t *event = feature_events.push (); |
| 695 | if (unlikely (!event)) |
| 696 | goto fail_features; |
| 697 | event->index = 0; /* This value does magic. */ |
| 698 | event->start = false; |
| 699 | event->feature = feature; |
| 700 | } |
| 701 | |
| 702 | /* Scan events and save features for each range. */ |
Behdad Esfahbod | 37b9561 | 2018-05-01 19:09:00 -0400 | [diff] [blame^] | 703 | hb_auto_t<hb_vector_t<active_feature_t> > active_features; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 704 | unsigned int last_index = 0; |
| 705 | for (unsigned int i = 0; i < feature_events.len; i++) |
| 706 | { |
| 707 | feature_event_t *event = &feature_events[i]; |
| 708 | |
| 709 | if (event->index != last_index) |
| 710 | { |
| 711 | /* Save a snapshot of active features and the range. */ |
| 712 | range_record_t *range = range_records.push (); |
| 713 | if (unlikely (!range)) |
| 714 | goto fail_features; |
| 715 | |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 716 | if (active_features.len) |
| 717 | { |
| 718 | CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 719 | |
| 720 | /* TODO sort and resolve conflicting features? */ |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 721 | /* active_features.qsort (); */ |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 722 | for (unsigned int j = 0; j < active_features.len; j++) |
| 723 | { |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 724 | CFStringRef keys[] = { |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 725 | kCTFontFeatureTypeIdentifierKey, |
| 726 | kCTFontFeatureSelectorIdentifierKey |
| 727 | }; |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 728 | CFNumberRef values[] = { |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 729 | CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature), |
| 730 | CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting) |
| 731 | }; |
Behdad Esfahbod | 80cc0a7 | 2017-10-17 11:14:48 -0700 | [diff] [blame] | 732 | static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), ""); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 733 | CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault, |
| 734 | (const void **) keys, |
| 735 | (const void **) values, |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 736 | ARRAY_LENGTH (keys), |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 737 | &kCFTypeDictionaryKeyCallBacks, |
| 738 | &kCFTypeDictionaryValueCallBacks); |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 739 | for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++) |
| 740 | CFRelease (values[i]); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 741 | |
| 742 | CFArrayAppendValue (features_array, dict); |
| 743 | CFRelease (dict); |
| 744 | |
| 745 | } |
| 746 | |
| 747 | CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault, |
| 748 | (const void **) &kCTFontFeatureSettingsAttribute, |
| 749 | (const void **) &features_array, |
| 750 | 1, |
| 751 | &kCFTypeDictionaryKeyCallBacks, |
| 752 | &kCFTypeDictionaryValueCallBacks); |
| 753 | CFRelease (features_array); |
| 754 | |
| 755 | CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes); |
| 756 | CFRelease (attributes); |
| 757 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 758 | range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 759 | CFRelease (font_desc); |
| 760 | } |
| 761 | else |
| 762 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 763 | range->font = nullptr; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | range->index_first = last_index; |
| 767 | range->index_last = event->index - 1; |
| 768 | |
| 769 | last_index = event->index; |
| 770 | } |
| 771 | |
| 772 | if (event->start) { |
| 773 | active_feature_t *feature = active_features.push (); |
| 774 | if (unlikely (!feature)) |
| 775 | goto fail_features; |
| 776 | *feature = event->feature; |
| 777 | } else { |
| 778 | active_feature_t *feature = active_features.find (&event->feature); |
| 779 | if (feature) |
| 780 | active_features.remove (feature - active_features.array); |
| 781 | } |
| 782 | } |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 783 | } |
| 784 | else |
| 785 | { |
| 786 | fail_features: |
| 787 | num_features = 0; |
| 788 | } |
| 789 | |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 790 | unsigned int scratch_size; |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 791 | hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 792 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 793 | #define ALLOCATE_ARRAY(Type, name, len, on_no_room) \ |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 794 | Type *name = (Type *) scratch; \ |
| 795 | { \ |
| 796 | unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 797 | if (unlikely (_consumed > scratch_size)) \ |
| 798 | { \ |
| 799 | on_no_room; \ |
| 800 | assert (0); \ |
| 801 | } \ |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 802 | scratch += _consumed; \ |
| 803 | scratch_size -= _consumed; \ |
| 804 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 805 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 806 | ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 807 | unsigned int chars_len = 0; |
| 808 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 809 | hb_codepoint_t c = buffer->info[i].codepoint; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 810 | if (likely (c <= 0xFFFFu)) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 811 | pchars[chars_len++] = c; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 812 | else if (unlikely (c > 0x10FFFFu)) |
| 813 | pchars[chars_len++] = 0xFFFDu; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 814 | else { |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 815 | pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 816 | pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1)); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 820 | ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/); |
Behdad Esfahbod | 9b3c60c | 2014-08-11 13:25:43 -0400 | [diff] [blame] | 821 | chars_len = 0; |
| 822 | for (unsigned int i = 0; i < buffer->len; i++) |
| 823 | { |
| 824 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 825 | unsigned int cluster = buffer->info[i].cluster; |
| 826 | log_clusters[chars_len++] = cluster; |
| 827 | if (hb_in_range (c, 0x10000u, 0x10FFFFu)) |
| 828 | log_clusters[chars_len++] = cluster; /* Surrogates. */ |
| 829 | } |
| 830 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 831 | #define FAIL(...) \ |
| 832 | HB_STMT_START { \ |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 833 | DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 834 | ret = false; \ |
| 835 | goto fail; \ |
| 836 | } HB_STMT_END; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 837 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 838 | bool ret = true; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 839 | CFStringRef string_ref = nullptr; |
| 840 | CTLineRef line = nullptr; |
Behdad Esfahbod | a782a5e | 2013-08-07 21:08:54 -0400 | [diff] [blame] | 841 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 842 | if (0) |
Behdad Esfahbod | a782a5e | 2013-08-07 21:08:54 -0400 | [diff] [blame] | 843 | { |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 844 | resize_and_retry: |
Behdad Esfahbod | 1b55077 | 2014-08-11 20:45:12 -0400 | [diff] [blame] | 845 | DEBUG_MSG (CORETEXT, buffer, "Buffer resize"); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 846 | /* string_ref uses the scratch-buffer for backing store, and line references |
| 847 | * string_ref (via attr_string). We must release those before resizing buffer. */ |
| 848 | assert (string_ref); |
| 849 | assert (line); |
| 850 | CFRelease (string_ref); |
| 851 | CFRelease (line); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 852 | string_ref = nullptr; |
| 853 | line = nullptr; |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 854 | |
| 855 | /* Get previous start-of-scratch-area, that we use later for readjusting |
| 856 | * our existing scratch arrays. */ |
| 857 | unsigned int old_scratch_used; |
| 858 | hb_buffer_t::scratch_buffer_t *old_scratch; |
| 859 | old_scratch = buffer->get_scratch_buffer (&old_scratch_used); |
| 860 | old_scratch_used = scratch - old_scratch; |
| 861 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 862 | if (unlikely (!buffer->ensure (buffer->allocated * 2))) |
Behdad Esfahbod | b9993d8 | 2014-08-10 17:40:24 -0400 | [diff] [blame] | 863 | FAIL ("Buffer resize failed"); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 864 | |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 865 | /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the |
| 866 | * cleanest way to do without completely restructuring the rest of this shaper. */ |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 867 | scratch = buffer->get_scratch_buffer (&scratch_size); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 868 | pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch))); |
| 869 | log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch))); |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 870 | scratch += old_scratch_used; |
| 871 | scratch_size -= old_scratch_used; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 872 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 873 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 874 | string_ref = CFStringCreateWithCharactersNoCopy (nullptr, |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 875 | pchars, chars_len, |
| 876 | kCFAllocatorNull); |
| 877 | if (unlikely (!string_ref)) |
| 878 | FAIL ("CFStringCreateWithCharactersNoCopy failed"); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 879 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 880 | /* Create an attributed string, populate it, and create a line from it, then release attributed string. */ |
| 881 | { |
Behdad Esfahbod | fd1a6aa | 2014-08-11 20:01:37 -0400 | [diff] [blame] | 882 | CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault, |
| 883 | chars_len); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 884 | if (unlikely (!attr_string)) |
| 885 | FAIL ("CFAttributedStringCreateMutable failed"); |
| 886 | CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref); |
Behdad Esfahbod | 3eb6a4d | 2014-08-12 19:10:33 -0400 | [diff] [blame] | 887 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 888 | { |
| 889 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
| 890 | kCTVerticalFormsAttributeName, kCFBooleanTrue); |
| 891 | } |
Behdad Esfahbod | 20076cc | 2014-08-12 19:26:35 -0400 | [diff] [blame] | 892 | |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 893 | if (buffer->props.language) |
| 894 | { |
Behdad Esfahbod | 20076cc | 2014-08-12 19:26:35 -0400 | [diff] [blame] | 895 | /* What's the iOS equivalent of this check? |
| 896 | * The symbols was introduced in iOS 7.0. |
| 897 | * At any rate, our fallback is safe and works fine. */ |
| 898 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090 |
| 899 | # define kCTLanguageAttributeName CFSTR ("NSLanguage") |
| 900 | #endif |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 901 | CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault, |
| 902 | hb_language_to_string (buffer->props.language), |
| 903 | kCFStringEncodingUTF8, |
| 904 | kCFAllocatorNull); |
| 905 | if (unlikely (!lang)) |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 906 | { |
| 907 | CFRelease (attr_string); |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 908 | FAIL ("CFStringCreateWithCStringNoCopy failed"); |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 909 | } |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 910 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
| 911 | kCTLanguageAttributeName, lang); |
| 912 | CFRelease (lang); |
| 913 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 914 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 915 | kCTFontAttributeName, ct_font); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 916 | |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 917 | if (num_features && range_records.len) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 918 | { |
| 919 | unsigned int start = 0; |
| 920 | range_record_t *last_range = &range_records[0]; |
| 921 | for (unsigned int k = 0; k < chars_len; k++) |
| 922 | { |
| 923 | range_record_t *range = last_range; |
| 924 | while (log_clusters[k] < range->index_first) |
| 925 | range--; |
| 926 | while (log_clusters[k] > range->index_last) |
| 927 | range++; |
| 928 | if (range != last_range) |
| 929 | { |
| 930 | if (last_range->font) |
| 931 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start), |
| 932 | kCTFontAttributeName, last_range->font); |
| 933 | |
| 934 | start = k; |
| 935 | } |
| 936 | |
| 937 | last_range = range; |
| 938 | } |
| 939 | if (start != chars_len && last_range->font) |
| 940 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start), |
| 941 | kCTFontAttributeName, last_range->font); |
| 942 | } |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 943 | /* Enable/disable kern if requested. |
| 944 | * |
| 945 | * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText. |
| 946 | */ |
| 947 | if (num_features) |
| 948 | { |
| 949 | unsigned int zeroint = 0; |
| 950 | CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint); |
| 951 | for (unsigned int i = 0; i < num_features; i++) |
| 952 | { |
| 953 | const hb_feature_t &feature = features[i]; |
| 954 | if (feature.tag == HB_TAG('k','e','r','n') && |
| 955 | feature.start < chars_len && feature.start < feature.end) |
| 956 | { |
| 957 | CFRange feature_range = CFRangeMake (feature.start, |
| 958 | MIN (feature.end, chars_len) - feature.start); |
| 959 | if (feature.value) |
| 960 | CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName); |
| 961 | else |
| 962 | CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero); |
| 963 | } |
| 964 | } |
| 965 | CFRelease (zero); |
| 966 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 967 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 968 | int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
| 969 | CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level); |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 970 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
| 971 | extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel; |
| 972 | #endif |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 973 | CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault, |
| 974 | (const void **) &kCTTypesetterOptionForcedEmbeddingLevel, |
| 975 | (const void **) &level_number, |
| 976 | 1, |
| 977 | &kCFTypeDictionaryKeyCallBacks, |
| 978 | &kCFTypeDictionaryValueCallBacks); |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 979 | CFRelease (level_number); |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 980 | if (unlikely (!options)) |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 981 | { |
| 982 | CFRelease (attr_string); |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 983 | FAIL ("CFDictionaryCreate failed"); |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 984 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 985 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 986 | CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options); |
| 987 | CFRelease (options); |
| 988 | CFRelease (attr_string); |
| 989 | if (unlikely (!typesetter)) |
| 990 | FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed"); |
| 991 | |
| 992 | line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0)); |
| 993 | CFRelease (typesetter); |
| 994 | if (unlikely (!line)) |
| 995 | FAIL ("CTTypesetterCreateLine failed"); |
| 996 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 997 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 998 | CFArrayRef glyph_runs = CTLineGetGlyphRuns (line); |
| 999 | unsigned int num_runs = CFArrayGetCount (glyph_runs); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1000 | DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1001 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1002 | buffer->len = 0; |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1003 | uint32_t status_and = ~0, status_or = 0; |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1004 | double advances_so_far = 0; |
Behdad Esfahbod | 24f17af | 2015-04-21 19:21:32 -0700 | [diff] [blame] | 1005 | /* For right-to-left runs, CoreText returns the glyphs positioned such that |
| 1006 | * any trailing whitespace is to the left of (0,0). Adjust coordinate system |
| 1007 | * to fix for that. Test with any RTL string with trailing spaces. |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 1008 | * https://crbug.com/469028 |
Behdad Esfahbod | 24f17af | 2015-04-21 19:21:32 -0700 | [diff] [blame] | 1009 | */ |
| 1010 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 1011 | { |
| 1012 | advances_so_far -= CTLineGetTrailingWhitespaceWidth (line); |
| 1013 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 1014 | advances_so_far = -advances_so_far; |
| 1015 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1016 | |
| 1017 | const CFRange range_all = CFRangeMake (0, 0); |
| 1018 | |
| 1019 | for (unsigned int i = 0; i < num_runs; i++) |
| 1020 | { |
| 1021 | CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i)); |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1022 | CTRunStatus run_status = CTRunGetStatus (run); |
| 1023 | status_or |= run_status; |
| 1024 | status_and &= run_status; |
| 1025 | DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1026 | double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1027 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 1028 | run_advance = -run_advance; |
| 1029 | DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1030 | |
| 1031 | /* CoreText does automatic font fallback (AKA "cascading") for characters |
| 1032 | * not supported by the requested font, and provides no way to turn it off, |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1033 | * so we must detect if the returned run uses a font other than the requested |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1034 | * one and fill in the buffer with .notdef glyphs instead of random glyph |
| 1035 | * indices from a different font. |
| 1036 | */ |
| 1037 | CFDictionaryRef attributes = CTRunGetAttributes (run); |
| 1038 | CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName)); |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 1039 | if (!CFEqual (run_ct_font, ct_font)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1040 | { |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1041 | /* The run doesn't use our main font instance. We have to figure out |
| 1042 | * whether font fallback happened, or this is just CoreText giving us |
| 1043 | * another CTFont using the same underlying CGFont. CoreText seems |
| 1044 | * to do that in a variety of situations, one of which being vertical |
| 1045 | * text, but also perhaps for caching reasons. |
| 1046 | * |
| 1047 | * First, see if it uses any of our subfonts created to set font features... |
| 1048 | * |
| 1049 | * Next, compare the CGFont to the one we used to create our fonts. |
| 1050 | * Even this doesn't work all the time. |
| 1051 | * |
| 1052 | * Finally, we compare PS names, which I don't think are unique... |
| 1053 | * |
| 1054 | * Looks like if we really want to be sure here we have to modify the |
| 1055 | * font to change the name table, similar to what we do in the uniscribe |
| 1056 | * backend. |
| 1057 | * |
| 1058 | * However, even that wouldn't work if we were passed in the CGFont to |
Behdad Esfahbod | 5908962 | 2016-04-04 14:54:32 -0700 | [diff] [blame] | 1059 | * construct a hb_face to begin with. |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1060 | * |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 1061 | * See: https://github.com/harfbuzz/harfbuzz/pull/36 |
Behdad Esfahbod | 5908962 | 2016-04-04 14:54:32 -0700 | [diff] [blame] | 1062 | * |
| 1063 | * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098 |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1064 | */ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1065 | bool matched = false; |
| 1066 | for (unsigned int i = 0; i < range_records.len; i++) |
| 1067 | if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font)) |
| 1068 | { |
| 1069 | matched = true; |
| 1070 | break; |
| 1071 | } |
| 1072 | if (!matched) |
| 1073 | { |
Bruce Mitchener | dae20fb | 2018-01-31 20:16:08 +0700 | [diff] [blame] | 1074 | CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1075 | if (run_cg_font) |
| 1076 | { |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 1077 | matched = CFEqual (run_cg_font, cg_font); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1078 | CFRelease (run_cg_font); |
| 1079 | } |
| 1080 | } |
| 1081 | if (!matched) |
| 1082 | { |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 1083 | CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 1084 | CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey); |
| 1085 | CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0); |
| 1086 | CFRelease (run_ps_name); |
| 1087 | CFRelease (font_ps_name); |
| 1088 | if (result == kCFCompareEqualTo) |
| 1089 | matched = true; |
| 1090 | } |
| 1091 | if (!matched) |
| 1092 | { |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1093 | CFRange range = CTRunGetStringRange (run); |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1094 | DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld", |
| 1095 | range.location, range.location + range.length); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1096 | if (!buffer->ensure_inplace (buffer->len + range.length)) |
| 1097 | goto resize_and_retry; |
| 1098 | hb_glyph_info_t *info = buffer->info + buffer->len; |
| 1099 | |
Behdad Esfahbod | b0b38bb | 2015-01-21 19:19:33 -0800 | [diff] [blame] | 1100 | hb_codepoint_t notdef = 0; |
| 1101 | hb_direction_t dir = buffer->props.direction; |
| 1102 | hb_position_t x_advance, y_advance, x_offset, y_offset; |
| 1103 | hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance); |
| 1104 | hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset); |
| 1105 | hb_position_t advance = x_advance + y_advance; |
| 1106 | x_offset = -x_offset; |
| 1107 | y_offset = -y_offset; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1108 | |
Behdad Esfahbod | 08acfe0 | 2014-08-12 18:57:08 -0400 | [diff] [blame] | 1109 | unsigned int old_len = buffer->len; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1110 | for (CFIndex j = range.location; j < range.location + range.length; j++) |
| 1111 | { |
| 1112 | UniChar ch = CFStringGetCharacterAtIndex (string_ref, j); |
| 1113 | if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j) |
| 1114 | { |
| 1115 | ch = CFStringGetCharacterAtIndex (string_ref, j - 1); |
| 1116 | if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu)) |
| 1117 | /* This is the second of a surrogate pair. Don't need .notdef |
| 1118 | * for this one. */ |
| 1119 | continue; |
| 1120 | } |
Behdad Esfahbod | 982d94e | 2015-01-28 10:51:33 -0800 | [diff] [blame] | 1121 | if (buffer->unicode->is_default_ignorable (ch)) |
| 1122 | continue; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1123 | |
| 1124 | info->codepoint = notdef; |
Behdad Esfahbod | 3c41ccb | 2014-08-11 15:11:59 -0400 | [diff] [blame] | 1125 | info->cluster = log_clusters[j]; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1126 | |
| 1127 | info->mask = advance; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1128 | info->var1.i32 = x_offset; |
| 1129 | info->var2.i32 = y_offset; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1130 | |
| 1131 | info++; |
| 1132 | buffer->len++; |
| 1133 | } |
Behdad Esfahbod | 08acfe0 | 2014-08-12 18:57:08 -0400 | [diff] [blame] | 1134 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 1135 | buffer->reverse_range (old_len, buffer->len); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1136 | advances_so_far += run_advance; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1137 | continue; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | unsigned int num_glyphs = CTRunGetGlyphCount (run); |
| 1142 | if (num_glyphs == 0) |
| 1143 | continue; |
| 1144 | |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 1145 | if (!buffer->ensure_inplace (buffer->len + num_glyphs)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1146 | goto resize_and_retry; |
| 1147 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1148 | hb_glyph_info_t *run_info = buffer->info + buffer->len; |
| 1149 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1150 | /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always |
| 1151 | * succeed, and so copying data to our own buffer will be rare. Reports |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1152 | * have it that this changed in OS X 10.10 Yosemite, and nullptr is returned |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1153 | * frequently. At any rate, we can test that codepath by setting USE_PTR |
| 1154 | * to false. */ |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1155 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1156 | #define USE_PTR true |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1157 | |
| 1158 | #define SCRATCH_SAVE() \ |
| 1159 | unsigned int scratch_size_saved = scratch_size; \ |
| 1160 | hb_buffer_t::scratch_buffer_t *scratch_saved = scratch |
| 1161 | |
| 1162 | #define SCRATCH_RESTORE() \ |
| 1163 | scratch_size = scratch_size_saved; \ |
| 1164 | scratch = scratch_saved; |
| 1165 | |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1166 | { /* Setup glyphs */ |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1167 | SCRATCH_SAVE(); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1168 | const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1169 | if (!glyphs) { |
| 1170 | ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry); |
| 1171 | CTRunGetGlyphs (run, range_all, glyph_buf); |
| 1172 | glyphs = glyph_buf; |
| 1173 | } |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1174 | const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1175 | if (!string_indices) { |
| 1176 | ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry); |
| 1177 | CTRunGetStringIndices (run, range_all, index_buf); |
| 1178 | string_indices = index_buf; |
| 1179 | } |
| 1180 | hb_glyph_info_t *info = run_info; |
| 1181 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 1182 | { |
| 1183 | info->codepoint = glyphs[j]; |
| 1184 | info->cluster = log_clusters[string_indices[j]]; |
| 1185 | info++; |
| 1186 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1187 | SCRATCH_RESTORE(); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1188 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1189 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1190 | /* Setup positions. |
| 1191 | * Note that CoreText does not return advances for glyphs. As such, |
| 1192 | * for all but last glyph, we use the delta position to next glyph as |
| 1193 | * advance (in the advance direction only), and for last glyph we set |
| 1194 | * whatever is needed to make the whole run's advance add up. */ |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1195 | SCRATCH_SAVE(); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1196 | const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1197 | if (!positions) { |
| 1198 | ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry); |
| 1199 | CTRunGetPositions (run, range_all, position_buf); |
| 1200 | positions = position_buf; |
| 1201 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1202 | hb_glyph_info_t *info = run_info; |
| 1203 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
| 1204 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1205 | hb_position_t x_offset = (positions[0].x - advances_so_far) * x_mult; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1206 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 1207 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1208 | double advance; |
| 1209 | if (likely (j + 1 < num_glyphs)) |
| 1210 | advance = positions[j + 1].x - positions[j].x; |
| 1211 | else /* last glyph */ |
| 1212 | advance = run_advance - (positions[j].x - positions[0].x); |
Behdad Esfahbod | 70622e5 | 2015-01-21 18:50:57 -0800 | [diff] [blame] | 1213 | info->mask = advance * x_mult; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1214 | info->var1.i32 = x_offset; |
| 1215 | info->var2.i32 = positions[j].y * y_mult; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1216 | info++; |
| 1217 | } |
| 1218 | } |
| 1219 | else |
| 1220 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1221 | hb_position_t y_offset = (positions[0].y - advances_so_far) * y_mult; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1222 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 1223 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1224 | double advance; |
| 1225 | if (likely (j + 1 < num_glyphs)) |
| 1226 | advance = positions[j + 1].y - positions[j].y; |
| 1227 | else /* last glyph */ |
| 1228 | advance = run_advance - (positions[j].y - positions[0].y); |
Behdad Esfahbod | 70622e5 | 2015-01-21 18:50:57 -0800 | [diff] [blame] | 1229 | info->mask = advance * y_mult; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1230 | info->var1.i32 = positions[j].x * x_mult; |
| 1231 | info->var2.i32 = y_offset; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1232 | info++; |
| 1233 | } |
| 1234 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1235 | SCRATCH_RESTORE(); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1236 | advances_so_far += run_advance; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1237 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1238 | #undef SCRATCH_RESTORE |
| 1239 | #undef SCRATCH_SAVE |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1240 | #undef USE_PTR |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1241 | #undef ALLOCATE_ARRAY |
| 1242 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1243 | buffer->len += num_glyphs; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1246 | /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel, |
Bruce Mitchener | 90218fa | 2018-01-31 20:44:45 +0700 | [diff] [blame] | 1247 | * or if it does, it doesn't respect it. So we get runs with wrong |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1248 | * directions. As such, disable the assert... It wouldn't crash, but |
| 1249 | * cursoring will be off... |
| 1250 | * |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 1251 | * https://crbug.com/419769 |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1252 | */ |
| 1253 | if (0) |
| 1254 | { |
| 1255 | /* Make sure all runs had the expected direction. */ |
| 1256 | bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
| 1257 | assert (bool (status_and & kCTRunStatusRightToLeft) == backward); |
| 1258 | assert (bool (status_or & kCTRunStatusRightToLeft) == backward); |
| 1259 | } |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1260 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1261 | buffer->clear_positions (); |
| 1262 | |
| 1263 | unsigned int count = buffer->len; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1264 | hb_glyph_info_t *info = buffer->info; |
| 1265 | hb_glyph_position_t *pos = buffer->pos; |
| 1266 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
| 1267 | for (unsigned int i = 0; i < count; i++) |
| 1268 | { |
| 1269 | pos->x_advance = info->mask; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1270 | pos->x_offset = info->var1.i32; |
| 1271 | pos->y_offset = info->var2.i32; |
Behdad Esfahbod | 239119a | 2017-08-13 15:08:34 -0700 | [diff] [blame] | 1272 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1273 | info++, pos++; |
| 1274 | } |
| 1275 | else |
| 1276 | for (unsigned int i = 0; i < count; i++) |
| 1277 | { |
| 1278 | pos->y_advance = info->mask; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1279 | pos->x_offset = info->var1.i32; |
| 1280 | pos->y_offset = info->var2.i32; |
Behdad Esfahbod | 239119a | 2017-08-13 15:08:34 -0700 | [diff] [blame] | 1281 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1282 | info++, pos++; |
| 1283 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1284 | |
| 1285 | /* Fix up clusters so that we never return out-of-order indices; |
| 1286 | * if core text has reordered glyphs, we'll merge them to the |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1287 | * beginning of the reordered cluster. CoreText is nice enough |
| 1288 | * to tell us whenever it has produced nonmonotonic results... |
| 1289 | * Note that we assume the input clusters were nonmonotonic to |
| 1290 | * begin with. |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1291 | * |
| 1292 | * This does *not* mean we'll form the same clusters as Uniscribe |
| 1293 | * or the native OT backend, only that the cluster indices will be |
| 1294 | * monotonic in the output buffer. */ |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1295 | if (count > 1 && (status_or & kCTRunStatusNonMonotonic)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1296 | { |
| 1297 | hb_glyph_info_t *info = buffer->info; |
| 1298 | if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) |
| 1299 | { |
| 1300 | unsigned int cluster = info[count - 1].cluster; |
| 1301 | for (unsigned int i = count - 1; i > 0; i--) |
| 1302 | { |
| 1303 | cluster = MIN (cluster, info[i - 1].cluster); |
| 1304 | info[i - 1].cluster = cluster; |
| 1305 | } |
| 1306 | } |
| 1307 | else |
| 1308 | { |
| 1309 | unsigned int cluster = info[0].cluster; |
| 1310 | for (unsigned int i = 1; i < count; i++) |
| 1311 | { |
| 1312 | cluster = MIN (cluster, info[i].cluster); |
| 1313 | info[i].cluster = cluster; |
| 1314 | } |
| 1315 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
Behdad Esfahbod | e4da380 | 2017-11-10 17:14:27 -0800 | [diff] [blame] | 1319 | buffer->unsafe_to_break_all (); |
| 1320 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 1321 | #undef FAIL |
| 1322 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1323 | fail: |
| 1324 | if (string_ref) |
| 1325 | CFRelease (string_ref); |
| 1326 | if (line) |
| 1327 | CFRelease (line); |
| 1328 | |
Behdad Esfahbod | 25f4fb9 | 2014-08-10 19:05:25 -0400 | [diff] [blame] | 1329 | for (unsigned int i = 0; i < range_records.len; i++) |
| 1330 | if (range_records[i].font) |
| 1331 | CFRelease (range_records[i].font); |
| 1332 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1333 | return ret; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1334 | } |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1335 | |
| 1336 | |
| 1337 | /* |
| 1338 | * AAT shaper |
| 1339 | */ |
| 1340 | |
Behdad Esfahbod | d4bb52b | 2017-02-09 14:13:25 -0800 | [diff] [blame] | 1341 | HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, face) |
| 1342 | HB_SHAPER_DATA_ENSURE_DEFINE(coretext_aat, font) |
| 1343 | |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1344 | /* |
| 1345 | * shaper face data |
| 1346 | */ |
| 1347 | |
| 1348 | struct hb_coretext_aat_shaper_face_data_t {}; |
| 1349 | |
| 1350 | hb_coretext_aat_shaper_face_data_t * |
| 1351 | _hb_coretext_aat_shaper_face_data_create (hb_face_t *face) |
| 1352 | { |
Behdad Esfahbod | 84686bf | 2017-10-11 15:02:48 +0200 | [diff] [blame] | 1353 | static const hb_tag_t tags[] = {HB_CORETEXT_TAG_MORX, HB_CORETEXT_TAG_MORT, HB_CORETEXT_TAG_KERX}; |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1354 | |
Behdad Esfahbod | 84686bf | 2017-10-11 15:02:48 +0200 | [diff] [blame] | 1355 | for (unsigned int i = 0; i < ARRAY_LENGTH (tags); i++) |
| 1356 | { |
| 1357 | hb_blob_t *blob = face->reference_table (tags[i]); |
| 1358 | if (hb_blob_get_length (blob)) |
| 1359 | { |
| 1360 | hb_blob_destroy (blob); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1361 | return hb_coretext_shaper_face_data_ensure (face) ? (hb_coretext_aat_shaper_face_data_t *) HB_SHAPER_DATA_SUCCEEDED : nullptr; |
Behdad Esfahbod | 84686bf | 2017-10-11 15:02:48 +0200 | [diff] [blame] | 1362 | } |
| 1363 | hb_blob_destroy (blob); |
| 1364 | } |
| 1365 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1366 | return nullptr; |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | void |
| 1370 | _hb_coretext_aat_shaper_face_data_destroy (hb_coretext_aat_shaper_face_data_t *data HB_UNUSED) |
| 1371 | { |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | /* |
| 1376 | * shaper font data |
| 1377 | */ |
| 1378 | |
| 1379 | struct hb_coretext_aat_shaper_font_data_t {}; |
| 1380 | |
| 1381 | hb_coretext_aat_shaper_font_data_t * |
| 1382 | _hb_coretext_aat_shaper_font_data_create (hb_font_t *font) |
| 1383 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1384 | return hb_coretext_shaper_font_data_ensure (font) ? (hb_coretext_aat_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED : nullptr; |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | void |
| 1388 | _hb_coretext_aat_shaper_font_data_destroy (hb_coretext_aat_shaper_font_data_t *data HB_UNUSED) |
| 1389 | { |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | /* |
| 1394 | * shaper shape_plan data |
| 1395 | */ |
| 1396 | |
| 1397 | struct hb_coretext_aat_shaper_shape_plan_data_t {}; |
| 1398 | |
| 1399 | hb_coretext_aat_shaper_shape_plan_data_t * |
| 1400 | _hb_coretext_aat_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, |
| 1401 | const hb_feature_t *user_features HB_UNUSED, |
Behdad Esfahbod | 72ada4f | 2016-09-10 03:57:24 -0700 | [diff] [blame] | 1402 | unsigned int num_user_features HB_UNUSED, |
| 1403 | const int *coords HB_UNUSED, |
| 1404 | unsigned int num_coords HB_UNUSED) |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1405 | { |
| 1406 | return (hb_coretext_aat_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; |
| 1407 | } |
| 1408 | |
| 1409 | void |
| 1410 | _hb_coretext_aat_shaper_shape_plan_data_destroy (hb_coretext_aat_shaper_shape_plan_data_t *data HB_UNUSED) |
| 1411 | { |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | /* |
| 1416 | * shaper |
| 1417 | */ |
| 1418 | |
| 1419 | hb_bool_t |
| 1420 | _hb_coretext_aat_shape (hb_shape_plan_t *shape_plan, |
| 1421 | hb_font_t *font, |
| 1422 | hb_buffer_t *buffer, |
| 1423 | const hb_feature_t *features, |
| 1424 | unsigned int num_features) |
| 1425 | { |
| 1426 | return _hb_coretext_shape (shape_plan, font, buffer, features, num_features); |
| 1427 | } |