hb-ot-layout.cc revision 0f7e6b2cead0c7d527ef68ededb27e0afd71d819
1/* 2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg 3 * Copyright (C) 2006 Behdad Esfahbod 4 * Copyright (C) 2007,2008,2009 Red Hat, Inc. 5 * 6 * This is part of HarfBuzz, an OpenType Layout engine library. 7 * 8 * Permission is hereby granted, without written agreement and without 9 * license or royalty fees, to use, copy, modify, and distribute this 10 * software and its documentation for any purpose, provided that the 11 * above copyright notice and the following two paragraphs appear in 12 * all copies of this software. 13 * 14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 18 * DAMAGE. 19 * 20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 25 * 26 * Red Hat Author(s): Behdad Esfahbod 27 */ 28 29#define HB_OT_LAYOUT_CC 30 31#include "hb-ot-layout.h" 32#include "hb-ot-layout-private.h" 33 34#include "hb-ot-layout-open-private.h" 35#include "hb-ot-layout-gdef-private.h" 36#include "hb-ot-layout-gsub-private.h" 37#include "hb-ot-layout-gpos-private.h" 38 39/* XXX */ 40#include "harfbuzz-buffer-private.h" 41 42#include <stdlib.h> 43#include <string.h> 44 45 46hb_ot_layout_t * 47hb_ot_layout_create (void) 48{ 49 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t)); 50 51 layout->gdef = &Null(GDEF); 52 layout->gsub = &Null(GSUB); 53 layout->gpos = &Null(GPOS); 54 55 return layout; 56} 57 58hb_ot_layout_t * 59hb_ot_layout_create_for_data (const char *font_data, 60 int face_index) 61{ 62 hb_ot_layout_t *layout; 63 64 if (HB_UNLIKELY (font_data == NULL)) 65 return hb_ot_layout_create (); 66 67 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t)); 68 69 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data); 70 const OpenTypeFontFace &face = font.get_face (face_index); 71 72 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag))); 73 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag))); 74 layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag))); 75 76 return layout; 77} 78 79void 80hb_ot_layout_destroy (hb_ot_layout_t *layout) 81{ 82 free (layout); 83} 84 85void 86hb_ot_layout_set_direction (hb_ot_layout_t *layout, 87 hb_bool_t r2l) 88{ 89 layout->gpos_info.r2l = !!r2l; 90} 91 92/* 93 * GDEF 94 */ 95 96/* TODO the public class_t is a mess */ 97 98hb_bool_t 99hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout) 100{ 101 return layout->gdef->has_glyph_classes (); 102} 103 104HB_OT_LAYOUT_INTERNAL hb_bool_t 105_hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout) 106{ 107 return layout->new_gdef.len > 0; 108} 109 110HB_OT_LAYOUT_INTERNAL unsigned int 111_hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout, 112 hb_codepoint_t glyph) 113{ 114 hb_ot_layout_class_t klass; 115 116 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was 117 * introduced without a version bump, so it may not be safe */ 118 klass = layout->gdef->get_mark_attachment_type (glyph); 119 if (klass) 120 return klass << 8; 121 122 klass = layout->gdef->get_glyph_class (glyph); 123 124 if (!klass && glyph < layout->new_gdef.len) 125 klass = layout->new_gdef.klasses[glyph]; 126 127 switch (klass) { 128 default: 129 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED; 130 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH; 131 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE; 132 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK; 133 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT; 134 } 135} 136 137HB_OT_LAYOUT_INTERNAL hb_bool_t 138_hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout, 139 HB_GlyphItem gitem, 140 unsigned int lookup_flags, 141 unsigned int *property) 142{ 143 hb_ot_layout_glyph_class_t basic_glyph_class; 144 unsigned int desired_attachment_class; 145 146 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN) 147 { 148 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex); 149 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED) 150 return false; 151 } 152 153 *property = gitem->gproperty; 154 155 /* If the glyph was found in the MarkAttachmentClass table, 156 * then that class value is the high byte of the result, 157 * otherwise the low byte contains the basic type of the glyph 158 * as defined by the GlyphClassDef table. 159 */ 160 if (*property & LookupFlag::MarkAttachmentType) 161 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK; 162 else 163 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property; 164 165 /* Not covered, if, for example, basic_glyph_class 166 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures 167 */ 168 if (lookup_flags & basic_glyph_class) 169 return false; 170 171 /* The high byte of lookup_flags has the meaning 172 * "ignore marks of attachment type different than 173 * the attachment type specified." 174 */ 175 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType; 176 if (desired_attachment_class) 177 { 178 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK && 179 *property != desired_attachment_class) 180 return false; 181 } 182 183 return true; 184} 185 186HB_OT_LAYOUT_INTERNAL void 187_hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout, 188 hb_codepoint_t glyph, 189 unsigned int property) 190{ 191 hb_ot_layout_glyph_class_t klass; 192 193 if (property & LookupFlag::MarkAttachmentType) 194 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK; 195 else 196 klass = (hb_ot_layout_glyph_class_t) property; 197 198 hb_ot_layout_set_glyph_class (layout, glyph, klass); 199} 200 201 202hb_ot_layout_glyph_class_t 203hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout, 204 hb_codepoint_t glyph) 205{ 206 unsigned int property; 207 hb_ot_layout_class_t klass; 208 209 property = _hb_ot_layout_get_glyph_property (layout, glyph); 210 211 if (property & LookupFlag::MarkAttachmentType) 212 return HB_OT_LAYOUT_GLYPH_CLASS_MARK; 213 214 return (hb_ot_layout_glyph_class_t) property; 215} 216 217void 218hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout, 219 hb_codepoint_t glyph, 220 hb_ot_layout_glyph_class_t klass) 221{ 222 /* TODO optimize this, similar to old harfbuzz code for example */ 223 224 hb_ot_layout_class_t gdef_klass; 225 int len = layout->new_gdef.len; 226 227 if (HB_UNLIKELY (glyph > 65535)) 228 return; 229 230 if (glyph >= len) { 231 int new_len; 232 unsigned char *new_klasses; 233 234 new_len = len == 0 ? 120 : 2 * len; 235 if (new_len > 65535) 236 new_len = 65535; 237 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char)); 238 239 if (HB_UNLIKELY (!new_klasses)) 240 return; 241 242 memset (new_klasses + len, 0, new_len - len); 243 244 layout->new_gdef.klasses = new_klasses; 245 layout->new_gdef.len = new_len; 246 } 247 248 switch (klass) { 249 default: 250 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break; 251 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break; 252 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break; 253 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break; 254 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break; 255 } 256 257 layout->new_gdef.klasses[glyph] = gdef_klass; 258 return; 259} 260 261void 262hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout, 263 uint16_t num_total_glyphs, 264 hb_codepoint_t *glyphs, 265 unsigned char *klasses, 266 uint16_t count) 267{ 268 if (HB_UNLIKELY (!count || !glyphs || !klasses)) 269 return; 270 271 if (layout->new_gdef.len == 0) { 272 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char)); 273 layout->new_gdef.len = count; 274 } 275 276 for (unsigned int i = 0; i < count; i++) 277 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]); 278} 279 280/* 281 * GSUB/GPOS 282 */ 283 284static const GSUBGPOS& 285get_gsubgpos_table (hb_ot_layout_t *layout, 286 hb_ot_layout_table_type_t table_type) 287{ 288 switch (table_type) { 289 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub); 290 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos); 291 default: return Null(GSUBGPOS); 292 } 293} 294 295 296unsigned int 297hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout, 298 hb_ot_layout_table_type_t table_type) 299{ 300 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 301 302 return g.get_script_count (); 303} 304 305hb_tag_t 306hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout, 307 hb_ot_layout_table_type_t table_type, 308 unsigned int script_index) 309{ 310 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 311 312 return g.get_script_tag (script_index); 313} 314 315hb_bool_t 316hb_ot_layout_table_find_script (hb_ot_layout_t *layout, 317 hb_ot_layout_table_type_t table_type, 318 hb_tag_t script_tag, 319 unsigned int *script_index) 320{ 321 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX); 322 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 323 324 if (g.find_script_index (script_tag, script_index)) 325 return TRUE; 326 327 /* try finding 'DFLT' */ 328 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index)) 329 return FALSE; 330 331 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 332 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index)) 333 return FALSE; 334 335 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 336 return FALSE; 337} 338 339unsigned int 340hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout, 341 hb_ot_layout_table_type_t table_type) 342{ 343 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 344 345 return g.get_feature_count (); 346} 347 348hb_tag_t 349hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout, 350 hb_ot_layout_table_type_t table_type, 351 unsigned int feature_index) 352{ 353 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 354 355 return g.get_feature_tag (feature_index); 356} 357 358hb_bool_t 359hb_ot_layout_table_find_feature (hb_ot_layout_t *layout, 360 hb_ot_layout_table_type_t table_type, 361 hb_tag_t feature_tag, 362 unsigned int *feature_index) 363{ 364 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 365 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 366 367 if (g.find_feature_index (feature_tag, feature_index)) 368 return TRUE; 369 370 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 371 return FALSE; 372} 373 374unsigned int 375hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout, 376 hb_ot_layout_table_type_t table_type) 377{ 378 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 379 380 return g.get_lookup_count (); 381} 382 383 384unsigned int 385hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout, 386 hb_ot_layout_table_type_t table_type, 387 unsigned int script_index) 388{ 389 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 390 391 return s.get_lang_sys_count (); 392} 393 394hb_tag_t 395hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout, 396 hb_ot_layout_table_type_t table_type, 397 unsigned int script_index, 398 unsigned int language_index) 399{ 400 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 401 402 return s.get_lang_sys_tag (language_index); 403} 404 405hb_bool_t 406hb_ot_layout_script_find_language (hb_ot_layout_t *layout, 407 hb_ot_layout_table_type_t table_type, 408 unsigned int script_index, 409 hb_tag_t language_tag, 410 unsigned int *language_index) 411{ 412 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX); 413 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 414 415 if (s.find_lang_sys_index (language_tag, language_index)) 416 return TRUE; 417 418 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 419 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index)) 420 return FALSE; 421 422 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX; 423 return FALSE; 424} 425 426hb_bool_t 427hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout, 428 hb_ot_layout_table_type_t table_type, 429 unsigned int script_index, 430 unsigned int language_index, 431 unsigned int *feature_index) 432{ 433 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index); 434 435 if (feature_index) *feature_index = l.get_required_feature_index (); 436 437 return l.has_required_feature (); 438} 439 440unsigned int 441hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout, 442 hb_ot_layout_table_type_t table_type, 443 unsigned int script_index, 444 unsigned int language_index) 445{ 446 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index); 447 448 return l.get_feature_count (); 449} 450 451unsigned int 452hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout, 453 hb_ot_layout_table_type_t table_type, 454 unsigned int script_index, 455 unsigned int language_index, 456 unsigned int num_feature) 457{ 458 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 459 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 460 461 return l.get_feature_index (num_feature); 462} 463 464hb_tag_t 465hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout, 466 hb_ot_layout_table_type_t table_type, 467 unsigned int script_index, 468 unsigned int language_index, 469 unsigned int num_feature) 470{ 471 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 472 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 473 unsigned int feature_index = l.get_feature_index (num_feature); 474 475 return g.get_feature_tag (feature_index); 476} 477 478 479hb_bool_t 480hb_ot_layout_language_find_feature (hb_ot_layout_t *layout, 481 hb_ot_layout_table_type_t table_type, 482 unsigned int script_index, 483 unsigned int language_index, 484 hb_tag_t feature_tag, 485 unsigned int *feature_index) 486{ 487 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 488 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 489 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 490 491 unsigned int num_features = l.get_feature_count (); 492 for (unsigned int i = 0; i < num_features; i++) { 493 unsigned int f_index = l.get_feature_index (i); 494 495 if (feature_tag == g.get_feature_tag (f_index)) { 496 if (feature_index) *feature_index = f_index; 497 return TRUE; 498 } 499 } 500 501 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 502 return FALSE; 503} 504 505unsigned int 506hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout, 507 hb_ot_layout_table_type_t table_type, 508 unsigned int feature_index) 509{ 510 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 511 const Feature &f = g.get_feature (feature_index); 512 513 return f.get_lookup_count (); 514} 515 516unsigned int 517hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout, 518 hb_ot_layout_table_type_t table_type, 519 unsigned int feature_index, 520 unsigned int num_lookup) 521{ 522 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 523 const Feature &f = g.get_feature (feature_index); 524 525 return f.get_lookup_index (num_lookup); 526} 527 528/* 529 * GSUB 530 */ 531 532hb_bool_t 533hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout, 534 hb_buffer_t *buffer, 535 unsigned int lookup_index, 536 hb_ot_layout_feature_mask_t mask) 537{ 538 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask); 539} 540 541/* 542 * GPOS 543 */ 544 545hb_bool_t 546hb_ot_layout_position_lookup (hb_ot_layout_t *layout, 547 hb_buffer_t *buffer, 548 unsigned int lookup_index, 549 hb_ot_layout_feature_mask_t mask) 550{ 551 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask); 552} 553 554 555 556/* TODO dupped, until he old code can be removed */ 557 558static HB_Error 559hb_buffer_duplicate_out_buffer( HB_Buffer buffer ) 560{ 561 if ( !buffer->alt_string ) 562 { 563 HB_Error error; 564 565 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) ) 566 return error; 567 } 568 569 buffer->out_string = buffer->alt_string; 570 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) ); 571 buffer->separate_out = TRUE; 572 573 return HB_Err_Ok; 574} 575 576 577 578HB_INTERNAL HB_Error 579_hb_buffer_add_output_glyph_ids( HB_Buffer buffer, 580 HB_UShort num_in, 581 HB_UShort num_out, 582 const GlyphID *glyph_data, 583 HB_UShort component, 584 HB_UShort ligID ) 585{ 586 HB_Error error; 587 HB_UShort i; 588 HB_UInt properties; 589 HB_UInt cluster; 590 591 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out ); 592 if ( error ) 593 return error; 594 595 if ( !buffer->separate_out ) 596 { 597 error = hb_buffer_duplicate_out_buffer( buffer ); 598 if ( error ) 599 return error; 600 } 601 602 properties = buffer->in_string[buffer->in_pos].properties; 603 cluster = buffer->in_string[buffer->in_pos].cluster; 604 if ( component == 0xFFFF ) 605 component = buffer->in_string[buffer->in_pos].component; 606 if ( ligID == 0xFFFF ) 607 ligID = buffer->in_string[buffer->in_pos].ligID; 608 609 for ( i = 0; i < num_out; i++ ) 610 { 611 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i]; 612 613 item->gindex = glyph_data[i]; 614 item->properties = properties; 615 item->cluster = cluster; 616 item->component = component; 617 item->ligID = ligID; 618 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN; 619 } 620 621 buffer->in_pos += num_in; 622 buffer->out_pos += num_out; 623 624 buffer->out_length = buffer->out_pos; 625 626 return HB_Err_Ok; 627} 628 629