a599eea6e94f5b5cde9be731fdd195249e86c8a1
[platform/upstream/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
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.
6  *
7  *  This is part of HarfBuzz, a text shaping library.
8  *
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.
14  *
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
19  * DAMAGE.
20  *
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.
26  *
27  * Red Hat Author(s): Behdad Esfahbod
28  * Google Author(s): Behdad Esfahbod
29  */
30
31 #include "hb.hh"
32
33 #ifndef HB_NO_OT_LAYOUT
34
35 #ifdef HB_NO_OT_TAG
36 #error "Cannot compile hb-ot-layout.cc with HB_NO_OT_TAG."
37 #endif
38
39 #include "hb-open-type.hh"
40 #include "hb-ot-layout.hh"
41 #include "hb-ot-face.hh"
42 #include "hb-ot-map.hh"
43 #include "hb-map.hh"
44
45 #include "hb-ot-kern-table.hh"
46 #include "hb-ot-layout-gdef-table.hh"
47 #include "hb-ot-layout-gsub-table.hh"
48 #include "hb-ot-layout-gpos-table.hh"
49 #include "hb-ot-layout-base-table.hh" // Just so we compile it; unused otherwise.
50 #include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise.
51 #include "hb-ot-name-table.hh"
52 #include "hb-ot-os2-table.hh"
53
54 #include "hb-aat-layout-morx-table.hh"
55 #include "hb-aat-layout-opbd-table.hh" // Just so we compile it; unused otherwise.
56
57 /**
58  * SECTION:hb-ot-layout
59  * @title: hb-ot-layout
60  * @short_description: OpenType Layout
61  * @include: hb-ot.h
62  *
63  * Functions for querying OpenType Layout features in the font face.
64  **/
65
66
67 /*
68  * kern
69  */
70
71 #ifndef HB_NO_OT_KERN
72 /**
73  * hb_ot_layout_has_kerning:
74  * @face: The #hb_face_t to work on
75  *
76  * Tests whether a face includes any kerning data in the 'kern' table.
77  * Does NOT test for kerning lookups in the GPOS table.
78  *
79  * Return value: %true if data found, %false otherwise
80  *
81  **/
82 bool
83 hb_ot_layout_has_kerning (hb_face_t *face)
84 {
85   return face->table.kern->has_data ();
86 }
87
88 /**
89  * hb_ot_layout_has_machine_kerning:
90  * @face: The #hb_face_t to work on
91  *
92  * Tests whether a face includes any state-machine kerning in the 'kern' table.
93  * Does NOT examine the GPOS table.
94  *
95  * Return value: %true if data found, %false otherwise
96  *
97  **/
98 bool
99 hb_ot_layout_has_machine_kerning (hb_face_t *face)
100 {
101   return face->table.kern->has_state_machine ();
102 }
103
104 /**
105  * hb_ot_layout_has_cross_kerning:
106  * @face: The #hb_face_t to work on
107  *
108  * Tests whether a face has any cross-stream kerning (i.e., kerns
109  * that make adjustments perpendicular to the direction of the text
110  * flow: Y adjustments in horizontal text or X adjustments in
111  * vertical text) in the 'kern' table.
112  *
113  * Does NOT examine the GPOS table.
114  *
115  * Return value: %true is data found, %false otherwise
116  *
117  **/
118 bool
119 hb_ot_layout_has_cross_kerning (hb_face_t *face)
120 {
121   return face->table.kern->has_cross_stream ();
122 }
123
124 void
125 hb_ot_layout_kern (const hb_ot_shape_plan_t *plan,
126                    hb_font_t *font,
127                    hb_buffer_t  *buffer)
128 {
129   hb_blob_t *blob = font->face->table.kern.get_blob ();
130   const AAT::kern& kern = *blob->as<AAT::kern> ();
131
132   AAT::hb_aat_apply_context_t c (plan, font, buffer, blob);
133
134   if (!buffer->message (font, "start table kern")) return;
135   kern.apply (&c);
136   (void) buffer->message (font, "end table kern");
137 }
138 #endif
139
140
141 /*
142  * GDEF
143  */
144
145 bool
146 OT::GDEF::is_blocklisted (hb_blob_t *blob,
147                           hb_face_t *face) const
148 {
149 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
150   return false;
151 #endif
152   /* The ugly business of blocklisting individual fonts' tables happen here!
153    * See this thread for why we finally had to bend in and do this:
154    * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
155    *
156    * In certain versions of Times New Roman Italic and Bold Italic,
157    * ASCII double quotation mark U+0022 has wrong glyph class 3 (mark)
158    * in GDEF.  Many versions of Tahoma have bad GDEF tables that
159    * incorrectly classify some spacing marks such as certain IPA
160    * symbols as glyph class 3. So do older versions of Microsoft
161    * Himalaya, and the version of Cantarell shipped by Ubuntu 16.04.
162    *
163    * Nuke the GDEF tables of to avoid unwanted width-zeroing.
164    *
165    * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
166    *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
167    *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
168    */
169   switch HB_CODEPOINT_ENCODE3(blob->length,
170                               face->table.GSUB->table.get_length (),
171                               face->table.GPOS->table.get_length ())
172   {
173     /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
174     case HB_CODEPOINT_ENCODE3 (442, 2874, 42038):
175     /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
176     case HB_CODEPOINT_ENCODE3 (430, 2874, 40662):
177     /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
178     case HB_CODEPOINT_ENCODE3 (442, 2874, 39116):
179     /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
180     case HB_CODEPOINT_ENCODE3 (430, 2874, 39374):
181     /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
182     case HB_CODEPOINT_ENCODE3 (490, 3046, 41638):
183     /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
184     case HB_CODEPOINT_ENCODE3 (478, 3046, 41902):
185     /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c  tahoma.ttf from Windows 8 */
186     case HB_CODEPOINT_ENCODE3 (898, 12554, 46470):
187     /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc  tahomabd.ttf from Windows 8 */
188     case HB_CODEPOINT_ENCODE3 (910, 12566, 47732):
189     /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e  tahoma.ttf from Windows 8.1 */
190     case HB_CODEPOINT_ENCODE3 (928, 23298, 59332):
191     /* sha1sum:6d400781948517c3c0441ba42acb309584b73033  tahomabd.ttf from Windows 8.1 */
192     case HB_CODEPOINT_ENCODE3 (940, 23310, 60732):
193     /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
194     case HB_CODEPOINT_ENCODE3 (964, 23836, 60072):
195     /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
196     case HB_CODEPOINT_ENCODE3 (976, 23832, 61456):
197     /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846  tahoma.ttf from Windows 10 */
198     case HB_CODEPOINT_ENCODE3 (994, 24474, 60336):
199     /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343  tahomabd.ttf from Windows 10 */
200     case HB_CODEPOINT_ENCODE3 (1006, 24470, 61740):
201     /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
202     case HB_CODEPOINT_ENCODE3 (1006, 24576, 61346):
203     /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
204     case HB_CODEPOINT_ENCODE3 (1018, 24572, 62828):
205     /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5  tahoma.ttf from Windows 10 AU */
206     case HB_CODEPOINT_ENCODE3 (1006, 24576, 61352):
207     /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2  tahomabd.ttf from Windows 10 AU */
208     case HB_CODEPOINT_ENCODE3 (1018, 24572, 62834):
209     /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7  Tahoma.ttf from Mac OS X 10.9 */
210     case HB_CODEPOINT_ENCODE3 (832, 7324, 47162):
211     /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba  Tahoma Bold.ttf from Mac OS X 10.9 */
212     case HB_CODEPOINT_ENCODE3 (844, 7302, 45474):
213     /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc  himalaya.ttf from Windows 7 */
214     case HB_CODEPOINT_ENCODE3 (180, 13054, 7254):
215     /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0  himalaya.ttf from Windows 8 */
216     case HB_CODEPOINT_ENCODE3 (192, 12638, 7254):
217     /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427  himalaya.ttf from Windows 8.1 */
218     case HB_CODEPOINT_ENCODE3 (192, 12690, 7254):
219     /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44  cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
220     /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371  cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
221     case HB_CODEPOINT_ENCODE3 (188, 248, 3852):
222     /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f  cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
223     /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b  cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
224     case HB_CODEPOINT_ENCODE3 (188, 264, 3426):
225     /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
226     case HB_CODEPOINT_ENCODE3 (1058, 47032, 11818):
227     /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
228     case HB_CODEPOINT_ENCODE3 (1046, 47030, 12600):
229     /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
230     case HB_CODEPOINT_ENCODE3 (1058, 71796, 16770):
231     /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
232     case HB_CODEPOINT_ENCODE3 (1046, 71790, 17862):
233     /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
234     case HB_CODEPOINT_ENCODE3 (1046, 71788, 17112):
235     /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
236     case HB_CODEPOINT_ENCODE3 (1058, 71794, 17514):
237     /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
238     case HB_CODEPOINT_ENCODE3 (1330, 109904, 57938):
239     /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
240     case HB_CODEPOINT_ENCODE3 (1330, 109904, 58972):
241     /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85  Padauk.ttf
242      *  "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
243     case HB_CODEPOINT_ENCODE3 (1004, 59092, 14836):
244       return true;
245   }
246   return false;
247 }
248
249 static void
250 _hb_ot_layout_set_glyph_props (hb_font_t *font,
251                                hb_buffer_t *buffer)
252 {
253   _hb_buffer_assert_gsubgpos_vars (buffer);
254
255   const OT::GDEF &gdef = *font->face->table.GDEF->table;
256   unsigned int count = buffer->len;
257   for (unsigned int i = 0; i < count; i++)
258   {
259     _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
260     _hb_glyph_info_clear_lig_props (&buffer->info[i]);
261     buffer->info[i].syllable() = 0;
262   }
263 }
264
265 /* Public API */
266
267 /**
268  * hb_ot_layout_has_glyph_classes:
269  * @face: #hb_face_t to work upon
270  *
271  * Tests whether a face has any glyph classes defined in its GDEF table.
272  *
273  * Return value: %true if data found, %false otherwise
274  *
275  **/
276 hb_bool_t
277 hb_ot_layout_has_glyph_classes (hb_face_t *face)
278 {
279   return face->table.GDEF->table->has_glyph_classes ();
280 }
281
282 /**
283  * hb_ot_layout_get_glyph_class:
284  * @face: The #hb_face_t to work on
285  * @glyph: The #hb_codepoint_t code point to query
286  *
287  * Fetches the GDEF class of the requested glyph in the specified face.
288  *
289  * Return value: The #hb_ot_layout_glyph_class_t glyph class of the given code
290  * point in the GDEF table of the face.
291  *
292  * Since: 0.9.7
293  **/
294 hb_ot_layout_glyph_class_t
295 hb_ot_layout_get_glyph_class (hb_face_t      *face,
296                               hb_codepoint_t  glyph)
297 {
298   return (hb_ot_layout_glyph_class_t) face->table.GDEF->table->get_glyph_class (glyph);
299 }
300
301 /**
302  * hb_ot_layout_get_glyphs_in_class:
303  * @face: The #hb_face_t to work on
304  * @klass: The #hb_ot_layout_glyph_class_t GDEF class to retrieve
305  * @glyphs: (out): The #hb_set_t set of all glyphs belonging to the requested
306  *          class.
307  *
308  * Retrieves the set of all glyphs from the face that belong to the requested
309  * glyph class in the face's GDEF table.
310  *
311  * Since: 0.9.7
312  **/
313 void
314 hb_ot_layout_get_glyphs_in_class (hb_face_t                  *face,
315                                   hb_ot_layout_glyph_class_t  klass,
316                                   hb_set_t                   *glyphs /* OUT */)
317 {
318   return face->table.GDEF->table->get_glyphs_in_class (klass, glyphs);
319 }
320
321 #ifndef HB_NO_LAYOUT_UNUSED
322 /**
323  * hb_ot_layout_get_attach_points:
324  * @face: The #hb_face_t to work on
325  * @glyph: The #hb_codepoint_t code point to query
326  * @start_offset: offset of the first attachment point to retrieve
327  * @point_count: (inout) (optional): Input = the maximum number of attachment points to return;
328  *               Output = the actual number of attachment points returned (may be zero)
329  * @point_array: (out) (array length=point_count): The array of attachment points found for the query
330  *
331  * Fetches a list of all attachment points for the specified glyph in the GDEF
332  * table of the face. The list returned will begin at the offset provided.
333  *
334  * Useful if the client program wishes to cache the list.
335  *
336  * Return value: Total number of attachment points for @glyph.
337  *
338  **/
339 unsigned int
340 hb_ot_layout_get_attach_points (hb_face_t      *face,
341                                 hb_codepoint_t  glyph,
342                                 unsigned int    start_offset,
343                                 unsigned int   *point_count /* IN/OUT */,
344                                 unsigned int   *point_array /* OUT */)
345 {
346   return face->table.GDEF->table->get_attach_points (glyph,
347                                                      start_offset,
348                                                      point_count,
349                                                      point_array);
350 }
351 /**
352  * hb_ot_layout_get_ligature_carets:
353  * @font: The #hb_font_t to work on
354  * @direction: The #hb_direction_t text direction to use
355  * @glyph: The #hb_codepoint_t code point to query
356  * @start_offset: offset of the first caret position to retrieve
357  * @caret_count: (inout) (optional): Input = the maximum number of caret positions to return;
358  *               Output = the actual number of caret positions returned (may be zero)
359  * @caret_array: (out) (array length=caret_count): The array of caret positions found for the query
360  *
361  * Fetches a list of the caret positions defined for a ligature glyph in the GDEF
362  * table of the font. The list returned will begin at the offset provided.
363  *
364  * Return value: Total number of ligature caret positions for @glyph.
365  *
366  **/
367 unsigned int
368 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
369                                   hb_direction_t  direction,
370                                   hb_codepoint_t  glyph,
371                                   unsigned int    start_offset,
372                                   unsigned int   *caret_count /* IN/OUT */,
373                                   hb_position_t  *caret_array /* OUT */)
374 {
375   return font->face->table.GDEF->table->get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
376 }
377 #endif
378
379
380 /*
381  * GSUB/GPOS
382  */
383
384 bool
385 OT::GSUB::is_blocklisted (hb_blob_t *blob HB_UNUSED,
386                           hb_face_t *face) const
387 {
388 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
389   return false;
390 #endif
391   return false;
392 }
393
394 bool
395 OT::GPOS::is_blocklisted (hb_blob_t *blob HB_UNUSED,
396                           hb_face_t *face HB_UNUSED) const
397 {
398 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
399   return false;
400 #endif
401   return false;
402 }
403
404 static const OT::GSUBGPOS&
405 get_gsubgpos_table (hb_face_t *face,
406                     hb_tag_t   table_tag)
407 {
408   switch (table_tag) {
409     case HB_OT_TAG_GSUB: return *face->table.GSUB->table;
410     case HB_OT_TAG_GPOS: return *face->table.GPOS->table;
411     default:             return Null (OT::GSUBGPOS);
412   }
413 }
414
415
416 /**
417  * hb_ot_layout_table_get_script_tags:
418  * @face: #hb_face_t to work upon
419  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
420  * @start_offset: offset of the first script tag to retrieve
421  * @script_count: (inout) (optional): Input = the maximum number of script tags to return;
422  *                Output = the actual number of script tags returned (may be zero)
423  * @script_tags: (out) (array length=script_count): The array of #hb_tag_t script tags found for the query
424  *
425  * Fetches a list of all scripts enumerated in the specified face's GSUB table
426  * or GPOS table. The list returned will begin at the offset provided.
427  *
428  * Return value: Total number of script tags.
429  *
430  **/
431 unsigned int
432 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
433                                     hb_tag_t      table_tag,
434                                     unsigned int  start_offset,
435                                     unsigned int *script_count /* IN/OUT */,
436                                     hb_tag_t     *script_tags  /* OUT */)
437 {
438   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
439
440   return g.get_script_tags (start_offset, script_count, script_tags);
441 }
442
443 #define HB_OT_TAG_LATIN_SCRIPT          HB_TAG ('l', 'a', 't', 'n')
444
445 /**
446  * hb_ot_layout_table_find_script:
447  * @face: #hb_face_t to work upon
448  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
449  * @script_tag: #hb_tag_t of the script tag requested
450  * @script_index: (out): The index of the requested script tag
451  *
452  * Fetches the index if a given script tag in the specified face's GSUB table
453  * or GPOS table.
454  *
455  * Return value: %true if the script is found, %false otherwise
456  *
457  **/
458 hb_bool_t
459 hb_ot_layout_table_find_script (hb_face_t    *face,
460                                 hb_tag_t      table_tag,
461                                 hb_tag_t      script_tag,
462                                 unsigned int *script_index /* OUT */)
463 {
464   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
465   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
466
467   if (g.find_script_index (script_tag, script_index))
468     return true;
469
470   /* try finding 'DFLT' */
471   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
472     return false;
473
474   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
475    * including many versions of DejaVu Sans Mono! */
476   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
477     return false;
478
479   /* try with 'latn'; some old fonts put their features there even though
480      they're really trying to support Thai, for example :( */
481   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
482     return false;
483
484   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
485   return false;
486 }
487
488 #ifndef HB_DISABLE_DEPRECATED
489 /**
490  * hb_ot_layout_table_choose_script:
491  * @face: #hb_face_t to work upon
492  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
493  * @script_tags: Array of #hb_tag_t script tags
494  * @script_index: (out): The index of the requested script tag
495  * @chosen_script: (out): #hb_tag_t of the script tag requested
496  *
497  * Deprecated since 2.0.0
498  **/
499 hb_bool_t
500 hb_ot_layout_table_choose_script (hb_face_t      *face,
501                                   hb_tag_t        table_tag,
502                                   const hb_tag_t *script_tags,
503                                   unsigned int   *script_index  /* OUT */,
504                                   hb_tag_t       *chosen_script /* OUT */)
505 {
506   const hb_tag_t *t;
507   for (t = script_tags; *t; t++);
508   return hb_ot_layout_table_select_script (face, table_tag, t - script_tags, script_tags, script_index, chosen_script);
509 }
510 #endif
511
512 /**
513  * hb_ot_layout_table_select_script:
514  * @face: #hb_face_t to work upon
515  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
516  * @script_count: Number of script tags in the array
517  * @script_tags: Array of #hb_tag_t script tags
518  * @script_index: (out) (optional): The index of the requested script
519  * @chosen_script: (out) (optional): #hb_tag_t of the requested script
520  *
521  * Selects an OpenType script for @table_tag from the @script_tags array.
522  *
523  * If the table does not have any of the requested scripts, then `DFLT`,
524  * `dflt`, and `latn` tags are tried in that order. If the table still does not
525  * have any of these scripts, @script_index and @chosen_script are set to
526  * #HB_OT_LAYOUT_NO_SCRIPT_INDEX.
527  *
528  * Return value:
529  * %true if one of the requested scripts is selected, %false if a fallback
530  * script is selected or if no scripts are selected.
531  *
532  * Since: 2.0.0
533  **/
534 hb_bool_t
535 hb_ot_layout_table_select_script (hb_face_t      *face,
536                                   hb_tag_t        table_tag,
537                                   unsigned int    script_count,
538                                   const hb_tag_t *script_tags,
539                                   unsigned int   *script_index  /* OUT */,
540                                   hb_tag_t       *chosen_script /* OUT */)
541 {
542   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
543   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
544   unsigned int i;
545
546   for (i = 0; i < script_count; i++)
547   {
548     if (g.find_script_index (script_tags[i], script_index))
549     {
550       if (chosen_script)
551         *chosen_script = script_tags[i];
552       return true;
553     }
554   }
555
556   /* try finding 'DFLT' */
557   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
558     if (chosen_script)
559       *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
560     return false;
561   }
562
563   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
564   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
565     if (chosen_script)
566       *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
567     return false;
568   }
569
570   /* try with 'latn'; some old fonts put their features there even though
571      they're really trying to support Thai, for example :( */
572   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
573     if (chosen_script)
574       *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
575     return false;
576   }
577
578   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
579   if (chosen_script)
580     *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
581   return false;
582 }
583
584
585 /**
586  * hb_ot_layout_table_get_feature_tags:
587  * @face: #hb_face_t to work upon
588  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
589  * @start_offset: offset of the first feature tag to retrieve
590  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
591  *                 Output = the actual number of feature tags returned (may be zero)
592  * @feature_tags: (out) (array length=feature_count): Array of feature tags found in the table
593  *
594  * Fetches a list of all feature tags in the given face's GSUB or GPOS table.
595  *
596  * Return value: Total number of feature tags.
597  *
598  **/
599 unsigned int
600 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
601                                      hb_tag_t      table_tag,
602                                      unsigned int  start_offset,
603                                      unsigned int *feature_count /* IN/OUT */,
604                                      hb_tag_t     *feature_tags  /* OUT */)
605 {
606   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
607
608   return g.get_feature_tags (start_offset, feature_count, feature_tags);
609 }
610
611
612 /**
613  * hb_ot_layout_table_find_feature:
614  * @face: #hb_face_t to work upon
615  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
616  * @feature_tag: The #hb_tag_t of the requested feature tag
617  * @feature_index: (out): The index of the requested feature
618  *
619  * Fetches the index for a given feature tag in the specified face's GSUB table
620  * or GPOS table.
621  *
622  * Return value: %true if the feature is found, %false otherwise
623  **/
624 bool
625 hb_ot_layout_table_find_feature (hb_face_t    *face,
626                                  hb_tag_t      table_tag,
627                                  hb_tag_t      feature_tag,
628                                  unsigned int *feature_index /* OUT */)
629 {
630   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
631   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
632
633   unsigned int num_features = g.get_feature_count ();
634   for (unsigned int i = 0; i < num_features; i++)
635   {
636     if (feature_tag == g.get_feature_tag (i)) {
637       if (feature_index) *feature_index = i;
638       return true;
639     }
640   }
641
642   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
643   return false;
644 }
645
646
647 /**
648  * hb_ot_layout_script_get_language_tags:
649  * @face: #hb_face_t to work upon
650  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
651  * @script_index: The index of the requested script tag
652  * @start_offset: offset of the first language tag to retrieve
653  * @language_count: (inout) (optional): Input = the maximum number of language tags to return;
654  *                  Output = the actual number of language tags returned (may be zero)
655  * @language_tags: (out) (array length=language_count): Array of language tags found in the table
656  *
657  * Fetches a list of language tags in the given face's GSUB or GPOS table, underneath
658  * the specified script index. The list returned will begin at the offset provided.
659  *
660  * Return value: Total number of language tags.
661  *
662  **/
663 unsigned int
664 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
665                                        hb_tag_t      table_tag,
666                                        unsigned int  script_index,
667                                        unsigned int  start_offset,
668                                        unsigned int *language_count /* IN/OUT */,
669                                        hb_tag_t     *language_tags  /* OUT */)
670 {
671   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
672
673   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
674 }
675
676
677 #ifndef HB_DISABLE_DEPRECATED
678 /**
679  * hb_ot_layout_script_find_language:
680  * @face: #hb_face_t to work upon
681  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
682  * @script_index: The index of the requested script tag
683  * @language_tag: The #hb_tag_t of the requested language
684  * @language_index: The index of the requested language
685  *
686  * Fetches the index of a given language tag in the specified face's GSUB table
687  * or GPOS table, underneath the specified script tag.
688  *
689  * Return value: %true if the language tag is found, %false otherwise
690  *
691  * Since: 0.6.0
692  * Deprecated: 2.0.0
693  **/
694 hb_bool_t
695 hb_ot_layout_script_find_language (hb_face_t    *face,
696                                    hb_tag_t      table_tag,
697                                    unsigned int  script_index,
698                                    hb_tag_t      language_tag,
699                                    unsigned int *language_index)
700 {
701   return hb_ot_layout_script_select_language (face,
702                                               table_tag,
703                                               script_index,
704                                               1,
705                                               &language_tag,
706                                               language_index);
707 }
708 #endif
709
710
711 /**
712  * hb_ot_layout_script_select_language:
713  * @face: #hb_face_t to work upon
714  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
715  * @script_index: The index of the requested script tag
716  * @language_count: The number of languages in the specified script
717  * @language_tags: The array of language tags
718  * @language_index: (out): The index of the requested language
719  *
720  * Fetches the index of the first language tag fom @language_tags that is present
721  * in the specified face's GSUB or GPOS table, underneath the specified script
722  * index.
723  *
724  * If none of the given language tags is found, %false is returned and
725  * @language_index is set to the default language index.
726  *
727  * Return value: %true if one of the given language tags is found, %false otherwise
728  *
729  * Since: 2.0.0
730  **/
731 hb_bool_t
732 hb_ot_layout_script_select_language (hb_face_t      *face,
733                                      hb_tag_t        table_tag,
734                                      unsigned int    script_index,
735                                      unsigned int    language_count,
736                                      const hb_tag_t *language_tags,
737                                      unsigned int   *language_index /* OUT */)
738 {
739   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
740   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
741   unsigned int i;
742
743   for (i = 0; i < language_count; i++)
744   {
745     if (s.find_lang_sys_index (language_tags[i], language_index))
746       return true;
747   }
748
749   /* try finding 'dflt' */
750   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
751     return false;
752
753   if (language_index)
754     *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
755   return false;
756 }
757
758
759 /**
760  * hb_ot_layout_language_get_required_feature_index:
761  * @face: #hb_face_t to work upon
762  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
763  * @script_index: The index of the requested script tag
764  * @language_index: The index of the requested language tag
765  * @feature_index: (out): The index of the requested feature
766  *
767  * Fetches the index of a requested feature in the given face's GSUB or GPOS table,
768  * underneath the specified script and language.
769  *
770  * Return value: %true if the feature is found, %false otherwise
771  *
772  **/
773 hb_bool_t
774 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
775                                                   hb_tag_t      table_tag,
776                                                   unsigned int  script_index,
777                                                   unsigned int  language_index,
778                                                   unsigned int *feature_index /* OUT */)
779 {
780   return hb_ot_layout_language_get_required_feature (face,
781                                                      table_tag,
782                                                      script_index,
783                                                      language_index,
784                                                      feature_index,
785                                                      nullptr);
786 }
787
788
789 /**
790  * hb_ot_layout_language_get_required_feature:
791  * @face: #hb_face_t to work upon
792  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
793  * @script_index: The index of the requested script tag
794  * @language_index: The index of the requested language tag
795  * @feature_index: (out): The index of the requested feature
796  * @feature_tag: (out): The #hb_tag_t of the requested feature
797  *
798  * Fetches the tag of a requested feature index in the given face's GSUB or GPOS table,
799  * underneath the specified script and language.
800  *
801  * Return value: %true if the feature is found, %false otherwise
802  *
803  * Since: 0.9.30
804  **/
805 hb_bool_t
806 hb_ot_layout_language_get_required_feature (hb_face_t    *face,
807                                             hb_tag_t      table_tag,
808                                             unsigned int  script_index,
809                                             unsigned int  language_index,
810                                             unsigned int *feature_index /* OUT */,
811                                             hb_tag_t     *feature_tag   /* OUT */)
812 {
813   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
814   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
815
816   unsigned int index = l.get_required_feature_index ();
817   if (feature_index) *feature_index = index;
818   if (feature_tag) *feature_tag = g.get_feature_tag (index);
819
820   return l.has_required_feature ();
821 }
822
823
824 /**
825  * hb_ot_layout_language_get_feature_indexes:
826  * @face: #hb_face_t to work upon
827  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
828  * @script_index: The index of the requested script tag
829  * @language_index: The index of the requested language tag
830  * @start_offset: offset of the first feature tag to retrieve
831  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
832  *                 Output: the actual number of feature tags returned (may be zero)
833  * @feature_indexes: (out) (array length=feature_count): The array of feature indexes found for the query
834  *
835  * Fetches a list of all features in the specified face's GSUB table
836  * or GPOS table, underneath the specified script and language. The list
837  * returned will begin at the offset provided.
838  *
839  * Return value: Total number of features.
840  **/
841 unsigned int
842 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
843                                            hb_tag_t      table_tag,
844                                            unsigned int  script_index,
845                                            unsigned int  language_index,
846                                            unsigned int  start_offset,
847                                            unsigned int *feature_count   /* IN/OUT */,
848                                            unsigned int *feature_indexes /* OUT */)
849 {
850   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
851   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
852
853   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
854 }
855
856
857 /**
858  * hb_ot_layout_language_get_feature_tags:
859  * @face: #hb_face_t to work upon
860  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
861  * @script_index: The index of the requested script tag
862  * @language_index: The index of the requested language tag
863  * @start_offset: offset of the first feature tag to retrieve
864  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
865  *                 Output = the actual number of feature tags returned (may be zero)
866  * @feature_tags: (out) (array length=feature_count): The array of #hb_tag_t feature tags found for the query
867  *
868  * Fetches a list of all features in the specified face's GSUB table
869  * or GPOS table, underneath the specified script and language. The list
870  * returned will begin at the offset provided.
871  *
872  * Return value: Total number of feature tags.
873  **/
874 unsigned int
875 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
876                                         hb_tag_t      table_tag,
877                                         unsigned int  script_index,
878                                         unsigned int  language_index,
879                                         unsigned int  start_offset,
880                                         unsigned int *feature_count /* IN/OUT */,
881                                         hb_tag_t     *feature_tags  /* OUT */)
882 {
883   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
884   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
885
886   static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
887   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
888
889   if (feature_tags) {
890     unsigned int count = *feature_count;
891     for (unsigned int i = 0; i < count; i++)
892       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
893   }
894
895   return ret;
896 }
897
898
899 /**
900  * hb_ot_layout_language_find_feature:
901  * @face: #hb_face_t to work upon
902  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
903  * @script_index: The index of the requested script tag
904  * @language_index: The index of the requested language tag
905  * @feature_tag: #hb_tag_t of the feature tag requested
906  * @feature_index: (out): The index of the requested feature
907  *
908  * Fetches the index of a given feature tag in the specified face's GSUB table
909  * or GPOS table, underneath the specified script and language.
910  *
911  * Return value: %true if the feature is found, %false otherwise
912  *
913  **/
914 hb_bool_t
915 hb_ot_layout_language_find_feature (hb_face_t    *face,
916                                     hb_tag_t      table_tag,
917                                     unsigned int  script_index,
918                                     unsigned int  language_index,
919                                     hb_tag_t      feature_tag,
920                                     unsigned int *feature_index /* OUT */)
921 {
922   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
923   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
924   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
925
926   unsigned int num_features = l.get_feature_count ();
927   for (unsigned int i = 0; i < num_features; i++) {
928     unsigned int f_index = l.get_feature_index (i);
929
930     if (feature_tag == g.get_feature_tag (f_index)) {
931       if (feature_index) *feature_index = f_index;
932       return true;
933     }
934   }
935
936   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
937   return false;
938 }
939
940
941 /**
942  * hb_ot_layout_feature_get_lookups:
943  * @face: #hb_face_t to work upon
944  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
945  * @feature_index: The index of the requested feature
946  * @start_offset: offset of the first lookup to retrieve
947  * @lookup_count: (inout) (optional): Input = the maximum number of lookups to return;
948  *                Output = the actual number of lookups returned (may be zero)
949  * @lookup_indexes: (out) (array length=lookup_count): The array of lookup indexes found for the query
950  *
951  * Fetches a list of all lookups enumerated for the specified feature, in
952  * the specified face's GSUB table or GPOS table. The list returned will
953  * begin at the offset provided.
954  *
955  * Return value: Total number of lookups.
956  *
957  * Since: 0.9.7
958  **/
959 unsigned int
960 hb_ot_layout_feature_get_lookups (hb_face_t    *face,
961                                   hb_tag_t      table_tag,
962                                   unsigned int  feature_index,
963                                   unsigned int  start_offset,
964                                   unsigned int *lookup_count   /* IN/OUT */,
965                                   unsigned int *lookup_indexes /* OUT */)
966 {
967   return hb_ot_layout_feature_with_variations_get_lookups (face,
968                                                            table_tag,
969                                                            feature_index,
970                                                            HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
971                                                            start_offset,
972                                                            lookup_count,
973                                                            lookup_indexes);
974 }
975
976
977 /**
978  * hb_ot_layout_table_get_lookup_count:
979  * @face: #hb_face_t to work upon
980  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
981  *
982  * Fetches the total number of lookups enumerated in the specified
983  * face's GSUB table or GPOS table.
984  *
985  * Return value: Total number of lookups.
986  *
987  * Since: 0.9.22
988  **/
989 unsigned int
990 hb_ot_layout_table_get_lookup_count (hb_face_t    *face,
991                                      hb_tag_t      table_tag)
992 {
993   return get_gsubgpos_table (face, table_tag).get_lookup_count ();
994 }
995
996
997 struct hb_collect_features_context_t
998 {
999   hb_collect_features_context_t (hb_face_t *face,
1000                                  hb_tag_t   table_tag,
1001                                  hb_set_t  *feature_indices_,
1002                                  const hb_tag_t *features)
1003
1004     : g (get_gsubgpos_table (face, table_tag)),
1005       feature_indices (feature_indices_),
1006       has_feature_filter (false),
1007       script_count (0),langsys_count (0), feature_index_count (0)
1008   {
1009     compute_feature_filter (features);
1010   }
1011
1012   void compute_feature_filter (const hb_tag_t *features)
1013   {
1014     if (features == nullptr)
1015     {
1016       has_feature_filter = false;
1017       return;
1018     }
1019
1020     has_feature_filter = true;
1021     hb_set_t features_set;
1022     for (; *features; features++)
1023       features_set.add (*features);
1024
1025     for (unsigned i = 0; i < g.get_feature_count (); i++)
1026     {
1027       hb_tag_t tag = g.get_feature_tag (i);
1028       if (features_set.has (tag))
1029         feature_indices_filter.add(i);
1030     }
1031   }
1032
1033   bool visited (const OT::Script &s)
1034   {
1035     /* We might have Null() object here.  Don't want to involve
1036      * that in the memoize.  So, detect empty objects and return. */
1037     if (unlikely (!s.has_default_lang_sys () &&
1038                   !s.get_lang_sys_count ()))
1039       return true;
1040
1041     if (script_count++ > HB_MAX_SCRIPTS)
1042       return true;
1043
1044     return visited (s, visited_script);
1045   }
1046   bool visited (const OT::LangSys &l)
1047   {
1048     /* We might have Null() object here.  Don't want to involve
1049      * that in the memoize.  So, detect empty objects and return. */
1050     if (unlikely (!l.has_required_feature () &&
1051                   !l.get_feature_count ()))
1052       return true;
1053
1054     if (langsys_count++ > HB_MAX_LANGSYS)
1055       return true;
1056
1057     return visited (l, visited_langsys);
1058   }
1059
1060   bool visited_feature_indices (unsigned count)
1061   {
1062     feature_index_count += count;
1063     return feature_index_count > HB_MAX_FEATURE_INDICES;
1064   }
1065
1066   private:
1067   template <typename T>
1068   bool visited (const T &p, hb_set_t &visited_set)
1069   {
1070     hb_codepoint_t delta = (hb_codepoint_t) ((uintptr_t) &p - (uintptr_t) &g);
1071      if (visited_set.has (delta))
1072       return true;
1073
1074     visited_set.add (delta);
1075     return false;
1076   }
1077
1078   public:
1079   const OT::GSUBGPOS &g;
1080   hb_set_t *feature_indices;
1081   hb_set_t  feature_indices_filter;
1082   bool has_feature_filter;
1083
1084   private:
1085   hb_set_t visited_script;
1086   hb_set_t visited_langsys;
1087   unsigned int script_count;
1088   unsigned int langsys_count;
1089   unsigned int feature_index_count;
1090 };
1091
1092 static void
1093 langsys_collect_features (hb_collect_features_context_t *c,
1094                           const OT::LangSys  &l)
1095 {
1096   if (c->visited (l)) return;
1097
1098   if (!c->has_feature_filter)
1099   {
1100     /* All features. */
1101     if (l.has_required_feature () && !c->visited_feature_indices (1))
1102       c->feature_indices->add (l.get_required_feature_index ());
1103
1104     // TODO(garretrieger): filter out indices >= feature count?
1105     if (!c->visited_feature_indices (l.featureIndex.len))
1106       l.add_feature_indexes_to (c->feature_indices);
1107   }
1108   else
1109   {
1110     if (c->feature_indices_filter.is_empty()) return;
1111     unsigned int num_features = l.get_feature_count ();
1112     for (unsigned int i = 0; i < num_features; i++)
1113     {
1114       unsigned int feature_index = l.get_feature_index (i);
1115       if (!c->feature_indices_filter.has (feature_index)) continue;
1116
1117       c->feature_indices->add (feature_index);
1118       c->feature_indices_filter.del (feature_index);
1119     }
1120   }
1121 }
1122
1123 static void
1124 script_collect_features (hb_collect_features_context_t *c,
1125                          const OT::Script   &s,
1126                          const hb_tag_t *languages)
1127 {
1128   if (c->visited (s)) return;
1129
1130   if (!languages)
1131   {
1132     /* All languages. */
1133     if (s.has_default_lang_sys ())
1134       langsys_collect_features (c,
1135                                 s.get_default_lang_sys ());
1136
1137
1138     unsigned int count = s.get_lang_sys_count ();
1139     for (unsigned int language_index = 0; language_index < count; language_index++)
1140       langsys_collect_features (c,
1141                                 s.get_lang_sys (language_index));
1142   }
1143   else
1144   {
1145     for (; *languages; languages++)
1146     {
1147       unsigned int language_index;
1148       if (s.find_lang_sys_index (*languages, &language_index))
1149         langsys_collect_features (c,
1150                                   s.get_lang_sys (language_index));
1151
1152     }
1153   }
1154 }
1155
1156
1157 /**
1158  * hb_ot_layout_collect_features:
1159  * @face: #hb_face_t to work upon
1160  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1161  * @scripts: The array of scripts to collect features for
1162  * @languages: The array of languages to collect features for
1163  * @features: The array of features to collect
1164  * @feature_indexes: (out): The array of feature indexes found for the query
1165  *
1166  * Fetches a list of all feature indexes in the specified face's GSUB table
1167  * or GPOS table, underneath the specified scripts, languages, and features.
1168  * If no list of scripts is provided, all scripts will be queried. If no list
1169  * of languages is provided, all languages will be queried. If no list of
1170  * features is provided, all features will be queried.
1171  *
1172  * Since: 1.8.5
1173  **/
1174 void
1175 hb_ot_layout_collect_features (hb_face_t      *face,
1176                                hb_tag_t        table_tag,
1177                                const hb_tag_t *scripts,
1178                                const hb_tag_t *languages,
1179                                const hb_tag_t *features,
1180                                hb_set_t       *feature_indexes /* OUT */)
1181 {
1182   hb_collect_features_context_t c (face, table_tag, feature_indexes, features);
1183   if (!scripts)
1184   {
1185     /* All scripts. */
1186     unsigned int count = c.g.get_script_count ();
1187     for (unsigned int script_index = 0; script_index < count; script_index++)
1188       script_collect_features (&c,
1189                                c.g.get_script (script_index),
1190                                languages);
1191   }
1192   else
1193   {
1194     for (; *scripts; scripts++)
1195     {
1196       unsigned int script_index;
1197       if (c.g.find_script_index (*scripts, &script_index))
1198         script_collect_features (&c,
1199                                  c.g.get_script (script_index),
1200                                  languages);
1201     }
1202   }
1203 }
1204
1205
1206 /**
1207  * hb_ot_layout_collect_lookups:
1208  * @face: #hb_face_t to work upon
1209  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1210  * @scripts: The array of scripts to collect lookups for
1211  * @languages: The array of languages to collect lookups for
1212  * @features: The array of features to collect lookups for
1213  * @lookup_indexes: (out): The array of lookup indexes found for the query
1214  *
1215  * Fetches a list of all feature-lookup indexes in the specified face's GSUB
1216  * table or GPOS table, underneath the specified scripts, languages, and
1217  * features. If no list of scripts is provided, all scripts will be queried.
1218  * If no list of languages is provided, all languages will be queried. If no
1219  * list of features is provided, all features will be queried.
1220  *
1221  * Since: 0.9.8
1222  **/
1223 void
1224 hb_ot_layout_collect_lookups (hb_face_t      *face,
1225                               hb_tag_t        table_tag,
1226                               const hb_tag_t *scripts,
1227                               const hb_tag_t *languages,
1228                               const hb_tag_t *features,
1229                               hb_set_t       *lookup_indexes /* OUT */)
1230 {
1231   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1232
1233   hb_set_t feature_indexes;
1234   hb_ot_layout_collect_features (face, table_tag, scripts, languages, features, &feature_indexes);
1235
1236   for (hb_codepoint_t feature_index = HB_SET_VALUE_INVALID;
1237        hb_set_next (&feature_indexes, &feature_index);)
1238     g.get_feature (feature_index).add_lookup_indexes_to (lookup_indexes);
1239
1240   g.feature_variation_collect_lookups (&feature_indexes, lookup_indexes);
1241 }
1242
1243
1244 #ifndef HB_NO_LAYOUT_COLLECT_GLYPHS
1245 /**
1246  * hb_ot_layout_lookup_collect_glyphs:
1247  * @face: #hb_face_t to work upon
1248  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1249  * @lookup_index: The index of the feature lookup to query
1250  * @glyphs_before: (out): Array of glyphs preceding the substitution range
1251  * @glyphs_input: (out): Array of input glyphs that would be substituted by the lookup
1252  * @glyphs_after: (out): Array of glyphs following the substitution range
1253  * @glyphs_output: (out): Array of glyphs that would be the substituted output of the lookup
1254  *
1255  * Fetches a list of all glyphs affected by the specified lookup in the
1256  * specified face's GSUB table or GPOS table.
1257  *
1258  * Since: 0.9.7
1259  **/
1260 void
1261 hb_ot_layout_lookup_collect_glyphs (hb_face_t    *face,
1262                                     hb_tag_t      table_tag,
1263                                     unsigned int  lookup_index,
1264                                     hb_set_t     *glyphs_before, /* OUT.  May be NULL */
1265                                     hb_set_t     *glyphs_input,  /* OUT.  May be NULL */
1266                                     hb_set_t     *glyphs_after,  /* OUT.  May be NULL */
1267                                     hb_set_t     *glyphs_output  /* OUT.  May be NULL */)
1268 {
1269   OT::hb_collect_glyphs_context_t c (face,
1270                                      glyphs_before,
1271                                      glyphs_input,
1272                                      glyphs_after,
1273                                      glyphs_output);
1274
1275   switch (table_tag)
1276   {
1277     case HB_OT_TAG_GSUB:
1278     {
1279       const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1280       l.collect_glyphs (&c);
1281       return;
1282     }
1283     case HB_OT_TAG_GPOS:
1284     {
1285       const OT::PosLookup& l = face->table.GPOS->table->get_lookup (lookup_index);
1286       l.collect_glyphs (&c);
1287       return;
1288     }
1289   }
1290 }
1291 #endif
1292
1293
1294 /* Variations support */
1295
1296
1297 /**
1298  * hb_ot_layout_table_find_feature_variations:
1299  * @face: #hb_face_t to work upon
1300  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1301  * @coords: The variation coordinates to query
1302  * @num_coords: The number of variation coordinates
1303  * @variations_index: (out): The array of feature variations found for the query
1304  *
1305  * Fetches a list of feature variations in the specified face's GSUB table
1306  * or GPOS table, at the specified variation coordinates.
1307  *
1308  * Return value: %true if feature variations were found, %false otherwise.
1309  *
1310  **/
1311 hb_bool_t
1312 hb_ot_layout_table_find_feature_variations (hb_face_t    *face,
1313                                             hb_tag_t      table_tag,
1314                                             const int    *coords,
1315                                             unsigned int  num_coords,
1316                                             unsigned int *variations_index /* out */)
1317 {
1318   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1319
1320   return g.find_variations_index (coords, num_coords, variations_index);
1321 }
1322
1323
1324 /**
1325  * hb_ot_layout_feature_with_variations_get_lookups:
1326  * @face: #hb_face_t to work upon
1327  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1328  * @feature_index: The index of the feature to query
1329  * @variations_index: The index of the feature variation to query
1330  * @start_offset: offset of the first lookup to retrieve
1331  * @lookup_count: (inout) (optional): Input = the maximum number of lookups to return;
1332  *                Output = the actual number of lookups returned (may be zero)
1333  * @lookup_indexes: (out) (array length=lookup_count): The array of lookups found for the query
1334  *
1335  * Fetches a list of all lookups enumerated for the specified feature, in
1336  * the specified face's GSUB table or GPOS table, enabled at the specified
1337  * variations index. The list returned will begin at the offset provided.
1338  *
1339  * Return value: Total number of lookups.
1340  *
1341  **/
1342 unsigned int
1343 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t    *face,
1344                                                   hb_tag_t      table_tag,
1345                                                   unsigned int  feature_index,
1346                                                   unsigned int  variations_index,
1347                                                   unsigned int  start_offset,
1348                                                   unsigned int *lookup_count /* IN/OUT */,
1349                                                   unsigned int *lookup_indexes /* OUT */)
1350 {
1351   static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
1352   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1353
1354   const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
1355
1356   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
1357 }
1358
1359
1360 /*
1361  * OT::GSUB
1362  */
1363
1364
1365 /**
1366  * hb_ot_layout_has_substitution:
1367  * @face: #hb_face_t to work upon
1368  *
1369  * Tests whether the specified face includes any GSUB substitutions.
1370  *
1371  * Return value: %true if data found, %false otherwise
1372  *
1373  **/
1374 hb_bool_t
1375 hb_ot_layout_has_substitution (hb_face_t *face)
1376 {
1377   return face->table.GSUB->table->has_data ();
1378 }
1379
1380
1381 /**
1382  * hb_ot_layout_lookup_would_substitute:
1383  * @face: #hb_face_t to work upon
1384  * @lookup_index: The index of the lookup to query
1385  * @glyphs: The sequence of glyphs to query for substitution
1386  * @glyphs_length: The length of the glyph sequence
1387  * @zero_context: #hb_bool_t indicating whether pre-/post-context are disallowed
1388  * in substitutions
1389  *
1390  * Tests whether a specified lookup in the specified face would
1391  * trigger a substitution on the given glyph sequence.
1392  *
1393  * Return value: %true if a substitution would be triggered, %false otherwise
1394  *
1395  * Since: 0.9.7
1396  **/
1397 hb_bool_t
1398 hb_ot_layout_lookup_would_substitute (hb_face_t            *face,
1399                                       unsigned int          lookup_index,
1400                                       const hb_codepoint_t *glyphs,
1401                                       unsigned int          glyphs_length,
1402                                       hb_bool_t             zero_context)
1403 {
1404   if (unlikely (lookup_index >= face->table.GSUB->lookup_count)) return false;
1405   OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
1406
1407   const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1408   return l.would_apply (&c, &face->table.GSUB->accels[lookup_index]);
1409 }
1410
1411
1412 /**
1413  * hb_ot_layout_substitute_start:
1414  * @font: #hb_font_t to use
1415  * @buffer: #hb_buffer_t buffer to work upon
1416  *
1417  * Called before substitution lookups are performed, to ensure that glyph
1418  * class and other properties are set on the glyphs in the buffer.
1419  *
1420  **/
1421 void
1422 hb_ot_layout_substitute_start (hb_font_t    *font,
1423                                hb_buffer_t  *buffer)
1424 {
1425   _hb_ot_layout_set_glyph_props (font, buffer);
1426 }
1427
1428 void
1429 hb_ot_layout_delete_glyphs_inplace (hb_buffer_t *buffer,
1430                                     bool (*filter) (const hb_glyph_info_t *info))
1431 {
1432   /* Merge clusters and delete filtered glyphs.
1433    * NOTE! We can't use out-buffer as we have positioning data. */
1434   unsigned int j = 0;
1435   unsigned int count = buffer->len;
1436   hb_glyph_info_t *info = buffer->info;
1437   hb_glyph_position_t *pos = buffer->pos;
1438   for (unsigned int i = 0; i < count; i++)
1439   {
1440     if (filter (&info[i]))
1441     {
1442       /* Merge clusters.
1443        * Same logic as buffer->delete_glyph(), but for in-place removal. */
1444
1445       unsigned int cluster = info[i].cluster;
1446       if (i + 1 < count && cluster == info[i + 1].cluster)
1447         continue; /* Cluster survives; do nothing. */
1448
1449       if (j)
1450       {
1451         /* Merge cluster backward. */
1452         if (cluster < info[j - 1].cluster)
1453         {
1454           unsigned int mask = info[i].mask;
1455           unsigned int old_cluster = info[j - 1].cluster;
1456           for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
1457             buffer->set_cluster (info[k - 1], cluster, mask);
1458         }
1459         continue;
1460       }
1461
1462       if (i + 1 < count)
1463         buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
1464
1465       continue;
1466     }
1467
1468     if (j != i)
1469     {
1470       info[j] = info[i];
1471       pos[j] = pos[i];
1472     }
1473     j++;
1474   }
1475   buffer->len = j;
1476 }
1477
1478 /**
1479  * hb_ot_layout_lookup_substitute_closure:
1480  * @face: #hb_face_t to work upon
1481  * @lookup_index: index of the feature lookup to query
1482  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookup
1483  *
1484  * Compute the transitive closure of glyphs needed for a
1485  * specified lookup.
1486  *
1487  * Since: 0.9.7
1488  **/
1489 void
1490 hb_ot_layout_lookup_substitute_closure (hb_face_t    *face,
1491                                         unsigned int  lookup_index,
1492                                         hb_set_t     *glyphs /* OUT */)
1493 {
1494   hb_map_t done_lookups_glyph_count;
1495   hb_hashmap_t<unsigned, hb_set_t *> done_lookups_glyph_set;
1496   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1497
1498   const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1499
1500   l.closure (&c, lookup_index);
1501
1502   for (auto _ : done_lookups_glyph_set.iter ())
1503     hb_set_destroy (_.second);
1504 }
1505
1506 /**
1507  * hb_ot_layout_lookups_substitute_closure:
1508  * @face: #hb_face_t to work upon
1509  * @lookups: The set of lookups to query
1510  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookups
1511  *
1512  * Compute the transitive closure of glyphs needed for all of the
1513  * provided lookups.
1514  *
1515  * Since: 1.8.1
1516  **/
1517 void
1518 hb_ot_layout_lookups_substitute_closure (hb_face_t      *face,
1519                                          const hb_set_t *lookups,
1520                                          hb_set_t       *glyphs /* OUT */)
1521 {
1522   hb_map_t done_lookups_glyph_count;
1523   hb_hashmap_t<unsigned, hb_set_t *> done_lookups_glyph_set;
1524   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1525   const OT::GSUB& gsub = *face->table.GSUB->table;
1526
1527   unsigned int iteration_count = 0;
1528   unsigned int glyphs_length;
1529   do
1530   {
1531     c.reset_lookup_visit_count ();
1532     glyphs_length = glyphs->get_population ();
1533     if (lookups)
1534     {
1535       for (hb_codepoint_t lookup_index = HB_SET_VALUE_INVALID; hb_set_next (lookups, &lookup_index);)
1536         gsub.get_lookup (lookup_index).closure (&c, lookup_index);
1537     }
1538     else
1539     {
1540       for (unsigned int i = 0; i < gsub.get_lookup_count (); i++)
1541         gsub.get_lookup (i).closure (&c, i);
1542     }
1543   } while (iteration_count++ <= HB_CLOSURE_MAX_STAGES &&
1544            glyphs_length != glyphs->get_population ());
1545
1546   for (auto _ : done_lookups_glyph_set.iter ())
1547     hb_set_destroy (_.second);
1548 }
1549
1550 /*
1551  * OT::GPOS
1552  */
1553
1554
1555 /**
1556  * hb_ot_layout_has_positioning:
1557  * @face: #hb_face_t to work upon
1558  *
1559  * Tests whether the specified face includes any GPOS positioning.
1560  *
1561  * Return value: %true if the face has GPOS data, %false otherwise
1562  *
1563  **/
1564 hb_bool_t
1565 hb_ot_layout_has_positioning (hb_face_t *face)
1566 {
1567   return face->table.GPOS->table->has_data ();
1568 }
1569
1570 /**
1571  * hb_ot_layout_position_start:
1572  * @font: #hb_font_t to use
1573  * @buffer: #hb_buffer_t buffer to work upon
1574  *
1575  * Called before positioning lookups are performed, to ensure that glyph
1576  * attachment types and glyph-attachment chains are set for the glyphs in the buffer.
1577  *
1578  **/
1579 void
1580 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
1581 {
1582   OT::GPOS::position_start (font, buffer);
1583 }
1584
1585
1586 /**
1587  * hb_ot_layout_position_finish_advances:
1588  * @font: #hb_font_t to use
1589  * @buffer: #hb_buffer_t buffer to work upon
1590  *
1591  * Called after positioning lookups are performed, to finish glyph advances.
1592  *
1593  **/
1594 void
1595 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
1596 {
1597   OT::GPOS::position_finish_advances (font, buffer);
1598 }
1599
1600 /**
1601  * hb_ot_layout_position_finish_offsets:
1602  * @font: #hb_font_t to use
1603  * @buffer: #hb_buffer_t buffer to work upon
1604  *
1605  * Called after positioning lookups are performed, to finish glyph offsets.
1606  *
1607  **/
1608 void
1609 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
1610 {
1611   OT::GPOS::position_finish_offsets (font, buffer);
1612 }
1613
1614
1615 #ifndef HB_NO_LAYOUT_FEATURE_PARAMS
1616 /**
1617  * hb_ot_layout_get_size_params:
1618  * @face: #hb_face_t to work upon
1619  * @design_size: (out): The design size of the face
1620  * @subfamily_id: (out): The identifier of the face within the font subfamily
1621  * @subfamily_name_id: (out): The ‘name’ table name ID of the face within the font subfamily
1622  * @range_start: (out): The minimum size of the recommended size range for the face
1623  * @range_end: (out): The maximum size of the recommended size range for the face
1624  *
1625  * Fetches optical-size feature data (i.e., the `size` feature from GPOS). Note that
1626  * the subfamily_id and the subfamily name string (accessible via the subfamily_name_id)
1627  * as used here are defined as pertaining only to fonts within a font family that differ
1628  * specifically in their respective size ranges; other ways to differentiate fonts within
1629  * a subfamily are not covered by the `size` feature.
1630  *
1631  * For more information on this distinction, see the [`size` feature documentation](
1632  * https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#tag-size).
1633  *
1634  * Return value: %true if data found, %false otherwise
1635  *
1636  * Since: 0.9.10
1637  **/
1638 hb_bool_t
1639 hb_ot_layout_get_size_params (hb_face_t       *face,
1640                               unsigned int    *design_size,       /* OUT.  May be NULL */
1641                               unsigned int    *subfamily_id,      /* OUT.  May be NULL */
1642                               hb_ot_name_id_t *subfamily_name_id, /* OUT.  May be NULL */
1643                               unsigned int    *range_start,       /* OUT.  May be NULL */
1644                               unsigned int    *range_end          /* OUT.  May be NULL */)
1645 {
1646   const OT::GPOS &gpos = *face->table.GPOS->table;
1647   const hb_tag_t tag = HB_TAG ('s','i','z','e');
1648
1649   unsigned int num_features = gpos.get_feature_count ();
1650   for (unsigned int i = 0; i < num_features; i++)
1651   {
1652     if (tag == gpos.get_feature_tag (i))
1653     {
1654       const OT::Feature &f = gpos.get_feature (i);
1655       const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
1656
1657       if (params.designSize)
1658       {
1659         if (design_size) *design_size = params.designSize;
1660         if (subfamily_id) *subfamily_id = params.subfamilyID;
1661         if (subfamily_name_id) *subfamily_name_id = params.subfamilyNameID;
1662         if (range_start) *range_start = params.rangeStart;
1663         if (range_end) *range_end = params.rangeEnd;
1664
1665         return true;
1666       }
1667     }
1668   }
1669
1670   if (design_size) *design_size = 0;
1671   if (subfamily_id) *subfamily_id = 0;
1672   if (subfamily_name_id) *subfamily_name_id = HB_OT_NAME_ID_INVALID;
1673   if (range_start) *range_start = 0;
1674   if (range_end) *range_end = 0;
1675
1676   return false;
1677 }
1678 /**
1679  * hb_ot_layout_feature_get_name_ids:
1680  * @face: #hb_face_t to work upon
1681  * @table_tag: table tag to query, "GSUB" or "GPOS".
1682  * @feature_index: index of feature to query.
1683  * @label_id: (out) (optional): The ‘name’ table name ID that specifies a string
1684  *            for a user-interface label for this feature. (May be NULL.)
1685  * @tooltip_id: (out) (optional): The ‘name’ table name ID that specifies a string
1686  *              that an application can use for tooltip text for this
1687  *              feature. (May be NULL.)
1688  * @sample_id: (out) (optional): The ‘name’ table name ID that specifies sample text
1689  *             that illustrates the effect of this feature. (May be NULL.)
1690  * @num_named_parameters: (out) (optional):  Number of named parameters. (May be zero.)
1691  * @first_param_id: (out) (optional): The first ‘name’ table name ID used to specify
1692  *                  strings for user-interface labels for the feature
1693  *                  parameters. (Must be zero if numParameters is zero.)
1694  *
1695  * Fetches name indices from feature parameters for "Stylistic Set" ('ssXX') or
1696  * "Character Variant" ('cvXX') features.
1697  *
1698  * Return value: %true if data found, %false otherwise
1699  *
1700  * Since: 2.0.0
1701  **/
1702 hb_bool_t
1703 hb_ot_layout_feature_get_name_ids (hb_face_t       *face,
1704                                    hb_tag_t         table_tag,
1705                                    unsigned int     feature_index,
1706                                    hb_ot_name_id_t *label_id,             /* OUT.  May be NULL */
1707                                    hb_ot_name_id_t *tooltip_id,           /* OUT.  May be NULL */
1708                                    hb_ot_name_id_t *sample_id,            /* OUT.  May be NULL */
1709                                    unsigned int    *num_named_parameters, /* OUT.  May be NULL */
1710                                    hb_ot_name_id_t *first_param_id        /* OUT.  May be NULL */)
1711 {
1712   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1713
1714   hb_tag_t feature_tag = g.get_feature_tag (feature_index);
1715   const OT::Feature &f = g.get_feature (feature_index);
1716
1717   const OT::FeatureParams &feature_params = f.get_feature_params ();
1718   if (&feature_params != &Null (OT::FeatureParams))
1719   {
1720     const OT::FeatureParamsStylisticSet& ss_params =
1721       feature_params.get_stylistic_set_params (feature_tag);
1722     if (&ss_params != &Null (OT::FeatureParamsStylisticSet)) /* ssXX */
1723     {
1724       if (label_id) *label_id = ss_params.uiNameID;
1725       // ssXX features don't have the rest
1726       if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1727       if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1728       if (num_named_parameters) *num_named_parameters = 0;
1729       if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1730       return true;
1731     }
1732     const OT::FeatureParamsCharacterVariants& cv_params =
1733       feature_params.get_character_variants_params (feature_tag);
1734     if (&cv_params != &Null (OT::FeatureParamsCharacterVariants)) /* cvXX */
1735     {
1736       if (label_id) *label_id = cv_params.featUILableNameID;
1737       if (tooltip_id) *tooltip_id = cv_params.featUITooltipTextNameID;
1738       if (sample_id) *sample_id = cv_params.sampleTextNameID;
1739       if (num_named_parameters) *num_named_parameters = cv_params.numNamedParameters;
1740       if (first_param_id) *first_param_id = cv_params.firstParamUILabelNameID;
1741       return true;
1742     }
1743   }
1744
1745   if (label_id) *label_id = HB_OT_NAME_ID_INVALID;
1746   if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1747   if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1748   if (num_named_parameters) *num_named_parameters = 0;
1749   if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1750   return false;
1751 }
1752 /**
1753  * hb_ot_layout_feature_get_characters:
1754  * @face: #hb_face_t to work upon
1755  * @table_tag: table tag to query, "GSUB" or "GPOS".
1756  * @feature_index: index of feature to query.
1757  * @start_offset: offset of the first character to retrieve
1758  * @char_count: (inout) (optional): Input = the maximum number of characters to return;
1759  *              Output = the actual number of characters returned (may be zero)
1760  * @characters: (out caller-allocates) (array length=char_count): A buffer pointer.
1761  *              The Unicode codepoints of the characters for which this feature provides
1762  *               glyph variants.
1763  *
1764  * Fetches a list of the characters defined as having a variant under the specified
1765  * "Character Variant" ("cvXX") feature tag.
1766  *
1767  * Return value: Number of total sample characters in the cvXX feature.
1768  *
1769  * Since: 2.0.0
1770  **/
1771 unsigned int
1772 hb_ot_layout_feature_get_characters (hb_face_t      *face,
1773                                      hb_tag_t        table_tag,
1774                                      unsigned int    feature_index,
1775                                      unsigned int    start_offset,
1776                                      unsigned int   *char_count, /* IN/OUT.  May be NULL */
1777                                      hb_codepoint_t *characters  /* OUT.     May be NULL */)
1778 {
1779   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1780   return g.get_feature (feature_index)
1781           .get_feature_params ()
1782           .get_character_variants_params(g.get_feature_tag (feature_index))
1783           .get_characters (start_offset, char_count, characters);
1784 }
1785 #endif
1786
1787
1788 /*
1789  * Parts of different types are implemented here such that they have direct
1790  * access to GSUB/GPOS lookups.
1791  */
1792
1793
1794 struct GSUBProxy
1795 {
1796   static constexpr unsigned table_index = 0u;
1797   static constexpr bool inplace = false;
1798   typedef OT::SubstLookup Lookup;
1799
1800   GSUBProxy (hb_face_t *face) :
1801     table (*face->table.GSUB->table),
1802     accels (face->table.GSUB->accels) {}
1803
1804   const OT::GSUB &table;
1805   const OT::hb_ot_layout_lookup_accelerator_t *accels;
1806 };
1807
1808 struct GPOSProxy
1809 {
1810   static constexpr unsigned table_index = 1u;
1811   static constexpr bool inplace = true;
1812   typedef OT::PosLookup Lookup;
1813
1814   GPOSProxy (hb_face_t *face) :
1815     table (*face->table.GPOS->table),
1816     accels (face->table.GPOS->accels) {}
1817
1818   const OT::GPOS &table;
1819   const OT::hb_ot_layout_lookup_accelerator_t *accels;
1820 };
1821
1822
1823 static inline bool
1824 apply_forward (OT::hb_ot_apply_context_t *c,
1825                const OT::hb_ot_layout_lookup_accelerator_t &accel)
1826 {
1827   bool ret = false;
1828   hb_buffer_t *buffer = c->buffer;
1829   while (buffer->idx < buffer->len && buffer->successful)
1830   {
1831     bool applied = false;
1832     if (accel.may_have (buffer->cur().codepoint) &&
1833         (buffer->cur().mask & c->lookup_mask) &&
1834         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1835      {
1836        applied = accel.apply (c);
1837      }
1838
1839     if (applied)
1840       ret = true;
1841     else
1842       (void) buffer->next_glyph ();
1843   }
1844   return ret;
1845 }
1846
1847 static inline bool
1848 apply_backward (OT::hb_ot_apply_context_t *c,
1849                const OT::hb_ot_layout_lookup_accelerator_t &accel)
1850 {
1851   bool ret = false;
1852   hb_buffer_t *buffer = c->buffer;
1853   do
1854   {
1855     if (accel.may_have (buffer->cur().codepoint) &&
1856         (buffer->cur().mask & c->lookup_mask) &&
1857         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1858      ret |= accel.apply (c);
1859
1860     /* The reverse lookup doesn't "advance" cursor (for good reason). */
1861     buffer->idx--;
1862
1863   }
1864   while ((int) buffer->idx >= 0);
1865   return ret;
1866 }
1867
1868 template <typename Proxy>
1869 static inline void
1870 apply_string (OT::hb_ot_apply_context_t *c,
1871               const typename Proxy::Lookup &lookup,
1872               const OT::hb_ot_layout_lookup_accelerator_t &accel)
1873 {
1874   hb_buffer_t *buffer = c->buffer;
1875
1876   if (unlikely (!buffer->len || !c->lookup_mask))
1877     return;
1878
1879   c->set_lookup_props (lookup.get_props ());
1880
1881   if (likely (!lookup.is_reverse ()))
1882   {
1883     /* in/out forward substitution/positioning */
1884     if (!Proxy::inplace)
1885       buffer->clear_output ();
1886
1887     buffer->idx = 0;
1888     apply_forward (c, accel);
1889
1890     if (!Proxy::inplace)
1891       buffer->sync ();
1892   }
1893   else
1894   {
1895     /* in-place backward substitution/positioning */
1896     assert (!buffer->have_output);
1897     buffer->idx = buffer->len - 1;
1898     apply_backward (c, accel);
1899   }
1900 }
1901
1902 template <typename Proxy>
1903 inline void hb_ot_map_t::apply (const Proxy &proxy,
1904                                 const hb_ot_shape_plan_t *plan,
1905                                 hb_font_t *font,
1906                                 hb_buffer_t *buffer) const
1907 {
1908   const unsigned int table_index = proxy.table_index;
1909   unsigned int i = 0;
1910   OT::hb_ot_apply_context_t c (table_index, font, buffer);
1911   c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1912
1913   for (unsigned int stage_index = 0; stage_index < stages[table_index].length; stage_index++)
1914   {
1915     const stage_map_t *stage = &stages[table_index][stage_index];
1916     for (; i < stage->last_lookup; i++)
1917     {
1918       unsigned int lookup_index = lookups[table_index][i].index;
1919       if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1920       c.set_lookup_index (lookup_index);
1921       c.set_lookup_mask (lookups[table_index][i].mask);
1922       c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1923       c.set_auto_zwnj (lookups[table_index][i].auto_zwnj);
1924       c.set_random (lookups[table_index][i].random);
1925
1926       apply_string<Proxy> (&c,
1927                            proxy.table.get_lookup (lookup_index),
1928                            proxy.accels[lookup_index]);
1929       (void) buffer->message (font, "end lookup %d", lookup_index);
1930     }
1931
1932     if (stage->pause_func)
1933       stage->pause_func (plan, font, buffer);
1934   }
1935 }
1936
1937 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1938 {
1939   GSUBProxy proxy (font->face);
1940   if (!buffer->message (font, "start table GSUB")) return;
1941   apply (proxy, plan, font, buffer);
1942   (void) buffer->message (font, "end table GSUB");
1943 }
1944
1945 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1946 {
1947   GPOSProxy proxy (font->face);
1948   if (!buffer->message (font, "start table GPOS")) return;
1949   apply (proxy, plan, font, buffer);
1950   (void) buffer->message (font, "end table GPOS");
1951 }
1952
1953 void
1954 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
1955                                 const OT::SubstLookup &lookup,
1956                                 const OT::hb_ot_layout_lookup_accelerator_t &accel)
1957 {
1958   apply_string<GSUBProxy> (c, lookup, accel);
1959 }
1960
1961 #ifndef HB_NO_BASE
1962 /**
1963  * hb_ot_layout_get_baseline:
1964  * @font: a font
1965  * @baseline_tag: a baseline tag
1966  * @direction: text direction.
1967  * @script_tag:  script tag.
1968  * @language_tag: language tag, currently unused.
1969  * @coord: (out): baseline value if found.
1970  *
1971  * Fetches a baseline value from the face.
1972  *
1973  * Return value: %true if found baseline value in the font.
1974  *
1975  * Since: 2.6.0
1976  **/
1977 hb_bool_t
1978 hb_ot_layout_get_baseline (hb_font_t                   *font,
1979                            hb_ot_layout_baseline_tag_t  baseline_tag,
1980                            hb_direction_t               direction,
1981                            hb_tag_t                     script_tag,
1982                            hb_tag_t                     language_tag,
1983                            hb_position_t               *coord        /* OUT.  May be NULL. */)
1984 {
1985   bool result = font->face->table.BASE->get_baseline (font, baseline_tag, direction, script_tag, language_tag, coord);
1986
1987   if (result && coord)
1988     *coord = HB_DIRECTION_IS_HORIZONTAL (direction) ? font->em_scale_y (*coord) : font->em_scale_x (*coord);
1989
1990   return result;
1991 }
1992 #endif
1993
1994
1995 struct hb_get_glyph_alternates_dispatch_t :
1996        hb_dispatch_context_t<hb_get_glyph_alternates_dispatch_t, unsigned>
1997 {
1998   static return_t default_return_value () { return 0; }
1999   bool stop_sublookup_iteration (return_t r) const { return r; }
2000
2001   hb_face_t *face;
2002
2003   hb_get_glyph_alternates_dispatch_t (hb_face_t *face) :
2004                                         face (face) {}
2005
2006   private:
2007   template <typename T, typename ...Ts> auto
2008   _dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
2009   ( obj.get_glyph_alternates (std::forward<Ts> (ds)...) )
2010   template <typename T, typename ...Ts> auto
2011   _dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
2012   ( default_return_value () )
2013   public:
2014   template <typename T, typename ...Ts> auto
2015   dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
2016   ( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
2017 };
2018
2019 /**
2020  * hb_ot_layout_lookup_get_glyph_alternates:
2021  * @face: a face.
2022  * @lookup_index: index of the feature lookup to query.
2023  * @glyph: a glyph id.
2024  * @start_offset: starting offset.
2025  * @alternate_count: (inout) (optional): Input = the maximum number of alternate glyphs to return;
2026  *                   Output = the actual number of alternate glyphs returned (may be zero).
2027  * @alternate_glyphs: (out caller-allocates) (array length=alternate_count): A glyphs buffer.
2028  *                    Alternate glyphs associated with the glyph id.
2029  *
2030  * Fetches alternates of a glyph from a given GSUB lookup index.
2031  *
2032  * Return value: Total number of alternates found in the specific lookup index for the given glyph id.
2033  *
2034  * Since: 2.6.8
2035  **/
2036 HB_EXTERN unsigned
2037 hb_ot_layout_lookup_get_glyph_alternates (hb_face_t      *face,
2038                                           unsigned        lookup_index,
2039                                           hb_codepoint_t  glyph,
2040                                           unsigned        start_offset,
2041                                           unsigned       *alternate_count  /* IN/OUT.  May be NULL. */,
2042                                           hb_codepoint_t *alternate_glyphs /* OUT.     May be NULL. */)
2043 {
2044   hb_get_glyph_alternates_dispatch_t c (face);
2045   const OT::SubstLookup &lookup = face->table.GSUB->table->get_lookup (lookup_index);
2046   auto ret = lookup.dispatch (&c, glyph, start_offset, alternate_count, alternate_glyphs);
2047   if (!ret && alternate_count) *alternate_count = 0;
2048   return ret;
2049 }
2050
2051 #endif