hb-ot-layout.cc revision e1593a6e620ee6a8184487185d1c95e0d1820ba5
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 92void 93hb_ot_layout_set_hinting (hb_ot_layout_t *layout, 94 hb_bool_t hinted) 95{ 96 layout->gpos_info.dvi = !hinted; 97} 98 99void 100hb_ot_layout_set_scale (hb_ot_layout_t *layout, 101 hb_16dot16_t x_scale, hb_16dot16_t y_scale) 102{ 103 layout->gpos_info.x_scale = x_scale; 104 layout->gpos_info.y_scale = y_scale; 105} 106 107void 108hb_ot_layout_set_ppem (hb_ot_layout_t *layout, 109 unsigned int x_ppem, unsigned int y_ppem) 110{ 111 layout->gpos_info.x_ppem = x_ppem; 112 layout->gpos_info.y_ppem = y_ppem; 113} 114 115 116/* 117 * GDEF 118 */ 119 120/* TODO the public class_t is a mess */ 121 122hb_bool_t 123hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout) 124{ 125 return layout->gdef->has_glyph_classes (); 126} 127 128HB_OT_LAYOUT_INTERNAL hb_bool_t 129_hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout) 130{ 131 return layout->new_gdef.len > 0; 132} 133 134HB_OT_LAYOUT_INTERNAL unsigned int 135_hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout, 136 hb_codepoint_t glyph) 137{ 138 hb_ot_layout_class_t klass; 139 140 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was 141 * introduced without a version bump, so it may not be safe */ 142 klass = layout->gdef->get_mark_attachment_type (glyph); 143 if (klass) 144 return klass << 8; 145 146 klass = layout->gdef->get_glyph_class (glyph); 147 148 if (!klass && glyph < layout->new_gdef.len) 149 klass = layout->new_gdef.klasses[glyph]; 150 151 switch (klass) { 152 default: 153 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED; 154 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH; 155 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE; 156 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK; 157 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT; 158 } 159} 160 161HB_OT_LAYOUT_INTERNAL hb_bool_t 162_hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout, 163 HB_GlyphItem gitem, 164 unsigned int lookup_flags, 165 unsigned int *property) 166{ 167 hb_ot_layout_glyph_class_t basic_glyph_class; 168 unsigned int desired_attachment_class; 169 170 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN) 171 { 172 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex); 173 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED) 174 return false; 175 } 176 177 *property = gitem->gproperty; 178 179 /* If the glyph was found in the MarkAttachmentClass table, 180 * then that class value is the high byte of the result, 181 * otherwise the low byte contains the basic type of the glyph 182 * as defined by the GlyphClassDef table. 183 */ 184 if (*property & LookupFlag::MarkAttachmentType) 185 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK; 186 else 187 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property; 188 189 /* Not covered, if, for example, basic_glyph_class 190 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures 191 */ 192 if (lookup_flags & basic_glyph_class) 193 return false; 194 195 /* The high byte of lookup_flags has the meaning 196 * "ignore marks of attachment type different than 197 * the attachment type specified." 198 */ 199 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType; 200 if (desired_attachment_class) 201 { 202 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK && 203 *property != desired_attachment_class) 204 return false; 205 } 206 207 return true; 208} 209 210HB_OT_LAYOUT_INTERNAL void 211_hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout, 212 hb_codepoint_t glyph, 213 unsigned int property) 214{ 215 hb_ot_layout_glyph_class_t klass; 216 217 if (property & LookupFlag::MarkAttachmentType) 218 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK; 219 else 220 klass = (hb_ot_layout_glyph_class_t) property; 221 222 hb_ot_layout_set_glyph_class (layout, glyph, klass); 223} 224 225 226hb_ot_layout_glyph_class_t 227hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout, 228 hb_codepoint_t glyph) 229{ 230 unsigned int property; 231 hb_ot_layout_class_t klass; 232 233 property = _hb_ot_layout_get_glyph_property (layout, glyph); 234 235 if (property & LookupFlag::MarkAttachmentType) 236 return HB_OT_LAYOUT_GLYPH_CLASS_MARK; 237 238 return (hb_ot_layout_glyph_class_t) property; 239} 240 241void 242hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout, 243 hb_codepoint_t glyph, 244 hb_ot_layout_glyph_class_t klass) 245{ 246 /* TODO optimize this, similar to old harfbuzz code for example */ 247 248 hb_ot_layout_class_t gdef_klass; 249 int len = layout->new_gdef.len; 250 251 if (HB_UNLIKELY (glyph > 65535)) 252 return; 253 254 if (glyph >= len) { 255 int new_len; 256 unsigned char *new_klasses; 257 258 new_len = len == 0 ? 120 : 2 * len; 259 if (new_len > 65535) 260 new_len = 65535; 261 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char)); 262 263 if (HB_UNLIKELY (!new_klasses)) 264 return; 265 266 memset (new_klasses + len, 0, new_len - len); 267 268 layout->new_gdef.klasses = new_klasses; 269 layout->new_gdef.len = new_len; 270 } 271 272 switch (klass) { 273 default: 274 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break; 275 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break; 276 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break; 277 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break; 278 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break; 279 } 280 281 layout->new_gdef.klasses[glyph] = gdef_klass; 282 return; 283} 284 285void 286hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout, 287 uint16_t num_total_glyphs, 288 hb_codepoint_t *glyphs, 289 unsigned char *klasses, 290 uint16_t count) 291{ 292 if (HB_UNLIKELY (!count || !glyphs || !klasses)) 293 return; 294 295 if (layout->new_gdef.len == 0) { 296 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char)); 297 layout->new_gdef.len = count; 298 } 299 300 for (unsigned int i = 0; i < count; i++) 301 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]); 302} 303 304/* 305 * GSUB/GPOS 306 */ 307 308static const GSUBGPOS& 309get_gsubgpos_table (hb_ot_layout_t *layout, 310 hb_ot_layout_table_type_t table_type) 311{ 312 switch (table_type) { 313 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub); 314 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos); 315 default: return Null(GSUBGPOS); 316 } 317} 318 319 320unsigned int 321hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout, 322 hb_ot_layout_table_type_t table_type) 323{ 324 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 325 326 return g.get_script_count (); 327} 328 329hb_tag_t 330hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout, 331 hb_ot_layout_table_type_t table_type, 332 unsigned int script_index) 333{ 334 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 335 336 return g.get_script_tag (script_index); 337} 338 339hb_bool_t 340hb_ot_layout_table_find_script (hb_ot_layout_t *layout, 341 hb_ot_layout_table_type_t table_type, 342 hb_tag_t script_tag, 343 unsigned int *script_index) 344{ 345 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX); 346 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 347 348 if (g.find_script_index (script_tag, script_index)) 349 return TRUE; 350 351 /* try finding 'DFLT' */ 352 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index)) 353 return FALSE; 354 355 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 356 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index)) 357 return FALSE; 358 359 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 360 return FALSE; 361} 362 363unsigned int 364hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout, 365 hb_ot_layout_table_type_t table_type) 366{ 367 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 368 369 return g.get_feature_count (); 370} 371 372hb_tag_t 373hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout, 374 hb_ot_layout_table_type_t table_type, 375 unsigned int feature_index) 376{ 377 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 378 379 return g.get_feature_tag (feature_index); 380} 381 382hb_bool_t 383hb_ot_layout_table_find_feature (hb_ot_layout_t *layout, 384 hb_ot_layout_table_type_t table_type, 385 hb_tag_t feature_tag, 386 unsigned int *feature_index) 387{ 388 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 389 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 390 391 if (g.find_feature_index (feature_tag, feature_index)) 392 return TRUE; 393 394 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 395 return FALSE; 396} 397 398unsigned int 399hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout, 400 hb_ot_layout_table_type_t table_type) 401{ 402 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 403 404 return g.get_lookup_count (); 405} 406 407 408unsigned int 409hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout, 410 hb_ot_layout_table_type_t table_type, 411 unsigned int script_index) 412{ 413 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 414 415 return s.get_lang_sys_count (); 416} 417 418hb_tag_t 419hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout, 420 hb_ot_layout_table_type_t table_type, 421 unsigned int script_index, 422 unsigned int language_index) 423{ 424 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 425 426 return s.get_lang_sys_tag (language_index); 427} 428 429hb_bool_t 430hb_ot_layout_script_find_language (hb_ot_layout_t *layout, 431 hb_ot_layout_table_type_t table_type, 432 unsigned int script_index, 433 hb_tag_t language_tag, 434 unsigned int *language_index) 435{ 436 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX); 437 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index); 438 439 if (s.find_lang_sys_index (language_tag, language_index)) 440 return TRUE; 441 442 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 443 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index)) 444 return FALSE; 445 446 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX; 447 return FALSE; 448} 449 450hb_bool_t 451hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout, 452 hb_ot_layout_table_type_t table_type, 453 unsigned int script_index, 454 unsigned int language_index, 455 unsigned int *feature_index) 456{ 457 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index); 458 459 if (feature_index) *feature_index = l.get_required_feature_index (); 460 461 return l.has_required_feature (); 462} 463 464unsigned int 465hb_ot_layout_language_get_feature_count (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{ 470 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index); 471 472 return l.get_feature_count (); 473} 474 475unsigned int 476hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout, 477 hb_ot_layout_table_type_t table_type, 478 unsigned int script_index, 479 unsigned int language_index, 480 unsigned int num_feature) 481{ 482 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 483 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 484 485 return l.get_feature_index (num_feature); 486} 487 488hb_tag_t 489hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout, 490 hb_ot_layout_table_type_t table_type, 491 unsigned int script_index, 492 unsigned int language_index, 493 unsigned int num_feature) 494{ 495 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 496 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 497 unsigned int feature_index = l.get_feature_index (num_feature); 498 499 return g.get_feature_tag (feature_index); 500} 501 502 503hb_bool_t 504hb_ot_layout_language_find_feature (hb_ot_layout_t *layout, 505 hb_ot_layout_table_type_t table_type, 506 unsigned int script_index, 507 unsigned int language_index, 508 hb_tag_t feature_tag, 509 unsigned int *feature_index) 510{ 511 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 512 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 513 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 514 515 unsigned int num_features = l.get_feature_count (); 516 for (unsigned int i = 0; i < num_features; i++) { 517 unsigned int f_index = l.get_feature_index (i); 518 519 if (feature_tag == g.get_feature_tag (f_index)) { 520 if (feature_index) *feature_index = f_index; 521 return TRUE; 522 } 523 } 524 525 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 526 return FALSE; 527} 528 529unsigned int 530hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout, 531 hb_ot_layout_table_type_t table_type, 532 unsigned int feature_index) 533{ 534 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 535 const Feature &f = g.get_feature (feature_index); 536 537 return f.get_lookup_count (); 538} 539 540unsigned int 541hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout, 542 hb_ot_layout_table_type_t table_type, 543 unsigned int feature_index, 544 unsigned int num_lookup) 545{ 546 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type); 547 const Feature &f = g.get_feature (feature_index); 548 549 return f.get_lookup_index (num_lookup); 550} 551 552/* 553 * GSUB 554 */ 555 556hb_bool_t 557hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout, 558 hb_buffer_t *buffer, 559 unsigned int lookup_index, 560 hb_ot_layout_feature_mask_t mask) 561{ 562 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask); 563} 564 565/* 566 * GPOS 567 */ 568 569hb_bool_t 570hb_ot_layout_position_lookup (hb_ot_layout_t *layout, 571 hb_buffer_t *buffer, 572 unsigned int lookup_index, 573 hb_ot_layout_feature_mask_t mask) 574{ 575 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask); 576} 577 578 579 580/* TODO dupped, until he old code can be removed */ 581 582static HB_Error 583hb_buffer_duplicate_out_buffer( HB_Buffer buffer ) 584{ 585 if ( !buffer->alt_string ) 586 { 587 HB_Error error; 588 589 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) ) 590 return error; 591 } 592 593 buffer->out_string = buffer->alt_string; 594 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) ); 595 buffer->separate_out = TRUE; 596 597 return HB_Err_Ok; 598} 599 600 601 602HB_INTERNAL HB_Error 603_hb_buffer_add_output_glyph_ids( HB_Buffer buffer, 604 HB_UShort num_in, 605 HB_UShort num_out, 606 const GlyphID *glyph_data, 607 HB_UShort component, 608 HB_UShort ligID ) 609{ 610 HB_Error error; 611 HB_UShort i; 612 HB_UInt properties; 613 HB_UInt cluster; 614 615 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out ); 616 if ( error ) 617 return error; 618 619 if ( !buffer->separate_out ) 620 { 621 error = hb_buffer_duplicate_out_buffer( buffer ); 622 if ( error ) 623 return error; 624 } 625 626 properties = buffer->in_string[buffer->in_pos].properties; 627 cluster = buffer->in_string[buffer->in_pos].cluster; 628 if ( component == 0xFFFF ) 629 component = buffer->in_string[buffer->in_pos].component; 630 if ( ligID == 0xFFFF ) 631 ligID = buffer->in_string[buffer->in_pos].ligID; 632 633 for ( i = 0; i < num_out; i++ ) 634 { 635 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i]; 636 637 item->gindex = glyph_data[i]; 638 item->properties = properties; 639 item->cluster = cluster; 640 item->component = component; 641 item->ligID = ligID; 642 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN; 643 } 644 645 buffer->in_pos += num_in; 646 buffer->out_pos += num_out; 647 648 buffer->out_length = buffer->out_pos; 649 650 return HB_Err_Ok; 651} 652 653