hb-ot-shape.cc revision 2fcbbdb41a322f54b61d9ce983ab54434504c5ed
1/* 2 * Copyright © 2009,2010 Red Hat, Inc. 3 * Copyright © 2010,2011,2012 Google, Inc. 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 * Red Hat Author(s): Behdad Esfahbod 26 * Google Author(s): Behdad Esfahbod 27 */ 28 29#define HB_SHAPER ot 30#define hb_ot_shaper_face_data_t hb_ot_layout_t 31#define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t 32#include "hb-shaper-impl-private.hh" 33 34#include "hb-ot-shape-private.hh" 35#include "hb-ot-shape-complex-private.hh" 36#include "hb-ot-shape-fallback-private.hh" 37#include "hb-ot-shape-normalize-private.hh" 38 39#include "hb-ot-layout-private.hh" 40#include "hb-set-private.hh" 41 42 43static hb_tag_t common_features[] = { 44 HB_TAG('c','c','m','p'), 45 HB_TAG('l','i','g','a'), 46 HB_TAG('l','o','c','l'), 47 HB_TAG('m','a','r','k'), 48 HB_TAG('m','k','m','k'), 49 HB_TAG('r','l','i','g'), 50}; 51 52 53static hb_tag_t horizontal_features[] = { 54 HB_TAG('c','a','l','t'), 55 HB_TAG('c','l','i','g'), 56 HB_TAG('c','u','r','s'), 57 HB_TAG('k','e','r','n'), 58 HB_TAG('r','c','l','t'), 59}; 60 61/* Note: 62 * Technically speaking, vrt2 and vert are mutually exclusive. 63 * According to the spec, valt and vpal are also mutually exclusive. 64 * But we apply them all for now. 65 */ 66static hb_tag_t vertical_features[] = { 67 HB_TAG('v','a','l','t'), 68 HB_TAG('v','e','r','t'), 69 HB_TAG('v','k','r','n'), 70 HB_TAG('v','p','a','l'), 71 HB_TAG('v','r','t','2'), 72}; 73 74 75 76static void 77hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner, 78 const hb_segment_properties_t *props, 79 const hb_feature_t *user_features, 80 unsigned int num_user_features) 81{ 82 hb_ot_map_builder_t *map = &planner->map; 83 84 switch (props->direction) { 85 case HB_DIRECTION_LTR: 86 map->add_bool_feature (HB_TAG ('l','t','r','a')); 87 map->add_bool_feature (HB_TAG ('l','t','r','m')); 88 break; 89 case HB_DIRECTION_RTL: 90 map->add_bool_feature (HB_TAG ('r','t','l','a')); 91 map->add_bool_feature (HB_TAG ('r','t','l','m'), false); 92 break; 93 case HB_DIRECTION_TTB: 94 case HB_DIRECTION_BTT: 95 case HB_DIRECTION_INVALID: 96 default: 97 break; 98 } 99 100#define ADD_FEATURES(array) \ 101 HB_STMT_START { \ 102 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \ 103 map->add_bool_feature (array[i]); \ 104 } HB_STMT_END 105 106 if (planner->shaper->collect_features) 107 planner->shaper->collect_features (planner); 108 109 ADD_FEATURES (common_features); 110 111 if (HB_DIRECTION_IS_HORIZONTAL (props->direction)) 112 ADD_FEATURES (horizontal_features); 113 else 114 ADD_FEATURES (vertical_features); 115 116 if (planner->shaper->override_features) 117 planner->shaper->override_features (planner); 118 119#undef ADD_FEATURES 120 121 for (unsigned int i = 0; i < num_user_features; i++) { 122 const hb_feature_t *feature = &user_features[i]; 123 map->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1)); 124 } 125} 126 127 128/* 129 * shaper face data 130 */ 131 132hb_ot_shaper_face_data_t * 133_hb_ot_shaper_face_data_create (hb_face_t *face) 134{ 135 return _hb_ot_layout_create (face); 136} 137 138void 139_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data) 140{ 141 _hb_ot_layout_destroy (data); 142} 143 144 145/* 146 * shaper font data 147 */ 148 149struct hb_ot_shaper_font_data_t {}; 150 151hb_ot_shaper_font_data_t * 152_hb_ot_shaper_font_data_create (hb_font_t *font) 153{ 154 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED; 155} 156 157void 158_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data) 159{ 160} 161 162 163/* 164 * shaper shape_plan data 165 */ 166 167hb_ot_shaper_shape_plan_data_t * 168_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan, 169 const hb_feature_t *user_features, 170 unsigned int num_user_features) 171{ 172 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t)); 173 if (unlikely (!plan)) 174 return NULL; 175 176 hb_ot_shape_planner_t planner (shape_plan); 177 178 planner.shaper = hb_ot_shape_complex_categorize (&shape_plan->props); 179 180 hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features); 181 182 planner.compile (*plan); 183 184 if (plan->shaper->data_create) { 185 plan->data = plan->shaper->data_create (plan); 186 if (unlikely (!plan->data)) 187 return NULL; 188 } 189 190 return plan; 191} 192 193void 194_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan) 195{ 196 if (plan->shaper->data_destroy) 197 plan->shaper->data_destroy (const_cast<void *> (plan->data)); 198 199 plan->finish (); 200 201 free (plan); 202} 203 204 205/* 206 * shaper 207 */ 208 209struct hb_ot_shape_context_t 210{ 211 hb_ot_shape_plan_t *plan; 212 hb_font_t *font; 213 hb_face_t *face; 214 hb_buffer_t *buffer; 215 const hb_feature_t *user_features; 216 unsigned int num_user_features; 217 218 /* Transient stuff */ 219 hb_direction_t target_direction; 220}; 221 222 223 224/* Main shaper */ 225 226 227/* Prepare */ 228 229static void 230hb_set_unicode_props (hb_buffer_t *buffer) 231{ 232 unsigned int count = buffer->len; 233 for (unsigned int i = 0; i < count; i++) 234 _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode); 235} 236 237static void 238hb_form_clusters (hb_buffer_t *buffer) 239{ 240 unsigned int count = buffer->len; 241 for (unsigned int i = 1; i < count; i++) 242 if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) & 243 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) | 244 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) | 245 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))) 246 buffer->merge_clusters (i - 1, i + 1); 247} 248 249static void 250hb_ensure_native_direction (hb_buffer_t *buffer) 251{ 252 hb_direction_t direction = buffer->props.direction; 253 254 /* TODO vertical: 255 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType 256 * Ogham fonts are supposed to be implemented BTT or not. Need to research that 257 * first. */ 258 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) || 259 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB)) 260 { 261 hb_buffer_reverse_clusters (buffer); 262 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); 263 } 264} 265 266 267/* Substitute */ 268 269static inline void 270hb_ot_mirror_chars (hb_ot_shape_context_t *c) 271{ 272 if (HB_DIRECTION_IS_FORWARD (c->target_direction)) 273 return; 274 275 hb_unicode_funcs_t *unicode = c->buffer->unicode; 276 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m')); 277 278 unsigned int count = c->buffer->len; 279 for (unsigned int i = 0; i < count; i++) { 280 hb_codepoint_t codepoint = unicode->mirroring (c->buffer->info[i].codepoint); 281 if (likely (codepoint == c->buffer->info[i].codepoint)) 282 c->buffer->info[i].mask |= rtlm_mask; 283 else 284 c->buffer->info[i].codepoint = codepoint; 285 } 286} 287 288static inline void 289hb_ot_shape_setup_masks (hb_ot_shape_context_t *c) 290{ 291 hb_ot_map_t *map = &c->plan->map; 292 293 hb_mask_t global_mask = map->get_global_mask (); 294 c->buffer->reset_masks (global_mask); 295 296 if (c->plan->shaper->setup_masks) 297 c->plan->shaper->setup_masks (c->plan, c->buffer, c->font); 298 299 for (unsigned int i = 0; i < c->num_user_features; i++) 300 { 301 const hb_feature_t *feature = &c->user_features[i]; 302 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) { 303 unsigned int shift; 304 hb_mask_t mask = map->get_mask (feature->tag, &shift); 305 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end); 306 } 307 } 308} 309 310static inline void 311hb_ot_map_glyphs_fast (hb_buffer_t *buffer) 312{ 313 /* Normalization process sets up glyph_index(), we just copy it. */ 314 unsigned int count = buffer->len; 315 for (unsigned int i = 0; i < count; i++) 316 buffer->info[i].codepoint = buffer->info[i].glyph_index(); 317} 318 319static inline void 320hb_ot_substitute_default (hb_ot_shape_context_t *c) 321{ 322 if (c->plan->shaper->preprocess_text) { 323 hb_synthesize_glyph_classes (c); /* XXX This is a hack for now. */ 324 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font); 325 } 326 327 hb_ot_mirror_chars (c); 328 329 HB_BUFFER_ALLOCATE_VAR (c->buffer, glyph_index); 330 331 _hb_ot_shape_normalize (c->font, c->buffer, 332 c->plan->shaper->normalization_preference ? 333 c->plan->shaper->normalization_preference (c->plan) : 334 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT); 335 336 hb_ot_shape_setup_masks (c); 337 338 hb_ot_map_glyphs_fast (c->buffer); 339 340 HB_BUFFER_DEALLOCATE_VAR (c->buffer, glyph_index); 341} 342 343static inline void 344hb_synthesize_glyph_classes (hb_ot_shape_context_t *c) 345{ 346 unsigned int count = c->buffer->len; 347 for (unsigned int i = 0; i < count; i++) 348 c->buffer->info[i].glyph_props() = _hb_glyph_info_get_general_category (&c->buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ? 349 HB_OT_LAYOUT_GLYPH_CLASS_MARK : 350 HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH; 351} 352 353 354static inline void 355hb_ot_substitute_complex (hb_ot_shape_context_t *c) 356{ 357 hb_ot_layout_substitute_start (c->font, c->buffer); 358 359 if (!hb_ot_layout_has_glyph_classes (c->face)) 360 hb_synthesize_glyph_classes (c); 361 362 if (hb_ot_layout_has_substitution (c->face)) 363 c->plan->substitute (c->font, c->buffer); 364 365 hb_ot_layout_substitute_finish (c->font, c->buffer); 366 367 return; 368} 369 370static inline void 371hb_ot_substitute (hb_ot_shape_context_t *c) 372{ 373 hb_ot_substitute_default (c); 374 hb_ot_substitute_complex (c); 375} 376 377/* Position */ 378 379static inline void 380hb_ot_position_default (hb_ot_shape_context_t *c) 381{ 382 hb_ot_layout_position_start (c->font, c->buffer); 383 384 unsigned int count = c->buffer->len; 385 for (unsigned int i = 0; i < count; i++) { 386 c->font->get_glyph_advance_for_direction (c->buffer->info[i].codepoint, 387 c->buffer->props.direction, 388 &c->buffer->pos[i].x_advance, 389 &c->buffer->pos[i].y_advance); 390 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint, 391 c->buffer->props.direction, 392 &c->buffer->pos[i].x_offset, 393 &c->buffer->pos[i].y_offset); 394 } 395} 396 397static inline bool 398hb_ot_position_complex (hb_ot_shape_context_t *c) 399{ 400 bool ret = false; 401 402 if (hb_ot_layout_has_positioning (c->face)) 403 { 404 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */ 405 406 unsigned int count = c->buffer->len; 407 for (unsigned int i = 0; i < count; i++) { 408 c->font->add_glyph_origin_for_direction (c->buffer->info[i].codepoint, 409 HB_DIRECTION_LTR, 410 &c->buffer->pos[i].x_offset, 411 &c->buffer->pos[i].y_offset); 412 } 413 414 c->plan->position (c->font, c->buffer); 415 416 for (unsigned int i = 0; i < count; i++) { 417 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint, 418 HB_DIRECTION_LTR, 419 &c->buffer->pos[i].x_offset, 420 &c->buffer->pos[i].y_offset); 421 } 422 423 ret = true; 424 } 425 426 hb_ot_layout_position_finish (c->font, c->buffer, c->plan->shaper->zero_width_attached_marks); 427 428 return ret; 429} 430 431static inline void 432hb_ot_position_complex_fallback (hb_ot_shape_context_t *c) 433{ 434 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer); 435} 436 437static inline void 438hb_ot_truetype_kern (hb_ot_shape_context_t *c) 439{ 440 /* TODO Check for kern=0 */ 441 unsigned int count = c->buffer->len; 442 for (unsigned int i = 1; i < count; i++) { 443 hb_position_t x_kern, y_kern, kern1, kern2; 444 c->font->get_glyph_kerning_for_direction (c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint, 445 c->buffer->props.direction, 446 &x_kern, &y_kern); 447 448 kern1 = x_kern >> 1; 449 kern2 = x_kern - kern1; 450 c->buffer->pos[i - 1].x_advance += kern1; 451 c->buffer->pos[i].x_advance += kern2; 452 c->buffer->pos[i].x_offset += kern2; 453 454 kern1 = y_kern >> 1; 455 kern2 = y_kern - kern1; 456 c->buffer->pos[i - 1].y_advance += kern1; 457 c->buffer->pos[i].y_advance += kern2; 458 c->buffer->pos[i].y_offset += kern2; 459 } 460} 461 462static inline void 463hb_position_complex_fallback_visual (hb_ot_shape_context_t *c) 464{ 465 hb_ot_truetype_kern (c); 466} 467 468static inline void 469hb_ot_position (hb_ot_shape_context_t *c) 470{ 471 hb_ot_position_default (c); 472 473 hb_bool_t fallback = !hb_ot_position_complex (c); 474 475 if (fallback) 476 hb_ot_position_complex_fallback (c); 477 478 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction)) 479 hb_buffer_reverse (c->buffer); 480 481 if (fallback) 482 hb_position_complex_fallback_visual (c); 483} 484 485 486/* Post-process */ 487 488static void 489hb_ot_hide_zerowidth (hb_ot_shape_context_t *c) 490{ 491 hb_codepoint_t space; 492 if (!c->font->get_glyph (' ', 0, &space)) 493 return; /* No point! */ 494 495 unsigned int count = c->buffer->len; 496 for (unsigned int i = 0; i < count; i++) 497 if (unlikely (!is_a_ligature (c->buffer->info[i]) && 498 _hb_glyph_info_is_zero_width (&c->buffer->info[i]))) { 499 c->buffer->info[i].codepoint = space; 500 c->buffer->pos[i].x_advance = 0; 501 c->buffer->pos[i].y_advance = 0; 502 } 503} 504 505 506/* Pull it all together! */ 507 508static void 509hb_ot_shape_internal (hb_ot_shape_context_t *c) 510{ 511 c->buffer->deallocate_var_all (); 512 513 /* Save the original direction, we use it later. */ 514 c->target_direction = c->buffer->props.direction; 515 516 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0); 517 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1); 518 519 c->buffer->clear_output (); 520 521 hb_set_unicode_props (c->buffer); 522 hb_form_clusters (c->buffer); 523 524 hb_ensure_native_direction (c->buffer); 525 526 hb_ot_substitute (c); 527 hb_ot_position (c); 528 529 hb_ot_hide_zerowidth (c); 530 531 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1); 532 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0); 533 534 c->buffer->props.direction = c->target_direction; 535 536 c->buffer->deallocate_var_all (); 537} 538 539 540hb_bool_t 541_hb_ot_shape (hb_shape_plan_t *shape_plan, 542 hb_font_t *font, 543 hb_buffer_t *buffer, 544 const hb_feature_t *features, 545 unsigned int num_features) 546{ 547 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features}; 548 hb_ot_shape_internal (&c); 549 550 return true; 551} 552 553 554 555static inline void 556hb_ot_map_glyphs_dumb (hb_font_t *font, 557 hb_buffer_t *buffer) 558{ 559 unsigned int count = buffer->len; 560 for (unsigned int i = 0; i < count; i++) 561 font->get_glyph (buffer->cur().codepoint, 0, &buffer->cur().codepoint); 562} 563 564void 565hb_ot_shape_glyphs_closure (hb_font_t *font, 566 hb_buffer_t *buffer, 567 const hb_feature_t *features, 568 unsigned int num_features, 569 hb_set_t *glyphs) 570{ 571 hb_ot_shape_plan_t plan; 572 573 buffer->guess_properties (); 574 575 /* TODO cache / ensure correct backend, etc. */ 576 hb_shape_plan_t *shape_plan = hb_shape_plan_create (font->face, &buffer->props, features, num_features, NULL); 577 578 /* TODO: normalization? have shapers do closure()? */ 579 /* TODO: Deal with mirrored chars? */ 580 hb_ot_map_glyphs_dumb (font, buffer); 581 582 /* Seed it. It's user's responsibility to have cleard glyphs 583 * if that's what they desire. */ 584 unsigned int count = buffer->len; 585 for (unsigned int i = 0; i < count; i++) 586 glyphs->add (buffer->info[i].codepoint); 587 588 /* And find transitive closure. */ 589 hb_set_t copy; 590 copy.init (); 591 592 do { 593 copy.set (glyphs); 594 HB_SHAPER_DATA_GET (shape_plan)->substitute_closure (font->face, glyphs); 595 } while (!copy.equal (glyphs)); 596 597 hb_shape_plan_destroy (shape_plan); 598} 599