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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #include "hb.hh" |
Behdad Esfahbod | aa3450c | 2019-06-17 22:41:49 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_CORETEXT |
| 32 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 33 | #include "hb-shaper-impl.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 | e0c5e0d | 2018-10-13 18:37:14 -0400 | [diff] [blame] | 36 | #include "hb-aat-layout.hh" |
Behdad Esfahbod | 6a2cbc6 | 2017-10-12 10:46:09 +0200 | [diff] [blame] | 37 | #include <math.h> |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 38 | |
Behdad Esfahbod | 04981ee | 2018-10-27 04:40:43 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * SECTION:hb-coretext |
| 42 | * @title: hb-coretext |
| 43 | * @short_description: CoreText integration |
| 44 | * @include: hb-coretext.h |
| 45 | * |
| 46 | * Functions for using HarfBuzz with the CoreText fonts. |
| 47 | **/ |
| 48 | |
Behdad Esfahbod | 06c1422 | 2017-10-11 15:29:53 +0200 | [diff] [blame] | 49 | /* https://developer.apple.com/documentation/coretext/1508745-ctfontcreatewithgraphicsfont */ |
Behdad Esfahbod | c2cf68d | 2017-10-13 10:30:19 +0200 | [diff] [blame] | 50 | #define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f |
Behdad Esfahbod | 95883fc | 2017-10-13 10:21:07 +0200 | [diff] [blame] | 51 | |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 52 | static void |
| 53 | release_table_data (void *user_data) |
| 54 | { |
| 55 | CFDataRef cf_data = reinterpret_cast<CFDataRef> (user_data); |
| 56 | CFRelease(cf_data); |
| 57 | } |
| 58 | |
| 59 | static hb_blob_t * |
Ebrahim Byagowi | 9fea6b4 | 2019-07-05 18:46:41 +0430 | [diff] [blame] | 60 | _hb_cg_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 61 | { |
| 62 | CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data); |
| 63 | CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag); |
| 64 | if (unlikely (!cf_data)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 65 | return nullptr; |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 66 | |
| 67 | const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data)); |
| 68 | const size_t length = CFDataGetLength (cf_data); |
| 69 | if (!data || !length) |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 70 | { |
| 71 | CFRelease (cf_data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 72 | return nullptr; |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 73 | } |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 74 | |
| 75 | return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY, |
| 76 | reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)), |
| 77 | release_table_data); |
| 78 | } |
| 79 | |
Behdad Esfahbod | e1b6d92 | 2017-10-11 15:51:31 +0200 | [diff] [blame] | 80 | static void |
| 81 | _hb_cg_font_release (void *data) |
| 82 | { |
| 83 | CGFontRelease ((CGFontRef) data); |
| 84 | } |
| 85 | |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 86 | |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 87 | static CTFontDescriptorRef |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 88 | get_last_resort_font_desc () |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 89 | { |
| 90 | // TODO Handle allocation failures? |
| 91 | CTFontDescriptorRef last_resort = CTFontDescriptorCreateWithNameAndSize (CFSTR("LastResort"), 0); |
| 92 | CFArrayRef cascade_list = CFArrayCreate (kCFAllocatorDefault, |
| 93 | (const void **) &last_resort, |
| 94 | 1, |
| 95 | &kCFTypeArrayCallBacks); |
| 96 | CFRelease (last_resort); |
| 97 | CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault, |
| 98 | (const void **) &kCTFontCascadeListAttribute, |
| 99 | (const void **) &cascade_list, |
| 100 | 1, |
| 101 | &kCFTypeDictionaryKeyCallBacks, |
| 102 | &kCFTypeDictionaryValueCallBacks); |
| 103 | CFRelease (cascade_list); |
| 104 | |
| 105 | CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes); |
| 106 | CFRelease (attributes); |
| 107 | return font_desc; |
| 108 | } |
| 109 | |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 110 | static void |
| 111 | release_data (void *info, const void *data, size_t size) |
| 112 | { |
| 113 | assert (hb_blob_get_length ((hb_blob_t *) info) == size && |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 114 | hb_blob_get_data ((hb_blob_t *) info, nullptr) == data); |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 115 | |
| 116 | hb_blob_destroy ((hb_blob_t *) info); |
| 117 | } |
| 118 | |
| 119 | static CGFontRef |
| 120 | create_cg_font (hb_face_t *face) |
| 121 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 122 | CGFontRef cg_font = nullptr; |
Behdad Esfahbod | e1b6d92 | 2017-10-11 15:51:31 +0200 | [diff] [blame] | 123 | if (face->destroy == _hb_cg_font_release) |
Behdad Esfahbod | ba3d49d | 2016-02-22 15:50:12 +0900 | [diff] [blame] | 124 | { |
| 125 | cg_font = CGFontRetain ((CGFontRef) face->user_data); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | hb_blob_t *blob = hb_face_reference_blob (face); |
| 130 | unsigned int blob_length; |
| 131 | const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 132 | if (unlikely (!blob_length)) |
| 133 | DEBUG_MSG (CORETEXT, face, "Face has empty blob"); |
| 134 | |
| 135 | CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); |
| 136 | if (likely (provider)) |
| 137 | { |
| 138 | cg_font = CGFontCreateWithDataProvider (provider); |
| 139 | if (unlikely (!cg_font)) |
| 140 | DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed"); |
| 141 | CGDataProviderRelease (provider); |
| 142 | } |
| 143 | } |
| 144 | return cg_font; |
| 145 | } |
| 146 | |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 147 | static CTFontRef |
| 148 | create_ct_font (CGFontRef cg_font, CGFloat font_size) |
| 149 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 150 | CTFontRef ct_font = nullptr; |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 151 | |
| 152 | /* CoreText does not enable trak table usage / tracking when creating a CTFont |
| 153 | * using CTFontCreateWithGraphicsFont. The only way of enabling tracking seems |
| 154 | * to be through the CTFontCreateUIFontForLanguage call. */ |
| 155 | CFStringRef cg_postscript_name = CGFontCopyPostScriptName (cg_font); |
| 156 | if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) || |
| 157 | CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay"))) |
| 158 | { |
Ken | fdfa3d2 | 2019-02-10 23:46:05 -0800 | [diff] [blame] | 159 | #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1080 |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 160 | # define kCTFontUIFontSystem kCTFontSystemFontType |
| 161 | # define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType |
| 162 | #endif |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 163 | CTFontUIFontType font_type = kCTFontUIFontSystem; |
| 164 | if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold"))) |
| 165 | font_type = kCTFontUIFontEmphasizedSystem; |
| 166 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 167 | ct_font = CTFontCreateUIFontForLanguage (font_type, font_size, nullptr); |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 168 | CFStringRef ct_result_name = CTFontCopyPostScriptName(ct_font); |
| 169 | if (CFStringCompare (ct_result_name, cg_postscript_name, 0) != kCFCompareEqualTo) |
| 170 | { |
| 171 | CFRelease(ct_font); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 172 | ct_font = nullptr; |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 173 | } |
| 174 | CFRelease (ct_result_name); |
| 175 | } |
| 176 | CFRelease (cg_postscript_name); |
| 177 | |
| 178 | if (!ct_font) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 179 | ct_font = CTFontCreateWithGraphicsFont (cg_font, font_size, nullptr, nullptr); |
Dominik Röttsches | dd4b321 | 2017-10-12 11:49:37 +0200 | [diff] [blame] | 180 | |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 181 | if (unlikely (!ct_font)) { |
| 182 | DEBUG_MSG (CORETEXT, cg_font, "Font CTFontCreateWithGraphicsFont() failed"); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 183 | return nullptr; |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 184 | } |
Behdad Esfahbod | 489acf6 | 2016-07-22 17:41:43 -0700 | [diff] [blame] | 185 | |
| 186 | /* crbug.com/576941 and crbug.com/625902 and the investigation in the latter |
| 187 | * bug indicate that the cascade list reconfiguration occasionally causes |
| 188 | * crashes in CoreText on OS X 10.9, thus let's skip this step on older |
Dominik Röttsches | b717cd7 | 2016-09-07 23:56:57 +0300 | [diff] [blame] | 189 | * operating system versions. Except for the emoji font, where _not_ |
| 190 | * reconfiguring the cascade list causes CoreText crashes. For details, see |
| 191 | * crbug.com/549610 */ |
Ebrahim Byagowi | fc4e671 | 2016-09-09 23:28:28 +0430 | [diff] [blame] | 192 | // 0x00070000 stands for "kCTVersionNumber10_10", see CoreText.h |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 193 | if (&CTGetCoreTextVersion != nullptr && CTGetCoreTextVersion() < 0x00070000) { |
Dominik Röttsches | b717cd7 | 2016-09-07 23:56:57 +0300 | [diff] [blame] | 194 | CFStringRef fontName = CTFontCopyPostScriptName (ct_font); |
| 195 | bool isEmojiFont = CFStringCompare (fontName, CFSTR("AppleColorEmoji"), 0) == kCFCompareEqualTo; |
| 196 | CFRelease (fontName); |
| 197 | if (!isEmojiFont) |
| 198 | return ct_font; |
| 199 | } |
Behdad Esfahbod | 489acf6 | 2016-07-22 17:41:43 -0700 | [diff] [blame] | 200 | |
Ebrahim Byagowi | 8b0d642 | 2018-04-23 18:37:35 +0430 | [diff] [blame] | 201 | CFURLRef original_url = nullptr; |
Ken | fdfa3d2 | 2019-02-10 23:46:05 -0800 | [diff] [blame] | 202 | #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 203 | ATSFontRef atsFont; |
| 204 | FSRef fsref; |
| 205 | OSStatus status; |
| 206 | atsFont = CTFontGetPlatformFont (ct_font, NULL); |
| 207 | status = ATSFontGetFileReference (atsFont, &fsref); |
| 208 | if (status == noErr) |
| 209 | original_url = CFURLCreateFromFSRef (NULL, &fsref); |
| 210 | #else |
| 211 | original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute); |
| 212 | #endif |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 213 | |
| 214 | /* Create font copy with cascade list that has LastResort first; this speeds up CoreText |
| 215 | * font fallback which we don't need anyway. */ |
| 216 | { |
Behdad Esfahbod | 90194ef | 2016-02-22 15:42:53 +0900 | [diff] [blame] | 217 | CTFontDescriptorRef last_resort_font_desc = get_last_resort_font_desc (); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 218 | 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] | 219 | CFRelease (last_resort_font_desc); |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 220 | if (new_ct_font) |
| 221 | { |
Behdad Esfahbod | fc9de44 | 2016-06-30 09:46:52 -0700 | [diff] [blame] | 222 | /* The CTFontCreateCopyWithAttributes call fails to stay on the same font |
| 223 | * when reconfiguring the cascade list and may switch to a different font |
| 224 | * when there are fonts that go by the same name, since the descriptor is |
| 225 | * just name and size. |
| 226 | * |
| 227 | * Avoid reconfiguring the cascade lists if the new font is outside the |
| 228 | * system locations that we cannot access from the sandboxed renderer |
| 229 | * process in Blink. This can be detected by the new file URL location |
| 230 | * that the newly found font points to. */ |
Ebrahim Byagowi | 8b0d642 | 2018-04-23 18:37:35 +0430 | [diff] [blame] | 231 | CFURLRef new_url = nullptr; |
Ken | fdfa3d2 | 2019-02-10 23:46:05 -0800 | [diff] [blame] | 232 | #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 233 | atsFont = CTFontGetPlatformFont (new_ct_font, NULL); |
| 234 | status = ATSFontGetFileReference (atsFont, &fsref); |
| 235 | if (status == noErr) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 236 | new_url = CFURLCreateFromFSRef (NULL, &fsref); |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 237 | #else |
| 238 | new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute); |
| 239 | #endif |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 240 | // Keep reconfigured font if URL cannot be retrieved (seems to be the case |
| 241 | // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606 |
| 242 | if (!original_url || !new_url || CFEqual (original_url, new_url)) { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 243 | CFRelease (ct_font); |
| 244 | ct_font = new_ct_font; |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 245 | } else { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 246 | CFRelease (new_ct_font); |
| 247 | DEBUG_MSG (CORETEXT, ct_font, "Discarding reconfigured CTFont, location changed."); |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 248 | } |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 249 | if (new_url) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 250 | CFRelease (new_url); |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | DEBUG_MSG (CORETEXT, ct_font, "Font copy with empty cascade list failed"); |
| 254 | } |
| 255 | |
Ebrahim Byagowi | 8744212 | 2016-07-12 03:49:21 +0430 | [diff] [blame] | 256 | if (original_url) |
| 257 | CFRelease (original_url); |
Dominik Röttsches | a022327 | 2016-06-16 14:19:39 +0200 | [diff] [blame] | 258 | return ct_font; |
Behdad Esfahbod | e561122 | 2016-02-22 15:28:37 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 261 | hb_coretext_face_data_t * |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 262 | _hb_coretext_shaper_face_data_create (hb_face_t *face) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 263 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 264 | CGFontRef cg_font = create_cg_font (face); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 265 | |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 266 | if (unlikely (!cg_font)) |
Behdad Esfahbod | a9e25e9 | 2014-03-14 19:55:46 -0700 | [diff] [blame] | 267 | { |
Behdad Esfahbod | 15063b1 | 2016-02-22 15:56:29 +0900 | [diff] [blame] | 268 | DEBUG_MSG (CORETEXT, face, "CGFont creation failed.."); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 269 | return nullptr; |
Behdad Esfahbod | 15063b1 | 2016-02-22 15:56:29 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 272 | return (hb_coretext_face_data_t *) cg_font; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 275 | void |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 276 | _hb_coretext_shaper_face_data_destroy (hb_coretext_face_data_t *data) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 277 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 278 | CFRelease ((CGFontRef) data); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 281 | hb_face_t * |
| 282 | hb_coretext_face_create (CGFontRef cg_font) |
| 283 | { |
Ebrahim Byagowi | 9fea6b4 | 2019-07-05 18:46:41 +0430 | [diff] [blame] | 284 | return hb_face_create_for_tables (_hb_cg_reference_table, CGFontRetain (cg_font), _hb_cg_font_release); |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 287 | /* |
| 288 | * Since: 0.9.10 |
| 289 | */ |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 290 | CGFontRef |
Behdad Esfahbod | e923e64 | 2012-12-09 19:39:40 -0500 | [diff] [blame] | 291 | hb_coretext_face_get_cg_font (hb_face_t *face) |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 292 | { |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 293 | return (CGFontRef) (const void *) face->data.coretext; |
Behdad Esfahbod | 9a83958 | 2012-12-09 18:47:36 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 296 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 297 | hb_coretext_font_data_t * |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 298 | _hb_coretext_shaper_font_data_create (hb_font_t *font) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 299 | { |
Dominik Röttsches | db7a73c | 2017-10-11 13:24:39 +0200 | [diff] [blame] | 300 | hb_face_t *face = font->face; |
Behdad Esfahbod | 0809b76 | 2018-11-16 08:29:47 -0500 | [diff] [blame] | 301 | const hb_coretext_face_data_t *face_data = face->data.coretext; |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 302 | if (unlikely (!face_data)) return nullptr; |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 303 | CGFontRef cg_font = (CGFontRef) (const void *) face->data.coretext; |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 304 | |
Ebrahim Byagowi | f3214df | 2019-09-14 10:56:00 +0430 | [diff] [blame] | 305 | CGFloat font_size = (CGFloat) (font->ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : font->ptem); |
Tor Arne Vestbø | f401f85 | 2018-12-17 00:48:35 +0100 | [diff] [blame] | 306 | CTFontRef ct_font = create_ct_font (cg_font, font_size); |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 307 | |
| 308 | if (unlikely (!ct_font)) |
| 309 | { |
| 310 | DEBUG_MSG (CORETEXT, font, "CGFont creation failed.."); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 311 | return nullptr; |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 314 | return (hb_coretext_font_data_t *) ct_font; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 317 | void |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 318 | _hb_coretext_shaper_font_data_destroy (hb_coretext_font_data_t *data) |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 319 | { |
Behdad Esfahbod | f334130 | 2017-10-11 13:17:46 +0200 | [diff] [blame] | 320 | CFRelease ((CTFontRef) data); |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 323 | static const hb_coretext_font_data_t * |
| 324 | hb_coretext_font_data_sync (hb_font_t *font) |
| 325 | { |
| 326 | retry: |
Behdad Esfahbod | 0809b76 | 2018-11-16 08:29:47 -0500 | [diff] [blame] | 327 | const hb_coretext_font_data_t *data = font->data.coretext; |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 328 | if (unlikely (!data)) return nullptr; |
| 329 | |
Ebrahim Byagowi | f3214df | 2019-09-14 10:56:00 +0430 | [diff] [blame] | 330 | if (fabs (CTFontGetSize ((CTFontRef) data) - (CGFloat) font->ptem) > .5) |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 331 | { |
| 332 | /* XXX-MT-bug |
| 333 | * Note that evaluating condition above can be dangerous if another thread |
| 334 | * got here first and destructed data. That's, as always, bad use pattern. |
| 335 | * If you modify the font (change font size), other threads must not be |
| 336 | * using it at the same time. However, since this check is delayed to |
| 337 | * when one actually tries to shape something, this is a XXX race condition |
| 338 | * (and the only one we have that I know of) right now. Ie. you modify the |
| 339 | * font size in one thread, then (supposedly safely) try to use it from two |
| 340 | * or more threads and BOOM! I'm not sure how to fix this. We want RCU. |
| 341 | */ |
| 342 | |
| 343 | /* Drop and recreate. */ |
| 344 | /* If someone dropped it in the mean time, throw it away and don't touch it. |
| 345 | * Otherwise, destruct it. */ |
Behdad Esfahbod | 50d1a41 | 2018-11-16 08:52:57 -0500 | [diff] [blame] | 346 | if (likely (font->data.coretext.cmpexch (const_cast<hb_coretext_font_data_t *> (data), nullptr))) |
Behdad Esfahbod | 0aab861 | 2018-11-16 08:43:25 -0500 | [diff] [blame] | 347 | _hb_coretext_shaper_font_data_destroy (const_cast<hb_coretext_font_data_t *> (data)); |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 348 | else |
| 349 | goto retry; |
| 350 | } |
| 351 | return font->data.coretext; |
| 352 | } |
| 353 | |
| 354 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 355 | /* |
| 356 | * Since: 1.7.2 |
| 357 | */ |
| 358 | hb_font_t * |
| 359 | hb_coretext_font_create (CTFontRef ct_font) |
| 360 | { |
Bruce Mitchener | dae20fb | 2018-01-31 20:16:08 +0700 | [diff] [blame] | 361 | CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr); |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 362 | hb_face_t *face = hb_coretext_face_create (cg_font); |
| 363 | CFRelease (cg_font); |
| 364 | hb_font_t *font = hb_font_create (face); |
| 365 | hb_face_destroy (face); |
| 366 | |
Behdad Esfahbod | 93ef20a | 2018-11-03 15:03:06 -0400 | [diff] [blame] | 367 | if (unlikely (hb_object_is_immutable (font))) |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 368 | return font; |
| 369 | |
Tor Arne Vestbø | f401f85 | 2018-12-17 00:48:35 +0100 | [diff] [blame] | 370 | hb_font_set_ptem (font, CTFontGetSize (ct_font)); |
Behdad Esfahbod | 1245395 | 2017-12-17 12:32:33 -0500 | [diff] [blame] | 371 | |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 372 | /* Let there be dragons here... */ |
Behdad Esfahbod | 0aab861 | 2018-11-16 08:43:25 -0500 | [diff] [blame] | 373 | font->data.coretext.cmpexch (nullptr, (hb_coretext_font_data_t *) CFRetain (ct_font)); |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 374 | |
| 375 | return font; |
| 376 | } |
| 377 | |
| 378 | CTFontRef |
| 379 | hb_coretext_font_get_ct_font (hb_font_t *font) |
| 380 | { |
Behdad Esfahbod | 0aab861 | 2018-11-16 08:43:25 -0500 | [diff] [blame] | 381 | const hb_coretext_font_data_t *data = hb_coretext_font_data_sync (font); |
Behdad Esfahbod | cfb9771 | 2018-11-16 03:24:22 -0500 | [diff] [blame] | 382 | return data ? (CTFontRef) data : nullptr; |
Behdad Esfahbod | d5e2930 | 2017-11-28 23:11:34 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 386 | /* |
| 387 | * shaper |
| 388 | */ |
| 389 | |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 390 | struct feature_record_t { |
| 391 | unsigned int feature; |
| 392 | unsigned int setting; |
| 393 | }; |
| 394 | |
| 395 | struct active_feature_t { |
| 396 | feature_record_t rec; |
| 397 | unsigned int order; |
| 398 | |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 399 | HB_INTERNAL static int cmp (const void *pa, const void *pb) { |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 400 | const active_feature_t *a = (const active_feature_t *) pa; |
| 401 | const active_feature_t *b = (const active_feature_t *) pb; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 402 | return a->rec.feature < b->rec.feature ? -1 : a->rec.feature > b->rec.feature ? 1 : |
| 403 | a->order < b->order ? -1 : a->order > b->order ? 1 : |
| 404 | a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 : |
| 405 | 0; |
| 406 | } |
| 407 | bool operator== (const active_feature_t *f) { |
| 408 | return cmp (this, f) == 0; |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | struct feature_event_t { |
| 413 | unsigned int index; |
| 414 | bool start; |
| 415 | active_feature_t feature; |
| 416 | |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 417 | HB_INTERNAL static int cmp (const void *pa, const void *pb) { |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 418 | const feature_event_t *a = (const feature_event_t *) pa; |
| 419 | const feature_event_t *b = (const feature_event_t *) pb; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 420 | return a->index < b->index ? -1 : a->index > b->index ? 1 : |
| 421 | a->start < b->start ? -1 : a->start > b->start ? 1 : |
| 422 | active_feature_t::cmp (&a->feature, &b->feature); |
| 423 | } |
| 424 | }; |
| 425 | |
| 426 | struct range_record_t { |
| 427 | CTFontRef font; |
| 428 | unsigned int index_first; /* == start */ |
| 429 | unsigned int index_last; /* == end - 1 */ |
| 430 | }; |
| 431 | |
| 432 | |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 433 | hb_bool_t |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 434 | _hb_coretext_shape (hb_shape_plan_t *shape_plan, |
| 435 | hb_font_t *font, |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 436 | hb_buffer_t *buffer, |
| 437 | const hb_feature_t *features, |
| 438 | unsigned int num_features) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 439 | { |
Behdad Esfahbod | 301168d | 2012-07-30 17:48:04 -0400 | [diff] [blame] | 440 | hb_face_t *face = font->face; |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 441 | CGFontRef cg_font = (CGFontRef) (const void *) face->data.coretext; |
Behdad Esfahbod | 0809b76 | 2018-11-16 08:29:47 -0500 | [diff] [blame] | 442 | CTFontRef ct_font = (CTFontRef) hb_coretext_font_data_sync (font); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 443 | |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 444 | CGFloat ct_font_size = CTFontGetSize (ct_font); |
Behdad Esfahbod | 061105e | 2016-02-22 14:59:39 +0900 | [diff] [blame] | 445 | CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size; |
| 446 | CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size; |
| 447 | |
Behdad Esfahbod | 624a299 | 2014-08-11 15:29:18 -0400 | [diff] [blame] | 448 | /* Attach marks to their bases, to match the 'ot' shaper. |
Behdad Esfahbod | 03fb6dd | 2018-10-03 21:02:16 +0200 | [diff] [blame] | 449 | * Adapted from a very old version of hb-ot-shape:hb_form_clusters(). |
Behdad Esfahbod | 624a299 | 2014-08-11 15:29:18 -0400 | [diff] [blame] | 450 | * Note that this only makes us be closer to the 'ot' shaper, |
| 451 | * but by no means the same. For example, if there's |
| 452 | * B1 M1 B2 M2, and B1-B2 form a ligature, M2's cluster will |
| 453 | * continue pointing to B2 even though B2 was merged into B1's |
| 454 | * cluster... */ |
Behdad Esfahbod | 62c2711 | 2016-02-22 15:07:20 +0900 | [diff] [blame] | 455 | if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES) |
Behdad Esfahbod | 624a299 | 2014-08-11 15:29:18 -0400 | [diff] [blame] | 456 | { |
| 457 | hb_unicode_funcs_t *unicode = buffer->unicode; |
| 458 | unsigned int count = buffer->len; |
| 459 | hb_glyph_info_t *info = buffer->info; |
| 460 | for (unsigned int i = 1; i < count; i++) |
| 461 | if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint))) |
| 462 | buffer->merge_clusters (i - 1, i + 1); |
| 463 | } |
| 464 | |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 465 | hb_vector_t<feature_record_t> feature_records; |
| 466 | hb_vector_t<range_record_t> range_records; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 467 | |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 468 | /* |
| 469 | * Set up features. |
| 470 | * (copied + modified from code from hb-uniscribe.cc) |
| 471 | */ |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 472 | if (num_features) |
| 473 | { |
| 474 | /* Sort features by start/end events. */ |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 475 | hb_vector_t<feature_event_t> feature_events; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 476 | for (unsigned int i = 0; i < num_features; i++) |
| 477 | { |
Behdad Esfahbod | cb05774 | 2018-10-13 17:03:32 -0400 | [diff] [blame] | 478 | const hb_aat_feature_mapping_t * mapping = hb_aat_layout_find_feature_mapping (features[i].tag); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 479 | if (!mapping) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 480 | continue; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 481 | |
| 482 | active_feature_t feature; |
| 483 | feature.rec.feature = mapping->aatFeatureType; |
| 484 | feature.rec.setting = features[i].value ? mapping->selectorToEnable : mapping->selectorToDisable; |
| 485 | feature.order = i; |
| 486 | |
| 487 | feature_event_t *event; |
| 488 | |
| 489 | event = feature_events.push (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 490 | event->index = features[i].start; |
| 491 | event->start = true; |
| 492 | event->feature = feature; |
| 493 | |
| 494 | event = feature_events.push (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 495 | event->index = features[i].end; |
| 496 | event->start = false; |
| 497 | event->feature = feature; |
| 498 | } |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 499 | feature_events.qsort (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 500 | /* Add a strategic final event. */ |
| 501 | { |
| 502 | active_feature_t feature; |
| 503 | feature.rec.feature = HB_TAG_NONE; |
| 504 | feature.rec.setting = 0; |
| 505 | feature.order = num_features + 1; |
| 506 | |
| 507 | feature_event_t *event = feature_events.push (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 508 | event->index = 0; /* This value does magic. */ |
| 509 | event->start = false; |
| 510 | event->feature = feature; |
| 511 | } |
| 512 | |
| 513 | /* Scan events and save features for each range. */ |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 514 | hb_vector_t<active_feature_t> active_features; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 515 | unsigned int last_index = 0; |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 516 | for (unsigned int i = 0; i < feature_events.length; i++) |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 517 | { |
| 518 | feature_event_t *event = &feature_events[i]; |
| 519 | |
| 520 | if (event->index != last_index) |
| 521 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 522 | /* Save a snapshot of active features and the range. */ |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 523 | range_record_t *range = range_records.push (); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 524 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 525 | if (active_features.length) |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 526 | { |
| 527 | CFMutableArrayRef features_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 528 | |
| 529 | /* TODO sort and resolve conflicting features? */ |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 530 | /* active_features.qsort (); */ |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 531 | for (unsigned int j = 0; j < active_features.length; j++) |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 532 | { |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 533 | CFStringRef keys[] = { |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 534 | kCTFontFeatureTypeIdentifierKey, |
| 535 | kCTFontFeatureSelectorIdentifierKey |
| 536 | }; |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 537 | CFNumberRef values[] = { |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 538 | CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.feature), |
| 539 | CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &active_features[j].rec.setting) |
| 540 | }; |
Behdad Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 541 | static_assert ((ARRAY_LENGTH_CONST (keys) == ARRAY_LENGTH_CONST (values)), ""); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 542 | CFDictionaryRef dict = CFDictionaryCreate (kCFAllocatorDefault, |
| 543 | (const void **) keys, |
| 544 | (const void **) values, |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 545 | ARRAY_LENGTH (keys), |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 546 | &kCFTypeDictionaryKeyCallBacks, |
| 547 | &kCFTypeDictionaryValueCallBacks); |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 548 | for (unsigned int i = 0; i < ARRAY_LENGTH (values); i++) |
| 549 | CFRelease (values[i]); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 550 | |
| 551 | CFArrayAppendValue (features_array, dict); |
| 552 | CFRelease (dict); |
| 553 | |
| 554 | } |
| 555 | |
| 556 | CFDictionaryRef attributes = CFDictionaryCreate (kCFAllocatorDefault, |
| 557 | (const void **) &kCTFontFeatureSettingsAttribute, |
| 558 | (const void **) &features_array, |
| 559 | 1, |
| 560 | &kCFTypeDictionaryKeyCallBacks, |
| 561 | &kCFTypeDictionaryValueCallBacks); |
| 562 | CFRelease (features_array); |
| 563 | |
| 564 | CTFontDescriptorRef font_desc = CTFontDescriptorCreateWithAttributes (attributes); |
| 565 | CFRelease (attributes); |
| 566 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 567 | range->font = CTFontCreateCopyWithAttributes (ct_font, 0.0, nullptr, font_desc); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 568 | CFRelease (font_desc); |
| 569 | } |
| 570 | else |
| 571 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 572 | range->font = nullptr; |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | range->index_first = last_index; |
| 576 | range->index_last = event->index - 1; |
| 577 | |
| 578 | last_index = event->index; |
| 579 | } |
| 580 | |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 581 | if (event->start) |
| 582 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 583 | active_features.push (event->feature); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 584 | } else { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 585 | active_feature_t *feature = active_features.find (&event->feature); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 586 | if (feature) |
Behdad Esfahbod | 2fb3a83 | 2019-05-10 18:40:29 -0700 | [diff] [blame] | 587 | active_features.remove (feature - active_features.arrayZ); |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 588 | } |
| 589 | } |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 590 | } |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 591 | |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 592 | unsigned int scratch_size; |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 593 | 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] | 594 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 595 | #define ALLOCATE_ARRAY(Type, name, len, on_no_room) \ |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 596 | Type *name = (Type *) scratch; \ |
Behdad Esfahbod | 68e12e6 | 2019-05-13 17:28:59 -0700 | [diff] [blame] | 597 | do { \ |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 598 | unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 599 | if (unlikely (_consumed > scratch_size)) \ |
| 600 | { \ |
| 601 | on_no_room; \ |
| 602 | assert (0); \ |
| 603 | } \ |
Behdad Esfahbod | 8fcadb9 | 2013-11-13 14:33:57 -0500 | [diff] [blame] | 604 | scratch += _consumed; \ |
| 605 | scratch_size -= _consumed; \ |
Behdad Esfahbod | 68e12e6 | 2019-05-13 17:28:59 -0700 | [diff] [blame] | 606 | } while (0) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 607 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 608 | ALLOCATE_ARRAY (UniChar, pchars, buffer->len * 2, /*nothing*/); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 609 | unsigned int chars_len = 0; |
| 610 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 611 | hb_codepoint_t c = buffer->info[i].codepoint; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 612 | if (likely (c <= 0xFFFFu)) |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 613 | pchars[chars_len++] = c; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 614 | else if (unlikely (c > 0x10FFFFu)) |
| 615 | pchars[chars_len++] = 0xFFFDu; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 616 | else { |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 617 | pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 618 | pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1)); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 622 | ALLOCATE_ARRAY (unsigned int, log_clusters, chars_len, /*nothing*/); |
Behdad Esfahbod | 9b3c60c | 2014-08-11 13:25:43 -0400 | [diff] [blame] | 623 | chars_len = 0; |
| 624 | for (unsigned int i = 0; i < buffer->len; i++) |
| 625 | { |
| 626 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 627 | unsigned int cluster = buffer->info[i].cluster; |
| 628 | log_clusters[chars_len++] = cluster; |
| 629 | if (hb_in_range (c, 0x10000u, 0x10FFFFu)) |
| 630 | log_clusters[chars_len++] = cluster; /* Surrogates. */ |
| 631 | } |
| 632 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 633 | #define FAIL(...) \ |
| 634 | HB_STMT_START { \ |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 635 | DEBUG_MSG (CORETEXT, nullptr, __VA_ARGS__); \ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 636 | ret = false; \ |
| 637 | goto fail; \ |
Behdad Esfahbod | 10bac21 | 2019-06-18 15:15:06 -0700 | [diff] [blame] | 638 | } HB_STMT_END |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 639 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 640 | bool ret = true; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 641 | CFStringRef string_ref = nullptr; |
| 642 | CTLineRef line = nullptr; |
Behdad Esfahbod | a782a5e | 2013-08-07 21:08:54 -0400 | [diff] [blame] | 643 | |
Bruce Mitchener | 8d1e479 | 2018-10-18 22:18:42 +0700 | [diff] [blame] | 644 | if (false) |
Behdad Esfahbod | a782a5e | 2013-08-07 21:08:54 -0400 | [diff] [blame] | 645 | { |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 646 | resize_and_retry: |
Behdad Esfahbod | 1b55077 | 2014-08-11 20:45:12 -0400 | [diff] [blame] | 647 | DEBUG_MSG (CORETEXT, buffer, "Buffer resize"); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 648 | /* string_ref uses the scratch-buffer for backing store, and line references |
| 649 | * string_ref (via attr_string). We must release those before resizing buffer. */ |
| 650 | assert (string_ref); |
| 651 | assert (line); |
| 652 | CFRelease (string_ref); |
| 653 | CFRelease (line); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 654 | string_ref = nullptr; |
| 655 | line = nullptr; |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 656 | |
| 657 | /* Get previous start-of-scratch-area, that we use later for readjusting |
| 658 | * our existing scratch arrays. */ |
| 659 | unsigned int old_scratch_used; |
| 660 | hb_buffer_t::scratch_buffer_t *old_scratch; |
| 661 | old_scratch = buffer->get_scratch_buffer (&old_scratch_used); |
| 662 | old_scratch_used = scratch - old_scratch; |
| 663 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 664 | if (unlikely (!buffer->ensure (buffer->allocated * 2))) |
Behdad Esfahbod | b9993d8 | 2014-08-10 17:40:24 -0400 | [diff] [blame] | 665 | FAIL ("Buffer resize failed"); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 666 | |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 667 | /* Adjust scratch, pchars, and log_cluster arrays. This is ugly, but really the |
| 668 | * cleanest way to do without completely restructuring the rest of this shaper. */ |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 669 | scratch = buffer->get_scratch_buffer (&scratch_size); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 670 | pchars = reinterpret_cast<UniChar *> (((char *) scratch + ((char *) pchars - (char *) old_scratch))); |
| 671 | log_clusters = reinterpret_cast<unsigned int *> (((char *) scratch + ((char *) log_clusters - (char *) old_scratch))); |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 672 | scratch += old_scratch_used; |
| 673 | scratch_size -= old_scratch_used; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 674 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 675 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 676 | string_ref = CFStringCreateWithCharactersNoCopy (nullptr, |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 677 | pchars, chars_len, |
| 678 | kCFAllocatorNull); |
| 679 | if (unlikely (!string_ref)) |
| 680 | FAIL ("CFStringCreateWithCharactersNoCopy failed"); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 681 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 682 | /* Create an attributed string, populate it, and create a line from it, then release attributed string. */ |
| 683 | { |
Behdad Esfahbod | fd1a6aa | 2014-08-11 20:01:37 -0400 | [diff] [blame] | 684 | CFMutableAttributedStringRef attr_string = CFAttributedStringCreateMutable (kCFAllocatorDefault, |
| 685 | chars_len); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 686 | if (unlikely (!attr_string)) |
| 687 | FAIL ("CFAttributedStringCreateMutable failed"); |
| 688 | CFAttributedStringReplaceString (attr_string, CFRangeMake (0, 0), string_ref); |
Behdad Esfahbod | 3eb6a4d | 2014-08-12 19:10:33 -0400 | [diff] [blame] | 689 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 690 | { |
| 691 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
| 692 | kCTVerticalFormsAttributeName, kCFBooleanTrue); |
| 693 | } |
Behdad Esfahbod | 20076cc | 2014-08-12 19:26:35 -0400 | [diff] [blame] | 694 | |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 695 | if (buffer->props.language) |
| 696 | { |
Behdad Esfahbod | 20076cc | 2014-08-12 19:26:35 -0400 | [diff] [blame] | 697 | /* What's the iOS equivalent of this check? |
| 698 | * The symbols was introduced in iOS 7.0. |
| 699 | * At any rate, our fallback is safe and works fine. */ |
Ken | fdfa3d2 | 2019-02-10 23:46:05 -0800 | [diff] [blame] | 700 | #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1090 |
Behdad Esfahbod | 20076cc | 2014-08-12 19:26:35 -0400 | [diff] [blame] | 701 | # define kCTLanguageAttributeName CFSTR ("NSLanguage") |
| 702 | #endif |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 703 | CFStringRef lang = CFStringCreateWithCStringNoCopy (kCFAllocatorDefault, |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 704 | hb_language_to_string (buffer->props.language), |
| 705 | kCFStringEncodingUTF8, |
| 706 | kCFAllocatorNull); |
| 707 | if (unlikely (!lang)) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 708 | { |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 709 | CFRelease (attr_string); |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 710 | FAIL ("CFStringCreateWithCStringNoCopy failed"); |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 711 | } |
Behdad Esfahbod | 1b3011c | 2014-08-12 19:17:19 -0400 | [diff] [blame] | 712 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
| 713 | kCTLanguageAttributeName, lang); |
| 714 | CFRelease (lang); |
| 715 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 716 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len), |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 717 | kCTFontAttributeName, ct_font); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 718 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 719 | if (num_features && range_records.length) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 720 | { |
| 721 | unsigned int start = 0; |
| 722 | range_record_t *last_range = &range_records[0]; |
| 723 | for (unsigned int k = 0; k < chars_len; k++) |
| 724 | { |
| 725 | range_record_t *range = last_range; |
| 726 | while (log_clusters[k] < range->index_first) |
| 727 | range--; |
| 728 | while (log_clusters[k] > range->index_last) |
| 729 | range++; |
| 730 | if (range != last_range) |
| 731 | { |
| 732 | if (last_range->font) |
| 733 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, k - start), |
| 734 | kCTFontAttributeName, last_range->font); |
| 735 | |
| 736 | start = k; |
| 737 | } |
| 738 | |
| 739 | last_range = range; |
| 740 | } |
| 741 | if (start != chars_len && last_range->font) |
| 742 | CFAttributedStringSetAttribute (attr_string, CFRangeMake (start, chars_len - start), |
| 743 | kCTFontAttributeName, last_range->font); |
| 744 | } |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 745 | /* Enable/disable kern if requested. |
| 746 | * |
| 747 | * Note: once kern is disabled, reenabling it doesn't currently seem to work in CoreText. |
| 748 | */ |
| 749 | if (num_features) |
| 750 | { |
| 751 | unsigned int zeroint = 0; |
| 752 | CFNumberRef zero = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &zeroint); |
| 753 | for (unsigned int i = 0; i < num_features; i++) |
| 754 | { |
| 755 | const hb_feature_t &feature = features[i]; |
| 756 | if (feature.tag == HB_TAG('k','e','r','n') && |
| 757 | feature.start < chars_len && feature.start < feature.end) |
| 758 | { |
| 759 | CFRange feature_range = CFRangeMake (feature.start, |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 760 | hb_min (feature.end, chars_len) - feature.start); |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 761 | if (feature.value) |
| 762 | CFAttributedStringRemoveAttribute (attr_string, feature_range, kCTKernAttributeName); |
| 763 | else |
| 764 | CFAttributedStringSetAttribute (attr_string, feature_range, kCTKernAttributeName, zero); |
| 765 | } |
| 766 | } |
| 767 | CFRelease (zero); |
| 768 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 769 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 770 | int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
| 771 | CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level); |
Ken | fdfa3d2 | 2019-02-10 23:46:05 -0800 | [diff] [blame] | 772 | #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
Ryan Schmidt | 58e569e | 2018-04-05 17:03:36 -0500 | [diff] [blame] | 773 | extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel; |
| 774 | #endif |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 775 | CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault, |
| 776 | (const void **) &kCTTypesetterOptionForcedEmbeddingLevel, |
| 777 | (const void **) &level_number, |
| 778 | 1, |
| 779 | &kCFTypeDictionaryKeyCallBacks, |
| 780 | &kCFTypeDictionaryValueCallBacks); |
Cosimo Lupo | 9813be3 | 2017-07-14 17:11:46 +0100 | [diff] [blame] | 781 | CFRelease (level_number); |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 782 | if (unlikely (!options)) |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 783 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 784 | CFRelease (attr_string); |
| 785 | FAIL ("CFDictionaryCreate failed"); |
Bruce Mitchener | 0c66043 | 2018-01-31 20:24:27 +0700 | [diff] [blame] | 786 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 787 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 788 | CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options); |
| 789 | CFRelease (options); |
| 790 | CFRelease (attr_string); |
| 791 | if (unlikely (!typesetter)) |
| 792 | FAIL ("CTTypesetterCreateWithAttributedStringAndOptions failed"); |
| 793 | |
| 794 | line = CTTypesetterCreateLine (typesetter, CFRangeMake(0, 0)); |
| 795 | CFRelease (typesetter); |
| 796 | if (unlikely (!line)) |
| 797 | FAIL ("CTTypesetterCreateLine failed"); |
| 798 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 799 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 800 | CFArrayRef glyph_runs = CTLineGetGlyphRuns (line); |
| 801 | unsigned int num_runs = CFArrayGetCount (glyph_runs); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 802 | DEBUG_MSG (CORETEXT, nullptr, "Num runs: %d", num_runs); |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 803 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 804 | buffer->len = 0; |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 805 | uint32_t status_and = ~0, status_or = 0; |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 806 | double advances_so_far = 0; |
Behdad Esfahbod | 24f17af | 2015-04-21 19:21:32 -0700 | [diff] [blame] | 807 | /* For right-to-left runs, CoreText returns the glyphs positioned such that |
| 808 | * any trailing whitespace is to the left of (0,0). Adjust coordinate system |
| 809 | * to fix for that. Test with any RTL string with trailing spaces. |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 810 | * https://crbug.com/469028 |
Behdad Esfahbod | 24f17af | 2015-04-21 19:21:32 -0700 | [diff] [blame] | 811 | */ |
| 812 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 813 | { |
| 814 | advances_so_far -= CTLineGetTrailingWhitespaceWidth (line); |
| 815 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 816 | advances_so_far = -advances_so_far; |
| 817 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 818 | |
| 819 | const CFRange range_all = CFRangeMake (0, 0); |
| 820 | |
| 821 | for (unsigned int i = 0; i < num_runs; i++) |
| 822 | { |
| 823 | CTRunRef run = static_cast<CTRunRef>(CFArrayGetValueAtIndex (glyph_runs, i)); |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 824 | CTRunStatus run_status = CTRunGetStatus (run); |
| 825 | status_or |= run_status; |
| 826 | status_and &= run_status; |
| 827 | DEBUG_MSG (CORETEXT, run, "CTRunStatus: %x", run_status); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 828 | double run_advance = CTRunGetTypographicBounds (run, range_all, nullptr, nullptr, nullptr); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 829 | if (HB_DIRECTION_IS_VERTICAL (buffer->props.direction)) |
| 830 | run_advance = -run_advance; |
| 831 | DEBUG_MSG (CORETEXT, run, "Run advance: %g", run_advance); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 832 | |
| 833 | /* CoreText does automatic font fallback (AKA "cascading") for characters |
| 834 | * not supported by the requested font, and provides no way to turn it off, |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 835 | * 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] | 836 | * one and fill in the buffer with .notdef glyphs instead of random glyph |
| 837 | * indices from a different font. |
| 838 | */ |
| 839 | CFDictionaryRef attributes = CTRunGetAttributes (run); |
| 840 | CTFontRef run_ct_font = static_cast<CTFontRef>(CFDictionaryGetValue (attributes, kCTFontAttributeName)); |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 841 | if (!CFEqual (run_ct_font, ct_font)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 842 | { |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 843 | /* The run doesn't use our main font instance. We have to figure out |
| 844 | * whether font fallback happened, or this is just CoreText giving us |
| 845 | * another CTFont using the same underlying CGFont. CoreText seems |
| 846 | * to do that in a variety of situations, one of which being vertical |
| 847 | * text, but also perhaps for caching reasons. |
| 848 | * |
| 849 | * First, see if it uses any of our subfonts created to set font features... |
| 850 | * |
| 851 | * Next, compare the CGFont to the one we used to create our fonts. |
| 852 | * Even this doesn't work all the time. |
| 853 | * |
| 854 | * Finally, we compare PS names, which I don't think are unique... |
| 855 | * |
| 856 | * Looks like if we really want to be sure here we have to modify the |
| 857 | * font to change the name table, similar to what we do in the uniscribe |
| 858 | * backend. |
| 859 | * |
| 860 | * However, even that wouldn't work if we were passed in the CGFont to |
Behdad Esfahbod | 5908962 | 2016-04-04 14:54:32 -0700 | [diff] [blame] | 861 | * construct a hb_face to begin with. |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 862 | * |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 863 | * See: https://github.com/harfbuzz/harfbuzz/pull/36 |
Behdad Esfahbod | 5908962 | 2016-04-04 14:54:32 -0700 | [diff] [blame] | 864 | * |
| 865 | * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098 |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 866 | */ |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 867 | bool matched = false; |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 868 | for (unsigned int i = 0; i < range_records.length; i++) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 869 | if (range_records[i].font && CFEqual (run_ct_font, range_records[i].font)) |
| 870 | { |
| 871 | matched = true; |
| 872 | break; |
| 873 | } |
| 874 | if (!matched) |
| 875 | { |
Bruce Mitchener | dae20fb | 2018-01-31 20:16:08 +0700 | [diff] [blame] | 876 | CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 877 | if (run_cg_font) |
| 878 | { |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 879 | matched = CFEqual (run_cg_font, cg_font); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 880 | CFRelease (run_cg_font); |
| 881 | } |
| 882 | } |
| 883 | if (!matched) |
| 884 | { |
Behdad Esfahbod | a8e466c | 2017-10-11 13:05:59 +0200 | [diff] [blame] | 885 | CFStringRef font_ps_name = CTFontCopyName (ct_font, kCTFontPostScriptNameKey); |
Behdad Esfahbod | fd0001d | 2014-08-12 10:32:41 -0400 | [diff] [blame] | 886 | CFStringRef run_ps_name = CTFontCopyName (run_ct_font, kCTFontPostScriptNameKey); |
| 887 | CFComparisonResult result = CFStringCompare (run_ps_name, font_ps_name, 0); |
| 888 | CFRelease (run_ps_name); |
| 889 | CFRelease (font_ps_name); |
| 890 | if (result == kCFCompareEqualTo) |
| 891 | matched = true; |
| 892 | } |
| 893 | if (!matched) |
| 894 | { |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 895 | CFRange range = CTRunGetStringRange (run); |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 896 | DEBUG_MSG (CORETEXT, run, "Run used fallback font: %ld..%ld", |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 897 | range.location, range.location + range.length); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 898 | if (!buffer->ensure_inplace (buffer->len + range.length)) |
| 899 | goto resize_and_retry; |
| 900 | hb_glyph_info_t *info = buffer->info + buffer->len; |
| 901 | |
Behdad Esfahbod | b0b38bb | 2015-01-21 19:19:33 -0800 | [diff] [blame] | 902 | hb_codepoint_t notdef = 0; |
| 903 | hb_direction_t dir = buffer->props.direction; |
| 904 | hb_position_t x_advance, y_advance, x_offset, y_offset; |
| 905 | hb_font_get_glyph_advance_for_direction (font, notdef, dir, &x_advance, &y_advance); |
| 906 | hb_font_get_glyph_origin_for_direction (font, notdef, dir, &x_offset, &y_offset); |
| 907 | hb_position_t advance = x_advance + y_advance; |
| 908 | x_offset = -x_offset; |
| 909 | y_offset = -y_offset; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 910 | |
Behdad Esfahbod | 08acfe0 | 2014-08-12 18:57:08 -0400 | [diff] [blame] | 911 | unsigned int old_len = buffer->len; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 912 | for (CFIndex j = range.location; j < range.location + range.length; j++) |
| 913 | { |
| 914 | UniChar ch = CFStringGetCharacterAtIndex (string_ref, j); |
| 915 | if (hb_in_range<UniChar> (ch, 0xDC00u, 0xDFFFu) && range.location < j) |
| 916 | { |
| 917 | ch = CFStringGetCharacterAtIndex (string_ref, j - 1); |
| 918 | if (hb_in_range<UniChar> (ch, 0xD800u, 0xDBFFu)) |
| 919 | /* This is the second of a surrogate pair. Don't need .notdef |
| 920 | * for this one. */ |
| 921 | continue; |
| 922 | } |
Behdad Esfahbod | 982d94e | 2015-01-28 10:51:33 -0800 | [diff] [blame] | 923 | if (buffer->unicode->is_default_ignorable (ch)) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 924 | continue; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 925 | |
| 926 | info->codepoint = notdef; |
Behdad Esfahbod | 3c41ccb | 2014-08-11 15:11:59 -0400 | [diff] [blame] | 927 | info->cluster = log_clusters[j]; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 928 | |
| 929 | info->mask = advance; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 930 | info->var1.i32 = x_offset; |
| 931 | info->var2.i32 = y_offset; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 932 | |
| 933 | info++; |
| 934 | buffer->len++; |
| 935 | } |
Behdad Esfahbod | 08acfe0 | 2014-08-12 18:57:08 -0400 | [diff] [blame] | 936 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 937 | buffer->reverse_range (old_len, buffer->len); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 938 | advances_so_far += run_advance; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 939 | continue; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | unsigned int num_glyphs = CTRunGetGlyphCount (run); |
| 944 | if (num_glyphs == 0) |
| 945 | continue; |
| 946 | |
Behdad Esfahbod | 81b8d97 | 2014-08-12 15:49:47 -0400 | [diff] [blame] | 947 | if (!buffer->ensure_inplace (buffer->len + num_glyphs)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 948 | goto resize_and_retry; |
| 949 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 950 | hb_glyph_info_t *run_info = buffer->info + buffer->len; |
| 951 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 952 | /* Testing used to indicate that CTRunGetGlyphsPtr, etc (almost?) always |
| 953 | * 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] | 954 | * 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] | 955 | * frequently. At any rate, we can test that codepath by setting USE_PTR |
| 956 | * to false. */ |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 957 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 958 | #define USE_PTR true |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 959 | |
| 960 | #define SCRATCH_SAVE() \ |
| 961 | unsigned int scratch_size_saved = scratch_size; \ |
| 962 | hb_buffer_t::scratch_buffer_t *scratch_saved = scratch |
| 963 | |
| 964 | #define SCRATCH_RESTORE() \ |
| 965 | scratch_size = scratch_size_saved; \ |
Misty De Meo | e710888 | 2019-06-18 15:20:38 -0700 | [diff] [blame] | 966 | scratch = scratch_saved |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 967 | |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 968 | { /* Setup glyphs */ |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 969 | SCRATCH_SAVE(); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 970 | const CGGlyph* glyphs = USE_PTR ? CTRunGetGlyphsPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 971 | if (!glyphs) { |
| 972 | ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs, goto resize_and_retry); |
| 973 | CTRunGetGlyphs (run, range_all, glyph_buf); |
| 974 | glyphs = glyph_buf; |
| 975 | } |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 976 | const CFIndex* string_indices = USE_PTR ? CTRunGetStringIndicesPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 977 | if (!string_indices) { |
| 978 | ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs, goto resize_and_retry); |
| 979 | CTRunGetStringIndices (run, range_all, index_buf); |
| 980 | string_indices = index_buf; |
| 981 | } |
| 982 | hb_glyph_info_t *info = run_info; |
| 983 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 984 | { |
| 985 | info->codepoint = glyphs[j]; |
| 986 | info->cluster = log_clusters[string_indices[j]]; |
| 987 | info++; |
| 988 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 989 | SCRATCH_RESTORE(); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 990 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 991 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 992 | /* Setup positions. |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 993 | * Note that CoreText does not return advances for glyphs. As such, |
| 994 | * for all but last glyph, we use the delta position to next glyph as |
| 995 | * advance (in the advance direction only), and for last glyph we set |
| 996 | * whatever is needed to make the whole run's advance add up. */ |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 997 | SCRATCH_SAVE(); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 998 | const CGPoint* positions = USE_PTR ? CTRunGetPositionsPtr (run) : nullptr; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 999 | if (!positions) { |
| 1000 | ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs, goto resize_and_retry); |
| 1001 | CTRunGetPositions (run, range_all, position_buf); |
| 1002 | positions = position_buf; |
| 1003 | } |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1004 | hb_glyph_info_t *info = run_info; |
| 1005 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
| 1006 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1007 | 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] | 1008 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 1009 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1010 | double advance; |
| 1011 | if (likely (j + 1 < num_glyphs)) |
| 1012 | advance = positions[j + 1].x - positions[j].x; |
| 1013 | else /* last glyph */ |
| 1014 | advance = run_advance - (positions[j].x - positions[0].x); |
Behdad Esfahbod | 70622e5 | 2015-01-21 18:50:57 -0800 | [diff] [blame] | 1015 | info->mask = advance * x_mult; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1016 | info->var1.i32 = x_offset; |
| 1017 | info->var2.i32 = positions[j].y * y_mult; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1018 | info++; |
| 1019 | } |
| 1020 | } |
| 1021 | else |
| 1022 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1023 | 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] | 1024 | for (unsigned int j = 0; j < num_glyphs; j++) |
| 1025 | { |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1026 | double advance; |
| 1027 | if (likely (j + 1 < num_glyphs)) |
| 1028 | advance = positions[j + 1].y - positions[j].y; |
| 1029 | else /* last glyph */ |
| 1030 | advance = run_advance - (positions[j].y - positions[0].y); |
Behdad Esfahbod | 70622e5 | 2015-01-21 18:50:57 -0800 | [diff] [blame] | 1031 | info->mask = advance * y_mult; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1032 | info->var1.i32 = positions[j].x * x_mult; |
| 1033 | info->var2.i32 = y_offset; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1034 | info++; |
| 1035 | } |
| 1036 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1037 | SCRATCH_RESTORE(); |
Behdad Esfahbod | 6917a04 | 2015-01-28 10:43:32 -0800 | [diff] [blame] | 1038 | advances_so_far += run_advance; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1039 | } |
Behdad Esfahbod | c3e924f | 2014-08-12 14:25:11 -0400 | [diff] [blame] | 1040 | #undef SCRATCH_RESTORE |
| 1041 | #undef SCRATCH_SAVE |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1042 | #undef USE_PTR |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1043 | #undef ALLOCATE_ARRAY |
| 1044 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1045 | buffer->len += num_glyphs; |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1048 | /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel, |
Bruce Mitchener | 90218fa | 2018-01-31 20:44:45 +0700 | [diff] [blame] | 1049 | * 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] | 1050 | * directions. As such, disable the assert... It wouldn't crash, but |
| 1051 | * cursoring will be off... |
| 1052 | * |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 1053 | * https://crbug.com/419769 |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1054 | */ |
Bruce Mitchener | 8d1e479 | 2018-10-18 22:18:42 +0700 | [diff] [blame] | 1055 | if (false) |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1056 | { |
| 1057 | /* Make sure all runs had the expected direction. */ |
Behdad Esfahbod | c5509be | 2019-04-08 14:50:58 -0400 | [diff] [blame] | 1058 | HB_UNUSED bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
Behdad Esfahbod | 50ad778 | 2015-08-18 10:22:16 +0100 | [diff] [blame] | 1059 | assert (bool (status_and & kCTRunStatusRightToLeft) == backward); |
| 1060 | assert (bool (status_or & kCTRunStatusRightToLeft) == backward); |
| 1061 | } |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1062 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1063 | buffer->clear_positions (); |
| 1064 | |
| 1065 | unsigned int count = buffer->len; |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1066 | hb_glyph_info_t *info = buffer->info; |
| 1067 | hb_glyph_position_t *pos = buffer->pos; |
| 1068 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
| 1069 | for (unsigned int i = 0; i < count; i++) |
| 1070 | { |
| 1071 | pos->x_advance = info->mask; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1072 | pos->x_offset = info->var1.i32; |
| 1073 | pos->y_offset = info->var2.i32; |
Behdad Esfahbod | 239119a | 2017-08-13 15:08:34 -0700 | [diff] [blame] | 1074 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1075 | info++, pos++; |
| 1076 | } |
| 1077 | else |
| 1078 | for (unsigned int i = 0; i < count; i++) |
| 1079 | { |
| 1080 | pos->y_advance = info->mask; |
Behdad Esfahbod | ed6962c | 2015-08-20 15:39:53 +0100 | [diff] [blame] | 1081 | pos->x_offset = info->var1.i32; |
| 1082 | pos->y_offset = info->var2.i32; |
Behdad Esfahbod | 239119a | 2017-08-13 15:08:34 -0700 | [diff] [blame] | 1083 | |
Behdad Esfahbod | 5a0eed3 | 2014-08-11 23:47:16 -0400 | [diff] [blame] | 1084 | info++, pos++; |
| 1085 | } |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1086 | |
| 1087 | /* Fix up clusters so that we never return out-of-order indices; |
| 1088 | * if core text has reordered glyphs, we'll merge them to the |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1089 | * beginning of the reordered cluster. CoreText is nice enough |
| 1090 | * to tell us whenever it has produced nonmonotonic results... |
| 1091 | * Note that we assume the input clusters were nonmonotonic to |
| 1092 | * begin with. |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1093 | * |
| 1094 | * This does *not* mean we'll form the same clusters as Uniscribe |
| 1095 | * or the native OT backend, only that the cluster indices will be |
| 1096 | * monotonic in the output buffer. */ |
Behdad Esfahbod | 10b1104 | 2014-08-11 20:02:45 -0400 | [diff] [blame] | 1097 | if (count > 1 && (status_or & kCTRunStatusNonMonotonic)) |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1098 | { |
| 1099 | hb_glyph_info_t *info = buffer->info; |
| 1100 | if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) |
| 1101 | { |
| 1102 | unsigned int cluster = info[count - 1].cluster; |
| 1103 | for (unsigned int i = count - 1; i > 0; i--) |
| 1104 | { |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1105 | cluster = hb_min (cluster, info[i - 1].cluster); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1106 | info[i - 1].cluster = cluster; |
| 1107 | } |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | unsigned int cluster = info[0].cluster; |
| 1112 | for (unsigned int i = 1; i < count; i++) |
| 1113 | { |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1114 | cluster = hb_min (cluster, info[i].cluster); |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1115 | info[i].cluster = cluster; |
| 1116 | } |
| 1117 | } |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Behdad Esfahbod | e4da380 | 2017-11-10 17:14:27 -0800 | [diff] [blame] | 1121 | buffer->unsafe_to_break_all (); |
| 1122 | |
Behdad Esfahbod | 4acce77 | 2014-08-11 17:46:50 -0400 | [diff] [blame] | 1123 | #undef FAIL |
| 1124 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1125 | fail: |
| 1126 | if (string_ref) |
| 1127 | CFRelease (string_ref); |
| 1128 | if (line) |
| 1129 | CFRelease (line); |
| 1130 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 1131 | for (unsigned int i = 0; i < range_records.length; i++) |
Behdad Esfahbod | 25f4fb9 | 2014-08-10 19:05:25 -0400 | [diff] [blame] | 1132 | if (range_records[i].font) |
| 1133 | CFRelease (range_records[i].font); |
| 1134 | |
Behdad Esfahbod | a6b8dc8 | 2014-08-11 15:08:19 -0400 | [diff] [blame] | 1135 | return ret; |
Jonathan Kew | aa6d849 | 2012-07-24 15:52:32 -0400 | [diff] [blame] | 1136 | } |
Behdad Esfahbod | c79865f | 2014-03-14 19:37:55 -0400 | [diff] [blame] | 1137 | |
| 1138 | |
Behdad Esfahbod | aa3450c | 2019-06-17 22:41:49 -0700 | [diff] [blame] | 1139 | #endif |