hb-ot-layout.cc revision cfe9882610489e1b917e09a74dfbf6bbba2e4a57
1/* 2 * Copyright © 1998-2004 David Turner and Werner Lemberg 3 * Copyright © 2006 Behdad Esfahbod 4 * Copyright © 2007,2008,2009 Red Hat, Inc. 5 * 6 * This is part of HarfBuzz, a text shaping 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#include "hb-ot-layout-private.hh" 30 31#include "hb-ot-layout-gdef-table.hh" 32#include "hb-ot-layout-gsub-table.hh" 33#include "hb-ot-layout-gpos-table.hh" 34#include "hb-ot-maxp-table.hh" 35 36 37#include <stdlib.h> 38#include <string.h> 39 40 41HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) 42hb_bool_t 43hb_ot_layout_ensure (hb_face_t *face) 44{ 45 return hb_ot_shaper_face_data_ensure (face); 46} 47 48 49hb_ot_layout_t * 50_hb_ot_layout_create (hb_face_t *face) 51{ 52 /* TODO Remove this object altogether */ 53 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t)); 54 55 layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GDEF)); 56 layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob); 57 58 layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GSUB)); 59 layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob); 60 61 layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GPOS)); 62 layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob); 63 64 return layout; 65} 66 67void 68_hb_ot_layout_destroy (hb_ot_layout_t *layout) 69{ 70 hb_blob_destroy (layout->gdef_blob); 71 hb_blob_destroy (layout->gsub_blob); 72 hb_blob_destroy (layout->gpos_blob); 73 74 free (layout); 75} 76 77static inline const GDEF& 78_get_gdef (hb_face_t *face) 79{ 80 /* XXX ensure ot_layout, and speed up */ 81 return *hb_ot_layout_from_face (face)->gdef; 82} 83static inline const GSUB& 84_get_gsub (hb_face_t *face) 85{ 86 /* XXX ensure ot_layout, and speed up */ 87 return *hb_ot_layout_from_face (face)->gsub; 88} 89static inline const GPOS& 90_get_gpos (hb_face_t *face) 91{ 92 /* XXX ensure ot_layout, and speed up */ 93 return *hb_ot_layout_from_face (face)->gpos; 94} 95 96 97/* 98 * GDEF 99 */ 100 101hb_bool_t 102hb_ot_layout_has_glyph_classes (hb_face_t *face) 103{ 104 return _get_gdef (face).has_glyph_classes (); 105} 106 107static inline unsigned int 108_hb_ot_layout_get_glyph_property (hb_face_t *face, 109 hb_glyph_info_t *info) 110{ 111 if (!info->props_cache()) 112 { 113 const GDEF &gdef = _get_gdef (face); 114 info->props_cache() = gdef.get_glyph_props (info->codepoint); 115 } 116 117 return info->props_cache(); 118} 119 120static inline hb_bool_t 121_hb_ot_layout_match_properties_mark (hb_face_t *face, 122 hb_codepoint_t glyph, 123 unsigned int glyph_props, 124 unsigned int lookup_props) 125{ 126 /* If using mark filtering sets, the high short of 127 * lookup_props has the set index. 128 */ 129 if (lookup_props & LookupFlag::UseMarkFilteringSet) 130 return _get_gdef (face).mark_set_covers (lookup_props >> 16, glyph); 131 132 /* The second byte of lookup_props has the meaning 133 * "ignore marks of attachment type different than 134 * the attachment type specified." 135 */ 136 if (lookup_props & LookupFlag::MarkAttachmentType) 137 return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType); 138 139 return true; 140} 141 142static inline hb_bool_t 143_hb_ot_layout_match_properties (hb_face_t *face, 144 hb_codepoint_t glyph, 145 unsigned int glyph_props, 146 unsigned int lookup_props) 147{ 148 /* Not covered, if, for example, glyph class is ligature and 149 * lookup_props includes LookupFlags::IgnoreLigatures 150 */ 151 if (glyph_props & lookup_props & LookupFlag::IgnoreFlags) 152 return false; 153 154 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK)) 155 return _hb_ot_layout_match_properties_mark (face, glyph, glyph_props, lookup_props); 156 157 return true; 158} 159 160hb_bool_t 161_hb_ot_layout_check_glyph_property (hb_face_t *face, 162 hb_glyph_info_t *ginfo, 163 unsigned int lookup_props, 164 unsigned int *property_out) 165{ 166 unsigned int property; 167 168 property = _hb_ot_layout_get_glyph_property (face, ginfo); 169 *property_out = property; 170 171 return _hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props); 172} 173 174hb_bool_t 175_hb_ot_layout_skip_mark (hb_face_t *face, 176 hb_glyph_info_t *ginfo, 177 unsigned int lookup_props, 178 unsigned int *property_out) 179{ 180 unsigned int property; 181 182 property = _hb_ot_layout_get_glyph_property (face, ginfo); 183 if (property_out) 184 *property_out = property; 185 186 /* If it's a mark, skip it if we don't accept it. */ 187 if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)) 188 return !_hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props); 189 190 /* If not a mark, don't skip. */ 191 return false; 192} 193 194 195 196unsigned int 197hb_ot_layout_get_attach_points (hb_face_t *face, 198 hb_codepoint_t glyph, 199 unsigned int start_offset, 200 unsigned int *point_count /* IN/OUT */, 201 unsigned int *point_array /* OUT */) 202{ 203 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array); 204} 205 206unsigned int 207hb_ot_layout_get_ligature_carets (hb_font_t *font, 208 hb_direction_t direction, 209 hb_codepoint_t glyph, 210 unsigned int start_offset, 211 unsigned int *caret_count /* IN/OUT */, 212 int *caret_array /* OUT */) 213{ 214 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array); 215} 216 217/* 218 * GSUB/GPOS 219 */ 220 221static const GSUBGPOS& 222get_gsubgpos_table (hb_face_t *face, 223 hb_tag_t table_tag) 224{ 225 switch (table_tag) { 226 case HB_OT_TAG_GSUB: return _get_gsub (face); 227 case HB_OT_TAG_GPOS: return _get_gpos (face); 228 default: return Null(GSUBGPOS); 229 } 230} 231 232 233unsigned int 234hb_ot_layout_table_get_script_tags (hb_face_t *face, 235 hb_tag_t table_tag, 236 unsigned int start_offset, 237 unsigned int *script_count /* IN/OUT */, 238 hb_tag_t *script_tags /* OUT */) 239{ 240 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 241 242 return g.get_script_tags (start_offset, script_count, script_tags); 243} 244 245hb_bool_t 246hb_ot_layout_table_find_script (hb_face_t *face, 247 hb_tag_t table_tag, 248 hb_tag_t script_tag, 249 unsigned int *script_index) 250{ 251 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX); 252 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 253 254 if (g.find_script_index (script_tag, script_index)) 255 return true; 256 257 /* try finding 'DFLT' */ 258 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) 259 return false; 260 261 /* try with 'dflt'; MS site has had typos and many fonts use it now :(. 262 * including many versions of DejaVu Sans Mono! */ 263 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) 264 return false; 265 266 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 267 return false; 268} 269 270hb_bool_t 271hb_ot_layout_table_choose_script (hb_face_t *face, 272 hb_tag_t table_tag, 273 const hb_tag_t *script_tags, 274 unsigned int *script_index, 275 hb_tag_t *chosen_script) 276{ 277 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX); 278 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 279 280 while (*script_tags) 281 { 282 if (g.find_script_index (*script_tags, script_index)) { 283 if (chosen_script) 284 *chosen_script = *script_tags; 285 return true; 286 } 287 script_tags++; 288 } 289 290 /* try finding 'DFLT' */ 291 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) { 292 if (chosen_script) 293 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT; 294 return false; 295 } 296 297 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 298 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) { 299 if (chosen_script) 300 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE; 301 return false; 302 } 303 304 /* try with 'latn'; some old fonts put their features there even though 305 they're really trying to support Thai, for example :( */ 306#define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n') 307 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) { 308 if (chosen_script) 309 *chosen_script = HB_OT_TAG_LATIN_SCRIPT; 310 return false; 311 } 312 313 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 314 if (chosen_script) 315 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX; 316 return false; 317} 318 319unsigned int 320hb_ot_layout_table_get_feature_tags (hb_face_t *face, 321 hb_tag_t table_tag, 322 unsigned int start_offset, 323 unsigned int *feature_count /* IN/OUT */, 324 hb_tag_t *feature_tags /* OUT */) 325{ 326 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 327 328 return g.get_feature_tags (start_offset, feature_count, feature_tags); 329} 330 331 332unsigned int 333hb_ot_layout_script_get_language_tags (hb_face_t *face, 334 hb_tag_t table_tag, 335 unsigned int script_index, 336 unsigned int start_offset, 337 unsigned int *language_count /* IN/OUT */, 338 hb_tag_t *language_tags /* OUT */) 339{ 340 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index); 341 342 return s.get_lang_sys_tags (start_offset, language_count, language_tags); 343} 344 345hb_bool_t 346hb_ot_layout_script_find_language (hb_face_t *face, 347 hb_tag_t table_tag, 348 unsigned int script_index, 349 hb_tag_t language_tag, 350 unsigned int *language_index) 351{ 352 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX); 353 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index); 354 355 if (s.find_lang_sys_index (language_tag, language_index)) 356 return true; 357 358 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */ 359 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index)) 360 return false; 361 362 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX; 363 return false; 364} 365 366hb_bool_t 367hb_ot_layout_language_get_required_feature_index (hb_face_t *face, 368 hb_tag_t table_tag, 369 unsigned int script_index, 370 unsigned int language_index, 371 unsigned int *feature_index) 372{ 373 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index); 374 375 if (feature_index) *feature_index = l.get_required_feature_index (); 376 377 return l.has_required_feature (); 378} 379 380unsigned int 381hb_ot_layout_language_get_feature_indexes (hb_face_t *face, 382 hb_tag_t table_tag, 383 unsigned int script_index, 384 unsigned int language_index, 385 unsigned int start_offset, 386 unsigned int *feature_count /* IN/OUT */, 387 unsigned int *feature_indexes /* OUT */) 388{ 389 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 390 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 391 392 return l.get_feature_indexes (start_offset, feature_count, feature_indexes); 393} 394 395unsigned int 396hb_ot_layout_language_get_feature_tags (hb_face_t *face, 397 hb_tag_t table_tag, 398 unsigned int script_index, 399 unsigned int language_index, 400 unsigned int start_offset, 401 unsigned int *feature_count /* IN/OUT */, 402 hb_tag_t *feature_tags /* OUT */) 403{ 404 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 405 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 406 407 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t)); 408 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags); 409 410 if (feature_tags) { 411 unsigned int count = *feature_count; 412 for (unsigned int i = 0; i < count; i++) 413 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]); 414 } 415 416 return ret; 417} 418 419 420hb_bool_t 421hb_ot_layout_language_find_feature (hb_face_t *face, 422 hb_tag_t table_tag, 423 unsigned int script_index, 424 unsigned int language_index, 425 hb_tag_t feature_tag, 426 unsigned int *feature_index) 427{ 428 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX); 429 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 430 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index); 431 432 unsigned int num_features = l.get_feature_count (); 433 for (unsigned int i = 0; i < num_features; i++) { 434 unsigned int f_index = l.get_feature_index (i); 435 436 if (feature_tag == g.get_feature_tag (f_index)) { 437 if (feature_index) *feature_index = f_index; 438 return true; 439 } 440 } 441 442 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX; 443 return false; 444} 445 446unsigned int 447hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face, 448 hb_tag_t table_tag, 449 unsigned int feature_index, 450 unsigned int start_offset, 451 unsigned int *lookup_count /* IN/OUT */, 452 unsigned int *lookup_indexes /* OUT */) 453{ 454 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag); 455 const Feature &f = g.get_feature (feature_index); 456 457 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes); 458} 459 460 461/* 462 * GSUB 463 */ 464 465hb_bool_t 466hb_ot_layout_has_substitution (hb_face_t *face) 467{ 468 return &_get_gsub (face) != &Null(GSUB); 469} 470 471hb_bool_t 472hb_ot_layout_would_substitute_lookup (hb_face_t *face, 473 const hb_codepoint_t *glyphs, 474 unsigned int glyphs_length, 475 unsigned int lookup_index) 476{ 477 if (unlikely (glyphs_length < 1 || glyphs_length > 2)) return false; 478 hb_would_apply_context_t c (face, glyphs[0], glyphs_length == 2 ? glyphs[1] : -1); 479 return _get_gsub (face).would_substitute_lookup (&c, lookup_index); 480} 481 482void 483hb_ot_layout_substitute_start (hb_buffer_t *buffer) 484{ 485 GSUB::substitute_start (buffer); 486} 487 488hb_bool_t 489hb_ot_layout_substitute_lookup (hb_face_t *face, 490 hb_buffer_t *buffer, 491 unsigned int lookup_index, 492 hb_mask_t mask) 493{ 494 hb_apply_context_t c (NULL, face, buffer, mask); 495 return _get_gsub (face).substitute_lookup (&c, lookup_index); 496} 497 498void 499hb_ot_layout_substitute_finish (hb_buffer_t *buffer HB_UNUSED) 500{ 501 GSUB::substitute_finish (buffer); 502} 503 504void 505hb_ot_layout_substitute_closure_lookup (hb_face_t *face, 506 hb_set_t *glyphs, 507 unsigned int lookup_index) 508{ 509 hb_closure_context_t c (face, glyphs); 510 _get_gsub (face).closure_lookup (&c, lookup_index); 511} 512 513/* 514 * GPOS 515 */ 516 517hb_bool_t 518hb_ot_layout_has_positioning (hb_face_t *face) 519{ 520 return &_get_gpos (face) != &Null(GPOS); 521} 522 523void 524hb_ot_layout_position_start (hb_buffer_t *buffer) 525{ 526 GPOS::position_start (buffer); 527} 528 529hb_bool_t 530hb_ot_layout_position_lookup (hb_font_t *font, 531 hb_buffer_t *buffer, 532 unsigned int lookup_index, 533 hb_mask_t mask) 534{ 535 hb_apply_context_t c (font, font->face, buffer, mask); 536 return _get_gpos (font->face).position_lookup (&c, lookup_index); 537} 538 539void 540hb_ot_layout_position_finish (hb_buffer_t *buffer) 541{ 542 GPOS::position_finish (buffer); 543} 544 545 546