hb-coretext.cc revision ac2085d4b391b0a72473ecac3dd6c22efe66833f
1/* 2 * Copyright © 2012 Mozilla Foundation. 3 * 4 * This is part of HarfBuzz, a text shaping library. 5 * 6 * Permission is hereby granted, without written agreement and without 7 * license or royalty fees, to use, copy, modify, and distribute this 8 * software and its documentation for any purpose, provided that the 9 * above copyright notice and the following two paragraphs appear in 10 * all copies of this software. 11 * 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 16 * DAMAGE. 17 * 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 23 * 24 * Mozilla Author(s): Jonathan Kew 25 */ 26 27#include "hb-private.hh" 28 29#define GlyphID GlyphID_mac 30#include <ApplicationServices/ApplicationServices.h> 31#undef GlyphID 32 33#include "hb-coretext.h" 34 35#include "hb-ot-name-table.hh" 36#include "hb-ot-tag.h" 37 38#include "hb-font-private.hh" 39#include "hb-buffer-private.hh" 40 41 42#ifndef HB_DEBUG_CORETEXT 43#define HB_DEBUG_CORETEXT (HB_DEBUG+0) 44#endif 45 46 47static hb_user_data_key_t hb_coretext_data_key; 48 49static struct hb_coretext_face_data_t { 50 CGFontRef cg_font; 51} _hb_coretext_face_data_nil = {0}; 52 53static void 54_hb_coretext_face_data_destroy (hb_coretext_face_data_t *data) 55{ 56 if (data->cg_font) 57 CFRelease (data->cg_font); 58 free (data); 59} 60 61static void 62release_data (void *info, const void *data, size_t size) 63{ 64 assert (hb_blob_get_length ((hb_blob_t *) info) == size && 65 hb_blob_get_data ((hb_blob_t *) info, NULL) == data); 66 67 hb_blob_destroy ((hb_blob_t *) info); 68} 69 70static hb_coretext_face_data_t * 71_hb_coretext_face_get_data (hb_face_t *face) 72{ 73 hb_coretext_face_data_t *data = (hb_coretext_face_data_t *) hb_face_get_user_data (face, &hb_coretext_data_key); 74 if (likely (data)) return data; 75 76 data = (hb_coretext_face_data_t *) calloc (1, sizeof (hb_coretext_face_data_t)); 77 if (unlikely (!data)) 78 return &_hb_coretext_face_data_nil; 79 80 81 hb_blob_t *blob = hb_face_reference_blob (face); 82 unsigned int blob_length; 83 const char *blob_data = hb_blob_get_data (blob, &blob_length); 84 if (unlikely (!blob_length)) 85 DEBUG_MSG (CORETEXT, face, "Face has empty blob"); 86 87 CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); 88 data->cg_font = CGFontCreateWithDataProvider (provider); 89 CGDataProviderRelease (provider); 90 91 if (unlikely (!data->cg_font)) 92 DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed"); 93 94 95 if (unlikely (!hb_face_set_user_data (face, &hb_coretext_data_key, data, 96 (hb_destroy_func_t) _hb_coretext_face_data_destroy, 97 false))) 98 { 99 _hb_coretext_face_data_destroy (data); 100 data = (hb_coretext_face_data_t *) hb_face_get_user_data (face, &hb_coretext_data_key); 101 if (data) 102 return data; 103 else 104 return &_hb_coretext_face_data_nil; 105 } 106 107 return data; 108} 109 110 111static struct hb_coretext_font_data_t { 112 CTFontRef ct_font; 113} _hb_coretext_font_data_nil = {0}; 114 115static void 116_hb_coretext_font_data_destroy (hb_coretext_font_data_t *data) 117{ 118 if (data->ct_font) 119 CFRelease (data->ct_font); 120 free (data); 121} 122 123static hb_coretext_font_data_t * 124_hb_coretext_font_get_data (hb_font_t *font) 125{ 126 hb_coretext_font_data_t *data = (hb_coretext_font_data_t *) hb_font_get_user_data (font, &hb_coretext_data_key); 127 if (likely (data)) return data; 128 129 data = (hb_coretext_font_data_t *) calloc (1, sizeof (hb_coretext_font_data_t)); 130 if (unlikely (!data)) 131 return &_hb_coretext_font_data_nil; 132 133 hb_coretext_face_data_t *face_data = _hb_coretext_face_get_data (font->face); 134 135 data->ct_font = CTFontCreateWithGraphicsFont (face_data->cg_font, font->y_scale, NULL, NULL); 136 if (unlikely (!data->ct_font)) 137 DEBUG_MSG (CORETEXT, font, "Font CTFontCreateWithGraphicsFont() failed"); 138 139 if (unlikely (!hb_font_set_user_data (font, &hb_coretext_data_key, data, 140 (hb_destroy_func_t) _hb_coretext_font_data_destroy, 141 false))) 142 { 143 _hb_coretext_font_data_destroy (data); 144 data = (hb_coretext_font_data_t *) hb_font_get_user_data (font, &hb_coretext_data_key); 145 if (data) 146 return data; 147 else 148 return &_hb_coretext_font_data_nil; 149 } 150 151 return data; 152} 153 154CTFontRef 155hb_coretext_font_get_ct_font (hb_font_t *font) 156{ 157 hb_coretext_font_data_t *font_data = _hb_coretext_font_get_data (font); 158 if (unlikely (!font_data)) 159 return 0; 160 return font_data->ct_font; 161} 162 163 164hb_bool_t 165_hb_coretext_shape (hb_font_t *font, 166 hb_buffer_t *buffer, 167 const hb_feature_t *features, 168 unsigned int num_features) 169{ 170 buffer->guess_properties (); 171 172#define FAIL(...) \ 173 HB_STMT_START { \ 174 DEBUG_MSG (CORETEXT, NULL, __VA_ARGS__); \ 175 return false; \ 176 } HB_STMT_END; 177 178 hb_coretext_face_data_t *face_data = _hb_coretext_face_get_data (font->face); 179 if (unlikely (!face_data->cg_font)) 180 FAIL ("Couldn't get face data"); 181 182 hb_coretext_font_data_t *font_data = _hb_coretext_font_get_data (font); 183 if (unlikely (!font_data->ct_font)) 184 FAIL ("Couldn't get font font"); 185 186 if (unlikely (!buffer->len)) 187 return true; 188 189 unsigned int scratch_size; 190 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); 191 192#define utf16_index() var1.u32 193 194 UniChar *pchars = (UniChar *) scratch; 195 unsigned int chars_len = 0; 196 for (unsigned int i = 0; i < buffer->len; i++) { 197 hb_codepoint_t c = buffer->info[i].codepoint; 198 buffer->info[i].utf16_index() = chars_len; 199 if (likely (c < 0x10000)) 200 pchars[chars_len++] = c; 201 else if (unlikely (c >= 0x110000)) 202 pchars[chars_len++] = 0xFFFD; 203 else { 204 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); 205 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); 206 } 207 } 208 209#undef utf16_index 210 211 CFStringRef string_ref = CFStringCreateWithCharactersNoCopy (kCFAllocatorDefault, 212 pchars, chars_len, 213 kCFAllocatorNull); 214 215 CFDictionaryRef attrs = CFDictionaryCreate (kCFAllocatorDefault, 216 (const void**) &kCTFontAttributeName, 217 (const void**) &font_data->ct_font, 218 1, // count of attributes 219 &kCFTypeDictionaryKeyCallBacks, 220 &kCFTypeDictionaryValueCallBacks); 221 222 // TODO: support features 223 224 // Now we can create an attributed string 225 CFAttributedStringRef attr_string = CFAttributedStringCreate (kCFAllocatorDefault, string_ref, attrs); 226 CFRelease (string_ref); 227 CFRelease (attrs); 228 229 // Create the CoreText line from our string, then we're done with it 230 CTLineRef line = CTLineCreateWithAttributedString (attr_string); 231 CFRelease (attr_string); 232 233 // and finally retrieve the glyph data and store into the gfxTextRun 234 CFArrayRef glyph_runs = CTLineGetGlyphRuns (line); 235 unsigned int num_runs = CFArrayGetCount (glyph_runs); 236 237 // Iterate through the glyph runs. 238 bool success = true; 239 buffer->len = 0; 240 241 const CFRange range_all = CFRangeMake (0, 0); 242 243 for (unsigned int i = 0; i < num_runs; i++) { 244 CTRunRef run = (CTRunRef) CFArrayGetValueAtIndex (glyph_runs, i); 245 246 unsigned int num_glyphs = CTRunGetGlyphCount (run); 247 if (num_glyphs == 0) 248 continue; 249 250 buffer->ensure (buffer->len + num_glyphs); 251 252 // retrieve the laid-out glyph data from the CTRun 253 254 // Testing indicates that CTRunGetGlyphsPtr (almost?) always succeeds, 255 // and so copying data to our own buffer with CTRunGetGlyphs will be 256 // extremely rare. 257 258 unsigned int scratch_size; 259 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); 260 261#define ALLOCATE_ARRAY(Type, name, len) \ 262 Type *name = (Type *) scratch; \ 263 scratch += (len) * sizeof ((name)[0]); \ 264 scratch_size -= (len) * sizeof ((name)[0]); 265 266 const CGGlyph* glyphs = CTRunGetGlyphsPtr (run); 267 if (!glyphs) { 268 ALLOCATE_ARRAY (CGGlyph, glyph_buf, num_glyphs); 269 CTRunGetGlyphs (run, range_all, glyph_buf); 270 glyphs = glyph_buf; 271 } 272 273 const CGPoint* positions = CTRunGetPositionsPtr (run); 274 if (!positions) { 275 ALLOCATE_ARRAY (CGPoint, position_buf, num_glyphs); 276 CTRunGetPositions (run, range_all, position_buf); 277 positions = position_buf; 278 } 279 280 const CFIndex* string_indices = CTRunGetStringIndicesPtr (run); 281 if (!string_indices) { 282 ALLOCATE_ARRAY (CFIndex, index_buf, num_glyphs); 283 CTRunGetStringIndices (run, range_all, index_buf); 284 string_indices = index_buf; 285 } 286 287#undef ALLOCATE_ARRAY 288 289 double run_width = CTRunGetTypographicBounds (run, range_all, NULL, NULL, NULL); 290 291 for (unsigned int j = 0; j < num_glyphs; j++) { 292 double advance = (j + 1 < num_glyphs ? positions[j + 1].x : positions[0].x + run_width) - positions[j].x; 293 294 hb_glyph_info_t *info = &buffer->info[buffer->len]; 295 hb_glyph_position_t *pos = &buffer->pos[buffer->len]; 296 297 info->codepoint = glyphs[j]; 298 info->cluster = string_indices[j]; 299 300 // currently, we do all x-positioning by setting the advance, we never use x-offset 301 info->mask = advance; 302 info->var1.u32 = 0; 303 info->var2.u32 = positions[j].y; 304 305 buffer->len++; 306 } 307 } 308 309 buffer->clear_positions (); 310 311 unsigned int count = buffer->len; 312 for (unsigned int i = 0; i < count; ++i) { 313 hb_glyph_info_t *info = &buffer->info[i]; 314 hb_glyph_position_t *pos = &buffer->pos[i]; 315 316 /* TODO vertical */ 317 pos->x_advance = info->mask; 318 pos->x_offset = info->var1.u32; 319 pos->y_offset = info->var2.u32; 320 } 321 322 // Fix up clusters so that we never return out-of-order indices; 323 // if core text has reordered glyphs, we'll merge them to the 324 // beginning of the reordered cluster. 325 // This does *not* mean we'll form the same clusters as Uniscribe 326 // or the native OT backend, only that the cluster indices will be 327 // non-decreasing in the output buffer. 328 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { 329 unsigned int prev_cluster = 0; 330 for (unsigned int i = 0; i < count; i++) { 331 unsigned int curr_cluster = buffer->info[i].cluster; 332 if (curr_cluster < prev_cluster) { 333 for (unsigned int j = i; j > 0; j--) { 334 if (buffer->info[j - 1].cluster > curr_cluster) 335 buffer->info[j - 1].cluster = curr_cluster; 336 else 337 break; 338 } 339 } 340 prev_cluster = curr_cluster; 341 } 342 } else { 343 // For RTL runs, we make them non-increasing instead. 344 unsigned int prev_cluster = (unsigned int)-1; 345 for (unsigned int i = 0; i < count; i++) { 346 unsigned int curr_cluster = buffer->info[i].cluster; 347 if (curr_cluster > prev_cluster) { 348 for (unsigned int j = i; j > 0; j--) { 349 if (buffer->info[j - 1].cluster < curr_cluster) 350 buffer->info[j - 1].cluster = curr_cluster; 351 else 352 break; 353 } 354 } 355 prev_cluster = curr_cluster; 356 } 357 } 358 359 return true; 360} 361