2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2006 Behdad Esfahbod
4 * Copyright © 2007,2008,2009 Red Hat, Inc.
5 * Copyright © 2012,2013 Google, Inc.
7 * This is part of HarfBuzz, a text shaping library.
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and its documentation for any purpose, provided that the
12 * above copyright notice and the following two paragraphs appear in
13 * all copies of this software.
15 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
16 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
17 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
21 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27 * Red Hat Author(s): Behdad Esfahbod
28 * Google Author(s): Behdad Esfahbod
31 #include "hb-open-type-private.hh"
32 #include "hb-ot-layout-private.hh"
34 #include "hb-ot-layout-gdef-table.hh"
35 #include "hb-ot-layout-gsub-table.hh"
36 #include "hb-ot-layout-gpos-table.hh"
37 #include "hb-ot-layout-jstf-table.hh"
39 #include "hb-ot-map-private.hh"
45 HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
48 _hb_ot_layout_create (hb_face_t *face)
50 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
51 if (unlikely (!layout))
54 layout->gdef_blob = OT::Sanitizer<OT::GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
55 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
57 layout->gsub_blob = OT::Sanitizer<OT::GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
58 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
60 layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
61 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
65 * The ugly business of blacklisting individual fonts' tables happen here!
66 * See this thread for why we finally had to bend in and do this:
67 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
69 unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
70 unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
71 unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
73 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
74 || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
75 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
76 || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
77 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
78 || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
79 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
80 || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
81 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
82 || (490 == gdef_len && 41638 == gpos_len && 3046 == gsub_len)
83 /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
84 || (478 == gdef_len && 41902 == gpos_len && 3046 == gsub_len)
87 /* In certain versions of Times New Roman Italic and Bold Italic,
88 * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
89 * glyph class 3 (mark) in GDEF. Nuke the GDEF to avoid zero-width
91 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
93 if (3 == layout->gdef->get_glyph_class (5))
94 layout->gdef = &OT::Null(OT::GDEF);
97 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
98 || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
99 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
100 || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
101 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
102 || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
103 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
104 || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
105 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
106 || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
107 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
108 || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
109 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
110 || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
111 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
112 || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
113 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
114 || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
115 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
116 || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
117 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
118 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
119 || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
120 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
121 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
122 || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
125 /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
126 * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
127 * and the version of Cantarell shipped by Ubuntu 16.04.
128 * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
129 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
130 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
131 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
133 layout->gdef = &OT::Null(OT::GDEF);
137 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
138 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
140 layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
141 layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
143 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
144 (layout->gpos_lookup_count && !layout->gpos_accels)))
146 _hb_ot_layout_destroy (layout);
150 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
151 layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
152 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
153 layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
159 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
161 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
162 layout->gsub_accels[i].fini ();
163 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
164 layout->gpos_accels[i].fini ();
166 free (layout->gsub_accels);
167 free (layout->gpos_accels);
169 hb_blob_destroy (layout->gdef_blob);
170 hb_blob_destroy (layout->gsub_blob);
171 hb_blob_destroy (layout->gpos_blob);
176 static inline const OT::GDEF&
177 _get_gdef (hb_face_t *face)
179 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
180 return *hb_ot_layout_from_face (face)->gdef;
182 static inline const OT::GSUB&
183 _get_gsub (hb_face_t *face)
185 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
186 return *hb_ot_layout_from_face (face)->gsub;
188 static inline const OT::GPOS&
189 _get_gpos (hb_face_t *face)
191 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
192 return *hb_ot_layout_from_face (face)->gpos;
201 hb_ot_layout_has_glyph_classes (hb_face_t *face)
203 return _get_gdef (face).has_glyph_classes ();
207 * hb_ot_layout_get_glyph_class:
211 hb_ot_layout_glyph_class_t
212 hb_ot_layout_get_glyph_class (hb_face_t *face,
213 hb_codepoint_t glyph)
215 return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
219 * hb_ot_layout_get_glyphs_in_class:
224 hb_ot_layout_get_glyphs_in_class (hb_face_t *face,
225 hb_ot_layout_glyph_class_t klass,
226 hb_set_t *glyphs /* OUT */)
228 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
232 hb_ot_layout_get_attach_points (hb_face_t *face,
233 hb_codepoint_t glyph,
234 unsigned int start_offset,
235 unsigned int *point_count /* IN/OUT */,
236 unsigned int *point_array /* OUT */)
238 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
242 hb_ot_layout_get_ligature_carets (hb_font_t *font,
243 hb_direction_t direction,
244 hb_codepoint_t glyph,
245 unsigned int start_offset,
246 unsigned int *caret_count /* IN/OUT */,
247 int *caret_array /* OUT */)
249 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
257 static const OT::GSUBGPOS&
258 get_gsubgpos_table (hb_face_t *face,
262 case HB_OT_TAG_GSUB: return _get_gsub (face);
263 case HB_OT_TAG_GPOS: return _get_gpos (face);
264 default: return OT::Null(OT::GSUBGPOS);
270 hb_ot_layout_table_get_script_tags (hb_face_t *face,
272 unsigned int start_offset,
273 unsigned int *script_count /* IN/OUT */,
274 hb_tag_t *script_tags /* OUT */)
276 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
278 return g.get_script_tags (start_offset, script_count, script_tags);
281 #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
284 hb_ot_layout_table_find_script (hb_face_t *face,
287 unsigned int *script_index)
289 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
290 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
292 if (g.find_script_index (script_tag, script_index))
295 /* try finding 'DFLT' */
296 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
299 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
300 * including many versions of DejaVu Sans Mono! */
301 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
304 /* try with 'latn'; some old fonts put their features there even though
305 they're really trying to support Thai, for example :( */
306 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
309 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
314 hb_ot_layout_table_choose_script (hb_face_t *face,
316 const hb_tag_t *script_tags,
317 unsigned int *script_index,
318 hb_tag_t *chosen_script)
320 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
321 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
325 if (g.find_script_index (*script_tags, script_index)) {
327 *chosen_script = *script_tags;
333 /* try finding 'DFLT' */
334 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
336 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
340 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
341 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
343 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
347 /* try with 'latn'; some old fonts put their features there even though
348 they're really trying to support Thai, for example :( */
349 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
351 *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
355 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
357 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
362 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
364 unsigned int start_offset,
365 unsigned int *feature_count /* IN/OUT */,
366 hb_tag_t *feature_tags /* OUT */)
368 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
370 return g.get_feature_tags (start_offset, feature_count, feature_tags);
374 hb_ot_layout_table_find_feature (hb_face_t *face,
376 hb_tag_t feature_tag,
377 unsigned int *feature_index)
379 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
380 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
382 unsigned int num_features = g.get_feature_count ();
383 for (unsigned int i = 0; i < num_features; i++)
385 if (feature_tag == g.get_feature_tag (i)) {
386 if (feature_index) *feature_index = i;
391 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
397 hb_ot_layout_script_get_language_tags (hb_face_t *face,
399 unsigned int script_index,
400 unsigned int start_offset,
401 unsigned int *language_count /* IN/OUT */,
402 hb_tag_t *language_tags /* OUT */)
404 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
406 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
410 hb_ot_layout_script_find_language (hb_face_t *face,
412 unsigned int script_index,
413 hb_tag_t language_tag,
414 unsigned int *language_index)
416 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
417 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
419 if (s.find_lang_sys_index (language_tag, language_index))
422 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
423 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
426 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
431 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
433 unsigned int script_index,
434 unsigned int language_index,
435 unsigned int *feature_index)
437 return hb_ot_layout_language_get_required_feature (face,
446 * hb_ot_layout_language_get_required_feature:
451 hb_ot_layout_language_get_required_feature (hb_face_t *face,
453 unsigned int script_index,
454 unsigned int language_index,
455 unsigned int *feature_index,
456 hb_tag_t *feature_tag)
458 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
459 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
461 unsigned int index = l.get_required_feature_index ();
462 if (feature_index) *feature_index = index;
463 if (feature_tag) *feature_tag = g.get_feature_tag (index);
465 return l.has_required_feature ();
469 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
471 unsigned int script_index,
472 unsigned int language_index,
473 unsigned int start_offset,
474 unsigned int *feature_count /* IN/OUT */,
475 unsigned int *feature_indexes /* OUT */)
477 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
478 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
480 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
484 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
486 unsigned int script_index,
487 unsigned int language_index,
488 unsigned int start_offset,
489 unsigned int *feature_count /* IN/OUT */,
490 hb_tag_t *feature_tags /* OUT */)
492 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
493 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
495 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
496 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
499 unsigned int count = *feature_count;
500 for (unsigned int i = 0; i < count; i++)
501 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
509 hb_ot_layout_language_find_feature (hb_face_t *face,
511 unsigned int script_index,
512 unsigned int language_index,
513 hb_tag_t feature_tag,
514 unsigned int *feature_index)
516 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
517 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
518 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
520 unsigned int num_features = l.get_feature_count ();
521 for (unsigned int i = 0; i < num_features; i++) {
522 unsigned int f_index = l.get_feature_index (i);
524 if (feature_tag == g.get_feature_tag (f_index)) {
525 if (feature_index) *feature_index = f_index;
530 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
535 * hb_ot_layout_feature_get_lookups:
540 hb_ot_layout_feature_get_lookups (hb_face_t *face,
542 unsigned int feature_index,
543 unsigned int start_offset,
544 unsigned int *lookup_count /* IN/OUT */,
545 unsigned int *lookup_indexes /* OUT */)
547 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
548 const OT::Feature &f = g.get_feature (feature_index);
550 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
554 * hb_ot_layout_table_get_lookup_count:
559 hb_ot_layout_table_get_lookup_count (hb_face_t *face,
566 return hb_ot_layout_from_face (face)->gsub_lookup_count;
570 return hb_ot_layout_from_face (face)->gpos_lookup_count;
577 _hb_ot_layout_collect_lookups_lookups (hb_face_t *face,
579 unsigned int feature_index,
580 hb_set_t *lookup_indexes /* OUT */)
582 unsigned int lookup_indices[32];
583 unsigned int offset, len;
587 len = ARRAY_LENGTH (lookup_indices);
588 hb_ot_layout_feature_get_lookups (face,
594 for (unsigned int i = 0; i < len; i++)
595 lookup_indexes->add (lookup_indices[i]);
598 } while (len == ARRAY_LENGTH (lookup_indices));
602 _hb_ot_layout_collect_lookups_features (hb_face_t *face,
604 unsigned int script_index,
605 unsigned int language_index,
606 const hb_tag_t *features,
607 hb_set_t *lookup_indexes /* OUT */)
611 unsigned int required_feature_index;
612 if (hb_ot_layout_language_get_required_feature (face,
616 &required_feature_index,
618 _hb_ot_layout_collect_lookups_lookups (face,
620 required_feature_index,
624 unsigned int feature_indices[32];
625 unsigned int offset, len;
629 len = ARRAY_LENGTH (feature_indices);
630 hb_ot_layout_language_get_feature_indexes (face,
637 for (unsigned int i = 0; i < len; i++)
638 _hb_ot_layout_collect_lookups_lookups (face,
644 } while (len == ARRAY_LENGTH (feature_indices));
648 for (; *features; features++)
650 unsigned int feature_index;
651 if (hb_ot_layout_language_find_feature (face,
657 _hb_ot_layout_collect_lookups_lookups (face,
666 _hb_ot_layout_collect_lookups_languages (hb_face_t *face,
668 unsigned int script_index,
669 const hb_tag_t *languages,
670 const hb_tag_t *features,
671 hb_set_t *lookup_indexes /* OUT */)
673 _hb_ot_layout_collect_lookups_features (face,
676 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
683 unsigned int count = hb_ot_layout_script_get_language_tags (face,
687 for (unsigned int language_index = 0; language_index < count; language_index++)
688 _hb_ot_layout_collect_lookups_features (face,
697 for (; *languages; languages++)
699 unsigned int language_index;
700 if (hb_ot_layout_script_find_language (face,
705 _hb_ot_layout_collect_lookups_features (face,
716 * hb_ot_layout_collect_lookups:
721 hb_ot_layout_collect_lookups (hb_face_t *face,
723 const hb_tag_t *scripts,
724 const hb_tag_t *languages,
725 const hb_tag_t *features,
726 hb_set_t *lookup_indexes /* OUT */)
731 unsigned int count = hb_ot_layout_table_get_script_tags (face,
734 for (unsigned int script_index = 0; script_index < count; script_index++)
735 _hb_ot_layout_collect_lookups_languages (face,
744 for (; *scripts; scripts++)
746 unsigned int script_index;
747 if (hb_ot_layout_table_find_script (face,
751 _hb_ot_layout_collect_lookups_languages (face,
762 * hb_ot_layout_lookup_collect_glyphs:
767 hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
769 unsigned int lookup_index,
770 hb_set_t *glyphs_before, /* OUT. May be NULL */
771 hb_set_t *glyphs_input, /* OUT. May be NULL */
772 hb_set_t *glyphs_after, /* OUT. May be NULL */
773 hb_set_t *glyphs_output /* OUT. May be NULL */)
775 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
777 OT::hb_collect_glyphs_context_t c (face,
787 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
788 l.collect_glyphs (&c);
793 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
794 l.collect_glyphs (&c);
806 hb_ot_layout_has_substitution (hb_face_t *face)
808 return &_get_gsub (face) != &OT::Null(OT::GSUB);
812 * hb_ot_layout_lookup_would_substitute:
817 hb_ot_layout_lookup_would_substitute (hb_face_t *face,
818 unsigned int lookup_index,
819 const hb_codepoint_t *glyphs,
820 unsigned int glyphs_length,
821 hb_bool_t zero_context)
823 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
824 return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
828 hb_ot_layout_lookup_would_substitute_fast (hb_face_t *face,
829 unsigned int lookup_index,
830 const hb_codepoint_t *glyphs,
831 unsigned int glyphs_length,
832 hb_bool_t zero_context)
834 if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
835 OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
837 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
839 return l.would_apply (&c, &hb_ot_layout_from_face (face)->gsub_accels[lookup_index]);
843 hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
845 OT::GSUB::substitute_start (font, buffer);
849 * hb_ot_layout_lookup_substitute_closure:
854 hb_ot_layout_lookup_substitute_closure (hb_face_t *face,
855 unsigned int lookup_index,
858 OT::hb_closure_context_t c (face, glyphs);
860 const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
870 hb_ot_layout_has_positioning (hb_face_t *face)
872 return &_get_gpos (face) != &OT::Null(OT::GPOS);
876 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
878 OT::GPOS::position_start (font, buffer);
882 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
884 OT::GPOS::position_finish_advances (font, buffer);
888 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
890 OT::GPOS::position_finish_offsets (font, buffer);
894 * hb_ot_layout_get_size_params:
899 hb_ot_layout_get_size_params (hb_face_t *face,
900 unsigned int *design_size, /* OUT. May be NULL */
901 unsigned int *subfamily_id, /* OUT. May be NULL */
902 unsigned int *subfamily_name_id, /* OUT. May be NULL */
903 unsigned int *range_start, /* OUT. May be NULL */
904 unsigned int *range_end /* OUT. May be NULL */)
906 const OT::GPOS &gpos = _get_gpos (face);
907 const hb_tag_t tag = HB_TAG ('s','i','z','e');
909 unsigned int num_features = gpos.get_feature_count ();
910 for (unsigned int i = 0; i < num_features; i++)
912 if (tag == gpos.get_feature_tag (i))
914 const OT::Feature &f = gpos.get_feature (i);
915 const OT::FeatureParamsSize ¶ms = f.get_feature_params ().get_size_params (tag);
917 if (params.designSize)
919 #define PARAM(a, A) if (a) *a = params.A
920 PARAM (design_size, designSize);
921 PARAM (subfamily_id, subfamilyID);
922 PARAM (subfamily_name_id, subfamilyNameID);
923 PARAM (range_start, rangeStart);
924 PARAM (range_end, rangeEnd);
932 #define PARAM(a, A) if (a) *a = 0
933 PARAM (design_size, designSize);
934 PARAM (subfamily_id, subfamilyID);
935 PARAM (subfamily_name_id, subfamilyNameID);
936 PARAM (range_start, rangeStart);
937 PARAM (range_end, rangeEnd);
945 * Parts of different types are implemented here such that they have direct
946 * access to GSUB/GPOS lookups.
952 static const unsigned int table_index = 0;
953 static const bool inplace = false;
954 typedef OT::SubstLookup Lookup;
956 GSUBProxy (hb_face_t *face) :
957 table (*hb_ot_layout_from_face (face)->gsub),
958 accels (hb_ot_layout_from_face (face)->gsub_accels) {}
960 const OT::GSUB &table;
961 const hb_ot_layout_lookup_accelerator_t *accels;
966 static const unsigned int table_index = 1;
967 static const bool inplace = true;
968 typedef OT::PosLookup Lookup;
970 GPOSProxy (hb_face_t *face) :
971 table (*hb_ot_layout_from_face (face)->gpos),
972 accels (hb_ot_layout_from_face (face)->gpos_accels) {}
974 const OT::GPOS &table;
975 const hb_ot_layout_lookup_accelerator_t *accels;
979 struct hb_get_subtables_context_t :
980 OT::hb_dispatch_context_t<hb_get_subtables_context_t, hb_void_t, HB_DEBUG_APPLY>
982 template <typename Type>
983 static inline bool apply_to (const void *obj, OT::hb_apply_context_t *c)
985 const Type *typed_obj = (const Type *) obj;
986 return typed_obj->apply (c);
989 typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_apply_context_t *c);
991 struct hb_applicable_t
993 inline void init (const void *obj_, hb_apply_func_t apply_func_)
996 apply_func = apply_func_;
999 inline bool apply (OT::hb_apply_context_t *c) const { return apply_func (obj, c); }
1003 hb_apply_func_t apply_func;
1006 typedef hb_auto_array_t<hb_applicable_t> array_t;
1008 /* Dispatch interface. */
1009 inline const char *get_name (void) { return "GET_SUBTABLES"; }
1010 template <typename T>
1011 inline return_t dispatch (const T &obj)
1013 hb_applicable_t *entry = array.push();
1015 entry->init (&obj, apply_to<T>);
1018 static return_t default_return_value (void) { return HB_VOID; }
1019 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
1021 hb_get_subtables_context_t (array_t &array_) :
1026 unsigned int debug_depth;
1030 apply_forward (OT::hb_apply_context_t *c,
1031 const hb_ot_layout_lookup_accelerator_t &accel,
1032 const hb_get_subtables_context_t::array_t &subtables)
1035 hb_buffer_t *buffer = c->buffer;
1036 while (buffer->idx < buffer->len && !buffer->in_error)
1038 bool applied = false;
1039 if (accel.may_have (buffer->cur().codepoint) &&
1040 (buffer->cur().mask & c->lookup_mask) &&
1041 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1043 for (unsigned int i = 0; i < subtables.len; i++)
1044 if (subtables[i].apply (c))
1054 buffer->next_glyph ();
1060 apply_backward (OT::hb_apply_context_t *c,
1061 const hb_ot_layout_lookup_accelerator_t &accel,
1062 const hb_get_subtables_context_t::array_t &subtables)
1065 hb_buffer_t *buffer = c->buffer;
1068 if (accel.may_have (buffer->cur().codepoint) &&
1069 (buffer->cur().mask & c->lookup_mask) &&
1070 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1072 for (unsigned int i = 0; i < subtables.len; i++)
1073 if (subtables[i].apply (c))
1079 /* The reverse lookup doesn't "advance" cursor (for good reason). */
1083 while ((int) buffer->idx >= 0);
1087 template <typename Proxy>
1089 apply_string (OT::hb_apply_context_t *c,
1090 const typename Proxy::Lookup &lookup,
1091 const hb_ot_layout_lookup_accelerator_t &accel)
1093 hb_buffer_t *buffer = c->buffer;
1095 if (unlikely (!buffer->len || !c->lookup_mask))
1098 c->set_lookup_props (lookup.get_props ());
1100 hb_get_subtables_context_t::array_t subtables;
1101 hb_get_subtables_context_t c_get_subtables (subtables);
1102 lookup.dispatch (&c_get_subtables);
1104 if (likely (!lookup.is_reverse ()))
1106 /* in/out forward substitution/positioning */
1107 if (Proxy::table_index == 0)
1108 buffer->clear_output ();
1112 ret = apply_forward (c, accel, subtables);
1115 if (!Proxy::inplace)
1116 buffer->swap_buffers ();
1118 assert (!buffer->has_separate_output ());
1123 /* in-place backward substitution/positioning */
1124 if (Proxy::table_index == 0)
1125 buffer->remove_output ();
1126 buffer->idx = buffer->len - 1;
1128 apply_backward (c, accel, subtables);
1132 template <typename Proxy>
1133 inline void hb_ot_map_t::apply (const Proxy &proxy,
1134 const hb_ot_shape_plan_t *plan,
1136 hb_buffer_t *buffer) const
1138 const unsigned int table_index = proxy.table_index;
1140 OT::hb_apply_context_t c (table_index, font, buffer);
1141 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1143 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1144 const stage_map_t *stage = &stages[table_index][stage_index];
1145 for (; i < stage->last_lookup; i++)
1147 unsigned int lookup_index = lookups[table_index][i].index;
1148 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1149 c.set_lookup_index (lookup_index);
1150 c.set_lookup_mask (lookups[table_index][i].mask);
1151 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1152 apply_string<Proxy> (&c,
1153 proxy.table.get_lookup (lookup_index),
1154 proxy.accels[lookup_index]);
1155 (void) buffer->message (font, "end lookup %d", lookup_index);
1158 if (stage->pause_func)
1160 buffer->clear_output ();
1161 stage->pause_func (plan, font, buffer);
1166 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1168 GSUBProxy proxy (font->face);
1169 apply (proxy, plan, font, buffer);
1172 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1174 GPOSProxy proxy (font->face);
1175 apply (proxy, plan, font, buffer);
1179 hb_ot_layout_substitute_lookup (OT::hb_apply_context_t *c,
1180 const OT::SubstLookup &lookup,
1181 const hb_ot_layout_lookup_accelerator_t &accel)
1183 apply_string<GSUBProxy> (c, lookup, accel);