xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-ot-layout.cc (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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
hb_ot_layout_has_kerning(hb_face_t * face)88 hb_ot_layout_has_kerning (hb_face_t *face)
89 {
90   return face->table.kern->table->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
hb_ot_layout_has_machine_kerning(hb_face_t * face)104 hb_ot_layout_has_machine_kerning (hb_face_t *face)
105 {
106   return face->table.kern->table->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
hb_ot_layout_has_cross_kerning(hb_face_t * face)124 hb_ot_layout_has_cross_kerning (hb_face_t *face)
125 {
126   return face->table.kern->table->has_cross_stream ();
127 }
128 
129 void
hb_ot_layout_kern(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)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 auto& kern = *font->face->table.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
is_blocklisted(hb_blob_t * blob,hb_face_t * face) const151 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
_hb_ot_layout_set_glyph_props(hb_font_t * font,hb_buffer_t * buffer)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
hb_ot_layout_has_glyph_classes(hb_face_t * face)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
hb_ot_layout_get_glyph_class(hb_face_t * face,hb_codepoint_t glyph)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
hb_ot_layout_get_glyphs_in_class(hb_face_t * face,hb_ot_layout_glyph_class_t klass,hb_set_t * glyphs)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
hb_ot_layout_get_attach_points(hb_face_t * face,hb_codepoint_t glyph,unsigned int start_offset,unsigned int * point_count,unsigned int * point_array)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
hb_ot_layout_get_ligature_carets(hb_font_t * font,hb_direction_t direction,hb_codepoint_t glyph,unsigned int start_offset,unsigned int * caret_count,hb_position_t * caret_array)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
is_blocklisted(hb_blob_t * blob HB_UNUSED,hb_face_t * face) const397 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
is_blocklisted(hb_blob_t * blob HB_UNUSED,hb_face_t * face HB_UNUSED) const407 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&
get_gsubgpos_table(hb_face_t * face,hb_tag_t table_tag)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
hb_ot_layout_table_get_script_tags(hb_face_t * face,hb_tag_t table_tag,unsigned int start_offset,unsigned int * script_count,hb_tag_t * script_tags)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
hb_ot_layout_table_find_script(hb_face_t * face,hb_tag_t table_tag,hb_tag_t script_tag,unsigned int * script_index)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
hb_ot_layout_table_choose_script(hb_face_t * face,hb_tag_t table_tag,const hb_tag_t * script_tags,unsigned int * script_index,hb_tag_t * chosen_script)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
hb_ot_layout_table_select_script(hb_face_t * face,hb_tag_t table_tag,unsigned int script_count,const hb_tag_t * script_tags,unsigned int * script_index,hb_tag_t * chosen_script)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
hb_ot_layout_table_get_feature_tags(hb_face_t * face,hb_tag_t table_tag,unsigned int start_offset,unsigned int * feature_count,hb_tag_t * feature_tags)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
hb_ot_layout_table_find_feature(hb_face_t * face,hb_tag_t table_tag,hb_tag_t feature_tag,unsigned int * feature_index)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
hb_ot_layout_script_get_language_tags(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int start_offset,unsigned int * language_count,hb_tag_t * language_tags)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
hb_ot_layout_script_find_language(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,hb_tag_t language_tag,unsigned int * language_index)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
hb_ot_layout_script_select_language2(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_count,const hb_tag_t * language_tags,unsigned int * language_index,hb_tag_t * chosen_language)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
hb_ot_layout_script_select_language(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_count,const hb_tag_t * language_tags,unsigned int * language_index)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
hb_ot_layout_language_get_required_feature_index(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_index,unsigned int * feature_index)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
hb_ot_layout_language_get_required_feature(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_index,unsigned int * feature_index,hb_tag_t * feature_tag)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
hb_ot_layout_language_get_feature_indexes(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_index,unsigned int start_offset,unsigned int * feature_count,unsigned int * feature_indexes)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
hb_ot_layout_language_get_feature_tags(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_index,unsigned int start_offset,unsigned int * feature_count,hb_tag_t * feature_tags)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
hb_ot_layout_language_find_feature(hb_face_t * face,hb_tag_t table_tag,unsigned int script_index,unsigned int language_index,hb_tag_t feature_tag,unsigned int * feature_index)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
hb_ot_layout_feature_get_lookups(hb_face_t * face,hb_tag_t table_tag,unsigned int feature_index,unsigned int start_offset,unsigned int * lookup_count,unsigned int * lookup_indexes)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
hb_ot_layout_table_get_lookup_count(hb_face_t * face,hb_tag_t table_tag)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 {
hb_collect_features_context_thb_collect_features_context_t1076   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 
compute_feature_filterhb_collect_features_context_t1089   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 
visitedhb_collect_features_context_t1110   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   }
visitedhb_collect_features_context_t1123   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 
visited_feature_indiceshb_collect_features_context_t1137   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>
visitedhb_collect_features_context_t1145   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
langsys_collect_features(hb_collect_features_context_t * c,const OT::LangSys & l)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
script_collect_features(hb_collect_features_context_t * c,const OT::Script & s,const hb_tag_t * languages)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
hb_ot_layout_collect_features(hb_face_t * face,hb_tag_t table_tag,const hb_tag_t * scripts,const hb_tag_t * languages,const hb_tag_t * features,hb_set_t * feature_indexes)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
hb_ot_layout_collect_features_map(hb_face_t * face,hb_tag_t table_tag,unsigned script_index,unsigned language_index,hb_map_t * feature_map)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
hb_ot_layout_collect_lookups(hb_face_t * face,hb_tag_t table_tag,const hb_tag_t * scripts,const hb_tag_t * languages,const hb_tag_t * features,hb_set_t * lookup_indexes)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
hb_ot_layout_lookup_collect_glyphs(hb_face_t * face,hb_tag_t table_tag,unsigned int lookup_index,hb_set_t * glyphs_before,hb_set_t * glyphs_input,hb_set_t * glyphs_after,hb_set_t * glyphs_output)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
hb_ot_layout_table_find_feature_variations(hb_face_t * face,hb_tag_t table_tag,const int * coords,unsigned int num_coords,unsigned int * variations_index)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   const OT::GDEF &gdef = *face->table.GDEF->table;
1447 
1448   auto instancer = OT::ItemVarStoreInstancer(&gdef.get_var_store(), nullptr,
1449 					     hb_array (coords, num_coords));
1450 
1451   return g.find_variations_index (coords, num_coords, variations_index, &instancer);
1452 }
1453 
1454 
1455 /**
1456  * hb_ot_layout_feature_with_variations_get_lookups:
1457  * @face: #hb_face_t to work upon
1458  * @table_tag: #HB_OT_TAG_GSUB or #HB_OT_TAG_GPOS
1459  * @feature_index: The index of the feature to query
1460  * @variations_index: The index of the feature variation to query
1461  * @start_offset: offset of the first lookup to retrieve
1462  * @lookup_count: (inout) (optional): Input = the maximum number of lookups to return;
1463  *                Output = the actual number of lookups returned (may be zero)
1464  * @lookup_indexes: (out) (array length=lookup_count): The array of lookups found for the query
1465  *
1466  * Fetches a list of all lookups enumerated for the specified feature, in
1467  * the specified face's GSUB table or GPOS table, enabled at the specified
1468  * variations index. The list returned will begin at the offset provided.
1469  *
1470  * Return value: Total number of lookups.
1471  *
1472  * Since: 1.4.0
1473  *
1474  **/
1475 unsigned int
hb_ot_layout_feature_with_variations_get_lookups(hb_face_t * face,hb_tag_t table_tag,unsigned int feature_index,unsigned int variations_index,unsigned int start_offset,unsigned int * lookup_count,unsigned int * lookup_indexes)1476 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t    *face,
1477 						  hb_tag_t      table_tag,
1478 						  unsigned int  feature_index,
1479 						  unsigned int  variations_index,
1480 						  unsigned int  start_offset,
1481 						  unsigned int *lookup_count /* IN/OUT */,
1482 						  unsigned int *lookup_indexes /* OUT */)
1483 {
1484   static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
1485   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1486 
1487   const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
1488 
1489   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
1490 }
1491 
1492 
1493 /*
1494  * OT::GSUB
1495  */
1496 
1497 
1498 /**
1499  * hb_ot_layout_has_substitution:
1500  * @face: #hb_face_t to work upon
1501  *
1502  * Tests whether the specified face includes any GSUB substitutions.
1503  *
1504  * Return value: `true` if data found, `false` otherwise
1505  *
1506  * Since: 0.6.0
1507  *
1508  **/
1509 hb_bool_t
hb_ot_layout_has_substitution(hb_face_t * face)1510 hb_ot_layout_has_substitution (hb_face_t *face)
1511 {
1512   return face->table.GSUB->table->has_data ();
1513 }
1514 
1515 
1516 /**
1517  * hb_ot_layout_lookup_would_substitute:
1518  * @face: #hb_face_t to work upon
1519  * @lookup_index: The index of the lookup to query
1520  * @glyphs: The sequence of glyphs to query for substitution
1521  * @glyphs_length: The length of the glyph sequence
1522  * @zero_context: #hb_bool_t indicating whether pre-/post-context are disallowed
1523  * in substitutions
1524  *
1525  * Tests whether a specified lookup in the specified face would
1526  * trigger a substitution on the given glyph sequence.
1527  *
1528  * Return value: `true` if a substitution would be triggered, `false` otherwise
1529  *
1530  * Since: 0.9.7
1531  **/
1532 hb_bool_t
hb_ot_layout_lookup_would_substitute(hb_face_t * face,unsigned int lookup_index,const hb_codepoint_t * glyphs,unsigned int glyphs_length,hb_bool_t zero_context)1533 hb_ot_layout_lookup_would_substitute (hb_face_t            *face,
1534 				      unsigned int          lookup_index,
1535 				      const hb_codepoint_t *glyphs,
1536 				      unsigned int          glyphs_length,
1537 				      hb_bool_t             zero_context)
1538 {
1539   auto &gsub = face->table.GSUB;
1540   if (unlikely (lookup_index >= gsub->lookup_count)) return false;
1541   OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
1542 
1543   const OT::SubstLookup& l = gsub->table->get_lookup (lookup_index);
1544   auto *accel = gsub->get_accel (lookup_index);
1545   return accel && l.would_apply (&c, accel);
1546 }
1547 
1548 
1549 /**
1550  * hb_ot_layout_substitute_start:
1551  * @font: #hb_font_t to use
1552  * @buffer: #hb_buffer_t buffer to work upon
1553  *
1554  * Called before substitution lookups are performed, to ensure that glyph
1555  * class and other properties are set on the glyphs in the buffer.
1556  *
1557  **/
1558 void
hb_ot_layout_substitute_start(hb_font_t * font,hb_buffer_t * buffer)1559 hb_ot_layout_substitute_start (hb_font_t    *font,
1560 			       hb_buffer_t  *buffer)
1561 {
1562   _hb_ot_layout_set_glyph_props (font, buffer);
1563 }
1564 
1565 /**
1566  * hb_ot_layout_lookup_substitute_closure:
1567  * @face: #hb_face_t to work upon
1568  * @lookup_index: index of the feature lookup to query
1569  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookup
1570  *
1571  * Compute the transitive closure of glyphs needed for a
1572  * specified lookup.
1573  *
1574  * Since: 0.9.7
1575  **/
1576 void
hb_ot_layout_lookup_substitute_closure(hb_face_t * face,unsigned int lookup_index,hb_set_t * glyphs)1577 hb_ot_layout_lookup_substitute_closure (hb_face_t    *face,
1578 					unsigned int  lookup_index,
1579 					hb_set_t     *glyphs /* OUT */)
1580 {
1581   hb_map_t done_lookups_glyph_count;
1582   hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> done_lookups_glyph_set;
1583   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1584 
1585   const OT::SubstLookup& l = face->table.GSUB->table->get_lookup (lookup_index);
1586 
1587   l.closure (&c, lookup_index);
1588 }
1589 
1590 /**
1591  * hb_ot_layout_lookups_substitute_closure:
1592  * @face: #hb_face_t to work upon
1593  * @lookups: The set of lookups to query
1594  * @glyphs: (out): Array of glyphs comprising the transitive closure of the lookups
1595  *
1596  * Compute the transitive closure of glyphs needed for all of the
1597  * provided lookups.
1598  *
1599  * Since: 1.8.1
1600  **/
1601 void
hb_ot_layout_lookups_substitute_closure(hb_face_t * face,const hb_set_t * lookups,hb_set_t * glyphs)1602 hb_ot_layout_lookups_substitute_closure (hb_face_t      *face,
1603 					 const hb_set_t *lookups,
1604 					 hb_set_t       *glyphs /* OUT */)
1605 {
1606   hb_map_t done_lookups_glyph_count;
1607   hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> done_lookups_glyph_set;
1608   OT::hb_closure_context_t c (face, glyphs, &done_lookups_glyph_count, &done_lookups_glyph_set);
1609   const GSUB& gsub = *face->table.GSUB->table;
1610 
1611   unsigned int iteration_count = 0;
1612   unsigned int glyphs_length;
1613   do
1614   {
1615     c.reset_lookup_visit_count ();
1616     glyphs_length = glyphs->get_population ();
1617     if (lookups)
1618     {
1619       for (auto lookup_index : *lookups)
1620 	gsub.get_lookup (lookup_index).closure (&c, lookup_index);
1621     }
1622     else
1623     {
1624       for (unsigned int i = 0; i < gsub.get_lookup_count (); i++)
1625 	gsub.get_lookup (i).closure (&c, i);
1626     }
1627   } while (iteration_count++ <= HB_CLOSURE_MAX_STAGES &&
1628 	   glyphs_length != glyphs->get_population ());
1629 }
1630 
1631 /*
1632  * GPOS
1633  */
1634 
1635 
1636 /**
1637  * hb_ot_layout_has_positioning:
1638  * @face: #hb_face_t to work upon
1639  *
1640  * Tests whether the specified face includes any GPOS positioning.
1641  *
1642  * Return value: `true` if the face has GPOS data, `false` otherwise
1643  *
1644  **/
1645 hb_bool_t
hb_ot_layout_has_positioning(hb_face_t * face)1646 hb_ot_layout_has_positioning (hb_face_t *face)
1647 {
1648   return face->table.GPOS->table->has_data ();
1649 }
1650 
1651 /**
1652  * hb_ot_layout_position_start:
1653  * @font: #hb_font_t to use
1654  * @buffer: #hb_buffer_t buffer to work upon
1655  *
1656  * Called before positioning lookups are performed, to ensure that glyph
1657  * attachment types and glyph-attachment chains are set for the glyphs in the buffer.
1658  *
1659  **/
1660 void
hb_ot_layout_position_start(hb_font_t * font,hb_buffer_t * buffer)1661 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
1662 {
1663   GPOS::position_start (font, buffer);
1664 }
1665 
1666 
1667 /**
1668  * hb_ot_layout_position_finish_advances:
1669  * @font: #hb_font_t to use
1670  * @buffer: #hb_buffer_t buffer to work upon
1671  *
1672  * Called after positioning lookups are performed, to finish glyph advances.
1673  *
1674  **/
1675 void
hb_ot_layout_position_finish_advances(hb_font_t * font,hb_buffer_t * buffer)1676 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
1677 {
1678   GPOS::position_finish_advances (font, buffer);
1679 }
1680 
1681 /**
1682  * hb_ot_layout_position_finish_offsets:
1683  * @font: #hb_font_t to use
1684  * @buffer: #hb_buffer_t buffer to work upon
1685  *
1686  * Called after positioning lookups are performed, to finish glyph offsets.
1687  *
1688  **/
1689 void
hb_ot_layout_position_finish_offsets(hb_font_t * font,hb_buffer_t * buffer)1690 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
1691 {
1692   GPOS::position_finish_offsets (font, buffer);
1693 }
1694 
1695 
1696 #ifndef HB_NO_LAYOUT_FEATURE_PARAMS
1697 /**
1698  * hb_ot_layout_get_size_params:
1699  * @face: #hb_face_t to work upon
1700  * @design_size: (out): The design size of the face
1701  * @subfamily_id: (out): The identifier of the face within the font subfamily
1702  * @subfamily_name_id: (out): The ‘name’ table name ID of the face within the font subfamily
1703  * @range_start: (out): The minimum size of the recommended size range for the face
1704  * @range_end: (out): The maximum size of the recommended size range for the face
1705  *
1706  * Fetches optical-size feature data (i.e., the `size` feature from GPOS). Note that
1707  * the subfamily_id and the subfamily name string (accessible via the subfamily_name_id)
1708  * as used here are defined as pertaining only to fonts within a font family that differ
1709  * specifically in their respective size ranges; other ways to differentiate fonts within
1710  * a subfamily are not covered by the `size` feature.
1711  *
1712  * For more information on this distinction, see the [`size` feature documentation](
1713  * https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#tag-size).
1714  *
1715  * Return value: `true` if data found, `false` otherwise
1716  *
1717  * Since: 0.9.10
1718  **/
1719 hb_bool_t
hb_ot_layout_get_size_params(hb_face_t * face,unsigned int * design_size,unsigned int * subfamily_id,hb_ot_name_id_t * subfamily_name_id,unsigned int * range_start,unsigned int * range_end)1720 hb_ot_layout_get_size_params (hb_face_t       *face,
1721 			      unsigned int    *design_size,       /* OUT.  May be NULL */
1722 			      unsigned int    *subfamily_id,      /* OUT.  May be NULL */
1723 			      hb_ot_name_id_t *subfamily_name_id, /* OUT.  May be NULL */
1724 			      unsigned int    *range_start,       /* OUT.  May be NULL */
1725 			      unsigned int    *range_end          /* OUT.  May be NULL */)
1726 {
1727   const GPOS &gpos = *face->table.GPOS->table;
1728   const hb_tag_t tag = HB_TAG ('s','i','z','e');
1729 
1730   unsigned int num_features = gpos.get_feature_count ();
1731   for (unsigned int i = 0; i < num_features; i++)
1732   {
1733     if (tag == gpos.get_feature_tag (i))
1734     {
1735       const OT::Feature &f = gpos.get_feature (i);
1736       const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
1737 
1738       if (params.designSize)
1739       {
1740 	if (design_size) *design_size = params.designSize;
1741 	if (subfamily_id) *subfamily_id = params.subfamilyID;
1742 	if (subfamily_name_id) *subfamily_name_id = params.subfamilyNameID;
1743 	if (range_start) *range_start = params.rangeStart;
1744 	if (range_end) *range_end = params.rangeEnd;
1745 
1746 	return true;
1747       }
1748     }
1749   }
1750 
1751   if (design_size) *design_size = 0;
1752   if (subfamily_id) *subfamily_id = 0;
1753   if (subfamily_name_id) *subfamily_name_id = HB_OT_NAME_ID_INVALID;
1754   if (range_start) *range_start = 0;
1755   if (range_end) *range_end = 0;
1756 
1757   return false;
1758 }
1759 
1760 
1761 /**
1762  * hb_ot_layout_feature_get_name_ids:
1763  * @face: #hb_face_t to work upon
1764  * @table_tag: table tag to query, "GSUB" or "GPOS".
1765  * @feature_index: index of feature to query.
1766  * @label_id: (out) (optional): The ‘name’ table name ID that specifies a string
1767  *            for a user-interface label for this feature. (May be NULL.)
1768  * @tooltip_id: (out) (optional): The ‘name’ table name ID that specifies a string
1769  *              that an application can use for tooltip text for this
1770  *              feature. (May be NULL.)
1771  * @sample_id: (out) (optional): The ‘name’ table name ID that specifies sample text
1772  *             that illustrates the effect of this feature. (May be NULL.)
1773  * @num_named_parameters: (out) (optional):  Number of named parameters. (May be zero.)
1774  * @first_param_id: (out) (optional): The first ‘name’ table name ID used to specify
1775  *                  strings for user-interface labels for the feature
1776  *                  parameters. (Must be zero if numParameters is zero.)
1777  *
1778  * Fetches name indices from feature parameters for "Stylistic Set" ('ssXX') or
1779  * "Character Variant" ('cvXX') features.
1780  *
1781  * Return value: `true` if data found, `false` otherwise
1782  *
1783  * Since: 2.0.0
1784  **/
1785 hb_bool_t
hb_ot_layout_feature_get_name_ids(hb_face_t * face,hb_tag_t table_tag,unsigned int feature_index,hb_ot_name_id_t * label_id,hb_ot_name_id_t * tooltip_id,hb_ot_name_id_t * sample_id,unsigned int * num_named_parameters,hb_ot_name_id_t * first_param_id)1786 hb_ot_layout_feature_get_name_ids (hb_face_t       *face,
1787 				   hb_tag_t         table_tag,
1788 				   unsigned int     feature_index,
1789 				   hb_ot_name_id_t *label_id,             /* OUT.  May be NULL */
1790 				   hb_ot_name_id_t *tooltip_id,           /* OUT.  May be NULL */
1791 				   hb_ot_name_id_t *sample_id,            /* OUT.  May be NULL */
1792 				   unsigned int    *num_named_parameters, /* OUT.  May be NULL */
1793 				   hb_ot_name_id_t *first_param_id        /* OUT.  May be NULL */)
1794 {
1795   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1796 
1797   hb_tag_t feature_tag = g.get_feature_tag (feature_index);
1798   const OT::Feature &f = g.get_feature (feature_index);
1799 
1800   const OT::FeatureParams &feature_params = f.get_feature_params ();
1801   if (&feature_params != &Null (OT::FeatureParams))
1802   {
1803     const OT::FeatureParamsStylisticSet& ss_params =
1804       feature_params.get_stylistic_set_params (feature_tag);
1805     if (&ss_params != &Null (OT::FeatureParamsStylisticSet)) /* ssXX */
1806     {
1807       if (label_id) *label_id = ss_params.uiNameID;
1808       // ssXX features don't have the rest
1809       if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1810       if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1811       if (num_named_parameters) *num_named_parameters = 0;
1812       if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1813       return true;
1814     }
1815     const OT::FeatureParamsCharacterVariants& cv_params =
1816       feature_params.get_character_variants_params (feature_tag);
1817     if (&cv_params != &Null (OT::FeatureParamsCharacterVariants)) /* cvXX */
1818     {
1819       if (label_id) *label_id = cv_params.featUILableNameID;
1820       if (tooltip_id) *tooltip_id = cv_params.featUITooltipTextNameID;
1821       if (sample_id) *sample_id = cv_params.sampleTextNameID;
1822       if (num_named_parameters) *num_named_parameters = cv_params.numNamedParameters;
1823       if (first_param_id) *first_param_id = cv_params.firstParamUILabelNameID;
1824       return true;
1825     }
1826   }
1827 
1828   if (label_id) *label_id = HB_OT_NAME_ID_INVALID;
1829   if (tooltip_id) *tooltip_id = HB_OT_NAME_ID_INVALID;
1830   if (sample_id) *sample_id = HB_OT_NAME_ID_INVALID;
1831   if (num_named_parameters) *num_named_parameters = 0;
1832   if (first_param_id) *first_param_id = HB_OT_NAME_ID_INVALID;
1833   return false;
1834 }
1835 /**
1836  * hb_ot_layout_feature_get_characters:
1837  * @face: #hb_face_t to work upon
1838  * @table_tag: table tag to query, "GSUB" or "GPOS".
1839  * @feature_index: index of feature to query.
1840  * @start_offset: offset of the first character to retrieve
1841  * @char_count: (inout) (optional): Input = the maximum number of characters to return;
1842  *              Output = the actual number of characters returned (may be zero)
1843  * @characters: (out caller-allocates) (array length=char_count): A buffer pointer.
1844  *              The Unicode codepoints of the characters for which this feature provides
1845  *               glyph variants.
1846  *
1847  * Fetches a list of the characters defined as having a variant under the specified
1848  * "Character Variant" ("cvXX") feature tag.
1849  *
1850  * Return value: Number of total sample characters in the cvXX feature.
1851  *
1852  * Since: 2.0.0
1853  **/
1854 unsigned int
hb_ot_layout_feature_get_characters(hb_face_t * face,hb_tag_t table_tag,unsigned int feature_index,unsigned int start_offset,unsigned int * char_count,hb_codepoint_t * characters)1855 hb_ot_layout_feature_get_characters (hb_face_t      *face,
1856 				     hb_tag_t        table_tag,
1857 				     unsigned int    feature_index,
1858 				     unsigned int    start_offset,
1859 				     unsigned int   *char_count, /* IN/OUT.  May be NULL */
1860 				     hb_codepoint_t *characters  /* OUT.     May be NULL */)
1861 {
1862   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1863   return g.get_feature (feature_index)
1864 	  .get_feature_params ()
1865 	  .get_character_variants_params(g.get_feature_tag (feature_index))
1866 	  .get_characters (start_offset, char_count, characters);
1867 }
1868 #endif
1869 
1870 
1871 /*
1872  * Parts of different types are implemented here such that they have direct
1873  * access to GSUB/GPOS lookups.
1874  */
1875 
1876 
1877 struct GSUBProxy
1878 {
1879   static constexpr unsigned table_index = 0u;
1880   static constexpr bool always_inplace = false;
1881   typedef OT::SubstLookup Lookup;
1882 
GSUBProxyGSUBProxy1883   GSUBProxy (hb_face_t *face) :
1884     accel (*face->table.GSUB) {}
1885 
1886   const GSUB::accelerator_t &accel;
1887 };
1888 
1889 struct GPOSProxy
1890 {
1891   static constexpr unsigned table_index = 1u;
1892   static constexpr bool always_inplace = true;
1893   typedef OT::PosLookup Lookup;
1894 
GPOSProxyGPOSProxy1895   GPOSProxy (hb_face_t *face) :
1896     accel (*face->table.GPOS) {}
1897 
1898   const GPOS::accelerator_t &accel;
1899 };
1900 
1901 
1902 static inline bool
apply_forward(OT::hb_ot_apply_context_t * c,const OT::hb_ot_layout_lookup_accelerator_t & accel,unsigned subtable_count)1903 apply_forward (OT::hb_ot_apply_context_t *c,
1904 	       const OT::hb_ot_layout_lookup_accelerator_t &accel,
1905 	       unsigned subtable_count)
1906 {
1907   bool use_cache = accel.cache_enter (c);
1908 
1909   bool ret = false;
1910   hb_buffer_t *buffer = c->buffer;
1911   while (buffer->idx < buffer->len && buffer->successful)
1912   {
1913     bool applied = false;
1914     if (accel.digest.may_have (buffer->cur().codepoint) &&
1915 	(buffer->cur().mask & c->lookup_mask) &&
1916 	c->check_glyph_property (&buffer->cur(), c->lookup_props))
1917      {
1918        applied = accel.apply (c, subtable_count, use_cache);
1919      }
1920 
1921     if (applied)
1922       ret = true;
1923     else
1924       (void) buffer->next_glyph ();
1925   }
1926 
1927   if (use_cache)
1928     accel.cache_leave (c);
1929 
1930   return ret;
1931 }
1932 
1933 static inline bool
apply_backward(OT::hb_ot_apply_context_t * c,const OT::hb_ot_layout_lookup_accelerator_t & accel,unsigned subtable_count)1934 apply_backward (OT::hb_ot_apply_context_t *c,
1935 	       const OT::hb_ot_layout_lookup_accelerator_t &accel,
1936 	       unsigned subtable_count)
1937 {
1938   bool ret = false;
1939   hb_buffer_t *buffer = c->buffer;
1940   do
1941   {
1942     if (accel.digest.may_have (buffer->cur().codepoint) &&
1943 	(buffer->cur().mask & c->lookup_mask) &&
1944 	c->check_glyph_property (&buffer->cur(), c->lookup_props))
1945       ret |= accel.apply (c, subtable_count, false);
1946 
1947     /* The reverse lookup doesn't "advance" cursor (for good reason). */
1948     buffer->idx--;
1949 
1950   }
1951   while ((int) buffer->idx >= 0);
1952   return ret;
1953 }
1954 
1955 template <typename Proxy>
1956 static inline bool
apply_string(OT::hb_ot_apply_context_t * c,const typename Proxy::Lookup & lookup,const OT::hb_ot_layout_lookup_accelerator_t & accel)1957 apply_string (OT::hb_ot_apply_context_t *c,
1958 	      const typename Proxy::Lookup &lookup,
1959 	      const OT::hb_ot_layout_lookup_accelerator_t &accel)
1960 {
1961   hb_buffer_t *buffer = c->buffer;
1962   unsigned subtable_count = lookup.get_subtable_count ();
1963 
1964   if (unlikely (!buffer->len || !c->lookup_mask))
1965     return false;
1966 
1967   bool ret = false;
1968 
1969   c->set_lookup_props (lookup.get_props ());
1970 
1971   if (likely (!lookup.is_reverse ()))
1972   {
1973     /* in/out forward substitution/positioning */
1974     if (!Proxy::always_inplace)
1975       buffer->clear_output ();
1976 
1977     buffer->idx = 0;
1978     ret = apply_forward (c, accel, subtable_count);
1979 
1980     if (!Proxy::always_inplace)
1981       buffer->sync ();
1982   }
1983   else
1984   {
1985     /* in-place backward substitution/positioning */
1986     assert (!buffer->have_output);
1987     buffer->idx = buffer->len - 1;
1988     ret = apply_backward (c, accel, subtable_count);
1989   }
1990 
1991   return ret;
1992 }
1993 
1994 template <typename Proxy>
apply(const Proxy & proxy,const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer) const1995 inline void hb_ot_map_t::apply (const Proxy &proxy,
1996 				const hb_ot_shape_plan_t *plan,
1997 				hb_font_t *font,
1998 				hb_buffer_t *buffer) const
1999 {
2000   const unsigned int table_index = proxy.table_index;
2001   unsigned int i = 0;
2002   OT::hb_ot_apply_context_t c (table_index, font, buffer, proxy.accel.get_blob ());
2003   c.set_recurse_func (Proxy::Lookup::template dispatch_recurse_func<OT::hb_ot_apply_context_t>);
2004 
2005   for (unsigned int stage_index = 0; stage_index < stages[table_index].length; stage_index++)
2006   {
2007     const stage_map_t *stage = &stages[table_index][stage_index];
2008     for (; i < stage->last_lookup; i++)
2009     {
2010       auto &lookup = lookups[table_index][i];
2011 
2012       unsigned int lookup_index = lookup.index;
2013 
2014       auto *accel = proxy.accel.get_accel (lookup_index);
2015       if (unlikely (!accel)) continue;
2016 
2017       if (buffer->messaging () &&
2018 	  !buffer->message (font, "start lookup %u feature '%c%c%c%c'", lookup_index, HB_UNTAG (lookup.feature_tag))) continue;
2019 
2020       /* c.digest is a digest of all the current glyphs in the buffer
2021        * (plus some past glyphs).
2022        *
2023        * Only try applying the lookup if there is any overlap. */
2024       if (accel->digest.may_have (c.digest))
2025       {
2026 	c.set_lookup_index (lookup_index);
2027 	c.set_lookup_mask (lookup.mask, false);
2028 	c.set_auto_zwj (lookup.auto_zwj, false);
2029 	c.set_auto_zwnj (lookup.auto_zwnj, false);
2030 	c.set_random (lookup.random);
2031 	c.set_per_syllable (lookup.per_syllable, false);
2032 	/* apply_string's set_lookup_props initializes the iterators. */
2033 
2034 	apply_string<Proxy> (&c,
2035 			     proxy.accel.table->get_lookup (lookup_index),
2036 			     *accel);
2037       }
2038       else if (buffer->messaging ())
2039 	(void) buffer->message (font, "skipped lookup %u feature '%c%c%c%c' because no glyph matches", lookup_index, HB_UNTAG (lookup.feature_tag));
2040 
2041       if (buffer->messaging ())
2042 	(void) buffer->message (font, "end lookup %u feature '%c%c%c%c'", lookup_index, HB_UNTAG (lookup.feature_tag));
2043     }
2044 
2045     if (stage->pause_func)
2046     {
2047       if (stage->pause_func (plan, font, buffer))
2048       {
2049 	/* Refresh working buffer digest since buffer changed. */
2050 	c.digest = buffer->digest ();
2051       }
2052     }
2053   }
2054 }
2055 
substitute(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer) const2056 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
2057 {
2058   GSUBProxy proxy (font->face);
2059   if (buffer->messaging () &&
2060       !buffer->message (font, "start table GSUB script tag '%c%c%c%c'", HB_UNTAG (chosen_script[0]))) return;
2061   apply (proxy, plan, font, buffer);
2062   if (buffer->messaging ())
2063     (void) buffer->message (font, "end table GSUB script tag '%c%c%c%c'", HB_UNTAG (chosen_script[0]));
2064 }
2065 
position(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer) const2066 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
2067 {
2068   GPOSProxy proxy (font->face);
2069   if (buffer->messaging () &&
2070       !buffer->message (font, "start table GPOS script tag '%c%c%c%c'", HB_UNTAG (chosen_script[1]))) return;
2071   apply (proxy, plan, font, buffer);
2072   if (buffer->messaging ())
2073     (void) buffer->message (font, "end table GPOS script tag '%c%c%c%c'", HB_UNTAG (chosen_script[1]));
2074 }
2075 
2076 void
hb_ot_layout_substitute_lookup(OT::hb_ot_apply_context_t * c,const OT::SubstLookup & lookup,const OT::hb_ot_layout_lookup_accelerator_t & accel)2077 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
2078 				const OT::SubstLookup &lookup,
2079 				const OT::hb_ot_layout_lookup_accelerator_t &accel)
2080 {
2081   apply_string<GSUBProxy> (c, lookup, accel);
2082 }
2083 
2084 #ifndef HB_NO_BASE
2085 
2086 static void
choose_base_tags(hb_script_t script,hb_language_t language,hb_tag_t * script_tag,hb_tag_t * language_tag)2087 choose_base_tags (hb_script_t    script,
2088 		  hb_language_t  language,
2089 		  hb_tag_t      *script_tag,
2090 		  hb_tag_t      *language_tag)
2091 {
2092   hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
2093   unsigned script_count = ARRAY_LENGTH (script_tags);
2094 
2095   hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
2096   unsigned language_count = ARRAY_LENGTH (language_tags);
2097 
2098   hb_ot_tags_from_script_and_language (script, language,
2099 				       &script_count, script_tags,
2100 				       &language_count, language_tags);
2101 
2102   *script_tag = script_count ? script_tags[script_count - 1] : HB_OT_TAG_DEFAULT_SCRIPT;
2103   *language_tag = language_count ? language_tags[language_count - 1] : HB_OT_TAG_DEFAULT_LANGUAGE;
2104 }
2105 
2106 /**
2107  * hb_ot_layout_get_font_extents:
2108  * @font: a font
2109  * @direction: text direction.
2110  * @script_tag:  script tag.
2111  * @language_tag: language tag.
2112  * @extents: (out) (nullable): font extents if found.
2113  *
2114  * Fetches script/language-specific font extents.  These values are
2115  * looked up in the `BASE` table's `MinMax` records.
2116  *
2117  * If no such extents are found, the default extents for the font are
2118  * fetched. As such, the return value of this function can for the
2119  * most part be ignored.  Note that the per-script/language extents
2120  * do not have a line-gap value, and the line-gap is set to zero in
2121  * that case.
2122  *
2123  * Return value: `true` if found script/language-specific font extents.
2124  *
2125  * Since: 8.0.0
2126  **/
2127 hb_bool_t
hb_ot_layout_get_font_extents(hb_font_t * font,hb_direction_t direction,hb_tag_t script_tag,hb_tag_t language_tag,hb_font_extents_t * extents)2128 hb_ot_layout_get_font_extents (hb_font_t         *font,
2129 			       hb_direction_t     direction,
2130 			       hb_tag_t           script_tag,
2131 			       hb_tag_t           language_tag,
2132 			       hb_font_extents_t *extents)
2133 {
2134   hb_position_t min = 0, max = 0;
2135   if (font->face->table.BASE->get_min_max (font, direction, script_tag, language_tag, HB_TAG_NONE,
2136 					   &min, &max))
2137   {
2138     if (extents)
2139     {
2140       extents->ascender  = max;
2141       extents->descender = min;
2142       extents->line_gap  = 0;
2143     }
2144     return true;
2145   }
2146 
2147   hb_font_get_extents_for_direction (font, direction, extents);
2148   return false;
2149 }
2150 
2151 /**
2152  * hb_ot_layout_get_font_extents2:
2153  * @font: a font
2154  * @direction: text direction.
2155  * @script:  script.
2156  * @language: (nullable): language.
2157  * @extents: (out) (nullable): font extents if found.
2158  *
2159  * Fetches script/language-specific font extents.  These values are
2160  * looked up in the `BASE` table's `MinMax` records.
2161  *
2162  * If no such extents are found, the default extents for the font are
2163  * fetched. As such, the return value of this function can for the
2164  * most part be ignored.  Note that the per-script/language extents
2165  * do not have a line-gap value, and the line-gap is set to zero in
2166  * that case.
2167  *
2168  * This function is like hb_ot_layout_get_font_extents() but takes
2169  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2170  *
2171  * Return value: `true` if found script/language-specific font extents.
2172  *
2173  * Since: 8.0.0
2174  **/
2175 hb_bool_t
hb_ot_layout_get_font_extents2(hb_font_t * font,hb_direction_t direction,hb_script_t script,hb_language_t language,hb_font_extents_t * extents)2176 hb_ot_layout_get_font_extents2 (hb_font_t         *font,
2177 				hb_direction_t     direction,
2178 				hb_script_t        script,
2179 				hb_language_t      language,
2180 				hb_font_extents_t *extents)
2181 {
2182   hb_tag_t script_tag, language_tag;
2183   choose_base_tags (script, language, &script_tag, &language_tag);
2184   return hb_ot_layout_get_font_extents (font,
2185 					direction,
2186 					script_tag,
2187 					language_tag,
2188 					extents);
2189 }
2190 
2191 /**
2192  * hb_ot_layout_get_horizontal_baseline_tag_for_script:
2193  * @script: a script tag.
2194  *
2195  * Fetches the dominant horizontal baseline tag used by @script.
2196  *
2197  * Return value: dominant baseline tag for the @script.
2198  *
2199  * Since: 4.0.0
2200  **/
2201 hb_ot_layout_baseline_tag_t
hb_ot_layout_get_horizontal_baseline_tag_for_script(hb_script_t script)2202 hb_ot_layout_get_horizontal_baseline_tag_for_script (hb_script_t script)
2203 {
2204   /* Keep in sync with hb_ot_layout_get_baseline_with_fallback */
2205   switch ((int) script)
2206   {
2207     /* Unicode-1.1 additions */
2208     case HB_SCRIPT_BENGALI:
2209     case HB_SCRIPT_DEVANAGARI:
2210     case HB_SCRIPT_GUJARATI:
2211     case HB_SCRIPT_GURMUKHI:
2212     /* Unicode-2.0 additions */
2213     case HB_SCRIPT_TIBETAN:
2214     /* Unicode-4.0 additions */
2215     case HB_SCRIPT_LIMBU:
2216     /* Unicode-4.1 additions */
2217     case HB_SCRIPT_SYLOTI_NAGRI:
2218     /* Unicode-5.0 additions */
2219     case HB_SCRIPT_PHAGS_PA:
2220     /* Unicode-5.2 additions */
2221     case HB_SCRIPT_MEETEI_MAYEK:
2222     /* Unicode-6.1 additions */
2223     case HB_SCRIPT_SHARADA:
2224     case HB_SCRIPT_TAKRI:
2225     /* Unicode-7.0 additions */
2226     case HB_SCRIPT_MODI:
2227     case HB_SCRIPT_SIDDHAM:
2228     case HB_SCRIPT_TIRHUTA:
2229     /* Unicode-9.0 additions */
2230     case HB_SCRIPT_MARCHEN:
2231     case HB_SCRIPT_NEWA:
2232     /* Unicode-10.0 additions */
2233     case HB_SCRIPT_SOYOMBO:
2234     case HB_SCRIPT_ZANABAZAR_SQUARE:
2235     /* Unicode-11.0 additions */
2236     case HB_SCRIPT_DOGRA:
2237     case HB_SCRIPT_GUNJALA_GONDI:
2238     /* Unicode-12.0 additions */
2239     case HB_SCRIPT_NANDINAGARI:
2240       return HB_OT_LAYOUT_BASELINE_TAG_HANGING;
2241 
2242     /* Unicode-1.1 additions */
2243     case HB_SCRIPT_HANGUL:
2244     case HB_SCRIPT_HAN:
2245     case HB_SCRIPT_HIRAGANA:
2246     case HB_SCRIPT_KATAKANA:
2247     /* Unicode-3.0 additions */
2248     case HB_SCRIPT_BOPOMOFO:
2249     /* Unicode-9.0 additions */
2250     case HB_SCRIPT_TANGUT:
2251     /* Unicode-10.0 additions */
2252     case HB_SCRIPT_NUSHU:
2253     /* Unicode-13.0 additions */
2254     case HB_SCRIPT_KHITAN_SMALL_SCRIPT:
2255       return HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT;
2256 
2257     default:
2258       return HB_OT_LAYOUT_BASELINE_TAG_ROMAN;
2259   }
2260 }
2261 
2262 /**
2263  * hb_ot_layout_get_baseline:
2264  * @font: a font
2265  * @baseline_tag: a baseline tag
2266  * @direction: text direction.
2267  * @script_tag:  script tag.
2268  * @language_tag: language tag, currently unused.
2269  * @coord: (out) (nullable): baseline value if found.
2270  *
2271  * Fetches a baseline value from the face.
2272  *
2273  * Return value: `true` if found baseline value in the font.
2274  *
2275  * Since: 2.6.0
2276  **/
2277 hb_bool_t
hb_ot_layout_get_baseline(hb_font_t * font,hb_ot_layout_baseline_tag_t baseline_tag,hb_direction_t direction,hb_tag_t script_tag,hb_tag_t language_tag,hb_position_t * coord)2278 hb_ot_layout_get_baseline (hb_font_t                   *font,
2279 			   hb_ot_layout_baseline_tag_t  baseline_tag,
2280 			   hb_direction_t               direction,
2281 			   hb_tag_t                     script_tag,
2282 			   hb_tag_t                     language_tag,
2283 			   hb_position_t               *coord        /* OUT.  May be NULL. */)
2284 {
2285   return font->face->table.BASE->get_baseline (font, baseline_tag, direction, script_tag, language_tag, coord);
2286 }
2287 
2288 /**
2289  * hb_ot_layout_get_baseline2:
2290  * @font: a font
2291  * @baseline_tag: a baseline tag
2292  * @direction: text direction.
2293  * @script:  script.
2294  * @language: (nullable): language, currently unused.
2295  * @coord: (out) (nullable): baseline value if found.
2296  *
2297  * Fetches a baseline value from the face.
2298  *
2299  * This function is like hb_ot_layout_get_baseline() but takes
2300  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2301  *
2302  * Return value: `true` if found baseline value in the font.
2303  *
2304  * Since: 8.0.0
2305  **/
2306 hb_bool_t
hb_ot_layout_get_baseline2(hb_font_t * font,hb_ot_layout_baseline_tag_t baseline_tag,hb_direction_t direction,hb_script_t script,hb_language_t language,hb_position_t * coord)2307 hb_ot_layout_get_baseline2 (hb_font_t                   *font,
2308 			    hb_ot_layout_baseline_tag_t  baseline_tag,
2309 			    hb_direction_t               direction,
2310 			    hb_script_t                  script,
2311 			    hb_language_t                language,
2312 			    hb_position_t               *coord        /* OUT.  May be NULL. */)
2313 {
2314   hb_tag_t script_tag, language_tag;
2315   choose_base_tags (script, language, &script_tag, &language_tag);
2316   return hb_ot_layout_get_baseline (font,
2317 				    baseline_tag,
2318 				    direction,
2319 				    script_tag,
2320 				    language_tag,
2321 				    coord);
2322 }
2323 
2324 /**
2325  * hb_ot_layout_get_baseline_with_fallback:
2326  * @font: a font
2327  * @baseline_tag: a baseline tag
2328  * @direction: text direction.
2329  * @script_tag:  script tag.
2330  * @language_tag: language tag, currently unused.
2331  * @coord: (out): baseline value if found.
2332  *
2333  * Fetches a baseline value from the face, and synthesizes
2334  * it if the font does not have it.
2335  *
2336  * Since: 4.0.0
2337  **/
2338 void
hb_ot_layout_get_baseline_with_fallback(hb_font_t * font,hb_ot_layout_baseline_tag_t baseline_tag,hb_direction_t direction,hb_tag_t script_tag,hb_tag_t language_tag,hb_position_t * coord)2339 hb_ot_layout_get_baseline_with_fallback (hb_font_t                   *font,
2340 					 hb_ot_layout_baseline_tag_t  baseline_tag,
2341 					 hb_direction_t               direction,
2342 					 hb_tag_t                     script_tag,
2343 					 hb_tag_t                     language_tag,
2344 					 hb_position_t               *coord /* OUT */)
2345 {
2346   if (hb_ot_layout_get_baseline (font,
2347 				 baseline_tag,
2348 				 direction,
2349 				 script_tag,
2350 				 language_tag,
2351 				 coord))
2352     return;
2353 
2354   /* Synthesize missing baselines.
2355    * See https://www.w3.org/TR/css-inline-3/#baseline-synthesis-fonts
2356    */
2357   switch (baseline_tag)
2358   {
2359   case HB_OT_LAYOUT_BASELINE_TAG_ROMAN:
2360     *coord = 0; // FIXME origin ?
2361     break;
2362 
2363   case HB_OT_LAYOUT_BASELINE_TAG_MATH:
2364     {
2365       hb_codepoint_t glyph;
2366       hb_glyph_extents_t extents;
2367       if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
2368 	  (hb_font_get_nominal_glyph (font, 0x2212u, &glyph) ||
2369 	   hb_font_get_nominal_glyph (font, '-', &glyph)) &&
2370 	  hb_font_get_glyph_extents (font, glyph, &extents))
2371       {
2372 	*coord = extents.y_bearing + extents.height / 2;
2373       }
2374       else
2375       {
2376 	hb_position_t x_height = font->y_scale / 2;
2377 #ifndef HB_NO_METRICS
2378 	hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_X_HEIGHT, &x_height);
2379 #endif
2380 	*coord = x_height / 2;
2381       }
2382     }
2383     break;
2384 
2385   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT:
2386   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT:
2387     {
2388       hb_position_t embox_top, embox_bottom;
2389 
2390       hb_ot_layout_get_baseline_with_fallback (font,
2391 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2392 					       direction,
2393 					       script_tag,
2394 					       language_tag,
2395 					       &embox_top);
2396       hb_ot_layout_get_baseline_with_fallback (font,
2397 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2398 					       direction,
2399 					       script_tag,
2400 					       language_tag,
2401 					       &embox_bottom);
2402 
2403       if (baseline_tag == HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT)
2404 	*coord = embox_top + (embox_bottom - embox_top) / 10;
2405       else
2406 	*coord = embox_bottom + (embox_top - embox_bottom) / 10;
2407     }
2408     break;
2409 
2410   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT:
2411     if (hb_ot_layout_get_baseline (font,
2412 				   HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2413 				   direction,
2414 				   script_tag,
2415 				   language_tag,
2416 				   coord))
2417       *coord += HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale;
2418     else
2419     {
2420       hb_font_extents_t font_extents;
2421       hb_font_get_extents_for_direction (font, direction, &font_extents);
2422       *coord = font_extents.ascender;
2423     }
2424     break;
2425 
2426   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT:
2427     if (hb_ot_layout_get_baseline (font,
2428 				   HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2429 				   direction,
2430 				   script_tag,
2431 				   language_tag,
2432 				   coord))
2433       *coord -= HB_DIRECTION_IS_HORIZONTAL (direction) ? font->y_scale : font->x_scale;
2434     else
2435     {
2436       hb_font_extents_t font_extents;
2437       hb_font_get_extents_for_direction (font, direction, &font_extents);
2438       *coord = font_extents.descender;
2439     }
2440     break;
2441 
2442   case HB_OT_LAYOUT_BASELINE_TAG_HANGING:
2443     if (HB_DIRECTION_IS_HORIZONTAL (direction))
2444     {
2445       hb_codepoint_t ch;
2446       hb_codepoint_t glyph;
2447       hb_glyph_extents_t extents;
2448 
2449       /* Keep in sync with hb_ot_layout_get_horizontal_baseline_for_script */
2450       switch ((int) script_tag)
2451       {
2452       /* Unicode-1.1 additions */
2453       case HB_SCRIPT_BENGALI:          ch = 0x0995u; break;
2454       case HB_SCRIPT_DEVANAGARI:       ch = 0x0915u; break;
2455       case HB_SCRIPT_GUJARATI:         ch = 0x0a95u; break;
2456       case HB_SCRIPT_GURMUKHI:         ch = 0x0a15u; break;
2457       /* Unicode-2.0 additions */
2458       case HB_SCRIPT_TIBETAN:          ch = 0x0f40u; break;
2459       /* Unicode-4.0 additions */
2460       case HB_SCRIPT_LIMBU:            ch = 0x1901u; break;
2461       /* Unicode-4.1 additions */
2462       case HB_SCRIPT_SYLOTI_NAGRI:     ch = 0xa807u; break;
2463       /* Unicode-5.0 additions */
2464       case HB_SCRIPT_PHAGS_PA:         ch = 0xa840u; break;
2465       /* Unicode-5.2 additions */
2466       case HB_SCRIPT_MEETEI_MAYEK:     ch = 0xabc0u; break;
2467       /* Unicode-6.1 additions */
2468       case HB_SCRIPT_SHARADA:          ch = 0x11191u; break;
2469       case HB_SCRIPT_TAKRI:            ch = 0x1168cu; break;
2470       /* Unicode-7.0 additions */
2471       case HB_SCRIPT_MODI:             ch = 0x1160eu;break;
2472       case HB_SCRIPT_SIDDHAM:          ch = 0x11590u; break;
2473       case HB_SCRIPT_TIRHUTA:          ch = 0x1148fu; break;
2474       /* Unicode-9.0 additions */
2475       case HB_SCRIPT_MARCHEN:          ch = 0x11c72u; break;
2476       case HB_SCRIPT_NEWA:             ch = 0x1140eu; break;
2477       /* Unicode-10.0 additions */
2478       case HB_SCRIPT_SOYOMBO:          ch = 0x11a5cu; break;
2479       case HB_SCRIPT_ZANABAZAR_SQUARE: ch = 0x11a0bu; break;
2480       /* Unicode-11.0 additions */
2481       case HB_SCRIPT_DOGRA:            ch = 0x1180au; break;
2482       case HB_SCRIPT_GUNJALA_GONDI:    ch = 0x11d6cu; break;
2483       /* Unicode-12.0 additions */
2484       case HB_SCRIPT_NANDINAGARI:      ch = 0x119b0u; break;
2485       default:                         ch = 0;        break;
2486       }
2487 
2488       if (ch &&
2489 	  hb_font_get_nominal_glyph (font, ch, &glyph) &&
2490 	  hb_font_get_glyph_extents (font, glyph, &extents))
2491 	*coord = extents.y_bearing;
2492       else
2493 	*coord = font->y_scale * 6 / 10; // FIXME makes assumptions about origin
2494     }
2495     else
2496       *coord = font->x_scale * 6 / 10; // FIXME makes assumptions about origin
2497     break;
2498 
2499   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_CENTRAL:
2500     {
2501       hb_position_t top, bottom;
2502       hb_ot_layout_get_baseline_with_fallback (font,
2503 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_TOP_OR_RIGHT,
2504 					       direction,
2505 					       script_tag,
2506 					       language_tag,
2507 					       &top);
2508       hb_ot_layout_get_baseline_with_fallback (font,
2509 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
2510 					       direction,
2511 					       script_tag,
2512 					       language_tag,
2513 					       &bottom);
2514       *coord = (top + bottom) / 2;
2515 
2516     }
2517     break;
2518 
2519   case HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_CENTRAL:
2520     {
2521       hb_position_t top, bottom;
2522       hb_ot_layout_get_baseline_with_fallback (font,
2523 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_TOP_OR_RIGHT,
2524 					       direction,
2525 					       script_tag,
2526 					       language_tag,
2527 					       &top);
2528       hb_ot_layout_get_baseline_with_fallback (font,
2529 					       HB_OT_LAYOUT_BASELINE_TAG_IDEO_FACE_BOTTOM_OR_LEFT,
2530 					       direction,
2531 					       script_tag,
2532 					       language_tag,
2533 					       &bottom);
2534       *coord = (top + bottom) / 2;
2535 
2536     }
2537     break;
2538 
2539   case _HB_OT_LAYOUT_BASELINE_TAG_MAX_VALUE:
2540   default:
2541     *coord = 0;
2542     break;
2543   }
2544 }
2545 
2546 /**
2547  * hb_ot_layout_get_baseline_with_fallback2:
2548  * @font: a font
2549  * @baseline_tag: a baseline tag
2550  * @direction: text direction.
2551  * @script:  script.
2552  * @language: (nullable): language, currently unused.
2553  * @coord: (out): baseline value if found.
2554  *
2555  * Fetches a baseline value from the face, and synthesizes
2556  * it if the font does not have it.
2557  *
2558  * This function is like hb_ot_layout_get_baseline_with_fallback() but takes
2559  * #hb_script_t and #hb_language_t instead of OpenType #hb_tag_t.
2560  *
2561  * Since: 8.0.0
2562  **/
2563 void
hb_ot_layout_get_baseline_with_fallback2(hb_font_t * font,hb_ot_layout_baseline_tag_t baseline_tag,hb_direction_t direction,hb_script_t script,hb_language_t language,hb_position_t * coord)2564 hb_ot_layout_get_baseline_with_fallback2 (hb_font_t                   *font,
2565 					  hb_ot_layout_baseline_tag_t  baseline_tag,
2566 					  hb_direction_t               direction,
2567 					  hb_script_t                  script,
2568 					  hb_language_t                language,
2569 					  hb_position_t               *coord        /* OUT */)
2570 {
2571   hb_tag_t script_tag, language_tag;
2572   choose_base_tags (script, language, &script_tag, &language_tag);
2573   hb_ot_layout_get_baseline_with_fallback (font,
2574 					   baseline_tag,
2575 					   direction,
2576 					   script_tag,
2577 					   language_tag,
2578 					   coord);
2579 }
2580 
2581 #endif
2582 
2583 
2584 struct hb_get_glyph_alternates_dispatch_t :
2585        hb_dispatch_context_t<hb_get_glyph_alternates_dispatch_t, unsigned>
2586 {
default_return_valuehb_get_glyph_alternates_dispatch_t2587   static return_t default_return_value () { return 0; }
stop_sublookup_iterationhb_get_glyph_alternates_dispatch_t2588   bool stop_sublookup_iteration (return_t r) const { return r; }
2589 
2590   private:
2591   template <typename T, typename ...Ts> auto
2592   _dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
2593   ( obj.get_glyph_alternates (std::forward<Ts> (ds)...) )
2594   template <typename T, typename ...Ts> auto
2595   _dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
2596   ( default_return_value () )
2597   public:
2598   template <typename T, typename ...Ts> auto
2599   dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
2600   ( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
2601 };
2602 
2603 #ifndef HB_NO_LAYOUT_RARELY_USED
2604 /**
2605  * hb_ot_layout_lookup_get_glyph_alternates:
2606  * @face: a face.
2607  * @lookup_index: index of the feature lookup to query.
2608  * @glyph: a glyph id.
2609  * @start_offset: starting offset.
2610  * @alternate_count: (inout) (optional): Input = the maximum number of alternate glyphs to return;
2611  *                   Output = the actual number of alternate glyphs returned (may be zero).
2612  * @alternate_glyphs: (out caller-allocates) (array length=alternate_count): A glyphs buffer.
2613  *                    Alternate glyphs associated with the glyph id.
2614  *
2615  * Fetches alternates of a glyph from a given GSUB lookup index.
2616  *
2617  * Return value: Total number of alternates found in the specific lookup index for the given glyph id.
2618  *
2619  * Since: 2.6.8
2620  **/
2621 HB_EXTERN unsigned
hb_ot_layout_lookup_get_glyph_alternates(hb_face_t * face,unsigned lookup_index,hb_codepoint_t glyph,unsigned start_offset,unsigned * alternate_count,hb_codepoint_t * alternate_glyphs)2622 hb_ot_layout_lookup_get_glyph_alternates (hb_face_t      *face,
2623 					  unsigned        lookup_index,
2624 					  hb_codepoint_t  glyph,
2625 					  unsigned        start_offset,
2626 					  unsigned       *alternate_count  /* IN/OUT.  May be NULL. */,
2627 					  hb_codepoint_t *alternate_glyphs /* OUT.     May be NULL. */)
2628 {
2629   hb_get_glyph_alternates_dispatch_t c;
2630   const OT::SubstLookup &lookup = face->table.GSUB->table->get_lookup (lookup_index);
2631   auto ret = lookup.dispatch (&c, glyph, start_offset, alternate_count, alternate_glyphs);
2632   if (!ret && alternate_count) *alternate_count = 0;
2633   return ret;
2634 }
2635 
2636 
2637 struct hb_position_single_dispatch_t :
2638        hb_dispatch_context_t<hb_position_single_dispatch_t, bool>
2639 {
default_return_valuehb_position_single_dispatch_t2640   static return_t default_return_value () { return false; }
stop_sublookup_iterationhb_position_single_dispatch_t2641   bool stop_sublookup_iteration (return_t r) const { return r; }
2642 
2643   private:
2644   template <typename T, typename ...Ts> auto
2645   _dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
2646   ( obj.position_single (std::forward<Ts> (ds)...) )
2647   template <typename T, typename ...Ts> auto
2648   _dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
2649   ( default_return_value () )
2650   public:
2651   template <typename T, typename ...Ts> auto
2652   dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
2653   ( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
2654 };
2655 
2656 /**
2657  * hb_ot_layout_lookup_get_optical_bound:
2658  * @font: a font.
2659  * @lookup_index: index of the feature lookup to query.
2660  * @direction: edge of the glyph to query.
2661  * @glyph: a glyph id.
2662  *
2663  * Fetches the optical bound of a glyph positioned at the margin of text.
2664  * The direction identifies which edge of the glyph to query.
2665  *
2666  * Return value: Adjustment value. Negative values mean the glyph will stick out of the margin.
2667  *
2668  * Since: 5.3.0
2669  **/
2670 hb_position_t
hb_ot_layout_lookup_get_optical_bound(hb_font_t * font,unsigned lookup_index,hb_direction_t direction,hb_codepoint_t glyph)2671 hb_ot_layout_lookup_get_optical_bound (hb_font_t      *font,
2672 				       unsigned        lookup_index,
2673 				       hb_direction_t  direction,
2674 				       hb_codepoint_t  glyph)
2675 {
2676   const OT::PosLookup &lookup = font->face->table.GPOS->table->get_lookup (lookup_index);
2677   hb_blob_t *blob = font->face->table.GPOS->get_blob ();
2678   hb_glyph_position_t pos = {0};
2679   hb_position_single_dispatch_t c;
2680   lookup.dispatch (&c, font, blob, direction, glyph, pos);
2681   hb_position_t ret = 0;
2682   switch (direction)
2683   {
2684     case HB_DIRECTION_LTR:
2685       ret = pos.x_offset;
2686       break;
2687     case HB_DIRECTION_RTL:
2688       ret = pos.x_advance - pos.x_offset;
2689       break;
2690     case HB_DIRECTION_TTB:
2691       ret = pos.y_offset;
2692       break;
2693     case HB_DIRECTION_BTT:
2694       ret = pos.y_advance - pos.y_offset;
2695       break;
2696     case HB_DIRECTION_INVALID:
2697     default:
2698       break;
2699   }
2700   return ret;
2701 }
2702 #endif
2703 
2704 
2705 #endif
2706