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