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