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