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-base-table.hh"
35 #include "hb-ot-layout-gdef-table.hh"
36 #include "hb-ot-layout-gsub-table.hh"
37 #include "hb-ot-layout-gpos-table.hh"
38 #include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise.
39 #include "hb-ot-name-table.hh" // Just so we compile it; unused otherwise.
40 #include "hb-ot-color-colr-table.hh"
41 #include "hb-ot-color-cpal-table.hh"
43 #include "hb-ot-map-private.hh"
46 #ifndef HB_NO_VISIBILITY
47 const void * const OT::_hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] = {};
52 _hb_ot_layout_create (hb_face_t *face)
54 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
55 if (unlikely (!layout))
58 layout->gdef_blob = OT::Sanitizer<OT::GDEF>().sanitize (face->reference_table (HB_OT_TAG_GDEF));
59 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
61 layout->gsub_blob = OT::Sanitizer<OT::GSUB>().sanitize (face->reference_table (HB_OT_TAG_GSUB));
62 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
64 layout->gpos_blob = OT::Sanitizer<OT::GPOS>().sanitize (face->reference_table (HB_OT_TAG_GPOS));
65 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
67 layout->base.init (face);
68 layout->colr.init (face);
69 layout->cpal.init (face);
70 layout->math.init (face);
71 layout->fvar.init (face);
72 layout->avar.init (face);
73 layout->ankr.init (face);
74 layout->kerx.init (face);
75 layout->morx.init (face);
76 layout->trak.init (face);
80 * The ugly business of blacklisting individual fonts' tables happen here!
81 * See this thread for why we finally had to bend in and do this:
82 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
84 unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
85 unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
86 unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
88 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
89 || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
90 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
91 || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
92 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
93 || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
94 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
95 || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
96 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
97 || (490 == gdef_len && 41638 == gpos_len && 3046 == gsub_len)
98 /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
99 || (478 == gdef_len && 41902 == gpos_len && 3046 == gsub_len)
102 /* In certain versions of Times New Roman Italic and Bold Italic,
103 * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
104 * glyph class 3 (mark) in GDEF. Nuke the GDEF to avoid zero-width
106 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
108 if (3 == layout->gdef->get_glyph_class (5))
109 layout->gdef = &OT::Null(OT::GDEF);
112 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
113 || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
114 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
115 || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
116 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
117 || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
118 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
119 || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
120 /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
121 || (964 == gdef_len && 60072 == gpos_len && 23836 == gsub_len)
122 /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
123 || (976 == gdef_len && 61456 == gpos_len && 23832 == gsub_len)
124 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
125 || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
126 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
127 || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
128 /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
129 || (1006 == gdef_len && 61346 == gpos_len && 24576 == gsub_len)
130 /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
131 || (1018 == gdef_len && 62828 == gpos_len && 24572 == gsub_len)
132 /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5 tahoma.ttf from Windows 10 AU */
133 || (1006 == gdef_len && 61352 == gpos_len && 24576 == gsub_len)
134 /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2 tahomabd.ttf from Windows 10 AU */
135 || (1018 == gdef_len && 62834 == gpos_len && 24572 == gsub_len)
136 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
137 || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
138 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
139 || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
140 /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc himalaya.ttf from Windows 7 */
141 || (180 == gdef_len && 7254 == gpos_len && 13054 == gsub_len)
142 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
143 || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
144 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
145 || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
146 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
147 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
148 || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
149 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
150 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
151 || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
152 /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
153 || (1058 == gdef_len && 11818 == gpos_len && 47032 == gsub_len)
154 /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
155 || (1046 == gdef_len && 12600 == gpos_len && 47030 == gsub_len)
156 /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
157 || (1058 == gdef_len && 16770 == gpos_len && 71796 == gsub_len)
158 /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
159 || (1046 == gdef_len && 17862 == gpos_len && 71790 == gsub_len)
160 /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
161 || (1046 == gdef_len && 17112 == gpos_len && 71788 == gsub_len)
162 /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
163 || (1058 == gdef_len && 17514 == gpos_len && 71794 == gsub_len)
164 /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
165 || (1330 == gdef_len && 57938 == gpos_len && 109904 == gsub_len)
166 /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
167 || (1330 == gdef_len && 58972 == gpos_len && 109904 == gsub_len)
168 /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85 Padauk.ttf
169 * "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
170 || (1004 == gdef_len && 14836 == gpos_len && 59092 == gsub_len)
173 /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
174 * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
175 * and the version of Cantarell shipped by Ubuntu 16.04.
176 * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
177 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
178 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
179 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
181 layout->gdef = &OT::Null(OT::GDEF);
185 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
186 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
188 layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
189 layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
191 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
192 (layout->gpos_lookup_count && !layout->gpos_accels)))
194 _hb_ot_layout_destroy (layout);
198 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
199 layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
200 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
201 layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
207 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
209 if (layout->gsub_accels)
210 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
211 layout->gsub_accels[i].fini ();
212 if (layout->gpos_accels)
213 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
214 layout->gpos_accels[i].fini ();
216 free (layout->gsub_accels);
217 free (layout->gpos_accels);
219 hb_blob_destroy (layout->gdef_blob);
220 hb_blob_destroy (layout->gsub_blob);
221 hb_blob_destroy (layout->gpos_blob);
223 layout->base.fini ();
224 layout->colr.fini ();
225 layout->cpal.fini ();
226 layout->math.fini ();
227 layout->fvar.fini ();
228 layout->avar.fini ();
229 layout->ankr.fini ();
230 layout->kerx.fini ();
231 layout->morx.fini ();
232 layout->trak.fini ();
237 // static inline const OT::BASE&
238 // _get_base (hb_face_t *face)
240 // if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::BASE);
241 // hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
242 // return *(layout->base.get ());
245 static inline const OT::GDEF&
246 _get_gdef (hb_face_t *face)
248 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
249 return *hb_ot_layout_from_face (face)->gdef;
251 static inline const OT::GSUB&
252 _get_gsub (hb_face_t *face)
254 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
255 return *hb_ot_layout_from_face (face)->gsub;
257 static inline const OT::GPOS&
258 _get_gpos (hb_face_t *face)
260 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
261 return *hb_ot_layout_from_face (face)->gpos;
269 hb_ot_layout_has_glyph_classes (hb_face_t *face)
271 return _get_gdef (face).has_glyph_classes ();
275 * hb_ot_layout_get_glyph_class:
279 hb_ot_layout_glyph_class_t
280 hb_ot_layout_get_glyph_class (hb_face_t *face,
281 hb_codepoint_t glyph)
283 return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
287 * hb_ot_layout_get_glyphs_in_class:
292 hb_ot_layout_get_glyphs_in_class (hb_face_t *face,
293 hb_ot_layout_glyph_class_t klass,
294 hb_set_t *glyphs /* OUT */)
296 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
300 hb_ot_layout_get_attach_points (hb_face_t *face,
301 hb_codepoint_t glyph,
302 unsigned int start_offset,
303 unsigned int *point_count /* IN/OUT */,
304 unsigned int *point_array /* OUT */)
306 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
310 hb_ot_layout_get_ligature_carets (hb_font_t *font,
311 hb_direction_t direction,
312 hb_codepoint_t glyph,
313 unsigned int start_offset,
314 unsigned int *caret_count /* IN/OUT */,
315 hb_position_t *caret_array /* OUT */)
317 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
325 static const OT::GSUBGPOS&
326 get_gsubgpos_table (hb_face_t *face,
330 case HB_OT_TAG_GSUB: return _get_gsub (face);
331 case HB_OT_TAG_GPOS: return _get_gpos (face);
332 default: return OT::Null(OT::GSUBGPOS);
338 hb_ot_layout_table_get_script_tags (hb_face_t *face,
340 unsigned int start_offset,
341 unsigned int *script_count /* IN/OUT */,
342 hb_tag_t *script_tags /* OUT */)
344 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
346 return g.get_script_tags (start_offset, script_count, script_tags);
349 #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
352 hb_ot_layout_table_find_script (hb_face_t *face,
355 unsigned int *script_index)
357 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
358 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
360 if (g.find_script_index (script_tag, script_index))
363 /* try finding 'DFLT' */
364 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
367 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
368 * including many versions of DejaVu Sans Mono! */
369 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
372 /* try with 'latn'; some old fonts put their features there even though
373 they're really trying to support Thai, for example :( */
374 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
377 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
382 hb_ot_layout_table_choose_script (hb_face_t *face,
384 const hb_tag_t *script_tags,
385 unsigned int *script_index,
386 hb_tag_t *chosen_script)
388 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
389 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
393 if (g.find_script_index (*script_tags, script_index)) {
395 *chosen_script = *script_tags;
401 /* try finding 'DFLT' */
402 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
404 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
408 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
409 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
411 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
415 /* try with 'latn'; some old fonts put their features there even though
416 they're really trying to support Thai, for example :( */
417 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
419 *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
423 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
425 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
430 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
432 unsigned int start_offset,
433 unsigned int *feature_count /* IN/OUT */,
434 hb_tag_t *feature_tags /* OUT */)
436 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
438 return g.get_feature_tags (start_offset, feature_count, feature_tags);
442 hb_ot_layout_table_find_feature (hb_face_t *face,
444 hb_tag_t feature_tag,
445 unsigned int *feature_index)
447 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
448 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
450 unsigned int num_features = g.get_feature_count ();
451 for (unsigned int i = 0; i < num_features; i++)
453 if (feature_tag == g.get_feature_tag (i)) {
454 if (feature_index) *feature_index = i;
459 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
465 hb_ot_layout_script_get_language_tags (hb_face_t *face,
467 unsigned int script_index,
468 unsigned int start_offset,
469 unsigned int *language_count /* IN/OUT */,
470 hb_tag_t *language_tags /* OUT */)
472 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
474 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
478 hb_ot_layout_script_find_language (hb_face_t *face,
480 unsigned int script_index,
481 hb_tag_t language_tag,
482 unsigned int *language_index)
484 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
485 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
487 if (s.find_lang_sys_index (language_tag, language_index))
490 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
491 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
494 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
499 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
501 unsigned int script_index,
502 unsigned int language_index,
503 unsigned int *feature_index)
505 return hb_ot_layout_language_get_required_feature (face,
514 * hb_ot_layout_language_get_required_feature:
519 hb_ot_layout_language_get_required_feature (hb_face_t *face,
521 unsigned int script_index,
522 unsigned int language_index,
523 unsigned int *feature_index,
524 hb_tag_t *feature_tag)
526 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
527 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
529 unsigned int index = l.get_required_feature_index ();
530 if (feature_index) *feature_index = index;
531 if (feature_tag) *feature_tag = g.get_feature_tag (index);
533 return l.has_required_feature ();
537 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
539 unsigned int script_index,
540 unsigned int language_index,
541 unsigned int start_offset,
542 unsigned int *feature_count /* IN/OUT */,
543 unsigned int *feature_indexes /* OUT */)
545 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
546 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
548 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
552 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
554 unsigned int script_index,
555 unsigned int language_index,
556 unsigned int start_offset,
557 unsigned int *feature_count /* IN/OUT */,
558 hb_tag_t *feature_tags /* OUT */)
560 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
561 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
563 static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
564 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
567 unsigned int count = *feature_count;
568 for (unsigned int i = 0; i < count; i++)
569 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
577 hb_ot_layout_language_find_feature (hb_face_t *face,
579 unsigned int script_index,
580 unsigned int language_index,
581 hb_tag_t feature_tag,
582 unsigned int *feature_index)
584 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
585 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
586 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
588 unsigned int num_features = l.get_feature_count ();
589 for (unsigned int i = 0; i < num_features; i++) {
590 unsigned int f_index = l.get_feature_index (i);
592 if (feature_tag == g.get_feature_tag (f_index)) {
593 if (feature_index) *feature_index = f_index;
598 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
603 * hb_ot_layout_feature_get_lookups:
608 hb_ot_layout_feature_get_lookups (hb_face_t *face,
610 unsigned int feature_index,
611 unsigned int start_offset,
612 unsigned int *lookup_count /* IN/OUT */,
613 unsigned int *lookup_indexes /* OUT */)
615 return hb_ot_layout_feature_with_variations_get_lookups (face,
618 HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
625 * hb_ot_layout_table_get_lookup_count:
630 hb_ot_layout_table_get_lookup_count (hb_face_t *face,
633 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return 0;
638 return hb_ot_layout_from_face (face)->gsub_lookup_count;
642 return hb_ot_layout_from_face (face)->gpos_lookup_count;
649 _hb_ot_layout_collect_lookups_lookups (hb_face_t *face,
651 unsigned int feature_index,
652 hb_set_t *lookup_indexes /* OUT */)
654 unsigned int lookup_indices[32];
655 unsigned int offset, len;
659 len = ARRAY_LENGTH (lookup_indices);
660 hb_ot_layout_feature_get_lookups (face,
666 for (unsigned int i = 0; i < len; i++)
667 lookup_indexes->add (lookup_indices[i]);
670 } while (len == ARRAY_LENGTH (lookup_indices));
674 _hb_ot_layout_collect_lookups_features (hb_face_t *face,
676 unsigned int script_index,
677 unsigned int language_index,
678 const hb_tag_t *features,
679 hb_set_t *lookup_indexes /* OUT */)
683 unsigned int required_feature_index;
684 if (hb_ot_layout_language_get_required_feature (face,
688 &required_feature_index,
690 _hb_ot_layout_collect_lookups_lookups (face,
692 required_feature_index,
696 unsigned int feature_indices[32];
697 unsigned int offset, len;
701 len = ARRAY_LENGTH (feature_indices);
702 hb_ot_layout_language_get_feature_indexes (face,
709 for (unsigned int i = 0; i < len; i++)
710 _hb_ot_layout_collect_lookups_lookups (face,
716 } while (len == ARRAY_LENGTH (feature_indices));
720 for (; *features; features++)
722 unsigned int feature_index;
723 if (hb_ot_layout_language_find_feature (face,
729 _hb_ot_layout_collect_lookups_lookups (face,
738 _hb_ot_layout_collect_lookups_languages (hb_face_t *face,
740 unsigned int script_index,
741 const hb_tag_t *languages,
742 const hb_tag_t *features,
743 hb_set_t *lookup_indexes /* OUT */)
745 _hb_ot_layout_collect_lookups_features (face,
748 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
755 unsigned int count = hb_ot_layout_script_get_language_tags (face,
758 0, nullptr, nullptr);
759 for (unsigned int language_index = 0; language_index < count; language_index++)
760 _hb_ot_layout_collect_lookups_features (face,
769 for (; *languages; languages++)
771 unsigned int language_index;
772 if (hb_ot_layout_script_find_language (face,
777 _hb_ot_layout_collect_lookups_features (face,
788 * hb_ot_layout_collect_lookups:
793 hb_ot_layout_collect_lookups (hb_face_t *face,
795 const hb_tag_t *scripts,
796 const hb_tag_t *languages,
797 const hb_tag_t *features,
798 hb_set_t *lookup_indexes /* OUT */)
803 unsigned int count = hb_ot_layout_table_get_script_tags (face,
805 0, nullptr, nullptr);
806 for (unsigned int script_index = 0; script_index < count; script_index++)
807 _hb_ot_layout_collect_lookups_languages (face,
816 for (; *scripts; scripts++)
818 unsigned int script_index;
819 if (hb_ot_layout_table_find_script (face,
823 _hb_ot_layout_collect_lookups_languages (face,
834 * hb_ot_layout_lookup_collect_glyphs:
839 hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
841 unsigned int lookup_index,
842 hb_set_t *glyphs_before, /* OUT. May be nullptr */
843 hb_set_t *glyphs_input, /* OUT. May be nullptr */
844 hb_set_t *glyphs_after, /* OUT. May be nullptr */
845 hb_set_t *glyphs_output /* OUT. May be nullptr */)
847 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
849 OT::hb_collect_glyphs_context_t c (face,
859 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
860 l.collect_glyphs (&c);
865 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
866 l.collect_glyphs (&c);
873 /* Variations support */
876 hb_ot_layout_table_find_feature_variations (hb_face_t *face,
879 unsigned int num_coords,
880 unsigned int *variations_index /* out */)
882 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
884 return g.find_variations_index (coords, num_coords, variations_index);
888 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face,
890 unsigned int feature_index,
891 unsigned int variations_index,
892 unsigned int start_offset,
893 unsigned int *lookup_count /* IN/OUT */,
894 unsigned int *lookup_indexes /* OUT */)
896 static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
897 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
899 const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
901 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
910 hb_ot_layout_has_substitution (hb_face_t *face)
912 return &_get_gsub (face) != &OT::Null(OT::GSUB);
916 * hb_ot_layout_lookup_would_substitute:
921 hb_ot_layout_lookup_would_substitute (hb_face_t *face,
922 unsigned int lookup_index,
923 const hb_codepoint_t *glyphs,
924 unsigned int glyphs_length,
925 hb_bool_t zero_context)
927 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
928 return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
932 hb_ot_layout_lookup_would_substitute_fast (hb_face_t *face,
933 unsigned int lookup_index,
934 const hb_codepoint_t *glyphs,
935 unsigned int glyphs_length,
936 hb_bool_t zero_context)
938 if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
939 OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
941 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
943 return l.would_apply (&c, &hb_ot_layout_from_face (face)->gsub_accels[lookup_index]);
947 hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
949 OT::GSUB::substitute_start (font, buffer);
953 * hb_ot_layout_lookup_substitute_closure:
958 hb_ot_layout_lookup_substitute_closure (hb_face_t *face,
959 unsigned int lookup_index,
962 OT::hb_closure_context_t c (face, glyphs);
964 const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
974 hb_ot_layout_has_positioning (hb_face_t *face)
976 return &_get_gpos (face) != &OT::Null(OT::GPOS);
980 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
982 OT::GPOS::position_start (font, buffer);
986 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
988 OT::GPOS::position_finish_advances (font, buffer);
992 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
994 OT::GPOS::position_finish_offsets (font, buffer);
998 * hb_ot_layout_get_size_params:
1003 hb_ot_layout_get_size_params (hb_face_t *face,
1004 unsigned int *design_size, /* OUT. May be nullptr */
1005 unsigned int *subfamily_id, /* OUT. May be nullptr */
1006 unsigned int *subfamily_name_id, /* OUT. May be nullptr */
1007 unsigned int *range_start, /* OUT. May be nullptr */
1008 unsigned int *range_end /* OUT. May be nullptr */)
1010 const OT::GPOS &gpos = _get_gpos (face);
1011 const hb_tag_t tag = HB_TAG ('s','i','z','e');
1013 unsigned int num_features = gpos.get_feature_count ();
1014 for (unsigned int i = 0; i < num_features; i++)
1016 if (tag == gpos.get_feature_tag (i))
1018 const OT::Feature &f = gpos.get_feature (i);
1019 const OT::FeatureParamsSize ¶ms = f.get_feature_params ().get_size_params (tag);
1021 if (params.designSize)
1023 #define PARAM(a, A) if (a) *a = params.A
1024 PARAM (design_size, designSize);
1025 PARAM (subfamily_id, subfamilyID);
1026 PARAM (subfamily_name_id, subfamilyNameID);
1027 PARAM (range_start, rangeStart);
1028 PARAM (range_end, rangeEnd);
1036 #define PARAM(a, A) if (a) *a = 0
1037 PARAM (design_size, designSize);
1038 PARAM (subfamily_id, subfamilyID);
1039 PARAM (subfamily_name_id, subfamilyNameID);
1040 PARAM (range_start, rangeStart);
1041 PARAM (range_end, rangeEnd);
1049 * Parts of different types are implemented here such that they have direct
1050 * access to GSUB/GPOS lookups.
1056 static const unsigned int table_index = 0;
1057 static const bool inplace = false;
1058 typedef OT::SubstLookup Lookup;
1060 GSUBProxy (hb_face_t *face) :
1061 table (*hb_ot_layout_from_face (face)->gsub),
1062 accels (hb_ot_layout_from_face (face)->gsub_accels) {}
1064 const OT::GSUB &table;
1065 const hb_ot_layout_lookup_accelerator_t *accels;
1070 static const unsigned int table_index = 1;
1071 static const bool inplace = true;
1072 typedef OT::PosLookup Lookup;
1074 GPOSProxy (hb_face_t *face) :
1075 table (*hb_ot_layout_from_face (face)->gpos),
1076 accels (hb_ot_layout_from_face (face)->gpos_accels) {}
1078 const OT::GPOS &table;
1079 const hb_ot_layout_lookup_accelerator_t *accels;
1083 struct hb_get_subtables_context_t :
1084 OT::hb_dispatch_context_t<hb_get_subtables_context_t, hb_void_t, HB_DEBUG_APPLY>
1086 template <typename Type>
1087 static inline bool apply_to (const void *obj, OT::hb_ot_apply_context_t *c)
1089 const Type *typed_obj = (const Type *) obj;
1090 return typed_obj->apply (c);
1093 typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_ot_apply_context_t *c);
1095 struct hb_applicable_t
1097 inline void init (const void *obj_, hb_apply_func_t apply_func_)
1100 apply_func = apply_func_;
1103 inline bool apply (OT::hb_ot_apply_context_t *c) const { return apply_func (obj, c); }
1107 hb_apply_func_t apply_func;
1110 typedef hb_auto_array_t<hb_applicable_t> array_t;
1112 /* Dispatch interface. */
1113 inline const char *get_name (void) { return "GET_SUBTABLES"; }
1114 template <typename T>
1115 inline return_t dispatch (const T &obj)
1117 hb_applicable_t *entry = array.push();
1119 entry->init (&obj, apply_to<T>);
1122 static return_t default_return_value (void) { return HB_VOID; }
1123 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
1125 hb_get_subtables_context_t (array_t &array_) :
1130 unsigned int debug_depth;
1134 apply_forward (OT::hb_ot_apply_context_t *c,
1135 const hb_ot_layout_lookup_accelerator_t &accel,
1136 const hb_get_subtables_context_t::array_t &subtables)
1139 hb_buffer_t *buffer = c->buffer;
1140 while (buffer->idx < buffer->len && !buffer->in_error)
1142 bool applied = false;
1143 if (accel.may_have (buffer->cur().codepoint) &&
1144 (buffer->cur().mask & c->lookup_mask) &&
1145 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1147 for (unsigned int i = 0; i < subtables.len; i++)
1148 if (subtables[i].apply (c))
1158 buffer->next_glyph ();
1164 apply_backward (OT::hb_ot_apply_context_t *c,
1165 const hb_ot_layout_lookup_accelerator_t &accel,
1166 const hb_get_subtables_context_t::array_t &subtables)
1169 hb_buffer_t *buffer = c->buffer;
1172 if (accel.may_have (buffer->cur().codepoint) &&
1173 (buffer->cur().mask & c->lookup_mask) &&
1174 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1176 for (unsigned int i = 0; i < subtables.len; i++)
1177 if (subtables[i].apply (c))
1183 /* The reverse lookup doesn't "advance" cursor (for good reason). */
1187 while ((int) buffer->idx >= 0);
1191 template <typename Proxy>
1193 apply_string (OT::hb_ot_apply_context_t *c,
1194 const typename Proxy::Lookup &lookup,
1195 const hb_ot_layout_lookup_accelerator_t &accel)
1197 hb_buffer_t *buffer = c->buffer;
1199 if (unlikely (!buffer->len || !c->lookup_mask))
1202 c->set_lookup_props (lookup.get_props ());
1204 hb_get_subtables_context_t::array_t subtables;
1205 hb_get_subtables_context_t c_get_subtables (subtables);
1206 lookup.dispatch (&c_get_subtables);
1208 if (likely (!lookup.is_reverse ()))
1210 /* in/out forward substitution/positioning */
1211 if (Proxy::table_index == 0)
1212 buffer->clear_output ();
1216 ret = apply_forward (c, accel, subtables);
1219 if (!Proxy::inplace)
1220 buffer->swap_buffers ();
1222 assert (!buffer->has_separate_output ());
1227 /* in-place backward substitution/positioning */
1228 if (Proxy::table_index == 0)
1229 buffer->remove_output ();
1230 buffer->idx = buffer->len - 1;
1232 apply_backward (c, accel, subtables);
1236 template <typename Proxy>
1237 inline void hb_ot_map_t::apply (const Proxy &proxy,
1238 const hb_ot_shape_plan_t *plan,
1240 hb_buffer_t *buffer) const
1242 const unsigned int table_index = proxy.table_index;
1244 OT::hb_ot_apply_context_t c (table_index, font, buffer);
1245 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1247 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1248 const stage_map_t *stage = &stages[table_index][stage_index];
1249 for (; i < stage->last_lookup; i++)
1251 unsigned int lookup_index = lookups[table_index][i].index;
1252 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1253 c.set_lookup_index (lookup_index);
1254 c.set_lookup_mask (lookups[table_index][i].mask);
1255 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1256 c.set_auto_zwnj (lookups[table_index][i].auto_zwnj);
1257 apply_string<Proxy> (&c,
1258 proxy.table.get_lookup (lookup_index),
1259 proxy.accels[lookup_index]);
1260 (void) buffer->message (font, "end lookup %d", lookup_index);
1263 if (stage->pause_func)
1265 buffer->clear_output ();
1266 stage->pause_func (plan, font, buffer);
1271 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1273 GSUBProxy proxy (font->face);
1274 apply (proxy, plan, font, buffer);
1277 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1279 GPOSProxy proxy (font->face);
1280 apply (proxy, plan, font, buffer);
1284 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
1285 const OT::SubstLookup &lookup,
1286 const hb_ot_layout_lookup_accelerator_t &accel)
1288 apply_string<GSUBProxy> (c, lookup, accel);
1299 // * hb_ot_base_has_data:
1300 // * @face: #hb_face_t to test
1302 // * This function allows to verify the presence of an OpenType BASE table on the
1305 // * Return value: true if face has a BASE table, false otherwise
1310 // hb_ot_base_has_data (hb_face_t *face)
1312 // return &_get_base (face) != &OT::Null(OT::BASE);