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_global_bool_feature (HB_TAG ('l','t','r','a')); 87 map->add_global_bool_feature (HB_TAG ('l','t','r','m')); 88 break; 89 case HB_DIRECTION_RTL: 90 map->add_global_bool_feature (HB_TAG ('r','t','l','a')); 91 map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE); 92 break; 93 case HB_DIRECTION_TTB: 94 case HB_DIRECTION_BTT: 95 case HB_DIRECTION_INVALID: 96 default: 97 break; 98 } 99 100 if (planner->shaper->collect_features) 101 planner->shaper->collect_features (planner); 102 103 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++) 104 map->add_global_bool_feature (common_features[i]); 105 106 if (HB_DIRECTION_IS_HORIZONTAL (props->direction)) 107 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++) 108 map->add_feature (horizontal_features[i], 1, F_GLOBAL | 109 (horizontal_features[i] == HB_TAG('k','e','r','n') ? 110 F_HAS_FALLBACK : F_NONE)); 111 else 112 for (unsigned int i = 0; i < ARRAY_LENGTH (vertical_features); i++) 113 map->add_feature (vertical_features[i], 1, F_GLOBAL | 114 (vertical_features[i] == HB_TAG('v','k','r','n') ? 115 F_HAS_FALLBACK : F_NONE)); 116 117 if (planner->shaper->override_features) 118 planner->shaper->override_features (planner); 119 120 for (unsigned int i = 0; i < num_user_features; i++) { 121 const hb_feature_t *feature = &user_features[i]; 122 map->add_feature (feature->tag, feature->value, 123 (feature->start == 0 && feature->end == (unsigned int) -1) ? 124 F_GLOBAL : F_NONE); 125 } 126} 127 128 129/* 130 * shaper face data 131 */ 132 133hb_ot_shaper_face_data_t * 134_hb_ot_shaper_face_data_create (hb_face_t *face) 135{ 136 return _hb_ot_layout_create (face); 137} 138 139void 140_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data) 141{ 142 _hb_ot_layout_destroy (data); 143} 144 145 146/* 147 * shaper font data 148 */ 149 150struct hb_ot_shaper_font_data_t {}; 151 152hb_ot_shaper_font_data_t * 153_hb_ot_shaper_font_data_create (hb_font_t *font) 154{ 155 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED; 156} 157 158void 159_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data) 160{ 161} 162 163 164/* 165 * shaper shape_plan data 166 */ 167 168hb_ot_shaper_shape_plan_data_t * 169_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan, 170 const hb_feature_t *user_features, 171 unsigned int num_user_features) 172{ 173 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t)); 174 if (unlikely (!plan)) 175 return NULL; 176 177 hb_ot_shape_planner_t planner (shape_plan); 178 179 planner.shaper = hb_ot_shape_complex_categorize (&planner); 180 181 hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features); 182 183 planner.compile (*plan); 184 185 if (plan->shaper->data_create) { 186 plan->data = plan->shaper->data_create (plan); 187 if (unlikely (!plan->data)) 188 return NULL; 189 } 190 191 return plan; 192} 193 194void 195_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan) 196{ 197 if (plan->shaper->data_destroy) 198 plan->shaper->data_destroy (const_cast<void *> (plan->data)); 199 200 plan->finish (); 201 202 free (plan); 203} 204 205 206/* 207 * shaper 208 */ 209 210struct hb_ot_shape_context_t 211{ 212 hb_ot_shape_plan_t *plan; 213 hb_font_t *font; 214 hb_face_t *face; 215 hb_buffer_t *buffer; 216 const hb_feature_t *user_features; 217 unsigned int num_user_features; 218 219 /* Transient stuff */ 220 hb_direction_t target_direction; 221}; 222 223 224 225/* Main shaper */ 226 227 228/* Prepare */ 229 230static void 231hb_set_unicode_props (hb_buffer_t *buffer) 232{ 233 unsigned int count = buffer->len; 234 for (unsigned int i = 0; i < count; i++) 235 _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode); 236} 237 238static void 239hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font) 240{ 241 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) || 242 _hb_glyph_info_get_general_category (&buffer->info[0]) != 243 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) 244 return; 245 246 hb_codepoint_t dottedcircle_glyph; 247 if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph)) 248 return; 249 250 hb_glyph_info_t dottedcircle; 251 dottedcircle.codepoint = 0x25CC; 252 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer->unicode); 253 254 buffer->clear_output (); 255 256 buffer->idx = 0; 257 hb_glyph_info_t info = dottedcircle; 258 info.cluster = buffer->cur().cluster; 259 info.mask = buffer->cur().mask; 260 buffer->output_info (info); 261 while (buffer->idx < buffer->len) 262 buffer->next_glyph (); 263 264 buffer->swap_buffers (); 265} 266 267static void 268hb_form_clusters (hb_buffer_t *buffer) 269{ 270 unsigned int count = buffer->len; 271 for (unsigned int i = 1; i < count; i++) 272 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[i]))) 273 buffer->merge_clusters (i - 1, i + 1); 274} 275 276static void 277hb_ensure_native_direction (hb_buffer_t *buffer) 278{ 279 hb_direction_t direction = buffer->props.direction; 280 281 /* TODO vertical: 282 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType 283 * Ogham fonts are supposed to be implemented BTT or not. Need to research that 284 * first. */ 285 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) || 286 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB)) 287 { 288 hb_buffer_reverse_clusters (buffer); 289 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); 290 } 291} 292 293 294/* Substitute */ 295 296static inline void 297hb_ot_mirror_chars (hb_ot_shape_context_t *c) 298{ 299 if (HB_DIRECTION_IS_FORWARD (c->target_direction)) 300 return; 301 302 hb_unicode_funcs_t *unicode = c->buffer->unicode; 303 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m')); 304 305 unsigned int count = c->buffer->len; 306 for (unsigned int i = 0; i < count; i++) { 307 hb_codepoint_t codepoint = unicode->mirroring (c->buffer->info[i].codepoint); 308 if (likely (codepoint == c->buffer->info[i].codepoint)) 309 c->buffer->info[i].mask |= rtlm_mask; 310 else 311 c->buffer->info[i].codepoint = codepoint; 312 } 313} 314 315static inline void 316hb_ot_shape_setup_masks (hb_ot_shape_context_t *c) 317{ 318 hb_ot_map_t *map = &c->plan->map; 319 320 hb_mask_t global_mask = map->get_global_mask (); 321 c->buffer->reset_masks (global_mask); 322 323 if (c->plan->shaper->setup_masks) 324 c->plan->shaper->setup_masks (c->plan, c->buffer, c->font); 325 326 for (unsigned int i = 0; i < c->num_user_features; i++) 327 { 328 const hb_feature_t *feature = &c->user_features[i]; 329 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) { 330 unsigned int shift; 331 hb_mask_t mask = map->get_mask (feature->tag, &shift); 332 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end); 333 } 334 } 335} 336 337static inline void 338hb_ot_map_glyphs_fast (hb_buffer_t *buffer) 339{ 340 /* Normalization process sets up glyph_index(), we just copy it. */ 341 unsigned int count = buffer->len; 342 for (unsigned int i = 0; i < count; i++) 343 buffer->info[i].codepoint = buffer->info[i].glyph_index(); 344} 345 346static inline void 347hb_synthesize_glyph_classes (hb_ot_shape_context_t *c) 348{ 349 unsigned int count = c->buffer->len; 350 for (unsigned int i = 0; i < count; i++) 351 c->buffer->info[i].glyph_props() = _hb_glyph_info_get_general_category (&c->buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ? 352 HB_OT_LAYOUT_GLYPH_PROPS_MARK : 353 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH; 354} 355 356static inline void 357hb_ot_substitute_default (hb_ot_shape_context_t *c) 358{ 359 if (c->plan->shaper->preprocess_text) 360 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font); 361 362 hb_ot_mirror_chars (c); 363 364 HB_BUFFER_ALLOCATE_VAR (c->buffer, glyph_index); 365 366 _hb_ot_shape_normalize (c->plan, c->buffer, c->font); 367 368 hb_ot_shape_setup_masks (c); 369 370 /* This is unfortunate to go here, but necessary... */ 371 if (!hb_ot_layout_has_positioning (c->face)) 372 _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, c->buffer); 373 374 hb_ot_map_glyphs_fast (c->buffer); 375 376 HB_BUFFER_DEALLOCATE_VAR (c->buffer, glyph_index); 377} 378 379static inline void 380hb_ot_substitute_complex (hb_ot_shape_context_t *c) 381{ 382 hb_ot_layout_substitute_start (c->font, c->buffer); 383 384 if (!hb_ot_layout_has_glyph_classes (c->face)) 385 hb_synthesize_glyph_classes (c); 386 387 c->plan->substitute (c->font, c->buffer); 388 389 hb_ot_layout_substitute_finish (c->font, c->buffer); 390 391 return; 392} 393 394static inline void 395hb_ot_substitute (hb_ot_shape_context_t *c) 396{ 397 hb_ot_substitute_default (c); 398 hb_ot_substitute_complex (c); 399} 400 401/* Position */ 402 403static inline void 404hb_ot_position_default (hb_ot_shape_context_t *c) 405{ 406 hb_ot_layout_position_start (c->font, c->buffer); 407 408 unsigned int count = c->buffer->len; 409 for (unsigned int i = 0; i < count; i++) 410 { 411 c->font->get_glyph_advance_for_direction (c->buffer->info[i].codepoint, 412 c->buffer->props.direction, 413 &c->buffer->pos[i].x_advance, 414 &c->buffer->pos[i].y_advance); 415 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint, 416 c->buffer->props.direction, 417 &c->buffer->pos[i].x_offset, 418 &c->buffer->pos[i].y_offset); 419 420 } 421 422 /* Zero'ing mark widths by GDEF (as used in Myanmar spec) happens 423 * *before* GPOS. */ 424 switch (c->plan->shaper->zero_width_marks) 425 { 426 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF: 427 for (unsigned int i = 0; i < count; i++) 428 if ((c->buffer->info[i].glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_MARK)) 429 { 430 c->buffer->pos[i].x_advance = 0; 431 c->buffer->pos[i].y_advance = 0; 432 } 433 break; 434 435 default: 436 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE: 437 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE: 438 break; 439 } 440} 441 442static inline bool 443hb_ot_position_complex (hb_ot_shape_context_t *c) 444{ 445 bool ret = false; 446 unsigned int count = c->buffer->len; 447 448 if (hb_ot_layout_has_positioning (c->face)) 449 { 450 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */ 451 452 for (unsigned int i = 0; i < count; i++) { 453 c->font->add_glyph_origin_for_direction (c->buffer->info[i].codepoint, 454 HB_DIRECTION_LTR, 455 &c->buffer->pos[i].x_offset, 456 &c->buffer->pos[i].y_offset); 457 } 458 459 c->plan->position (c->font, c->buffer); 460 461 for (unsigned int i = 0; i < count; i++) { 462 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint, 463 HB_DIRECTION_LTR, 464 &c->buffer->pos[i].x_offset, 465 &c->buffer->pos[i].y_offset); 466 } 467 468 ret = true; 469 } 470 471 /* Zero'ing mark widths by Unicode happens 472 * *after* GPOS. */ 473 switch (c->plan->shaper->zero_width_marks) 474 { 475 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE: 476 for (unsigned int i = 0; i < count; i++) 477 if (_hb_glyph_info_get_general_category (&c->buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) 478 { 479 c->buffer->pos[i].x_advance = 0; 480 c->buffer->pos[i].y_advance = 0; 481 } 482 break; 483 484 default: 485 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE: 486 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF: 487 break; 488 } 489 490 hb_ot_layout_position_finish (c->font, c->buffer); 491 492 return ret; 493} 494 495static inline void 496hb_ot_position (hb_ot_shape_context_t *c) 497{ 498 hb_ot_position_default (c); 499 500 hb_bool_t fallback = !hb_ot_position_complex (c); 501 502 if (fallback && c->plan->shaper->fallback_position) 503 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer); 504 505 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction)) 506 hb_buffer_reverse (c->buffer); 507 508 /* Visual fallback goes here. */ 509 510 if (fallback) 511 _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer); 512} 513 514 515/* Post-process */ 516 517static void 518hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c) 519{ 520 if (c->buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) 521 return; 522 523 hb_codepoint_t space = 0; 524 525 unsigned int count = c->buffer->len; 526 for (unsigned int i = 0; i < count; i++) 527 if (unlikely (!is_a_ligature (c->buffer->info[i]) && 528 _hb_glyph_info_is_default_ignorable (&c->buffer->info[i]))) 529 { 530 if (!space) { 531 /* We assume that the space glyph is not gid0. */ 532 if (unlikely (!c->font->get_glyph (' ', 0, &space)) || !space) 533 return; /* No point! */ 534 } 535 c->buffer->info[i].codepoint = space; 536 c->buffer->pos[i].x_advance = 0; 537 c->buffer->pos[i].y_advance = 0; 538 } 539} 540 541 542/* Pull it all together! */ 543 544static void 545hb_ot_shape_internal (hb_ot_shape_context_t *c) 546{ 547 c->buffer->deallocate_var_all (); 548 549 /* Save the original direction, we use it later. */ 550 c->target_direction = c->buffer->props.direction; 551 552 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0); 553 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1); 554 555 c->buffer->clear_output (); 556 557 hb_set_unicode_props (c->buffer); 558 hb_insert_dotted_circle (c->buffer, c->font); 559 hb_form_clusters (c->buffer); 560 561 hb_ensure_native_direction (c->buffer); 562 563 hb_ot_substitute (c); 564 hb_ot_position (c); 565 566 hb_ot_hide_default_ignorables (c); 567 568 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1); 569 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0); 570 571 c->buffer->props.direction = c->target_direction; 572 573 c->buffer->deallocate_var_all (); 574} 575 576 577hb_bool_t 578_hb_ot_shape (hb_shape_plan_t *shape_plan, 579 hb_font_t *font, 580 hb_buffer_t *buffer, 581 const hb_feature_t *features, 582 unsigned int num_features) 583{ 584 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features}; 585 hb_ot_shape_internal (&c); 586 587 return true; 588} 589 590 591void 592hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan, 593 hb_tag_t table_tag, 594 hb_set_t *lookup_indexes /* OUT */) 595{ 596 /* XXX Does the first part always succeed? */ 597 HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes); 598} 599 600 601/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */ 602static void 603add_char (hb_font_t *font, 604 hb_unicode_funcs_t *unicode, 605 hb_bool_t mirror, 606 hb_codepoint_t u, 607 hb_set_t *glyphs) 608{ 609 hb_codepoint_t glyph; 610 if (font->get_glyph (u, 0, &glyph)) 611 glyphs->add (glyph); 612 if (mirror) 613 { 614 hb_codepoint_t m = unicode->mirroring (u); 615 if (m != u && font->get_glyph (m, 0, &glyph)) 616 glyphs->add (glyph); 617 } 618} 619 620 621void 622hb_ot_shape_glyphs_closure (hb_font_t *font, 623 hb_buffer_t *buffer, 624 const hb_feature_t *features, 625 unsigned int num_features, 626 hb_set_t *glyphs) 627{ 628 hb_ot_shape_plan_t plan; 629 630 const char *shapers[] = {"ot", NULL}; 631 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props, 632 features, num_features, shapers); 633 634 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL; 635 636 unsigned int count = buffer->len; 637 for (unsigned int i = 0; i < count; i++) 638 add_char (font, buffer->unicode, mirror, buffer->info[i].codepoint, glyphs); 639 640 hb_set_t lookups; 641 lookups.init (); 642 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups); 643 644 /* And find transitive closure. */ 645 hb_set_t copy; 646 copy.init (); 647 do { 648 copy.set (glyphs); 649 for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);) 650 hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs); 651 } while (!copy.is_equal (glyphs)); 652 653 hb_shape_plan_destroy (shape_plan); 654} 655