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