hb-ot-layout.cc revision 468769b8f5332940278244e744ec2bd5a5dc5ee9
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-private.h" 32 33#include "hb-ot-layout-gdef-private.hh" 34#include "hb-ot-layout-gsub-private.hh" 35#include "hb-ot-layout-gpos-private.hh" 36 37 38#include <stdlib.h> 39#include <string.h> 40 41 42void 43_hb_ot_layout_init (hb_face_t *face) 44{ 45 hb_ot_layout_t *layout = &face->ot_layout; 46 47 layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF)); 48 layout->gdef = &Sanitizer<GDEF>::lock_instance (layout->gdef_blob); 49 50 layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB)); 51 layout->gsub = &Sanitizer<GSUB>::lock_instance (layout->gsub_blob); 52 53 layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS)); 54 layout->gpos = &Sanitizer<GPOS>::lock_instance (layout->gpos_blob); 55} 56 57void 58_hb_ot_layout_fini (hb_face_t *face) 59{ 60 hb_ot_layout_t *layout = &face->ot_layout; 61 62 hb_blob_unlock (layout->gdef_blob); 63 hb_blob_unlock (layout->gsub_blob); 64 hb_blob_unlock (layout->gpos_blob); 65 66 hb_blob_destroy (layout->gdef_blob); 67 hb_blob_destroy (layout->gsub_blob); 68 hb_blob_destroy (layout->gpos_blob); 69 70 free (layout->new_gdef.klasses); 71} 72 73static const GDEF& 74_get_gdef (hb_face_t *face) 75{ 76 return HB_LIKELY (face->ot_layout.gdef) ? *face->ot_layout.gdef : Null(GDEF); 77} 78 79static const GSUB& 80_get_gsub (hb_face_t *face) 81{ 82 return HB_LIKELY (face->ot_layout.gsub) ? *face->ot_layout.gsub : Null(GSUB); 83} 84 85static const GPOS& 86_get_gpos (hb_face_t *face) 87{ 88 return HB_LIKELY (face->ot_layout.gpos) ? *face->ot_layout.gpos : Null(GPOS); 89} 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_face_t *face) 100{ 101 return _get_gdef (face).has_glyph_classes (); 102} 103 104HB_INTERNAL hb_bool_t 105_hb_ot_layout_has_new_glyph_classes (hb_face_t *face) 106{ 107 return face->ot_layout.new_gdef.len > 0; 108} 109 110static unsigned int 111_hb_ot_layout_get_glyph_property (hb_face_t *face, 112 hb_codepoint_t glyph) 113{ 114 hb_ot_layout_class_t klass; 115 const GDEF &gdef = _get_gdef (face); 116 117 klass = gdef.get_glyph_class (glyph); 118 119 if (!klass && glyph < face->ot_layout.new_gdef.len) 120 klass = face->ot_layout.new_gdef.klasses[glyph]; 121 122 switch (klass) { 123 default: 124 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED; 125 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH; 126 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE; 127 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT; 128 case GDEF::MarkGlyph: 129 klass = gdef.get_mark_attachment_type (glyph); 130 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8); 131 } 132} 133 134HB_INTERNAL hb_bool_t 135_hb_ot_layout_check_glyph_property (hb_face_t *face, 136 hb_internal_glyph_info_t *ginfo, 137 unsigned int lookup_flags, 138 unsigned int *property_out) 139{ 140 unsigned int property; 141 142 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN) 143 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint); 144 property = ginfo->gproperty; 145 if (property_out) 146 *property_out = property; 147 148 /* Not covered, if, for example, glyph class is ligature and 149 * lookup_flags includes LookupFlags::IgnoreLigatures 150 */ 151 if (property & lookup_flags) 152 return false; 153 154 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK) 155 { 156 /* If using mark filtering sets, the high short of 157 * lookup_flags has the set index. 158 */ 159 if (lookup_flags & LookupFlag::UseMarkFilteringSet) 160 return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint); 161 162 /* The second byte of lookup_flags has the meaning 163 * "ignore marks of attachment type different than 164 * the attachment type specified." 165 */ 166 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType) 167 return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType); 168 } 169 170 return true; 171} 172 173HB_INTERNAL hb_bool_t 174_hb_ot_layout_skip_mark (hb_face_t *face, 175 hb_internal_glyph_info_t *ginfo, 176 unsigned int lookup_flags, 177 unsigned int *property_out) 178{ 179 unsigned int property; 180 181 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN) 182 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint); 183 property = ginfo->gproperty; 184 if (property_out) 185 *property_out = property; 186 187 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK) 188 { 189 /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */ 190 if (lookup_flags & LookupFlag::IgnoreMarks) 191 return true; 192 193 /* If using mark filtering sets, the high short of lookup_flags has the set index. */ 194 if (lookup_flags & LookupFlag::UseMarkFilteringSet) 195 return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint); 196 197 /* The second byte of lookup_flags has the meaning "ignore marks of attachment type 198 * different than the attachment type specified." */ 199 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType) 200 return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType); 201 } 202 203 return false; 204} 205 206HB_INTERNAL void 207_hb_ot_layout_set_glyph_class (hb_face_t *face, 208 hb_codepoint_t glyph, 209 hb_ot_layout_glyph_class_t klass) 210{ 211 if (HB_OBJECT_IS_INERT (face)) 212 return; 213 214 /* TODO optimize this? similar to old harfbuzz code for example */ 215 216 hb_ot_layout_t *layout = &face->ot_layout; 217 hb_ot_layout_class_t gdef_klass; 218 unsigned int len = layout->new_gdef.len; 219 220 if (HB_UNLIKELY (glyph > 65535)) 221 return; 222 223 /* XXX this is not threadsafe */ 224 if (glyph >= len) { 225 int new_len; 226 unsigned char *new_klasses; 227 228 new_len = len == 0 ? 120 : 2 * len; 229 if (new_len > 65535) 230 new_len = 65535; 231 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char)); 232 233 if (HB_UNLIKELY (!new_klasses)) 234 return; 235 236 memset (new_klasses + len, 0, new_len - len); 237 238 layout->new_gdef.klasses = new_klasses; 239 layout->new_gdef.len = new_len; 240 } 241 242 switch (klass) { 243 default: 244 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break; 245 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break; 246 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break; 247 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break; 248 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break; 249 } 250 251 layout->new_gdef.klasses[glyph] = gdef_klass; 252 return; 253} 254 255HB_INTERNAL void 256_hb_ot_layout_set_glyph_property (hb_face_t *face, 257 hb_codepoint_t glyph, 258 unsigned int property) 259{ _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); } 260 261 262hb_ot_layout_glyph_class_t 263hb_ot_layout_get_glyph_class (hb_face_t *face, 264 hb_codepoint_t glyph) 265{ 266 return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff); 267} 268 269void 270hb_ot_layout_set_glyph_class (hb_face_t *face, 271 hb_codepoint_t glyph, 272 hb_ot_layout_glyph_class_t klass) 273{ 274 _hb_ot_layout_set_glyph_class (face, glyph, klass); 275} 276 277void 278hb_ot_layout_build_glyph_classes (hb_face_t *face, 279 uint16_t num_total_glyphs, 280 hb_codepoint_t *glyphs, 281 unsigned char *klasses, 282 uint16_t count) 283{ 284 if (HB_OBJECT_IS_INERT (face)) 285 return; 286 287 hb_ot_layout_t *layout = &face->ot_layout; 288 289 if (HB_UNLIKELY (!count || !glyphs || !klasses)) 290 return; 291 292 if (layout->new_gdef.len == 0) { 293 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char)); 294 layout->new_gdef.len = count; 295 } 296 297 for (unsigned int i = 0; i < count; i++) 298 _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]); 299} 300 301hb_bool_t 302hb_ot_layout_get_attach_points (hb_face_t *face, 303 hb_codepoint_t glyph, 304 unsigned int *point_count /* IN/OUT */, 305 unsigned int *point_array /* OUT */) 306{ 307 return _get_gdef (face).get_attach_points (glyph, point_count, point_array); 308} 309 310hb_bool_t 311hb_ot_layout_get_lig_carets (hb_face_t *face, 312 hb_font_t *font, 313 hb_codepoint_t glyph, 314 unsigned int *caret_count /* IN/OUT */, 315 int *caret_array /* OUT */) 316{ 317 hb_ot_layout_context_t context; 318 context.font = font; 319 context.face = face; 320 return _get_gdef (face).get_lig_carets (&context, glyph, caret_count, caret_array); 321} 322 323/* 324 * GSUB/GPOS 325 */ 326 327static const GSUBGPOS& 328get_gsubgpos_table (hb_face_t *face, 329 hb_tag_t table_tag) 330{ 331 switch (table_tag) { 332 case HB_OT_TAG_GSUB: return _get_gsub (face); 333 case HB_OT_TAG_GPOS: return _get_gpos (face); 334 default: return Null(GSUBGPOS); 335 } 336} 337 338 339hb_bool_t 340hb_ot_layout_table_get_script_tags (hb_face_t *face, 341 hb_tag_t table_tag, 342 unsigned int *script_count /* IN/OUT */, 343 hb_tag_t *script_tags /* OUT */) 344{ 345 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 346 347 return g.get_script_tags (script_count, script_tags); 348} 349 350hb_bool_t 351hb_ot_layout_table_find_script (hb_face_t *face, 352 hb_tag_t table_tag, 353 hb_tag_t script_tag, 354 unsigned int *script_index) 355{ 356 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX); 357 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 358 359 if (g.find_script_index (script_tag, script_index)) 360 return TRUE; 361 362 /* try finding 'DFLT' */ 363 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index)) 364 return FALSE; 365 366 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 367 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index)) 368 return FALSE; 369 370 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 371 return FALSE; 372} 373 374hb_bool_t 375hb_ot_layout_table_get_feature_tags (hb_face_t *face, 376 hb_tag_t table_tag, 377 unsigned int *feature_count /* IN/OUT */, 378 hb_tag_t *feature_tags /* OUT */) 379{ 380 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 381 382 return g.get_feature_tags (feature_count, feature_tags); 383} 384 385hb_bool_t 386hb_ot_layout_table_find_feature (hb_face_t *face, 387 hb_tag_t table_tag, 388 hb_tag_t feature_tag, 389 unsigned int *feature_index) 390{ 391 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 392 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 393 394 if (g.find_feature_index (feature_tag, feature_index)) 395 return TRUE; 396 397 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 398 return FALSE; 399} 400 401 402hb_bool_t 403hb_ot_layout_script_get_language_tags (hb_face_t *face, 404 hb_tag_t table_tag, 405 unsigned int script_index, 406 unsigned int *language_count /* IN/OUT */, 407 hb_tag_t *language_tags /* OUT */) 408{ 409 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index); 410 411 return s.get_lang_sys_tags (language_count, language_tags); 412} 413 414hb_bool_t 415hb_ot_layout_script_find_language (hb_face_t *face, 416 hb_tag_t table_tag, 417 unsigned int script_index, 418 hb_tag_t language_tag, 419 unsigned int *language_index) 420{ 421 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX); 422 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index); 423 424 if (s.find_lang_sys_index (language_tag, language_index)) 425 return TRUE; 426 427 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 428 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index)) 429 return FALSE; 430 431 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX; 432 return FALSE; 433} 434 435hb_bool_t 436hb_ot_layout_language_get_required_feature_index (hb_face_t *face, 437 hb_tag_t table_tag, 438 unsigned int script_index, 439 unsigned int language_index, 440 unsigned int *feature_index) 441{ 442 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index); 443 444 if (feature_index) *feature_index = l.get_required_feature_index (); 445 446 return l.has_required_feature (); 447} 448 449hb_bool_t 450hb_ot_layout_language_get_feature_indexes (hb_face_t *face, 451 hb_tag_t table_tag, 452 unsigned int script_index, 453 unsigned int language_index, 454 unsigned int *feature_count /* IN/OUT */, 455 unsigned int *feature_indexes /* OUT */) 456{ 457 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 458 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 459 460 return l.get_feature_indexes (feature_count, feature_indexes); 461} 462 463hb_bool_t 464hb_ot_layout_language_get_feature_tags (hb_face_t *face, 465 hb_tag_t table_tag, 466 unsigned int script_index, 467 unsigned int language_index, 468 unsigned int *feature_count /* IN/OUT */, 469 hb_tag_t *feature_tags /* OUT */) 470{ 471 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 472 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 473 474 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t)); 475 unsigned int count = feature_count ? *feature_count : 0; 476 hb_bool_t ret = l.get_feature_indexes (feature_count, (unsigned int *) feature_tags); 477 478 count = feature_count ? MIN (count, *feature_count) : 0; 479 for (unsigned int i = 0; i < count; i++) 480 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]); 481 482 return ret; 483} 484 485 486hb_bool_t 487hb_ot_layout_language_find_feature (hb_face_t *face, 488 hb_tag_t table_tag, 489 unsigned int script_index, 490 unsigned int language_index, 491 hb_tag_t feature_tag, 492 unsigned int *feature_index) 493{ 494 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 495 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 496 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 497 498 unsigned int num_features = l.get_feature_count (); 499 for (unsigned int i = 0; i < num_features; i++) { 500 unsigned int f_index = l.get_feature_index (i); 501 502 if (feature_tag == g.get_feature_tag (f_index)) { 503 if (feature_index) *feature_index = f_index; 504 return TRUE; 505 } 506 } 507 508 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 509 return FALSE; 510} 511 512hb_bool_t 513hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face, 514 hb_tag_t table_tag, 515 unsigned int feature_index, 516 unsigned int *lookup_count /* IN/OUT */, 517 unsigned int *lookup_indexes /* OUT */) 518{ 519 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 520 const Feature &f = g.get_feature (feature_index); 521 522 return f.get_lookup_indexes (lookup_count, lookup_indexes); 523} 524 525 526/* 527 * GSUB 528 */ 529 530hb_bool_t 531hb_ot_layout_has_substitution (hb_face_t *face) 532{ 533 return &_get_gsub (face) != &Null(GSUB); 534} 535 536hb_bool_t 537hb_ot_layout_substitute_lookup (hb_face_t *face, 538 hb_buffer_t *buffer, 539 unsigned int lookup_index, 540 hb_mask_t mask) 541{ 542 hb_ot_layout_context_t context; 543 context.font = NULL; 544 context.face = face; 545 return _get_gsub (face).substitute_lookup (&context, buffer, lookup_index, mask); 546} 547 548 549/* 550 * GPOS 551 */ 552 553hb_bool_t 554hb_ot_layout_has_positioning (hb_face_t *face) 555{ 556 return &_get_gpos (face) != &Null(GPOS); 557} 558 559hb_bool_t 560hb_ot_layout_position_lookup (hb_face_t *face, 561 hb_font_t *font, 562 hb_buffer_t *buffer, 563 unsigned int lookup_index, 564 hb_mask_t mask) 565{ 566 hb_ot_layout_context_t context; 567 context.font = font; 568 context.face = face; 569 return _get_gpos (face).position_lookup (&context, buffer, lookup_index, mask); 570} 571