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