Fix build error
[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"
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 using OT::Layout::GSUB;
58 using OT::Layout::GPOS;
59
60 /**
61  * SECTION:hb-ot-layout
62  * @title: hb-ot-layout
63  * @short_description: OpenType Layout
64  * @include: hb-ot.h
65  *
66  * Functions for querying OpenType Layout features in the font face.
67  * See the [OpenType specification](http://www.microsoft.com/typography/otspec/)
68  * for details.
69  **/
70
71
72 /*
73  * kern
74  */
75
76 #ifndef HB_NO_OT_KERN
77 /**
78  * hb_ot_layout_has_kerning:
79  * @face: The #hb_face_t to work on
80  *
81  * Tests whether a face includes any kerning data in the 'kern' table.
82  * Does NOT test for kerning lookups in the GPOS table.
83  *
84  * Return value: `true` if data found, `false` otherwise
85  *
86  **/
87 bool
88 hb_ot_layout_has_kerning (hb_face_t *face)
89 {
90   return face->table.kern->has_data ();
91 }
92
93 /**
94  * hb_ot_layout_has_machine_kerning:
95  * @face: The #hb_face_t to work on
96  *
97  * Tests whether a face includes any state-machine kerning in the 'kern' table.
98  * Does NOT examine the GPOS table.
99  *
100  * Return value: `true` if data found, `false` otherwise
101  *
102  **/
103 bool
104 hb_ot_layout_has_machine_kerning (hb_face_t *face)
105 {
106   return face->table.kern->has_state_machine ();
107 }
108
109 /**
110  * hb_ot_layout_has_cross_kerning:
111  * @face: The #hb_face_t to work on
112  *
113  * Tests whether a face has any cross-stream kerning (i.e., kerns
114  * that make adjustments perpendicular to the direction of the text
115  * flow: Y adjustments in horizontal text or X adjustments in
116  * vertical text) in the 'kern' table.
117  *
118  * Does NOT examine the GPOS table.
119  *
120  * Return value: `true` is data found, `false` otherwise
121  *
122  **/
123 bool
124 hb_ot_layout_has_cross_kerning (hb_face_t *face)
125 {
126   return face->table.kern->has_cross_stream ();
127 }
128
129 void
130 hb_ot_layout_kern (const hb_ot_shape_plan_t *plan,
131                    hb_font_t *font,
132                    hb_buffer_t  *buffer)
133 {
134   hb_blob_t *blob = font->face->table.kern.get_blob ();
135   const AAT::kern& kern = *blob->as<AAT::kern> ();
136
137   AAT::hb_aat_apply_context_t c (plan, font, buffer, blob);
138
139   if (!buffer->message (font, "start table kern")) return;
140   kern.apply (&c);
141   (void) buffer->message (font, "end table kern");
142 }
143 #endif
144
145
146 /*
147  * GDEF
148  */
149
150 bool
151 OT::GDEF::is_blocklisted (hb_blob_t *blob,
152                           hb_face_t *face) const
153 {
154 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
155   return false;
156 #endif
157   /* The ugly business of blocklisting individual fonts' tables happen here!
158    * See this thread for why we finally had to bend in and do this:
159    * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
160    *
161    * In certain versions of Times New Roman Italic and Bold Italic,
162    * ASCII double quotation mark U+0022 has wrong glyph class 3 (mark)
163    * in GDEF.  Many versions of Tahoma have bad GDEF tables that
164    * incorrectly classify some spacing marks such as certain IPA
165    * symbols as glyph class 3. So do older versions of Microsoft
166    * Himalaya, and the version of Cantarell shipped by Ubuntu 16.04.
167    *
168    * Nuke the GDEF tables of to avoid unwanted width-zeroing.
169    *
170    * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
171    *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
172    *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
173    */
174   switch HB_CODEPOINT_ENCODE3(blob->length,
175                               face->table.GSUB->table.get_length (),
176                               face->table.GPOS->table.get_length ())
177   {
178     /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
179     case HB_CODEPOINT_ENCODE3 (442, 2874, 42038):
180     /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
181     case HB_CODEPOINT_ENCODE3 (430, 2874, 40662):
182     /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
183     case HB_CODEPOINT_ENCODE3 (442, 2874, 39116):
184     /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
185     case HB_CODEPOINT_ENCODE3 (430, 2874, 39374):
186     /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
187     case HB_CODEPOINT_ENCODE3 (490, 3046, 41638):
188     /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
189     case HB_CODEPOINT_ENCODE3 (478, 3046, 41902):
190     /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c  tahoma.ttf from Windows 8 */
191     case HB_CODEPOINT_ENCODE3 (898, 12554, 46470):
192     /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc  tahomabd.ttf from Windows 8 */
193     case HB_CODEPOINT_ENCODE3 (910, 12566, 47732):
194     /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e  tahoma.ttf from Windows 8.1 */
195     case HB_CODEPOINT_ENCODE3 (928, 23298, 59332):
196     /* sha1sum:6d400781948517c3c0441ba42acb309584b73033  tahomabd.ttf from Windows 8.1 */
197     case HB_CODEPOINT_ENCODE3 (940, 23310, 60732):
198     /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
199     case HB_CODEPOINT_ENCODE3 (964, 23836, 60072):
200     /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
201     case HB_CODEPOINT_ENCODE3 (976, 23832, 61456):
202     /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846  tahoma.ttf from Windows 10 */
203     case HB_CODEPOINT_ENCODE3 (994, 24474, 60336):
204     /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343  tahomabd.ttf from Windows 10 */
205     case HB_CODEPOINT_ENCODE3 (1006, 24470, 61740):
206     /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
207     case HB_CODEPOINT_ENCODE3 (1006, 24576, 61346):
208     /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
209     case HB_CODEPOINT_ENCODE3 (1018, 24572, 62828):
210     /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5  tahoma.ttf from Windows 10 AU */
211     case HB_CODEPOINT_ENCODE3 (1006, 24576, 61352):
212     /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2  tahomabd.ttf from Windows 10 AU */
213     case HB_CODEPOINT_ENCODE3 (1018, 24572, 62834):
214     /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7  Tahoma.ttf from Mac OS X 10.9 */
215     case HB_CODEPOINT_ENCODE3 (832, 7324, 47162):
216     /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba  Tahoma Bold.ttf from Mac OS X 10.9 */
217     case HB_CODEPOINT_ENCODE3 (844, 7302, 45474):
218     /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc  himalaya.ttf from Windows 7 */
219     case HB_CODEPOINT_ENCODE3 (180, 13054, 7254):
220     /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0  himalaya.ttf from Windows 8 */
221     case HB_CODEPOINT_ENCODE3 (192, 12638, 7254):
222     /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427  himalaya.ttf from Windows 8.1 */
223     case HB_CODEPOINT_ENCODE3 (192, 12690, 7254):
224     /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44  cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
225     /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371  cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
226     case HB_CODEPOINT_ENCODE3 (188, 248, 3852):
227     /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f  cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
228     /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b  cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
229     case HB_CODEPOINT_ENCODE3 (188, 264, 3426):
230     /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
231     case HB_CODEPOINT_ENCODE3 (1058, 47032, 11818):
232     /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
233     case HB_CODEPOINT_ENCODE3 (1046, 47030, 12600):
234     /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
235     case HB_CODEPOINT_ENCODE3 (1058, 71796, 16770):
236     /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
237     case HB_CODEPOINT_ENCODE3 (1046, 71790, 17862):
238     /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
239     case HB_CODEPOINT_ENCODE3 (1046, 71788, 17112):
240     /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
241     case HB_CODEPOINT_ENCODE3 (1058, 71794, 17514):
242     /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
243     case HB_CODEPOINT_ENCODE3 (1330, 109904, 57938):
244     /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
245     case HB_CODEPOINT_ENCODE3 (1330, 109904, 58972):
246     /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85  Padauk.ttf
247      *  "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
248     case HB_CODEPOINT_ENCODE3 (1004, 59092, 14836):
249       return true;
250   }
251   return false;
252 }
253
254 static void
255 _hb_ot_layout_set_glyph_props (hb_font_t *font,
256                                hb_buffer_t *buffer)
257 {
258   _hb_buffer_assert_gsubgpos_vars (buffer);
259
260   const auto &gdef = *font->face->table.GDEF;
261   unsigned int count = buffer->len;
262   hb_glyph_info_t *info = buffer->info;
263   for (unsigned int i = 0; i < count; i++)
264   {
265     _hb_glyph_info_set_glyph_props (&info[i], gdef.get_glyph_props (info[i].codepoint));
266     _hb_glyph_info_clear_lig_props (&info[i]);
267   }
268 }
269
270 /* Public API */
271
272 /**
273  * hb_ot_layout_has_glyph_classes:
274  * @face: #hb_face_t to work upon
275  *
276  * Tests whether a face has any glyph classes defined in its GDEF table.
277  *
278  * Return value: `true` if data found, `false` otherwise
279  *
280  **/
281 hb_bool_t
282 hb_ot_layout_has_glyph_classes (hb_face_t *face)
283 {
284   return face->table.GDEF->table->has_glyph_classes ();
285 }
286
287 /**
288  * hb_ot_layout_get_glyph_class:
289  * @face: The #hb_face_t to work on
290  * @glyph: The #hb_codepoint_t code point to query
291  *
292  * Fetches the GDEF class of the requested glyph in the specified face.
293  *
294  * Return value: The #hb_ot_layout_glyph_class_t glyph class of the given code
295  * point in the GDEF table of the face.
296  *
297  * Since: 0.9.7
298  **/
299 hb_ot_layout_glyph_class_t
300 hb_ot_layout_get_glyph_class (hb_face_t      *face,
301                               hb_codepoint_t  glyph)
302 {
303   return (hb_ot_layout_glyph_class_t) face->table.GDEF->table->get_glyph_class (glyph);
304 }
305
306 /**
307  * hb_ot_layout_get_glyphs_in_class:
308  * @face: The #hb_face_t to work on
309  * @klass: The #hb_ot_layout_glyph_class_t GDEF class to retrieve
310  * @glyphs: (out): The #hb_set_t set of all glyphs belonging to the requested
311  *          class.
312  *
313  * Retrieves the set of all glyphs from the face that belong to the requested
314  * glyph class in the face's GDEF table.
315  *
316  * Since: 0.9.7
317  **/
318 void
319 hb_ot_layout_get_glyphs_in_class (hb_face_t                  *face,
320                                   hb_ot_layout_glyph_class_t  klass,
321                                   hb_set_t                   *glyphs /* OUT */)
322 {
323   return face->table.GDEF->table->get_glyphs_in_class (klass, glyphs);
324 }
325
326 #ifndef HB_NO_LAYOUT_UNUSED
327 /**
328  * hb_ot_layout_get_attach_points:
329  * @face: The #hb_face_t to work on
330  * @glyph: The #hb_codepoint_t code point to query
331  * @start_offset: offset of the first attachment point to retrieve
332  * @point_count: (inout) (optional): Input = the maximum number of attachment points to return;
333  *               Output = the actual number of attachment points returned (may be zero)
334  * @point_array: (out) (array length=point_count): The array of attachment points found for the query
335  *
336  * Fetches a list of all attachment points for the specified glyph in the GDEF
337  * table of the face. The list returned will begin at the offset provided.
338  *
339  * Useful if the client program wishes to cache the list.
340  *
341  * Return value: Total number of attachment points for @glyph.
342  *
343  **/
344 unsigned int
345 hb_ot_layout_get_attach_points (hb_face_t      *face,
346                                 hb_codepoint_t  glyph,
347                                 unsigned int    start_offset,
348                                 unsigned int   *point_count /* IN/OUT */,
349                                 unsigned int   *point_array /* OUT */)
350 {
351   return face->table.GDEF->table->get_attach_points (glyph,
352                                                      start_offset,
353                                                      point_count,
354                                                      point_array);
355 }
356 /**
357  * hb_ot_layout_get_ligature_carets:
358  * @font: The #hb_font_t to work on
359  * @direction: The #hb_direction_t text direction to use
360  * @glyph: The #hb_codepoint_t code point to query
361  * @start_offset: offset of the first caret position to retrieve
362  * @caret_count: (inout) (optional): Input = the maximum number of caret positions to return;
363  *               Output = the actual number of caret positions returned (may be zero)
364  * @caret_array: (out) (array length=caret_count): The array of caret positions found for the query
365  *
366  * Fetches a list of the caret positions defined for a ligature glyph in the GDEF
367  * table of the font. The list returned will begin at the offset provided.
368  *
369  * Note that a ligature that is formed from n characters will have n-1
370  * caret positions. The first character is not represented in the array,
371  * since its caret position is the glyph position.
372  *
373  * The positions returned by this function are 'unshaped', and will have to
374  * be fixed up for kerning that may be applied to the ligature glyph.
375  *
376  * Return value: Total number of ligature caret positions for @glyph.
377  *
378  **/
379 unsigned int
380 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
381                                   hb_direction_t  direction,
382                                   hb_codepoint_t  glyph,
383                                   unsigned int    start_offset,
384                                   unsigned int   *caret_count /* IN/OUT */,
385                                   hb_position_t  *caret_array /* OUT */)
386 {
387   return font->face->table.GDEF->table->get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
388 }
389 #endif
390
391
392 /*
393  * GSUB/GPOS
394  */
395
396 bool
397 GSUB::is_blocklisted (hb_blob_t *blob HB_UNUSED,
398                           hb_face_t *face) const
399 {
400 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
401   return false;
402 #endif
403   return false;
404 }
405
406 bool
407 GPOS::is_blocklisted (hb_blob_t *blob HB_UNUSED,
408                           hb_face_t *face HB_UNUSED) const
409 {
410 #ifdef HB_NO_OT_LAYOUT_BLOCKLIST
411   return false;
412 #endif
413   return false;
414 }
415
416 static const OT::GSUBGPOS&
417 get_gsubgpos_table (hb_face_t *face,
418                     hb_tag_t   table_tag)
419 {
420   switch (table_tag) {
421     case HB_OT_TAG_GSUB: return *face->table.GSUB->table;
422     case HB_OT_TAG_GPOS: return *face->table.GPOS->table;
423     default:             return Null (OT::GSUBGPOS);
424   }
425 }
426
427
428 /**
429  * hb_ot_layout_table_get_script_tags:
430  * @face: #hb_face_t to work upon
431  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
432  * @start_offset: offset of the first script tag to retrieve
433  * @script_count: (inout) (optional): Input = the maximum number of script tags to return;
434  *                Output = the actual number of script tags returned (may be zero)
435  * @script_tags: (out) (array length=script_count): The array of #hb_tag_t script tags found for the query
436  *
437  * Fetches a list of all scripts enumerated in the specified face's GSUB table
438  * or GPOS table. The list returned will begin at the offset provided.
439  *
440  * Return value: Total number of script tags.
441  *
442  **/
443 unsigned int
444 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
445                                     hb_tag_t      table_tag,
446                                     unsigned int  start_offset,
447                                     unsigned int *script_count /* IN/OUT */,
448                                     hb_tag_t     *script_tags  /* OUT */)
449 {
450   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
451
452   return g.get_script_tags (start_offset, script_count, script_tags);
453 }
454
455 #define HB_OT_TAG_LATIN_SCRIPT          HB_TAG ('l', 'a', 't', 'n')
456
457 /**
458  * hb_ot_layout_table_find_script:
459  * @face: #hb_face_t to work upon
460  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
461  * @script_tag: #hb_tag_t of the script tag requested
462  * @script_index: (out): The index of the requested script tag
463  *
464  * Fetches the index if a given script tag in the specified face's GSUB table
465  * or GPOS table.
466  *
467  * Return value: `true` if the script is found, `false` otherwise
468  *
469  **/
470 hb_bool_t
471 hb_ot_layout_table_find_script (hb_face_t    *face,
472                                 hb_tag_t      table_tag,
473                                 hb_tag_t      script_tag,
474                                 unsigned int *script_index /* OUT */)
475 {
476   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
477   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
478
479   if (g.find_script_index (script_tag, script_index))
480     return true;
481
482   /* try finding 'DFLT' */
483   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
484     return false;
485
486   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
487    * including many versions of DejaVu Sans Mono! */
488   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
489     return false;
490
491   /* try with 'latn'; some old fonts put their features there even though
492      they're really trying to support Thai, for example :( */
493   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
494     return false;
495
496   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
497   return false;
498 }
499
500 #ifndef HB_DISABLE_DEPRECATED
501 /**
502  * hb_ot_layout_table_choose_script:
503  * @face: #hb_face_t to work upon
504  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
505  * @script_tags: Array of #hb_tag_t script tags
506  * @script_index: (out): The index of the chosen script
507  * @chosen_script: (out): #hb_tag_t of the chosen script
508  *
509  * Deprecated since 2.0.0
510  **/
511 hb_bool_t
512 hb_ot_layout_table_choose_script (hb_face_t      *face,
513                                   hb_tag_t        table_tag,
514                                   const hb_tag_t *script_tags,
515                                   unsigned int   *script_index  /* OUT */,
516                                   hb_tag_t       *chosen_script /* OUT */)
517 {
518   const hb_tag_t *t;
519   for (t = script_tags; *t; t++);
520   return hb_ot_layout_table_select_script (face, table_tag, t - script_tags, script_tags, script_index, chosen_script);
521 }
522 #endif
523
524 /**
525  * hb_ot_layout_table_select_script:
526  * @face: #hb_face_t to work upon
527  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
528  * @script_count: Number of script tags in the array
529  * @script_tags: Array of #hb_tag_t script tags
530  * @script_index: (out) (optional): The index of the requested script
531  * @chosen_script: (out) (optional): #hb_tag_t of the requested script
532  *
533  * Selects an OpenType script for @table_tag from the @script_tags array.
534  *
535  * If the table does not have any of the requested scripts, then `DFLT`,
536  * `dflt`, and `latn` tags are tried in that order. If the table still does not
537  * have any of these scripts, @script_index is set to
538  * #HB_OT_LAYOUT_NO_SCRIPT_INDEX and @chosen_script is set to #HB_TAG_NONE.
539  *
540  * Return value:
541  * `true` if one of the requested scripts is selected, `false` if a fallback
542  * script is selected or if no scripts are selected.
543  *
544  * Since: 2.0.0
545  **/
546 hb_bool_t
547 hb_ot_layout_table_select_script (hb_face_t      *face,
548                                   hb_tag_t        table_tag,
549                                   unsigned int    script_count,
550                                   const hb_tag_t *script_tags,
551                                   unsigned int   *script_index  /* OUT */,
552                                   hb_tag_t       *chosen_script /* OUT */)
553 {
554   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
555   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
556   unsigned int i;
557
558   for (i = 0; i < script_count; i++)
559   {
560     if (g.find_script_index (script_tags[i], script_index))
561     {
562       if (chosen_script)
563         *chosen_script = script_tags[i];
564       return true;
565     }
566   }
567
568   /* try finding 'DFLT' */
569   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
570     if (chosen_script)
571       *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
572     return false;
573   }
574
575   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
576   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
577     if (chosen_script)
578       *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
579     return false;
580   }
581
582   /* try with 'latn'; some old fonts put their features there even though
583      they're really trying to support Thai, for example :( */
584   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
585     if (chosen_script)
586       *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
587     return false;
588   }
589
590   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
591   if (chosen_script)
592     *chosen_script = HB_TAG_NONE;
593   return false;
594 }
595
596
597 /**
598  * hb_ot_layout_table_get_feature_tags:
599  * @face: #hb_face_t to work upon
600  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
601  * @start_offset: offset of the first feature tag to retrieve
602  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
603  *                 Output = the actual number of feature tags returned (may be zero)
604  * @feature_tags: (out) (array length=feature_count): Array of feature tags found in the table
605  *
606  * Fetches a list of all feature tags in the given face's GSUB or GPOS table.
607  * Note that there might be duplicate feature tags, belonging to different
608  * script/language-system pairs of the table.
609  *
610  * Return value: Total number of feature tags.
611  *
612  * Since: 0.6.0
613  *
614  **/
615 unsigned int
616 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
617                                      hb_tag_t      table_tag,
618                                      unsigned int  start_offset,
619                                      unsigned int *feature_count /* IN/OUT */,
620                                      hb_tag_t     *feature_tags  /* OUT */)
621 {
622   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
623
624   return g.get_feature_tags (start_offset, feature_count, feature_tags);
625 }
626
627
628 /**
629  * hb_ot_layout_table_find_feature:
630  * @face: #hb_face_t to work upon
631  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
632  * @feature_tag: The #hb_tag_t of the requested feature tag
633  * @feature_index: (out): The index of the requested feature
634  *
635  * Fetches the index for a given feature tag in the specified face's GSUB table
636  * or GPOS table.
637  *
638  * Return value: `true` if the feature is found, `false` otherwise
639  *
640  * Since: 0.6.0
641  *
642  **/
643 bool
644 hb_ot_layout_table_find_feature (hb_face_t    *face,
645                                  hb_tag_t      table_tag,
646                                  hb_tag_t      feature_tag,
647                                  unsigned int *feature_index /* OUT */)
648 {
649   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
650   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
651
652   unsigned int num_features = g.get_feature_count ();
653   for (unsigned int i = 0; i < num_features; i++)
654   {
655     if (feature_tag == g.get_feature_tag (i)) {
656       if (feature_index) *feature_index = i;
657       return true;
658     }
659   }
660
661   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
662   return false;
663 }
664
665
666 /**
667  * hb_ot_layout_script_get_language_tags:
668  * @face: #hb_face_t to work upon
669  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
670  * @script_index: The index of the requested script tag
671  * @start_offset: offset of the first language tag to retrieve
672  * @language_count: (inout) (optional): Input = the maximum number of language tags to return;
673  *                  Output = the actual number of language tags returned (may be zero)
674  * @language_tags: (out) (array length=language_count): Array of language tags found in the table
675  *
676  * Fetches a list of language tags in the given face's GSUB or GPOS table, underneath
677  * the specified script index. The list returned will begin at the offset provided.
678  *
679  * Return value: Total number of language tags.
680  *
681  * Since: 0.6.0
682  *
683  **/
684 unsigned int
685 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
686                                        hb_tag_t      table_tag,
687                                        unsigned int  script_index,
688                                        unsigned int  start_offset,
689                                        unsigned int *language_count /* IN/OUT */,
690                                        hb_tag_t     *language_tags  /* OUT */)
691 {
692   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
693
694   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
695 }
696
697
698 #ifndef HB_DISABLE_DEPRECATED
699 /**
700  * hb_ot_layout_script_find_language:
701  * @face: #hb_face_t to work upon
702  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
703  * @script_index: The index of the requested script tag
704  * @language_tag: The #hb_tag_t of the requested language
705  * @language_index: The index of the requested language
706  *
707  * Fetches the index of a given language tag in the specified face's GSUB table
708  * or GPOS table, underneath the specified script tag.
709  *
710  * Return value: `true` if the language tag is found, `false` otherwise
711  *
712  * Since: 0.6.0
713  * Deprecated: 2.0.0
714  **/
715 hb_bool_t
716 hb_ot_layout_script_find_language (hb_face_t    *face,
717                                    hb_tag_t      table_tag,
718                                    unsigned int  script_index,
719                                    hb_tag_t      language_tag,
720                                    unsigned int *language_index)
721 {
722   return hb_ot_layout_script_select_language (face,
723                                               table_tag,
724                                               script_index,
725                                               1,
726                                               &language_tag,
727                                               language_index);
728 }
729 #endif
730
731
732 /**
733  * hb_ot_layout_script_select_language2:
734  * @face: #hb_face_t to work upon
735  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
736  * @script_index: The index of the requested script tag
737  * @language_count: The number of languages in the specified script
738  * @language_tags: The array of language tags
739  * @language_index: (out): The index of the chosen language
740  * @chosen_language: (out): #hb_tag_t of the chosen language
741  *
742  * Fetches the index of the first language tag fom @language_tags that is present
743  * in the specified face's GSUB or GPOS table, underneath the specified script
744  * index.
745  *
746  * If none of the given language tags is found, `false` is returned and
747  * @language_index is set to #HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX and
748  * @chosen_language is set to #HB_TAG_NONE.
749  *
750  * Return value: `true` if one of the given language tags is found, `false` otherwise
751  *
752  * Since: 7.0.0
753  **/
754 hb_bool_t
755 hb_ot_layout_script_select_language2 (hb_face_t      *face,
756                                      hb_tag_t        table_tag,
757                                      unsigned int    script_index,
758                                      unsigned int    language_count,
759                                      const hb_tag_t *language_tags,
760                                      unsigned int   *language_index /* OUT */,
761                                      hb_tag_t       *chosen_language /* OUT */)
762 {
763   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
764   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
765   unsigned int i;
766
767   for (i = 0; i < language_count; i++)
768   {
769     if (s.find_lang_sys_index (language_tags[i], language_index))
770     {
771       if (chosen_language)
772         *chosen_language = language_tags[i];
773       return true;
774     }
775   }
776
777   /* try finding 'dflt' */
778   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
779   {
780     if (chosen_language)
781       *chosen_language = HB_OT_TAG_DEFAULT_LANGUAGE;
782     return false;
783   }
784
785   if (language_index)
786     *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
787   if (chosen_language)
788     *chosen_language = HB_TAG_NONE;
789   return false;
790 }
791
792 /**
793  * hb_ot_layout_script_select_language:
794  * @face: #hb_face_t to work upon
795  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
796  * @script_index: The index of the requested script tag
797  * @language_count: The number of languages in the specified script
798  * @language_tags: The array of language tags
799  * @language_index: (out): The index of the requested language
800  *
801  * Fetches the index of the first language tag fom @language_tags that is present
802  * in the specified face's GSUB or GPOS table, underneath the specified script
803  * index.
804  *
805  * If none of the given language tags is found, `false` is returned and
806  * @language_index is set to the default language index.
807  *
808  * Return value: `true` if one of the given language tags is found, `false` otherwise
809  *
810  * Since: 2.0.0
811  **/
812 hb_bool_t
813 hb_ot_layout_script_select_language (hb_face_t      *face,
814                                      hb_tag_t        table_tag,
815                                      unsigned int    script_index,
816                                      unsigned int    language_count,
817                                      const hb_tag_t *language_tags,
818                                      unsigned int   *language_index /* OUT */)
819 {
820   return hb_ot_layout_script_select_language2 (face, table_tag,
821                                                script_index,
822                                                language_count, language_tags,
823                                                language_index, nullptr);
824 }
825
826 /**
827  * hb_ot_layout_language_get_required_feature_index:
828  * @face: #hb_face_t to work upon
829  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
830  * @script_index: The index of the requested script tag
831  * @language_index: The index of the requested language tag
832  * @feature_index: (out): The index of the requested feature
833  *
834  * Fetches the index of a requested feature in the given face's GSUB or GPOS table,
835  * underneath the specified script and language.
836  *
837  * Return value: `true` if the feature is found, `false` otherwise
838  *
839  * Since: 0.6.0
840  *
841  **/
842 hb_bool_t
843 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
844                                                   hb_tag_t      table_tag,
845                                                   unsigned int  script_index,
846                                                   unsigned int  language_index,
847                                                   unsigned int *feature_index /* OUT */)
848 {
849   return hb_ot_layout_language_get_required_feature (face,
850                                                      table_tag,
851                                                      script_index,
852                                                      language_index,
853                                                      feature_index,
854                                                      nullptr);
855 }
856
857
858 /**
859  * hb_ot_layout_language_get_required_feature:
860  * @face: #hb_face_t to work upon
861  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
862  * @script_index: The index of the requested script tag
863  * @language_index: The index of the requested language tag
864  * @feature_index: (out): The index of the requested feature
865  * @feature_tag: (out): The #hb_tag_t of the requested feature
866  *
867  * Fetches the tag of a requested feature index in the given face's GSUB or GPOS table,
868  * underneath the specified script and language.
869  *
870  * Return value: `true` if the feature is found, `false` otherwise
871  *
872  * Since: 0.9.30
873  **/
874 hb_bool_t
875 hb_ot_layout_language_get_required_feature (hb_face_t    *face,
876                                             hb_tag_t      table_tag,
877                                             unsigned int  script_index,
878                                             unsigned int  language_index,
879                                             unsigned int *feature_index /* OUT */,
880                                             hb_tag_t     *feature_tag   /* OUT */)
881 {
882   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
883   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
884
885   unsigned int index = l.get_required_feature_index ();
886   if (feature_index) *feature_index = index;
887   if (feature_tag) *feature_tag = g.get_feature_tag (index);
888
889   return l.has_required_feature ();
890 }
891
892
893 /**
894  * hb_ot_layout_language_get_feature_indexes:
895  * @face: #hb_face_t to work upon
896  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
897  * @script_index: The index of the requested script tag
898  * @language_index: The index of the requested language tag
899  * @start_offset: offset of the first feature tag to retrieve
900  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
901  *                 Output: the actual number of feature tags returned (may be zero)
902  * @feature_indexes: (out) (array length=feature_count): The array of feature indexes found for the query
903  *
904  * Fetches a list of all features in the specified face's GSUB table
905  * or GPOS table, underneath the specified script and language. The list
906  * returned will begin at the offset provided.
907  *
908  * Return value: Total number of features.
909  *
910  * Since: 0.6.0
911  *
912  **/
913 unsigned int
914 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
915                                            hb_tag_t      table_tag,
916                                            unsigned int  script_index,
917                                            unsigned int  language_index,
918                                            unsigned int  start_offset,
919                                            unsigned int *feature_count   /* IN/OUT */,
920                                            unsigned int *feature_indexes /* OUT */)
921 {
922   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
923   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
924
925   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
926 }
927
928
929 /**
930  * hb_ot_layout_language_get_feature_tags:
931  * @face: #hb_face_t to work upon
932  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
933  * @script_index: The index of the requested script tag
934  * @language_index: The index of the requested language tag
935  * @start_offset: offset of the first feature tag to retrieve
936  * @feature_count: (inout) (optional): Input = the maximum number of feature tags to return;
937  *                 Output = the actual number of feature tags returned (may be zero)
938  * @feature_tags: (out) (array length=feature_count): The array of #hb_tag_t feature tags found for the query
939  *
940  * Fetches a list of all features in the specified face's GSUB table
941  * or GPOS table, underneath the specified script and language. The list
942  * returned will begin at the offset provided.
943  *
944  * Return value: Total number of feature tags.
945  *
946  * Since: 0.6.0
947  *
948  **/
949 unsigned int
950 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
951                                         hb_tag_t      table_tag,
952                                         unsigned int  script_index,
953                                         unsigned int  language_index,
954                                         unsigned int  start_offset,
955                                         unsigned int *feature_count /* IN/OUT */,
956                                         hb_tag_t     *feature_tags  /* OUT */)
957 {
958   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
959   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
960
961   static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
962   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
963
964   if (feature_tags) {
965     unsigned int count = *feature_count;
966     for (unsigned int i = 0; i < count; i++)
967       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
968   }
969
970   return ret;
971 }
972
973
974 /**
975  * hb_ot_layout_language_find_feature:
976  * @face: #hb_face_t to work upon
977  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
978  * @script_index: The index of the requested script tag
979  * @language_index: The index of the requested language tag
980  * @feature_tag: #hb_tag_t of the feature tag requested
981  * @feature_index: (out): The index of the requested feature
982  *
983  * Fetches the index of a given feature tag in the specified face's GSUB table
984  * or GPOS table, underneath the specified script and language.
985  *
986  * Return value: `true` if the feature is found, `false` otherwise
987  *
988  * Since: 0.6.0
989  *
990  **/
991 hb_bool_t
992 hb_ot_layout_language_find_feature (hb_face_t    *face,
993                                     hb_tag_t      table_tag,
994                                     unsigned int  script_index,
995                                     unsigned int  language_index,
996                                     hb_tag_t      feature_tag,
997                                     unsigned int *feature_index /* OUT */)
998 {
999   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
1000   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1001   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
1002
1003   unsigned int num_features = l.get_feature_count ();
1004   for (unsigned int i = 0; i < num_features; i++) {
1005     unsigned int f_index = l.get_feature_index (i);
1006
1007     if (feature_tag == g.get_feature_tag (f_index)) {
1008       if (feature_index) *feature_index = f_index;
1009       return true;
1010     }
1011   }
1012
1013   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
1014   return false;
1015 }
1016
1017
1018 /**
1019  * hb_ot_layout_feature_get_lookups:
1020  * @face: #hb_face_t to work upon
1021  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1022  * @feature_index: The index of the requested feature
1023  * @start_offset: offset of the first lookup to retrieve
1024  * @lookup_count: (inout) (optional): Input = the maximum number of lookups to return;
1025  *                Output = the actual number of lookups returned (may be zero)
1026  * @lookup_indexes: (out) (array length=lookup_count): The array of lookup indexes found for the query
1027  *
1028  * Fetches a list of all lookups enumerated for the specified feature, in
1029  * the specified face's GSUB table or GPOS table. The list returned will
1030  * begin at the offset provided.
1031  *
1032  * Return value: Total number of lookups.
1033  *
1034  * Since: 0.9.7
1035  **/
1036 unsigned int
1037 hb_ot_layout_feature_get_lookups (hb_face_t    *face,
1038                                   hb_tag_t      table_tag,
1039                                   unsigned int  feature_index,
1040                                   unsigned int  start_offset,
1041                                   unsigned int *lookup_count   /* IN/OUT */,
1042                                   unsigned int *lookup_indexes /* OUT */)
1043 {
1044   return hb_ot_layout_feature_with_variations_get_lookups (face,
1045                                                            table_tag,
1046                                                            feature_index,
1047                                                            HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
1048                                                            start_offset,
1049                                                            lookup_count,
1050                                                            lookup_indexes);
1051 }
1052
1053
1054 /**
1055  * hb_ot_layout_table_get_lookup_count:
1056  * @face: #hb_face_t to work upon
1057  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1058  *
1059  * Fetches the total number of lookups enumerated in the specified
1060  * face's GSUB table or GPOS table.
1061  *
1062  * Return value: Total number of lookups.
1063  *
1064  * Since: 0.9.22
1065  **/
1066 unsigned int
1067 hb_ot_layout_table_get_lookup_count (hb_face_t    *face,
1068                                      hb_tag_t      table_tag)
1069 {
1070   return get_gsubgpos_table (face, table_tag).get_lookup_count ();
1071 }
1072
1073
1074 struct hb_collect_features_context_t
1075 {
1076   hb_collect_features_context_t (hb_face_t *face,
1077                                  hb_tag_t   table_tag,
1078                                  hb_set_t  *feature_indices_,
1079                                  const hb_tag_t *features)
1080
1081     : g (get_gsubgpos_table (face, table_tag)),
1082       feature_indices (feature_indices_),
1083       has_feature_filter (false),
1084       script_count (0),langsys_count (0), feature_index_count (0)
1085   {
1086     compute_feature_filter (features);
1087   }
1088
1089   void compute_feature_filter (const hb_tag_t *features)
1090   {
1091     if (features == nullptr)
1092     {
1093       has_feature_filter = false;
1094       return;
1095     }
1096
1097     has_feature_filter = true;
1098     hb_set_t features_set;
1099     for (; *features; features++)
1100       features_set.add (*features);
1101
1102     for (unsigned i = 0; i < g.get_feature_count (); i++)
1103     {
1104       hb_tag_t tag = g.get_feature_tag (i);
1105       if (features_set.has (tag))
1106         feature_indices_filter.add(i);
1107     }
1108   }
1109
1110   bool visited (const OT::Script &s)
1111   {
1112     /* We might have Null() object here.  Don't want to involve
1113      * that in the memoize.  So, detect empty objects and return. */
1114     if (unlikely (!s.has_default_lang_sys () &&
1115                   !s.get_lang_sys_count ()))
1116       return true;
1117
1118     if (script_count++ > HB_MAX_SCRIPTS)
1119       return true;
1120
1121     return visited (s, visited_script);
1122   }
1123   bool visited (const OT::LangSys &l)
1124   {
1125     /* We might have Null() object here.  Don't want to involve
1126      * that in the memoize.  So, detect empty objects and return. */
1127     if (unlikely (!l.has_required_feature () &&
1128                   !l.get_feature_count ()))
1129       return true;
1130
1131     if (langsys_count++ > HB_MAX_LANGSYS)
1132       return true;
1133
1134     return visited (l, visited_langsys);
1135   }
1136
1137   bool visited_feature_indices (unsigned count)
1138   {
1139     feature_index_count += count;
1140     return feature_index_count > HB_MAX_FEATURE_INDICES;
1141   }
1142
1143   private:
1144   template <typename T>
1145   bool visited (const T &p, hb_set_t &visited_set)
1146   {
1147     hb_codepoint_t delta = (hb_codepoint_t) ((uintptr_t) &p - (uintptr_t) &g);
1148      if (visited_set.has (delta))
1149       return true;
1150
1151     visited_set.add (delta);
1152     return false;
1153   }
1154
1155   public:
1156   const OT::GSUBGPOS &g;
1157   hb_set_t *feature_indices;
1158   hb_set_t  feature_indices_filter;
1159   bool has_feature_filter;
1160
1161   private:
1162   hb_set_t visited_script;
1163   hb_set_t visited_langsys;
1164   unsigned int script_count;
1165   unsigned int langsys_count;
1166   unsigned int feature_index_count;
1167 };
1168
1169 static void
1170 langsys_collect_features (hb_collect_features_context_t *c,
1171                           const OT::LangSys  &l)
1172 {
1173   if (c->visited (l)) return;
1174
1175   if (!c->has_feature_filter)
1176   {
1177     /* All features. */
1178     if (l.has_required_feature () && !c->visited_feature_indices (1))
1179       c->feature_indices->add (l.get_required_feature_index ());
1180
1181     // TODO(garretrieger): filter out indices >= feature count?
1182     if (!c->visited_feature_indices (l.featureIndex.len))
1183       l.add_feature_indexes_to (c->feature_indices);
1184   }
1185   else
1186   {
1187     if (c->feature_indices_filter.is_empty()) return;
1188     unsigned int num_features = l.get_feature_count ();
1189     for (unsigned int i = 0; i < num_features; i++)
1190     {
1191       unsigned int feature_index = l.get_feature_index (i);
1192       if (!c->feature_indices_filter.has (feature_index)) continue;
1193
1194       c->feature_indices->add (feature_index);
1195       c->feature_indices_filter.del (feature_index);
1196     }
1197   }
1198 }
1199
1200 static void
1201 script_collect_features (hb_collect_features_context_t *c,
1202                          const OT::Script   &s,
1203                          const hb_tag_t *languages)
1204 {
1205   if (c->visited (s)) return;
1206
1207   if (!languages)
1208   {
1209     /* All languages. */
1210     if (s.has_default_lang_sys ())
1211       langsys_collect_features (c,
1212                                 s.get_default_lang_sys ());
1213
1214
1215     unsigned int count = s.get_lang_sys_count ();
1216     for (unsigned int language_index = 0; language_index < count; language_index++)
1217       langsys_collect_features (c,
1218                                 s.get_lang_sys (language_index));
1219   }
1220   else
1221   {
1222     for (; *languages; languages++)
1223     {
1224       unsigned int language_index;
1225       if (s.find_lang_sys_index (*languages, &language_index))
1226         langsys_collect_features (c,
1227                                   s.get_lang_sys (language_index));
1228
1229     }
1230   }
1231 }
1232
1233
1234 /**
1235  * hb_ot_layout_collect_features:
1236  * @face: #hb_face_t to work upon
1237  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1238  * @scripts: (nullable) (array zero-terminated=1): The array of scripts to collect features for,
1239  *   terminated by %HB_TAG_NONE
1240  * @languages: (nullable) (array zero-terminated=1): The array of languages to collect features for,
1241  *   terminated by %HB_TAG_NONE
1242  * @features: (nullable) (array zero-terminated=1): The array of features to collect,
1243  *   terminated by %HB_TAG_NONE
1244  * @feature_indexes: (out): The set of feature indexes found for the query
1245  *
1246  * Fetches a list of all feature indexes in the specified face's GSUB table
1247  * or GPOS table, underneath the specified scripts, languages, and features.
1248  * If no list of scripts is provided, all scripts will be queried. If no list
1249  * of languages is provided, all languages will be queried. If no list of
1250  * features is provided, all features will be queried.
1251  *
1252  * Since: 1.8.5
1253  **/
1254 void
1255 hb_ot_layout_collect_features (hb_face_t      *face,
1256                                hb_tag_t        table_tag,
1257                                const hb_tag_t *scripts,
1258                                const hb_tag_t *languages,
1259                                const hb_tag_t *features,
1260                                hb_set_t       *feature_indexes /* OUT */)
1261 {
1262   hb_collect_features_context_t c (face, table_tag, feature_indexes, features);
1263   if (!scripts)
1264   {
1265     /* All scripts. */
1266     unsigned int count = c.g.get_script_count ();
1267     for (unsigned int script_index = 0; script_index < count; script_index++)
1268       script_collect_features (&c,
1269                                c.g.get_script (script_index),
1270                                languages);
1271   }
1272   else
1273   {
1274     for (; *scripts; scripts++)
1275     {
1276       unsigned int script_index;
1277       if (c.g.find_script_index (*scripts, &script_index))
1278         script_collect_features (&c,
1279                                  c.g.get_script (script_index),
1280                                  languages);
1281     }
1282   }
1283 }
1284
1285 /**
1286  * hb_ot_layout_collect_features_map:
1287  * @face: #hb_face_t to work upon
1288  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1289  * @script_index: The index of the requested script tag
1290  * @language_index: The index of the requested language tag
1291  * @feature_map: (out): The map of feature tag to feature index.
1292  *
1293  * Fetches the mapping from feature tags to feature indexes for
1294  * the specified script and language.
1295  *
1296  * Since: 8.1.0
1297  **/
1298 void
1299 hb_ot_layout_collect_features_map (hb_face_t      *face,
1300                                    hb_tag_t        table_tag,
1301                                    unsigned        script_index,
1302                                    unsigned        language_index,
1303                                    hb_map_t       *feature_map /* OUT */)
1304 {
1305   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1306   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
1307
1308   unsigned int count = l.get_feature_indexes (0, nullptr, nullptr);
1309   feature_map->alloc (count);
1310
1311   /* Loop in reverse, such that earlier entries win. That emulates
1312    * a linear search, which seems to be what other implementations do.
1313    * We found that with arialuni_t.ttf, the "ur" language system has
1314    * duplicate features, and the earlier ones work but not later ones.
1315    */
1316   for (unsigned int i = count; i; i--)
1317   {
1318     unsigned feature_index = 0;
1319     unsigned feature_count = 1;
1320     l.get_feature_indexes (i - 1, &feature_count, &feature_index);
1321     if (!feature_count)
1322       break;
1323     hb_tag_t feature_tag = g.get_feature_tag (feature_index);
1324     feature_map->set (feature_tag, feature_index);
1325   }
1326 }
1327
1328
1329 /**
1330  * hb_ot_layout_collect_lookups:
1331  * @face: #hb_face_t to work upon
1332  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1333  * @scripts: (nullable) (array zero-terminated=1): The array of scripts to collect lookups for,
1334  *   terminated by %HB_TAG_NONE
1335  * @languages: (nullable) (array zero-terminated=1): The array of languages to collect lookups for,
1336  *   terminated by %HB_TAG_NONE
1337  * @features: (nullable) (array zero-terminated=1): The array of features to collect lookups for,
1338  *   terminated by %HB_TAG_NONE
1339  * @lookup_indexes: (out): The array of lookup indexes found for the query
1340  *
1341  * Fetches a list of all feature-lookup indexes in the specified face's GSUB
1342  * table or GPOS table, underneath the specified scripts, languages, and
1343  * features. If no list of scripts is provided, all scripts will be queried.
1344  * If no list of languages is provided, all languages will be queried. If no
1345  * list of features is provided, all features will be queried.
1346  *
1347  * Since: 0.9.8
1348  **/
1349 void
1350 hb_ot_layout_collect_lookups (hb_face_t      *face,
1351                               hb_tag_t        table_tag,
1352                               const hb_tag_t *scripts,
1353                               const hb_tag_t *languages,
1354                               const hb_tag_t *features,
1355                               hb_set_t       *lookup_indexes /* OUT */)
1356 {
1357   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1358
1359   hb_set_t feature_indexes;
1360   hb_ot_layout_collect_features (face, table_tag, scripts, languages, features, &feature_indexes);
1361
1362   for (auto feature_index : feature_indexes)
1363     g.get_feature (feature_index).add_lookup_indexes_to (lookup_indexes);
1364
1365   g.feature_variation_collect_lookups (&feature_indexes, nullptr, lookup_indexes);
1366 }
1367
1368
1369 #ifndef HB_NO_LAYOUT_COLLECT_GLYPHS
1370 /**
1371  * hb_ot_layout_lookup_collect_glyphs:
1372  * @face: #hb_face_t to work upon
1373  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1374  * @lookup_index: The index of the feature lookup to query
1375  * @glyphs_before: (out): Array of glyphs preceding the substitution range
1376  * @glyphs_input: (out): Array of input glyphs that would be substituted by the lookup
1377  * @glyphs_after: (out): Array of glyphs following the substitution range
1378  * @glyphs_output: (out): Array of glyphs that would be the substituted output of the lookup
1379  *
1380  * Fetches a list of all glyphs affected by the specified lookup in the
1381  * specified face's GSUB table or GPOS table.
1382  *
1383  * Since: 0.9.7
1384  **/
1385 void
1386 hb_ot_layout_lookup_collect_glyphs (hb_face_t    *face,
1387                                     hb_tag_t      table_tag,
1388                                     unsigned int  lookup_index,
1389                                     hb_set_t     *glyphs_before, /* OUT.  May be NULL */
1390                                     hb_set_t     *glyphs_input,  /* OUT.  May be NULL */
1391                                     hb_set_t     *glyphs_after,  /* OUT.  May be NULL */
1392                                     hb_set_t     *glyphs_output  /* OUT.  May be NULL */)
1393 {
1394   OT::hb_collect_glyphs_context_t c (face,
1395                                      glyphs_before,
1396                                      glyphs_input,
1397                                      glyphs_after,
1398                                      glyphs_output);
1399
1400   switch (table_tag)
1401   {
1402     case HB_OT_TAG_GSUB:
1403     {
1404       const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1405       l.collect_glyphs (&c);
1406       return;
1407     }
1408     case HB_OT_TAG_GPOS:
1409     {
1410       const OT::PosLookup& l = face->table.GPOS->table->get_lookup (lookup_index);
1411       l.collect_glyphs (&c);
1412       return;
1413     }
1414   }
1415 }
1416 #endif
1417
1418
1419 /* Variations support */
1420
1421
1422 /**
1423  * hb_ot_layout_table_find_feature_variations:
1424  * @face: #hb_face_t to work upon
1425  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1426  * @coords: The variation coordinates to query
1427  * @num_coords: The number of variation coordinates
1428  * @variations_index: (out): The array of feature variations found for the query
1429  *
1430  * Fetches a list of feature variations in the specified face's GSUB table
1431  * or GPOS table, at the specified variation coordinates.
1432  *
1433  * Return value: `true` if feature variations were found, `false` otherwise.
1434  *
1435  * Since: 1.4.0
1436  *
1437  **/
1438 hb_bool_t
1439 hb_ot_layout_table_find_feature_variations (hb_face_t    *face,
1440                                             hb_tag_t      table_tag,
1441                                             const int    *coords,
1442                                             unsigned int  num_coords,
1443                                             unsigned int *variations_index /* out */)
1444 {
1445   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1446
1447   return g.find_variations_index (coords, num_coords, variations_index);
1448 }
1449
1450
1451 /**
1452  * hb_ot_layout_feature_with_variations_get_lookups:
1453  * @face: #hb_face_t to work upon
1454  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1455  * @feature_index: The index of the feature to query
1456  * @variations_index: The index of the feature variation to query
1457  * @start_offset: offset of the first lookup to retrieve
1458  * @lookup_count: (inout) (optional): Input = the maximum number of lookups to return;
1459  *                Output = the actual number of lookups returned (may be zero)
1460  * @lookup_indexes: (out) (array length=lookup_count): The array of lookups found for the query
1461  *
1462  * Fetches a list of all lookups enumerated for the specified feature, in
1463  * the specified face's GSUB table or GPOS table, enabled at the specified
1464  * variations index. The list returned will begin at the offset provided.
1465  *
1466  * Return value: Total number of lookups.
1467  *
1468  * Since: 1.4.0
1469  *
1470  **/
1471 unsigned int
1472 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t    *face,
1473                                                   hb_tag_t      table_tag,
1474                                                   unsigned int  feature_index,
1475                                                   unsigned int  variations_index,
1476                                                   unsigned int  start_offset,
1477                                                   unsigned int *lookup_count /* IN/OUT */,
1478                                                   unsigned int *lookup_indexes /* OUT */)
1479 {
1480   static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
1481   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1482
1483   const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
1484
1485   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
1486 }
1487
1488
1489 /*
1490  * OT::GSUB
1491  */
1492
1493
1494 /**
1495  * hb_ot_layout_has_substitution:
1496  * @face: #hb_face_t to work upon
1497  *
1498  * Tests whether the specified face includes any GSUB substitutions.
1499  *
1500  * Return value: `true` if data found, `false` otherwise
1501  *
1502  * Since: 0.6.0
1503  *
1504  **/
1505 hb_bool_t
1506 hb_ot_layout_has_substitution (hb_face_t *face)
1507 {
1508   return face->table.GSUB->table->has_data ();
1509 }
1510
1511
1512 /**
1513  * hb_ot_layout_lookup_would_substitute:
1514  * @face: #hb_face_t to work upon
1515  * @lookup_index: The index of the lookup to query
1516  * @glyphs: The sequence of glyphs to query for substitution
1517  * @glyphs_length: The length of the glyph sequence
1518  * @zero_context: #hb_bool_t indicating whether pre-/post-context are disallowed
1519  * in substitutions
1520  *
1521  * Tests whether a specified lookup in the specified face would
1522  * trigger a substitution on the given glyph sequence.
1523  *
1524  * Return value: `true` if a substitution would be triggered, `false` otherwise
1525  *
1526  * Since: 0.9.7
1527  **/
1528 hb_bool_t
1529 hb_ot_layout_lookup_would_substitute (hb_face_t            *face,
1530                                       unsigned int          lookup_index,
1531                                       const hb_codepoint_t *glyphs,
1532                                       unsigned int          glyphs_length,
1533                                       hb_bool_t             zero_context)
1534 {
1535   auto &gsub = face->table.GSUB;
1536   if (unlikely (lookup_index >= gsub->lookup_count)) return false;
1537   OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
1538
1539   const OT::SubstLookup& l = gsub->table->get_lookup (lookup_index);
1540   auto *accel = gsub->get_accel (lookup_index);
1541   return accel && l.would_apply (&c, accel);
1542 }
1543
1544
1545 /**
1546  * hb_ot_layout_substitute_start:
1547  * @font: #hb_font_t to use
1548  * @buffer: #hb_buffer_t buffer to work upon
1549  *
1550  * Called before substitution lookups are performed, to ensure that glyph
1551  * class and other properties are set on the glyphs in the buffer.
1552  *
1553  **/
1554 void
1555 hb_ot_layout_substitute_start (hb_font_t    *font,
1556                                hb_buffer_t  *buffer)
1557 {
1558   _hb_ot_layout_set_glyph_props (font, buffer);
1559 }
1560
1561 /**
1562  * hb_ot_layout_lookup_substitute_closure:
1563  * @face: #hb_face_t to work upon
1564  * @lookup_index: index of the feature lookup to query
1565  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookup
1566  *
1567  * Compute the transitive closure of glyphs needed for a
1568  * specified lookup.
1569  *
1570  * Since: 0.9.7
1571  **/
1572 void
1573 hb_ot_layout_lookup_substitute_closure (hb_face_t    *face,
1574                                         unsigned int  lookup_index,
1575                                         hb_set_t     *glyphs /* OUT */)
1576 {
1577   hb_map_t done_lookups_glyph_count;
1578   hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> done_lookups_glyph_set;
1579   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1580
1581   const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1582
1583   l.closure (&c, lookup_index);
1584 }
1585
1586 /**
1587  * hb_ot_layout_lookups_substitute_closure:
1588  * @face: #hb_face_t to work upon
1589  * @lookups: The set of lookups to query
1590  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookups
1591  *
1592  * Compute the transitive closure of glyphs needed for all of the
1593  * provided lookups.
1594  *
1595  * Since: 1.8.1
1596  **/
1597 void
1598 hb_ot_layout_lookups_substitute_closure (hb_face_t      *face,
1599                                          const hb_set_t *lookups,
1600                                          hb_set_t       *glyphs /* OUT */)
1601 {
1602   hb_map_t done_lookups_glyph_count;
1603   hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> done_lookups_glyph_set;
1604   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1605   const GSUB& gsub = *face->table.GSUB->table;
1606
1607   unsigned int iteration_count = 0;
1608   unsigned int glyphs_length;
1609   do
1610   {
1611     c.reset_lookup_visit_count ();
1612     glyphs_length = glyphs->get_population ();
1613     if (lookups)
1614     {
1615       for (auto lookup_index : *lookups)
1616         gsub.get_lookup (lookup_index).closure (&c, lookup_index);
1617     }
1618     else
1619     {
1620       for (unsigned int i = 0; i < gsub.get_lookup_count (); i++)
1621         gsub.get_lookup (i).closure (&c, i);
1622     }
1623   } while (iteration_count++ <= HB_CLOSURE_MAX_STAGES &&
1624            glyphs_length != glyphs->get_population ());
1625 }
1626
1627 /*
1628  * GPOS
1629  */
1630
1631
1632 /**
1633  * hb_ot_layout_has_positioning:
1634  * @face: #hb_face_t to work upon
1635  *
1636  * Tests whether the specified face includes any GPOS positioning.
1637  *
1638  * Return value: `true` if the face has GPOS data, `false` otherwise
1639  *
1640  **/
1641 hb_bool_t
1642 hb_ot_layout_has_positioning (hb_face_t *face)
1643 {
1644   return face->table.GPOS->table->has_data ();
1645 }
1646
1647 /**
1648  * hb_ot_layout_position_start:
1649  * @font: #hb_font_t to use
1650  * @buffer: #hb_buffer_t buffer to work upon
1651  *
1652  * Called before positioning lookups are performed, to ensure that glyph
1653  * attachment types and glyph-attachment chains are set for the glyphs in the buffer.
1654  *
1655  **/
1656 void
1657 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
1658 {
1659   GPOS::position_start (font, buffer);
1660 }
1661
1662
1663 /**
1664  * hb_ot_layout_position_finish_advances:
1665  * @font: #hb_font_t to use
1666  * @buffer: #hb_buffer_t buffer to work upon
1667  *
1668  * Called after positioning lookups are performed, to finish glyph advances.
1669  *
1670  **/
1671 void
1672 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
1673 {
1674   GPOS::position_finish_advances (font, buffer);
1675 }
1676
1677 /**
1678  * hb_ot_layout_position_finish_offsets:
1679  * @font: #hb_font_t to use
1680  * @buffer: #hb_buffer_t buffer to work upon
1681  *
1682  * Called after positioning lookups are performed, to finish glyph offsets.
1683  *
1684  **/
1685 void
1686 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
1687 {
1688   GPOS::position_finish_offsets (font, buffer);
1689 }
1690
1691
1692 #ifndef HB_NO_LAYOUT_FEATURE_PARAMS
1693 /**
1694  * hb_ot_layout_get_size_params:
1695  * @face: #hb_face_t to work upon
1696  * @design_size: (out): The design size of the face
1697  * @subfamily_id: (out): The identifier of the face within the font subfamily
1698  * @subfamily_name_id: (out): The ‘name’ table name ID of the face within the font subfamily
1699  * @range_start: (out): The minimum size of the recommended size range for the face
1700  * @range_end: (out): The maximum size of the recommended size range for the face
1701  *
1702  * Fetches optical-size feature data (i.e., the `size` feature from GPOS). Note that
1703  * the subfamily_id and the subfamily name string (accessible via the subfamily_name_id)
1704  * as used here are defined as pertaining only to fonts within a font family that differ
1705  * specifically in their respective size ranges; other ways to differentiate fonts within
1706  * a subfamily are not covered by the `size` feature.
1707  *
1708  * For more information on this distinction, see the [`size` feature documentation](
1709  * https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#tag-size).
1710  *
1711  * Return value: `true` if data found, `false` otherwise
1712  *
1713  * Since: 0.9.10
1714  **/
1715 hb_bool_t
1716 hb_ot_layout_get_size_params (hb_face_t       *face,
1717                               unsigned int    *design_size,       /* OUT.  May be NULL */
1718                               unsigned int    *subfamily_id,      /* OUT.  May be NULL */
1719                               hb_ot_name_id_t *subfamily_name_id, /* OUT.  May be NULL */
1720                               unsigned int    *range_start,       /* OUT.  May be NULL */
1721                               unsigned int    *range_end          /* OUT.  May be NULL */)
1722 {
1723   const GPOS &gpos = *face->table.GPOS->table;
1724   const hb_tag_t tag = HB_TAG ('s','i','z','e');
1725
1726   unsigned int num_features = gpos.get_feature_count ();
1727   for (unsigned int i = 0; i < num_features; i++)
1728   {
1729     if (tag == gpos.get_feature_tag (i))
1730     {
1731       const OT::Feature &f = gpos.get_feature (i);
1732       const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
1733
1734       if (params.designSize)
1735       {
1736         if (design_size) *design_size = params.designSize;
1737         if (subfamily_id) *subfamily_id = params.subfamilyID;
1738         if (subfamily_name_id) *subfamily_name_id = params.subfamilyNameID;
1739         if (range_start) *range_start = params.rangeStart;
1740         if (range_end) *range_end = params.rangeEnd;
1741
1742         return true;
1743       }
1744     }
1745   }
1746
1747   if (design_size) *design_size = 0;
1748   if (subfamily_id) *subfamily_id = 0;
1749   if (subfamily_name_id) *subfamily_name_id = HB_OT_NAME_ID_INVALID;
1750   if (range_start) *range_start = 0;
1751   if (range_end) *range_end = 0;
1752
1753   return false;
1754 }
1755
1756
1757 /**
1758  * hb_ot_layout_feature_get_name_ids:
1759  * @face: #hb_face_t to work upon
1760  * @table_tag: table tag to query, "GSUB" or "GPOS".
1761  * @feature_index: index of feature to query.
1762  * @label_id: (out) (optional): The ‘name’ table name ID that specifies a string
1763  *            for a user-interface label for this feature. (May be NULL.)
1764  * @tooltip_id: (out) (optional): The ‘name’ table name ID that specifies a string
1765  *              that an application can use for tooltip text for this
1766  *              feature. (May be NULL.)
1767  * @sample_id: (out) (optional): The ‘name’ table name ID that specifies sample text
1768  *             that illustrates the effect of this feature. (May be NULL.)
1769  * @num_named_parameters: (out) (optional):  Number of named parameters. (May be zero.)
1770  * @first_param_id: (out) (optional): The first ‘name’ table name ID used to specify
1771  *                  strings for user-interface labels for the feature
1772  *                  parameters. (Must be zero if numParameters is zero.)
1773  *
1774  * Fetches name indices from feature parameters for "Stylistic Set" ('ssXX') or
1775  * "Character Variant" ('cvXX') features.
1776  *
1777  * Return value: `true` if data found, `false` otherwise
1778  *
1779  * Since: 2.0.0
1780  **/
1781 hb_bool_t
1782 hb_ot_layout_feature_get_name_ids (hb_face_t       *face,
1783                                    hb_tag_t         table_tag,
1784                                    unsigned int     feature_index,
1785                                    hb_ot_name_id_t *label_id,             /* OUT.  May be NULL */
1786                                    hb_ot_name_id_t *tooltip_id,           /* OUT.  May be NULL */
1787                                    hb_ot_name_id_t *sample_id,            /* OUT.  May be NULL */
1788                                    unsigned int    *num_named_parameters, /* OUT.  May be NULL */
1789                                    hb_ot_name_id_t *first_param_id        /* OUT.  May be NULL */)
1790 {
1791   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1792
1793   hb_tag_t feature_tag = g.get_feature_tag (feature_index);
1794   const OT::Feature &f = g.get_feature (feature_index);
1795
1796   const OT::FeatureParams &feature_params = f.get_feature_params ();
1797   if (&feature_params != &Null (OT::FeatureParams))
1798   {
1799     const OT::FeatureParamsStylisticSet& ss_params =
1800       feature_params.get_stylistic_set_params (feature_tag);
1801     if (&ss_params != &Null (OT::FeatureParamsStylisticSet)) /* ssXX */
1802     {
1803       if (label_id) *label_id = ss_params.uiNameID;
1804       // ssXX features don't have the rest
1805       if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1806       if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1807       if (num_named_parameters) *num_named_parameters = 0;
1808       if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1809       return true;
1810     }
1811     const OT::FeatureParamsCharacterVariants& cv_params =
1812       feature_params.get_character_variants_params (feature_tag);
1813     if (&cv_params != &Null (OT::FeatureParamsCharacterVariants)) /* cvXX */
1814     {
1815       if (label_id) *label_id = cv_params.featUILableNameID;
1816       if (tooltip_id) *tooltip_id = cv_params.featUITooltipTextNameID;
1817       if (sample_id) *sample_id = cv_params.sampleTextNameID;
1818       if (num_named_parameters) *num_named_parameters = cv_params.numNamedParameters;
1819       if (first_param_id) *first_param_id = cv_params.firstParamUILabelNameID;
1820       return true;
1821     }
1822   }
1823
1824   if (label_id) *label_id = HB_OT_NAME_ID_INVALID;
1825   if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1826   if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1827   if (num_named_parameters) *num_named_parameters = 0;
1828   if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1829   return false;
1830 }
1831 /**
1832  * hb_ot_layout_feature_get_characters:
1833  * @face: #hb_face_t to work upon
1834  * @table_tag: table tag to query, "GSUB" or "GPOS".
1835  * @feature_index: index of feature to query.
1836  * @start_offset: offset of the first character to retrieve
1837  * @char_count: (inout) (optional): Input = the maximum number of characters to return;
1838  *              Output = the actual number of characters returned (may be zero)
1839  * @characters: (out caller-allocates) (array length=char_count): A buffer pointer.
1840  *              The Unicode codepoints of the characters for which this feature provides
1841  *               glyph variants.
1842  *
1843  * Fetches a list of the characters defined as having a variant under the specified
1844  * "Character Variant" ("cvXX") feature tag.
1845  *
1846  * Return value: Number of total sample characters in the cvXX feature.
1847  *
1848  * Since: 2.0.0
1849  **/
1850 unsigned int
1851 hb_ot_layout_feature_get_characters (hb_face_t      *face,
1852                                      hb_tag_t        table_tag,
1853                                      unsigned int    feature_index,
1854                                      unsigned int    start_offset,
1855                                      unsigned int   *char_count, /* IN/OUT.  May be NULL */
1856                                      hb_codepoint_t *characters  /* OUT.     May be NULL */)
1857 {
1858   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1859   return g.get_feature (feature_index)
1860           .get_feature_params ()
1861           .get_character_variants_params(g.get_feature_tag (feature_index))
1862           .get_characters (start_offset, char_count, characters);
1863 }
1864 #endif
1865
1866
1867 /*
1868  * Parts of different types are implemented here such that they have direct
1869  * access to GSUB/GPOS lookups.
1870  */
1871
1872
1873 struct GSUBProxy
1874 {
1875   static constexpr unsigned table_index = 0u;
1876   static constexpr bool always_inplace = false;
1877   typedef OT::SubstLookup Lookup;
1878
1879   GSUBProxy (hb_face_t *face) :
1880     accel (*face->table.GSUB) {}
1881
1882   const GSUB::accelerator_t &accel;
1883 };
1884
1885 struct GPOSProxy
1886 {
1887   static constexpr unsigned table_index = 1u;
1888   static constexpr bool always_inplace = true;
1889   typedef OT::PosLookup Lookup;
1890
1891   GPOSProxy (hb_face_t *face) :
1892     accel (*face->table.GPOS) {}
1893
1894   const GPOS::accelerator_t &accel;
1895 };
1896
1897
1898 static inline bool
1899 apply_forward (OT::hb_ot_apply_context_t *c,
1900                const OT::hb_ot_layout_lookup_accelerator_t &accel,
1901                unsigned subtable_count)
1902 {
1903   bool use_cache = accel.cache_enter (c);
1904
1905   bool ret = false;
1906   hb_buffer_t *buffer = c->buffer;
1907   while (buffer->idx < buffer->len && buffer->successful)
1908   {
1909     bool applied = false;
1910     if (accel.digest.may_have (buffer->cur().codepoint) &&
1911         (buffer->cur().mask & c->lookup_mask) &&
1912         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1913      {
1914        applied = accel.apply (c, subtable_count, use_cache);
1915      }
1916
1917     if (applied)
1918       ret = true;
1919     else
1920       (void) buffer->next_glyph ();
1921   }
1922
1923   if (use_cache)
1924     accel.cache_leave (c);
1925
1926   return ret;
1927 }
1928
1929 static inline bool
1930 apply_backward (OT::hb_ot_apply_context_t *c,
1931                const OT::hb_ot_layout_lookup_accelerator_t &accel,
1932                unsigned subtable_count)
1933 {
1934   bool ret = false;
1935   hb_buffer_t *buffer = c->buffer;
1936   do
1937   {
1938     if (accel.digest.may_have (buffer->cur().codepoint) &&
1939         (buffer->cur().mask & c->lookup_mask) &&
1940         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1941       ret |= accel.apply (c, subtable_count, false);
1942
1943     /* The reverse lookup doesn't "advance" cursor (for good reason). */
1944     buffer->idx--;
1945
1946   }
1947   while ((int) buffer->idx >= 0);
1948   return ret;
1949 }
1950
1951 template <typename Proxy>
1952 static inline bool
1953 apply_string (OT::hb_ot_apply_context_t *c,
1954               const typename Proxy::Lookup &lookup,
1955               const OT::hb_ot_layout_lookup_accelerator_t &accel)
1956 {
1957   hb_buffer_t *buffer = c->buffer;
1958   unsigned subtable_count = lookup.get_subtable_count ();
1959
1960   if (unlikely (!buffer->len || !c->lookup_mask))
1961     return false;
1962
1963   bool ret = false;
1964
1965   c->set_lookup_props (lookup.get_props ());
1966
1967   if (likely (!lookup.is_reverse ()))
1968   {
1969     /* in/out forward substitution/positioning */
1970     if (!Proxy::always_inplace)
1971       buffer->clear_output ();
1972
1973     buffer->idx = 0;
1974     ret = apply_forward (c, accel, subtable_count);
1975
1976     if (!Proxy::always_inplace)
1977       buffer->sync ();
1978   }
1979   else
1980   {
1981     /* in-place backward substitution/positioning */
1982     assert (!buffer->have_output);
1983     buffer->idx = buffer->len - 1;
1984     ret = apply_backward (c, accel, subtable_count);
1985   }
1986
1987   return ret;
1988 }
1989
1990 template <typename Proxy>
1991 inline void hb_ot_map_t::apply (const Proxy &proxy,
1992                                 const hb_ot_shape_plan_t *plan,
1993                                 hb_font_t *font,
1994                                 hb_buffer_t *buffer) const
1995 {
1996   const unsigned int table_index = proxy.table_index;
1997   unsigned int i = 0;
1998   OT::hb_ot_apply_context_t c (table_index, font, buffer, proxy.accel.get_blob ());
1999   c.set_recurse_func (Proxy::Lookup::template dispatch_recurse_func<OT::hb_ot_apply_context_t>);
2000
2001   for (unsigned int stage_index = 0; stage_index < stages[table_index].length; stage_index++)
2002   {
2003     const stage_map_t *stage = &stages[table_index][stage_index];
2004     for (; i < stage->last_lookup; i++)
2005     {
2006       auto &lookup = lookups[table_index][i];
2007
2008       unsigned int lookup_index = lookup.index;
2009
2010       auto *accel = proxy.accel.get_accel (lookup_index);
2011       if (unlikely (!accel)) continue;
2012
2013       if (buffer->messaging () &&
2014           !buffer->message (font, "start lookup %u feature '%c%c%c%c'", lookup_index, HB_UNTAG (lookup.feature_tag))) continue;
2015
2016       /* c.digest is a digest of all the current glyphs in the buffer
2017        * (plus some past glyphs).
2018        *
2019        * Only try applying the lookup if there is any overlap. */
2020       if (accel->digest.may_have (c.digest))
2021       {
2022         c.set_lookup_index (lookup_index);
2023         c.set_lookup_mask (lookup.mask, false);
2024         c.set_auto_zwj (lookup.auto_zwj, false);
2025         c.set_auto_zwnj (lookup.auto_zwnj, false);
2026         c.set_random (lookup.random);
2027         c.set_per_syllable (lookup.per_syllable, false);
2028         /* apply_string's set_lookup_props initializes the iterators. */
2029
2030         apply_string<Proxy> (&c,
2031                              proxy.accel.table->get_lookup (lookup_index),
2032                              *accel);
2033       }
2034       else if (buffer->messaging ())
2035         (void) buffer->message (font, "skipped lookup %u feature '%c%c%c%c' because no glyph matches", lookup_index, HB_UNTAG (lookup.feature_tag));
2036
2037       if (buffer->messaging ())
2038         (void) buffer->message (font, "end lookup %u feature '%c%c%c%c'", lookup_index, HB_UNTAG (lookup.feature_tag));
2039     }
2040
2041     if (stage->pause_func)
2042     {
2043       if (stage->pause_func (plan, font, buffer))
2044       {
2045         /* Refresh working buffer digest since buffer changed. */
2046         c.digest = buffer->digest ();
2047       }
2048     }
2049   }
2050 }
2051
2052 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
2053 {
2054   GSUBProxy proxy (font->face);
2055   if (buffer->messaging () &&
2056       !buffer->message (font, "start table GSUB script tag '%c%c%c%c'", HB_UNTAG (chosen_script[0]))) return;
2057   apply (proxy, plan, font, buffer);
2058   if (buffer->messaging ())
2059     (void) buffer->message (font, "end table GSUB script tag '%c%c%c%c'", HB_UNTAG (chosen_script[0]));
2060 }
2061
2062 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
2063 {
2064   GPOSProxy proxy (font->face);
2065   if (buffer->messaging () &&
2066       !buffer->message (font, "start table GPOS script tag '%c%c%c%c'", HB_UNTAG (chosen_script[1]))) return;
2067   apply (proxy, plan, font, buffer);
2068   if (buffer->messaging ())
2069     (void) buffer->message (font, "end table GPOS script tag '%c%c%c%c'", HB_UNTAG (chosen_script[1]));
2070 }
2071
2072 void
2073 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
2074                                 const OT::SubstLookup &lookup,
2075                                 const OT::hb_ot_layout_lookup_accelerator_t &accel)
2076 {
2077   apply_string<GSUBProxy> (c, lookup, accel);
2078 }
2079
2080 #ifndef HB_NO_BASE
2081
2082 static void
2083 choose_base_tags (hb_script_t    script,
2084                   hb_language_t  language,
2085                   hb_tag_t      *script_tag,
2086                   hb_tag_t      *language_tag)
2087 {
2088   hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
2089   unsigned script_count = ARRAY_LENGTH (script_tags);
2090
2091   hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
2092   unsigned language_count = ARRAY_LENGTH (language_tags);
2093
2094   hb_ot_tags_from_script_and_language (script, language,
2095                                        &script_count, script_tags,
2096                                        &language_count, language_tags);
2097
2098   *script_tag = script_count ? script_tags[script_count - 1] : HB_OT_TAG_DEFAULT_SCRIPT;
2099   *language_tag = language_count ? language_tags[language_count - 1] : HB_OT_TAG_DEFAULT_LANGUAGE;
2100 }
2101
2102 /**
2103  * hb_ot_layout_get_font_extents:
2104  * @font: a font
2105  * @direction: text direction.
2106  * @script_tag:  script tag.
2107  * @language_tag: language tag.
2108  * @extents: (out) (nullable): font extents if found.
2109  *
2110  * Fetches script/language-specific font extents.  These values are
2111  * looked up in the `BASE` table's `MinMax` records.
2112  *
2113  * If no such extents are found, the default extents for the font are
2114  * fetched. As such, the return value of this function can for the
2115  * most part be ignored.  Note that the per-script/language extents
2116  * do not have a line-gap value, and the line-gap is set to zero in
2117  * that case.
2118  *
2119  * Return value: `true` if found script/language-specific font extents.
2120  *
2121  * Since: 8.0.0
2122  **/
2123 hb_bool_t
2124 hb_ot_layout_get_font_extents (hb_font_t         *font,
2125                                hb_direction_t     direction,
2126                                hb_tag_t           script_tag,
2127                                hb_tag_t           language_tag,
2128                                hb_font_extents_t *extents)
2129 {
2130   hb_position_t min, max;
2131   if (font->face->table.BASE->get_min_max (font, direction, script_tag, language_tag, HB_TAG_NONE,
2132                                            &min, &max))
2133   {
2134     if (extents)
2135     {
2136       extents->ascender  = max;
2137       extents->descender = min;
2138       extents->line_gap  = 0;
2139     }
2140     return true;
2141   }
2142
2143   hb_font_get_extents_for_direction (font, direction, extents);
2144   return false;
2145 }
2146
2147 /**
2148  * hb_ot_layout_get_font_extents2:
2149  * @font: a font
2150  * @direction: text direction.
2151  * @script:  script.
2152  * @language: (nullable): language.
2153  * @extents: (out) (nullable): font extents if found.
2154  *
2155  * Fetches script/language-specific font extents.  These values are
2156  * looked up in the `BASE` table's `MinMax` records.
2157  *
2158  * If no such extents are found, the default extents for the font are
2159  * fetched. As such, the return value of this function can for the
2160  * most part be ignored.  Note that the per-script/language extents
2161  * do not have a line-gap value, and the line-gap is set to zero in
2162  * that case.
2163  *
2164  * This function is like hb_ot_layout_get_font_extents() but takes
2165  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2166  *
2167  * Return value: `true` if found script/language-specific font extents.
2168  *
2169  * Since: 8.0.0
2170  **/
2171 hb_bool_t
2172 hb_ot_layout_get_font_extents2 (hb_font_t         *font,
2173                                 hb_direction_t     direction,
2174                                 hb_script_t        script,
2175                                 hb_language_t      language,
2176                                 hb_font_extents_t *extents)
2177 {
2178   hb_tag_t script_tag, language_tag;
2179   choose_base_tags (script, language, &script_tag, &language_tag);
2180   return hb_ot_layout_get_font_extents (font,
2181                                         direction,
2182                                         script_tag,
2183                                         language_tag,
2184                                         extents);
2185 }
2186
2187 /**
2188  * hb_ot_layout_get_horizontal_baseline_tag_for_script:
2189  * @script: a script tag.
2190  *
2191  * Fetches the dominant horizontal baseline tag used by @script.
2192  *
2193  * Return value: dominant baseline tag for the @script.
2194  *
2195  * Since: 4.0.0
2196  **/
2197 hb_ot_layout_baseline_tag_t
2198 hb_ot_layout_get_horizontal_baseline_tag_for_script (hb_script_t script)
2199 {
2200   /* Keep in sync with hb_ot_layout_get_baseline_with_fallback */
2201   switch ((int) script)
2202   {
2203     /* Unicode-1.1 additions */
2204     case HB_SCRIPT_BENGALI:
2205     case HB_SCRIPT_DEVANAGARI:
2206     case HB_SCRIPT_GUJARATI:
2207     case HB_SCRIPT_GURMUKHI:
2208     /* Unicode-2.0 additions */
2209     case HB_SCRIPT_TIBETAN:
2210     /* Unicode-4.0 additions */
2211     case HB_SCRIPT_LIMBU:
2212     /* Unicode-4.1 additions */
2213     case HB_SCRIPT_SYLOTI_NAGRI:
2214     /* Unicode-5.0 additions */
2215     case HB_SCRIPT_PHAGS_PA:
2216     /* Unicode-5.2 additions */
2217     case HB_SCRIPT_MEETEI_MAYEK:
2218     /* Unicode-6.1 additions */
2219     case HB_SCRIPT_SHARADA:
2220     case HB_SCRIPT_TAKRI:
2221     /* Unicode-7.0 additions */
2222     case HB_SCRIPT_MODI:
2223     case HB_SCRIPT_SIDDHAM:
2224     case HB_SCRIPT_TIRHUTA:
2225     /* Unicode-9.0 additions */
2226     case HB_SCRIPT_MARCHEN:
2227     case HB_SCRIPT_NEWA:
2228     /* Unicode-10.0 additions */
2229     case HB_SCRIPT_SOYOMBO:
2230     case HB_SCRIPT_ZANABAZAR_SQUARE:
2231     /* Unicode-11.0 additions */
2232     case HB_SCRIPT_DOGRA:
2233     case HB_SCRIPT_GUNJALA_GONDI:
2234     /* Unicode-12.0 additions */
2235     case HB_SCRIPT_NANDINAGARI:
2236       return HB_OT_LAYOUT_BASELINE_TAG_HANGING;
2237
2238     /* Unicode-1.1 additions */
2239     case HB_SCRIPT_HANGUL:
2240     case HB_SCRIPT_HAN:
2241     case HB_SCRIPT_HIRAGANA:
2242     case HB_SCRIPT_KATAKANA:
2243     /* Unicode-3.0 additions */
2244     case HB_SCRIPT_BOPOMOFO:
2245     /* Unicode-9.0 additions */
2246     case HB_SCRIPT_TANGUT:
2247     /* Unicode-10.0 additions */
2248     case HB_SCRIPT_NUSHU:
2249     /* Unicode-13.0 additions */
2250     case HB_SCRIPT_KHITAN_SMALL_SCRIPT:
2251       return HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT;
2252
2253     default:
2254       return HB_OT_LAYOUT_BASELINE_TAG_ROMAN;
2255   }
2256 }
2257
2258 /**
2259  * hb_ot_layout_get_baseline:
2260  * @font: a font
2261  * @baseline_tag: a baseline tag
2262  * @direction: text direction.
2263  * @script_tag:  script tag.
2264  * @language_tag: language tag, currently unused.
2265  * @coord: (out) (nullable): baseline value if found.
2266  *
2267  * Fetches a baseline value from the face.
2268  *
2269  * Return value: `true` if found baseline value in the font.
2270  *
2271  * Since: 2.6.0
2272  **/
2273 hb_bool_t
2274 hb_ot_layout_get_baseline (hb_font_t                   *font,
2275                            hb_ot_layout_baseline_tag_t  baseline_tag,
2276                            hb_direction_t               direction,
2277                            hb_tag_t                     script_tag,
2278                            hb_tag_t                     language_tag,
2279                            hb_position_t               *coord        /* OUT.  May be NULL. */)
2280 {
2281   return font->face->table.BASE->get_baseline (font, baseline_tag, direction, script_tag, language_tag, coord);
2282 }
2283
2284 /**
2285  * hb_ot_layout_get_baseline2:
2286  * @font: a font
2287  * @baseline_tag: a baseline tag
2288  * @direction: text direction.
2289  * @script:  script.
2290  * @language: (nullable): language, currently unused.
2291  * @coord: (out) (nullable): baseline value if found.
2292  *
2293  * Fetches a baseline value from the face.
2294  *
2295  * This function is like hb_ot_layout_get_baseline() but takes
2296  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2297  *
2298  * Return value: `true` if found baseline value in the font.
2299  *
2300  * Since: 8.0.0
2301  **/
2302 hb_bool_t
2303 hb_ot_layout_get_baseline2 (hb_font_t                   *font,
2304                             hb_ot_layout_baseline_tag_t  baseline_tag,
2305                             hb_direction_t               direction,
2306                             hb_script_t                  script,
2307                             hb_language_t                language,
2308                             hb_position_t               *coord        /* OUT.  May be NULL. */)
2309 {
2310   hb_tag_t script_tag, language_tag;
2311   choose_base_tags (script, language, &script_tag, &language_tag);
2312   return hb_ot_layout_get_baseline (font,
2313                                     baseline_tag,
2314                                     direction,
2315                                     script_tag,
2316                                     language_tag,
2317                                     coord);
2318 }
2319
2320 /**
2321  * hb_ot_layout_get_baseline_with_fallback:
2322  * @font: a font
2323  * @baseline_tag: a baseline tag
2324  * @direction: text direction.
2325  * @script_tag:  script tag.
2326  * @language_tag: language tag, currently unused.
2327  * @coord: (out): baseline value if found.
2328  *
2329  * Fetches a baseline value from the face, and synthesizes
2330  * it if the font does not have it.
2331  *
2332  * Since: 4.0.0
2333  **/
2334 void
2335 hb_ot_layout_get_baseline_with_fallback (hb_font_t                   *font,
2336                                          hb_ot_layout_baseline_tag_t  baseline_tag,
2337                                          hb_direction_t               direction,
2338                                          hb_tag_t                     script_tag,
2339                                          hb_tag_t                     language_tag,
2340                                          hb_position_t               *coord /* OUT */)
2341 {
2342   if (hb_ot_layout_get_baseline (font,
2343                                  baseline_tag,
2344                                  direction,
2345                                  script_tag,
2346                                  language_tag,
2347                                  coord))
2348     return;
2349
2350   /* Synthesize missing baselines.
2351    * See https://www.w3.org/TR/css-inline-3/#baseline-synthesis-fonts
2352    */
2353   switch (baseline_tag)
2354   {
2355   case HB_OT_LAYOUT_BASELINE_TAG_ROMAN:
2356     *coord = 0; // FIXME origin ?
2357     break;
2358
2359   case HB_OT_LAYOUT_BASELINE_TAG_MATH:
2360     {
2361       hb_codepoint_t glyph;
2362       hb_glyph_extents_t extents;
2363       if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
2364           (hb_font_get_nominal_glyph (font, 0x2212u, &glyph) ||
2365            hb_font_get_nominal_glyph (font, '-', &glyph)) &&
2366           hb_font_get_glyph_extents (font, glyph, &extents))
2367       {
2368         *coord = extents.y_bearing + extents.height / 2;
2369       }
2370       else
2371       {
2372         hb_position_t x_height = font->y_scale / 2;
2373 #ifndef HB_NO_METRICS
2374         hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_X_HEIGHT, &x_height);
2375 #endif
2376         *coord = x_height / 2;
2377       }
2378     }
2379     break;
2380
2381   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT:
2382   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT:
2383     {
2384       hb_position_t embox_top, embox_bottom;
2385
2386       hb_ot_layout_get_baseline_with_fallback (font,
2387                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2388                                                direction,
2389                                                script_tag,
2390                                                language_tag,
2391                                                &embox_top);
2392       hb_ot_layout_get_baseline_with_fallback (font,
2393                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2394                                                direction,
2395                                                script_tag,
2396                                                language_tag,
2397                                                &embox_bottom);
2398
2399       if (baseline_tag == HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT)
2400         *coord = embox_top + (embox_bottom - embox_top) / 10;
2401       else
2402         *coord = embox_bottom + (embox_top - embox_bottom) / 10;
2403     }
2404     break;
2405
2406   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT:
2407     if (hb_ot_layout_get_baseline (font,
2408                                    HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2409                                    direction,
2410                                    script_tag,
2411                                    language_tag,
2412                                    coord))
2413       *coord += HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale;
2414     else
2415     {
2416       hb_font_extents_t font_extents;
2417       hb_font_get_extents_for_direction (font, direction, &font_extents);
2418       *coord = font_extents.ascender;
2419     }
2420     break;
2421
2422   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT:
2423     if (hb_ot_layout_get_baseline (font,
2424                                    HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2425                                    direction,
2426                                    script_tag,
2427                                    language_tag,
2428                                    coord))
2429       *coord -= HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale;
2430     else
2431     {
2432       hb_font_extents_t font_extents;
2433       hb_font_get_extents_for_direction (font, direction, &font_extents);
2434       *coord = font_extents.descender;
2435     }
2436     break;
2437
2438   case HB_OT_LAYOUT_BASELINE_TAG_HANGING:
2439     if (HB_DIRECTION_IS_HORIZONTAL (direction))
2440     {
2441       hb_codepoint_t ch;
2442       hb_codepoint_t glyph;
2443       hb_glyph_extents_t extents;
2444
2445       /* Keep in sync with hb_ot_layout_get_horizontal_baseline_for_script */
2446       switch ((int) script_tag)
2447       {
2448       /* Unicode-1.1 additions */
2449       case HB_SCRIPT_BENGALI:          ch = 0x0995u; break;
2450       case HB_SCRIPT_DEVANAGARI:       ch = 0x0915u; break;
2451       case HB_SCRIPT_GUJARATI:         ch = 0x0a95u; break;
2452       case HB_SCRIPT_GURMUKHI:         ch = 0x0a15u; break;
2453       /* Unicode-2.0 additions */
2454       case HB_SCRIPT_TIBETAN:          ch = 0x0f40u; break;
2455       /* Unicode-4.0 additions */
2456       case HB_SCRIPT_LIMBU:            ch = 0x1901u; break;
2457       /* Unicode-4.1 additions */
2458       case HB_SCRIPT_SYLOTI_NAGRI:     ch = 0xa807u; break;
2459       /* Unicode-5.0 additions */
2460       case HB_SCRIPT_PHAGS_PA:         ch = 0xa840u; break;
2461       /* Unicode-5.2 additions */
2462       case HB_SCRIPT_MEETEI_MAYEK:     ch = 0xabc0u; break;
2463       /* Unicode-6.1 additions */
2464       case HB_SCRIPT_SHARADA:          ch = 0x11191u; break;
2465       case HB_SCRIPT_TAKRI:            ch = 0x1168cu; break;
2466       /* Unicode-7.0 additions */
2467       case HB_SCRIPT_MODI:             ch = 0x1160eu;break;
2468       case HB_SCRIPT_SIDDHAM:          ch = 0x11590u; break;
2469       case HB_SCRIPT_TIRHUTA:          ch = 0x1148fu; break;
2470       /* Unicode-9.0 additions */
2471       case HB_SCRIPT_MARCHEN:          ch = 0x11c72u; break;
2472       case HB_SCRIPT_NEWA:             ch = 0x1140eu; break;
2473       /* Unicode-10.0 additions */
2474       case HB_SCRIPT_SOYOMBO:          ch = 0x11a5cu; break;
2475       case HB_SCRIPT_ZANABAZAR_SQUARE: ch = 0x11a0bu; break;
2476       /* Unicode-11.0 additions */
2477       case HB_SCRIPT_DOGRA:            ch = 0x1180au; break;
2478       case HB_SCRIPT_GUNJALA_GONDI:    ch = 0x11d6cu; break;
2479       /* Unicode-12.0 additions */
2480       case HB_SCRIPT_NANDINAGARI:      ch = 0x119b0u; break;
2481       default:                         ch = 0;        break;
2482       }
2483
2484       if (ch &&
2485           hb_font_get_nominal_glyph (font, ch, &glyph) &&
2486           hb_font_get_glyph_extents (font, glyph, &extents))
2487         *coord = extents.y_bearing;
2488       else
2489         *coord = font->y_scale * 6 / 10; // FIXME makes assumptions about origin
2490     }
2491     else
2492       *coord = font->x_scale * 6 / 10; // FIXME makes assumptions about origin
2493     break;
2494
2495   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL:
2496     {
2497       hb_position_t top, bottom;
2498       hb_ot_layout_get_baseline_with_fallback (font,
2499                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2500                                                direction,
2501                                                script_tag,
2502                                                language_tag,
2503                                                &top);
2504       hb_ot_layout_get_baseline_with_fallback (font,
2505                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2506                                                direction,
2507                                                script_tag,
2508                                                language_tag,
2509                                                &bottom);
2510       *coord = (top + bottom) / 2;
2511
2512     }
2513     break;
2514
2515   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL:
2516     {
2517       hb_position_t top, bottom;
2518       hb_ot_layout_get_baseline_with_fallback (font,
2519                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT,
2520                                                direction,
2521                                                script_tag,
2522                                                language_tag,
2523                                                &top);
2524       hb_ot_layout_get_baseline_with_fallback (font,
2525                                                HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT,
2526                                                direction,
2527                                                script_tag,
2528                                                language_tag,
2529                                                &bottom);
2530       *coord = (top + bottom) / 2;
2531
2532     }
2533     break;
2534
2535   case _HB_OT_LAYOUT_BASELINE_TAG_MAX_VALUE:
2536   default:
2537     *coord = 0;
2538     break;
2539   }
2540 }
2541
2542 /**
2543  * hb_ot_layout_get_baseline_with_fallback2:
2544  * @font: a font
2545  * @baseline_tag: a baseline tag
2546  * @direction: text direction.
2547  * @script:  script.
2548  * @language: (nullable): language, currently unused.
2549  * @coord: (out): baseline value if found.
2550  *
2551  * Fetches a baseline value from the face, and synthesizes
2552  * it if the font does not have it.
2553  *
2554  * This function is like hb_ot_layout_get_baseline_with_fallback() but takes
2555  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2556  *
2557  * Since: 8.0.0
2558  **/
2559 void
2560 hb_ot_layout_get_baseline_with_fallback2 (hb_font_t                   *font,
2561                                           hb_ot_layout_baseline_tag_t  baseline_tag,
2562                                           hb_direction_t               direction,
2563                                           hb_script_t                  script,
2564                                           hb_language_t                language,
2565                                           hb_position_t               *coord        /* OUT */)
2566 {
2567   hb_tag_t script_tag, language_tag;
2568   choose_base_tags (script, language, &script_tag, &language_tag);
2569   hb_ot_layout_get_baseline_with_fallback (font,
2570                                            baseline_tag,
2571                                            direction,
2572                                            script_tag,
2573                                            language_tag,
2574                                            coord);
2575 }
2576
2577 #endif
2578
2579
2580 struct hb_get_glyph_alternates_dispatch_t :
2581        hb_dispatch_context_t<hb_get_glyph_alternates_dispatch_t, unsigned>
2582 {
2583   static return_t default_return_value () { return 0; }
2584   bool stop_sublookup_iteration (return_t r) const { return r; }
2585
2586   private:
2587   template <typename T, typename ...Ts> auto
2588   _dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
2589   ( obj.get_glyph_alternates (std::forward<Ts> (ds)...) )
2590   template <typename T, typename ...Ts> auto
2591   _dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
2592   ( default_return_value () )
2593   public:
2594   template <typename T, typename ...Ts> auto
2595   dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
2596   ( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
2597 };
2598
2599 #ifndef HB_NO_LAYOUT_RARELY_USED
2600 /**
2601  * hb_ot_layout_lookup_get_glyph_alternates:
2602  * @face: a face.
2603  * @lookup_index: index of the feature lookup to query.
2604  * @glyph: a glyph id.
2605  * @start_offset: starting offset.
2606  * @alternate_count: (inout) (optional): Input = the maximum number of alternate glyphs to return;
2607  *                   Output = the actual number of alternate glyphs returned (may be zero).
2608  * @alternate_glyphs: (out caller-allocates) (array length=alternate_count): A glyphs buffer.
2609  *                    Alternate glyphs associated with the glyph id.
2610  *
2611  * Fetches alternates of a glyph from a given GSUB lookup index.
2612  *
2613  * Return value: Total number of alternates found in the specific lookup index for the given glyph id.
2614  *
2615  * Since: 2.6.8
2616  **/
2617 HB_EXTERN unsigned
2618 hb_ot_layout_lookup_get_glyph_alternates (hb_face_t      *face,
2619                                           unsigned        lookup_index,
2620                                           hb_codepoint_t  glyph,
2621                                           unsigned        start_offset,
2622                                           unsigned       *alternate_count  /* IN/OUT.  May be NULL. */,
2623                                           hb_codepoint_t *alternate_glyphs /* OUT.     May be NULL. */)
2624 {
2625   hb_get_glyph_alternates_dispatch_t c;
2626   const OT::SubstLookup &lookup = face->table.GSUB->table->get_lookup (lookup_index);
2627   auto ret = lookup.dispatch (&c, glyph, start_offset, alternate_count, alternate_glyphs);
2628   if (!ret && alternate_count) *alternate_count = 0;
2629   return ret;
2630 }
2631
2632
2633 struct hb_position_single_dispatch_t :
2634        hb_dispatch_context_t<hb_position_single_dispatch_t, bool>
2635 {
2636   static return_t default_return_value () { return false; }
2637   bool stop_sublookup_iteration (return_t r) const { return r; }
2638
2639   private:
2640   template <typename T, typename ...Ts> auto
2641   _dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
2642   ( obj.position_single (std::forward<Ts> (ds)...) )
2643   template <typename T, typename ...Ts> auto
2644   _dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
2645   ( default_return_value () )
2646   public:
2647   template <typename T, typename ...Ts> auto
2648   dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
2649   ( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
2650 };
2651
2652 /**
2653  * hb_ot_layout_lookup_get_optical_bound:
2654  * @font: a font.
2655  * @lookup_index: index of the feature lookup to query.
2656  * @direction: edge of the glyph to query.
2657  * @glyph: a glyph id.
2658  *
2659  * Fetches the optical bound of a glyph positioned at the margin of text.
2660  * The direction identifies which edge of the glyph to query.
2661  *
2662  * Return value: Adjustment value. Negative values mean the glyph will stick out of the margin.
2663  *
2664  * Since: 5.3.0
2665  **/
2666 hb_position_t
2667 hb_ot_layout_lookup_get_optical_bound (hb_font_t      *font,
2668                                        unsigned        lookup_index,
2669                                        hb_direction_t  direction,
2670                                        hb_codepoint_t  glyph)
2671 {
2672   const OT::PosLookup &lookup = font->face->table.GPOS->table->get_lookup (lookup_index);
2673   hb_blob_t *blob = font->face->table.GPOS->get_blob ();
2674   hb_glyph_position_t pos = {0};
2675   hb_position_single_dispatch_t c;
2676   lookup.dispatch (&c, font, blob, direction, glyph, pos);
2677   hb_position_t ret = 0;
2678   switch (direction)
2679   {
2680     case HB_DIRECTION_LTR:
2681       ret = pos.x_offset;
2682       break;
2683     case HB_DIRECTION_RTL:
2684       ret = pos.x_advance - pos.x_offset;
2685       break;
2686     case HB_DIRECTION_TTB:
2687       ret = pos.y_offset;
2688       break;
2689     case HB_DIRECTION_BTT:
2690       ret = pos.y_advance - pos.y_offset;
2691       break;
2692     case HB_DIRECTION_INVALID:
2693     default:
2694       break;
2695   }
2696   return ret;
2697 }
2698 #endif
2699
2700
2701 #endif