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