blob: 7f3e6c73d4e1a27dc6b8314610d067619c75a025 [file] [log] [blame]
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2006 Behdad Esfahbod
4 * Copyright © 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04005 * Copyright © 2012,2013 Google, Inc.
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -05006 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04007 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -05008 *
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
Behdad Esfahbodf8603662012-07-30 02:38:39 -040028 * Google Author(s): Behdad Esfahbod
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050029 */
30
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070031#include "hb-open-type.hh"
32#include "hb-ot-layout.hh"
Behdad Esfahbodb9291002018-08-26 01:15:47 -070033#include "hb-ot-face.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070034#include "hb-ot-map.hh"
35#include "hb-map.hh"
Ebrahim Byagowi5b9c2342018-02-27 22:56:17 +033036
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020037#include "hb-ot-layout-gdef-table.hh"
38#include "hb-ot-layout-gsub-table.hh"
39#include "hb-ot-layout-gpos-table.hh"
Behdad Esfahbod203dc442018-05-03 21:03:27 -040040
41// Just so we compile them; unused otherwise:
42#include "hb-ot-layout-base-table.hh"
43#include "hb-ot-layout-jstf-table.hh"
Behdad Esfahboda5195882018-10-08 23:57:45 -040044#include "hb-ot-kern-table.hh"
Behdad Esfahbod203dc442018-05-03 21:03:27 -040045#include "hb-ot-name-table.hh"
Behdad Esfahbodd2c96812013-05-02 18:18:24 -040046
Behdad Esfahbodaff831e2008-01-24 06:03:45 -050047
Behdad Esfahboda5195882018-10-08 23:57:45 -040048static const OT::kern::accelerator_t& _get_kern (hb_face_t *face)
49{
50 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::kern::accelerator_t);
51 return *hb_ot_face_data (face)->kern;
52}
Behdad Esfahbodfee0f412018-08-28 18:27:41 -070053const OT::GDEF& _get_gdef (hb_face_t *face)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040054{
Behdad Esfahbodabc12f72018-05-08 02:23:36 -070055 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::GDEF);
Behdad Esfahboda2680682018-08-26 15:18:46 -070056 return *hb_ot_face_data (face)->GDEF->table;
Behdad Esfahbod4096fbe2018-08-26 01:25:54 -070057}
Behdad Esfahbodd8c57e82018-08-26 09:03:31 -070058static hb_blob_t * _get_gsub_blob (hb_face_t *face)
Behdad Esfahbod4096fbe2018-08-26 01:25:54 -070059{
60 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return hb_blob_get_empty ();
Behdad Esfahboda2680682018-08-26 15:18:46 -070061 return hb_ot_face_data (face)->GSUB->blob;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040062}
Behdad Esfahbod39990942018-08-26 09:12:25 -070063static inline const OT::GSUB& _get_gsub (hb_face_t *face)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040064{
Behdad Esfahbodabc12f72018-05-08 02:23:36 -070065 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::GSUB);
Behdad Esfahboda2680682018-08-26 15:18:46 -070066 return *hb_ot_face_data (face)->GSUB->table;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040067}
Behdad Esfahbodfee0f412018-08-28 18:27:41 -070068const OT::GSUB& _get_gsub_relaxed (hb_face_t *face)
Behdad Esfahbod39990942018-08-26 09:12:25 -070069{
Behdad Esfahboda2680682018-08-26 15:18:46 -070070 return *hb_ot_face_data (face)->GSUB.get_relaxed ()->table;
Behdad Esfahbod39990942018-08-26 09:12:25 -070071}
Behdad Esfahbodd8c57e82018-08-26 09:03:31 -070072static hb_blob_t * _get_gpos_blob (hb_face_t *face)
Behdad Esfahbod4096fbe2018-08-26 01:25:54 -070073{
74 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return hb_blob_get_empty ();
Behdad Esfahboda2680682018-08-26 15:18:46 -070075 return hb_ot_face_data (face)->GPOS->blob;
Behdad Esfahbod4096fbe2018-08-26 01:25:54 -070076}
Behdad Esfahbod39990942018-08-26 09:12:25 -070077static inline const OT::GPOS& _get_gpos (hb_face_t *face)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040078{
Behdad Esfahbodabc12f72018-05-08 02:23:36 -070079 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::GPOS);
Behdad Esfahboda2680682018-08-26 15:18:46 -070080 return *hb_ot_face_data (face)->GPOS->table;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040081}
Behdad Esfahbodfee0f412018-08-28 18:27:41 -070082const OT::GPOS& _get_gpos_relaxed (hb_face_t *face)
Behdad Esfahbod39990942018-08-26 09:12:25 -070083{
Behdad Esfahboda2680682018-08-26 15:18:46 -070084 return *hb_ot_face_data (face)->GPOS.get_relaxed ()->table;
Behdad Esfahbod39990942018-08-26 09:12:25 -070085}
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040086
Behdad Esfahbodd8c57e82018-08-26 09:03:31 -070087
Behdad Esfahbod590d55c2008-01-24 19:13:50 -050088/*
Behdad Esfahboda5195882018-10-08 23:57:45 -040089 * kern
90 */
91
92hb_bool_t
93hb_ot_layout_has_kerning (hb_face_t *face)
94{
95 return _get_kern (face).has_data ();
96}
97
98void
99hb_ot_layout_kern (hb_font_t *font,
100 hb_buffer_t *buffer,
101 hb_mask_t kern_mask)
102{
103 _get_kern (font->face).apply (font, buffer, kern_mask);
104}
105
106
107/*
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500108 * GDEF
109 */
110
Behdad Esfahbodd8c57e82018-08-26 09:03:31 -0700111static bool
112_hb_ot_blacklist_gdef (unsigned int gdef_len,
113 unsigned int gsub_len,
114 unsigned int gpos_len)
115{
116 /* The ugly business of blacklisting individual fonts' tables happen here!
117 * See this thread for why we finally had to bend in and do this:
118 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
119 *
120 * In certain versions of Times New Roman Italic and Bold Italic,
121 * ASCII double quotation mark U+0022 has wrong glyph class 3 (mark)
122 * in GDEF. Many versions of Tahoma have bad GDEF tables that
123 * incorrectly classify some spacing marks such as certain IPA
124 * symbols as glyph class 3. So do older versions of Microsoft
125 * Himalaya, and the version of Cantarell shipped by Ubuntu 16.04.
126 *
127 * Nuke the GDEF tables of to avoid unwanted width-zeroing.
128 *
129 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
130 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
131 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
132 */
133#define ENCODE(x,y,z) ((int64_t) (x) << 32 | (int64_t) (y) << 16 | (z))
134 switch ENCODE(gdef_len, gsub_len, gpos_len)
135 {
136 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
137 case ENCODE (442, 2874, 42038):
138 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
139 case ENCODE (430, 2874, 40662):
140 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
141 case ENCODE (442, 2874, 39116):
142 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
143 case ENCODE (430, 2874, 39374):
144 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
145 case ENCODE (490, 3046, 41638):
146 /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
147 case ENCODE (478, 3046, 41902):
148 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
149 case ENCODE (898, 12554, 46470):
150 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
151 case ENCODE (910, 12566, 47732):
152 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
153 case ENCODE (928, 23298, 59332):
154 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
155 case ENCODE (940, 23310, 60732):
156 /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
157 case ENCODE (964, 23836, 60072):
158 /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
159 case ENCODE (976, 23832, 61456):
160 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
161 case ENCODE (994, 24474, 60336):
162 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
163 case ENCODE (1006, 24470, 61740):
164 /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
165 case ENCODE (1006, 24576, 61346):
166 /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
167 case ENCODE (1018, 24572, 62828):
168 /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5 tahoma.ttf from Windows 10 AU */
169 case ENCODE (1006, 24576, 61352):
170 /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2 tahomabd.ttf from Windows 10 AU */
171 case ENCODE (1018, 24572, 62834):
172 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
173 case ENCODE (832, 7324, 47162):
174 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
175 case ENCODE (844, 7302, 45474):
176 /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc himalaya.ttf from Windows 7 */
177 case ENCODE (180, 13054, 7254):
178 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
179 case ENCODE (192, 12638, 7254):
180 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
181 case ENCODE (192, 12690, 7254):
182 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
183 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
184 case ENCODE (188, 248, 3852):
185 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
186 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
187 case ENCODE (188, 264, 3426):
188 /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
189 case ENCODE (1058, 47032, 11818):
190 /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
191 case ENCODE (1046, 47030, 12600):
192 /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
193 case ENCODE (1058, 71796, 16770):
194 /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
195 case ENCODE (1046, 71790, 17862):
196 /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
197 case ENCODE (1046, 71788, 17112):
198 /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
199 case ENCODE (1058, 71794, 17514):
200 /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
201 case ENCODE (1330, 109904, 57938):
202 /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
203 case ENCODE (1330, 109904, 58972):
204 /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85 Padauk.ttf
205 * "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
206 case ENCODE (1004, 59092, 14836):
207 return true;
208#undef ENCODE
209 }
210 return false;
211}
212
213inline void
214OT::GDEF::accelerator_t::init (hb_face_t *face)
215{
216 this->blob = hb_sanitize_context_t().reference_table<GDEF> (face);
217
218 if (unlikely (_hb_ot_blacklist_gdef (this->blob->length,
219 _get_gsub_blob (face)->length,
220 _get_gpos_blob (face)->length)))
221 {
222 hb_blob_destroy (this->blob);
223 this->blob = hb_blob_get_empty ();
224 }
225
226 table = this->blob->as<GDEF> ();
227}
228
Behdad Esfahbodc624e182018-08-26 09:19:20 -0700229static void
230_hb_ot_layout_set_glyph_props (hb_font_t *font,
231 hb_buffer_t *buffer)
232{
233 _hb_buffer_assert_gsubgpos_vars (buffer);
234
235 const OT::GDEF &gdef = _get_gdef (font->face);
236 unsigned int count = buffer->len;
237 for (unsigned int i = 0; i < count; i++)
238 {
239 _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
240 _hb_glyph_info_clear_lig_props (&buffer->info[i]);
241 buffer->info[i].syllable() = 0;
242 }
243}
244
245/* Public API */
246
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500247hb_bool_t
Behdad Esfahbod52ea4772009-11-06 17:45:38 -0500248hb_ot_layout_has_glyph_classes (hb_face_t *face)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500249{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400250 return _get_gdef (face).has_glyph_classes ();
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500251}
252
Sascha Brawer01c3a882015-06-01 13:22:01 +0200253/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500254 * hb_ot_layout_get_glyph_class:
255 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200256 * Since: 0.9.7
257 **/
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800258hb_ot_layout_glyph_class_t
259hb_ot_layout_get_glyph_class (hb_face_t *face,
260 hb_codepoint_t glyph)
261{
262 return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
263}
Behdad Esfahbod4a2d8442010-11-03 15:28:56 -0400264
Sascha Brawer01c3a882015-06-01 13:22:01 +0200265/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500266 * hb_ot_layout_get_glyphs_in_class:
267 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200268 * Since: 0.9.7
269 **/
Behdad Esfahbod89ca8ee2012-11-16 13:53:40 -0800270void
271hb_ot_layout_get_glyphs_in_class (hb_face_t *face,
272 hb_ot_layout_glyph_class_t klass,
273 hb_set_t *glyphs /* OUT */)
274{
275 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
276}
277
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500278unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400279hb_ot_layout_get_attach_points (hb_face_t *face,
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400280 hb_codepoint_t glyph,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500281 unsigned int start_offset,
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400282 unsigned int *point_count /* IN/OUT */,
283 unsigned int *point_array /* OUT */)
284{
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500285 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400286}
287
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500288unsigned int
Behdad Esfahbode2046742010-10-27 12:32:02 -0400289hb_ot_layout_get_ligature_carets (hb_font_t *font,
Behdad Esfahbode2046742010-10-27 12:32:02 -0400290 hb_direction_t direction,
291 hb_codepoint_t glyph,
292 unsigned int start_offset,
293 unsigned int *caret_count /* IN/OUT */,
Ebrahim Byagowi3b0e47c2017-06-19 14:47:09 +0430294 hb_position_t *caret_array /* OUT */)
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400295{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400296 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400297}
298
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400299
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500300/*
301 * GSUB/GPOS
302 */
303
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400304static const OT::GSUBGPOS&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400305get_gsubgpos_table (hb_face_t *face,
306 hb_tag_t table_tag)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500307{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400308 switch (table_tag) {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400309 case HB_OT_TAG_GSUB: return _get_gsub (face);
310 case HB_OT_TAG_GPOS: return _get_gpos (face);
Behdad Esfahbodabc12f72018-05-08 02:23:36 -0700311 default: return Null(OT::GSUBGPOS);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500312 }
313}
314
315
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500316unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400317hb_ot_layout_table_get_script_tags (hb_face_t *face,
318 hb_tag_t table_tag,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500319 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400320 unsigned int *script_count /* IN/OUT */,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330321 hb_tag_t *script_tags /* OUT */)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500322{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400323 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500324
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500325 return g.get_script_tags (start_offset, script_count, script_tags);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500326}
327
Behdad Esfahbodeb45c0a2013-01-16 22:07:50 -0600328#define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
329
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500330hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400331hb_ot_layout_table_find_script (hb_face_t *face,
332 hb_tag_t table_tag,
333 hb_tag_t script_tag,
334 unsigned int *script_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500335{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200336 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400337 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500338
339 if (g.find_script_index (script_tag, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400340 return true;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500341
342 /* try finding 'DFLT' */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500343 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400344 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500345
Behdad Esfahboddca8aff2010-09-28 16:25:23 -0400346 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
347 * including many versions of DejaVu Sans Mono! */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500348 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400349 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500350
Behdad Esfahbodeb45c0a2013-01-16 22:07:50 -0600351 /* try with 'latn'; some old fonts put their features there even though
352 they're really trying to support Thai, for example :( */
353 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
354 return false;
355
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500356 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400357 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500358}
359
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100360hb_bool_t
361hb_ot_layout_table_choose_script (hb_face_t *face,
362 hb_tag_t table_tag,
363 const hb_tag_t *script_tags,
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400364 unsigned int *script_index,
365 hb_tag_t *chosen_script)
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100366{
David Corbett91067712017-12-08 11:21:14 -0500367 const hb_tag_t *t;
368 for (t = script_tags; *t; t++);
369 return hb_ot_layout_table_select_script (face, table_tag, t - script_tags, script_tags, script_index, chosen_script);
370}
371
Behdad Esfahbod80616642018-10-11 14:16:55 -0400372/**
373 * hb_ot_layout_table_select_script:
374 *
Behdad Esfahbod3d9a0302018-10-18 05:58:17 -0700375 * Since: 2.0.0
Behdad Esfahbod80616642018-10-11 14:16:55 -0400376 **/
David Corbett91067712017-12-08 11:21:14 -0500377hb_bool_t
378hb_ot_layout_table_select_script (hb_face_t *face,
379 hb_tag_t table_tag,
380 unsigned int script_count,
381 const hb_tag_t *script_tags,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330382 unsigned int *script_index /* OUT */,
David Corbett91067712017-12-08 11:21:14 -0500383 hb_tag_t *chosen_script /* OUT */)
384{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200385 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400386 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
David Corbett91067712017-12-08 11:21:14 -0500387 unsigned int i;
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100388
David Corbett91067712017-12-08 11:21:14 -0500389 for (i = 0; i < script_count; i++)
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100390 {
David Corbett91067712017-12-08 11:21:14 -0500391 if (g.find_script_index (script_tags[i], script_index))
392 {
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400393 if (chosen_script)
David Corbett91067712017-12-08 11:21:14 -0500394 *chosen_script = script_tags[i];
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400395 return true;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400396 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100397 }
398
399 /* try finding 'DFLT' */
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400400 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
401 if (chosen_script)
402 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400403 return false;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400404 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100405
406 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400407 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
408 if (chosen_script)
409 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400410 return false;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400411 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100412
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500413 /* try with 'latn'; some old fonts put their features there even though
414 they're really trying to support Thai, for example :( */
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500415 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
416 if (chosen_script)
417 *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400418 return false;
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500419 }
420
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100421 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400422 if (chosen_script)
423 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400424 return false;
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100425}
426
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500427unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400428hb_ot_layout_table_get_feature_tags (hb_face_t *face,
429 hb_tag_t table_tag,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500430 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400431 unsigned int *feature_count /* IN/OUT */,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330432 hb_tag_t *feature_tags /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500433{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400434 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500435
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500436 return g.get_feature_tags (start_offset, feature_count, feature_tags);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500437}
438
Behdad Esfahbod0f98fe82015-07-23 11:52:11 +0100439hb_bool_t
440hb_ot_layout_table_find_feature (hb_face_t *face,
441 hb_tag_t table_tag,
442 hb_tag_t feature_tag,
443 unsigned int *feature_index)
444{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200445 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
Behdad Esfahbod0f98fe82015-07-23 11:52:11 +0100446 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
447
448 unsigned int num_features = g.get_feature_count ();
449 for (unsigned int i = 0; i < num_features; i++)
450 {
451 if (feature_tag == g.get_feature_tag (i)) {
452 if (feature_index) *feature_index = i;
453 return true;
454 }
455 }
456
457 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
458 return false;
459}
460
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500461
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500462unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400463hb_ot_layout_script_get_language_tags (hb_face_t *face,
464 hb_tag_t table_tag,
465 unsigned int script_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500466 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400467 unsigned int *language_count /* IN/OUT */,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330468 hb_tag_t *language_tags /* OUT */)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500469{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400470 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500471
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500472 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500473}
474
475hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400476hb_ot_layout_script_find_language (hb_face_t *face,
477 hb_tag_t table_tag,
478 unsigned int script_index,
479 hb_tag_t language_tag,
480 unsigned int *language_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500481{
David Corbett91067712017-12-08 11:21:14 -0500482 return hb_ot_layout_script_select_language (face, table_tag, script_index, 1, &language_tag, language_index);
483}
484
Behdad Esfahbod80616642018-10-11 14:16:55 -0400485/**
486 * hb_ot_layout_script_select_language:
487 *
Behdad Esfahbod3d9a0302018-10-18 05:58:17 -0700488 * Since: 2.0.0
Behdad Esfahbod80616642018-10-11 14:16:55 -0400489 **/
David Corbett91067712017-12-08 11:21:14 -0500490hb_bool_t
Behdad Esfahbodcf975ac2018-10-11 14:07:44 -0400491hb_ot_layout_script_select_language (hb_face_t *face,
492 hb_tag_t table_tag,
493 unsigned int script_index,
494 unsigned int language_count,
495 const hb_tag_t *language_tags,
496 unsigned int *language_index /* OUT */)
David Corbett91067712017-12-08 11:21:14 -0500497{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200498 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400499 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
David Corbett91067712017-12-08 11:21:14 -0500500 unsigned int i;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500501
David Corbett91067712017-12-08 11:21:14 -0500502 for (i = 0; i < language_count; i++)
503 {
504 if (s.find_lang_sys_index (language_tags[i], language_index))
505 return true;
506 }
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500507
David Corbett91067712017-12-08 11:21:14 -0500508 /* try finding 'dflt' */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500509 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400510 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500511
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500512 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400513 return false;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500514}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500515
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500516hb_bool_t
Behdad Esfahbod911ca382014-06-24 10:20:36 -0600517hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
518 hb_tag_t table_tag,
519 unsigned int script_index,
520 unsigned int language_index,
521 unsigned int *feature_index)
522{
523 return hb_ot_layout_language_get_required_feature (face,
524 table_tag,
525 script_index,
526 language_index,
527 feature_index,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200528 nullptr);
Behdad Esfahbod911ca382014-06-24 10:20:36 -0600529}
530
Sascha Brawer01c3a882015-06-01 13:22:01 +0200531/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500532 * hb_ot_layout_language_get_required_feature:
533 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200534 * Since: 0.9.30
535 **/
Behdad Esfahbod911ca382014-06-24 10:20:36 -0600536hb_bool_t
Jonathan Kewda132932014-04-27 14:05:24 +0100537hb_ot_layout_language_get_required_feature (hb_face_t *face,
538 hb_tag_t table_tag,
539 unsigned int script_index,
540 unsigned int language_index,
541 unsigned int *feature_index,
542 hb_tag_t *feature_tag)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500543{
Jonathan Kewda132932014-04-27 14:05:24 +0100544 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
545 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500546
Jonathan Kewda132932014-04-27 14:05:24 +0100547 unsigned int index = l.get_required_feature_index ();
548 if (feature_index) *feature_index = index;
549 if (feature_tag) *feature_tag = g.get_feature_tag (index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500550
551 return l.has_required_feature ();
552}
553
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500554unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400555hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
556 hb_tag_t table_tag,
557 unsigned int script_index,
558 unsigned int language_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500559 unsigned int start_offset,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330560 unsigned int *feature_count /* IN/OUT */,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400561 unsigned int *feature_indexes /* OUT */)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500562{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400563 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
564 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500565
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500566 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500567}
568
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500569unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400570hb_ot_layout_language_get_feature_tags (hb_face_t *face,
571 hb_tag_t table_tag,
572 unsigned int script_index,
573 unsigned int language_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500574 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400575 unsigned int *feature_count /* IN/OUT */,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330576 hb_tag_t *feature_tags /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500577{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400578 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
579 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500580
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200581 static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500582 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400583
Behdad Esfahbod98977492009-08-27 01:32:17 -0400584 if (feature_tags) {
585 unsigned int count = *feature_count;
586 for (unsigned int i = 0; i < count; i++)
587 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
588 }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400589
590 return ret;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500591}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500592
593
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500594hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400595hb_ot_layout_language_find_feature (hb_face_t *face,
596 hb_tag_t table_tag,
597 unsigned int script_index,
598 unsigned int language_index,
599 hb_tag_t feature_tag,
600 unsigned int *feature_index)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500601{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200602 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400603 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
604 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500605
Behdad Esfahbodf4c95142009-05-17 04:59:56 -0400606 unsigned int num_features = l.get_feature_count ();
607 for (unsigned int i = 0; i < num_features; i++) {
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500608 unsigned int f_index = l.get_feature_index (i);
609
610 if (feature_tag == g.get_feature_tag (f_index)) {
611 if (feature_index) *feature_index = f_index;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400612 return true;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500613 }
614 }
615
616 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400617 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500618}
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500619
Sascha Brawer01c3a882015-06-01 13:22:01 +0200620/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400621 * hb_ot_layout_feature_get_lookups:
622 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200623 * Since: 0.9.7
624 **/
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500625unsigned int
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800626hb_ot_layout_feature_get_lookups (hb_face_t *face,
627 hb_tag_t table_tag,
628 unsigned int feature_index,
629 unsigned int start_offset,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330630 unsigned int *lookup_count /* IN/OUT */,
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800631 unsigned int *lookup_indexes /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500632{
Behdad Esfahbodec87ba92016-09-10 03:53:11 -0700633 return hb_ot_layout_feature_with_variations_get_lookups (face,
634 table_tag,
635 feature_index,
636 HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
637 start_offset,
638 lookup_count,
639 lookup_indexes);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500640}
641
Sascha Brawer01c3a882015-06-01 13:22:01 +0200642/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400643 * hb_ot_layout_table_get_lookup_count:
644 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200645 * Since: 0.9.22
646 **/
Behdad Esfahbod27674b42013-10-03 14:54:50 -0400647unsigned int
648hb_ot_layout_table_get_lookup_count (hb_face_t *face,
649 hb_tag_t table_tag)
650{
Behdad Esfahbod96828b92018-10-25 20:34:29 -0700651 return get_gsubgpos_table (face, table_tag).get_lookup_count ();
Behdad Esfahbod27674b42013-10-03 14:54:50 -0400652}
653
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700654
655struct hb_collect_features_context_t
656{
657 hb_collect_features_context_t (hb_face_t *face,
658 hb_tag_t table_tag,
659 hb_set_t *feature_indexes_)
660 : g (get_gsubgpos_table (face, table_tag)),
661 feature_indexes (feature_indexes_) {}
662
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700663 bool inline visited (const OT::Script &s)
664 {
665 /* We might have Null() object here. Don't want to involve
666 * that in the memoize. So, detect empty objects and return. */
667 if (unlikely (!s.has_default_lang_sys () &&
668 !s.get_lang_sys_count ()))
669 return true;
670
671 return visited (s, visited_script);
672 }
673 bool inline visited (const OT::LangSys &l)
674 {
675 /* We might have Null() object here. Don't want to involve
676 * that in the memoize. So, detect empty objects and return. */
677 if (unlikely (!l.has_required_feature () &&
678 !l.get_feature_count ()))
679 return true;
680
681 return visited (l, visited_langsys);
682 }
683
684 private:
685 template <typename T>
686 bool inline visited (const T &p, hb_set_t &visited_set)
687 {
688 hb_codepoint_t delta = (hb_codepoint_t) ((uintptr_t) &p - (uintptr_t) &g);
689 if (visited_set.has (delta))
690 return true;
691
692 visited_set.add (delta);
693 return false;
694 }
695
696 public:
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700697 const OT::GSUBGPOS &g;
698 hb_set_t *feature_indexes;
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700699
700 private:
701 hb_auto_t<hb_set_t> visited_script;
702 hb_auto_t<hb_set_t> visited_langsys;
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700703};
704
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500705static void
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700706langsys_collect_features (hb_collect_features_context_t *c,
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700707 const OT::LangSys &l,
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700708 const hb_tag_t *features)
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500709{
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700710 if (c->visited (l)) return;
711
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500712 if (!features)
713 {
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700714 /* All features. */
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700715 if (l.has_required_feature ())
716 c->feature_indexes->add (l.get_required_feature_index ());
Behdad Esfahbod5f85c802013-06-26 20:14:18 -0400717
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700718 l.add_feature_indexes_to (c->feature_indexes);
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600719 }
720 else
721 {
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700722 /* Ugh. Any faster way? */
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500723 for (; *features; features++)
724 {
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700725 hb_tag_t feature_tag = *features;
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700726 unsigned int num_features = l.get_feature_count ();
727 for (unsigned int i = 0; i < num_features; i++)
728 {
729 unsigned int feature_index = l.get_feature_index (i);
730
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700731 if (feature_tag == c->g.get_feature_tag (feature_index))
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700732 {
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700733 c->feature_indexes->add (feature_index);
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700734 break;
735 }
736 }
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500737 }
738 }
739}
740
741static void
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700742script_collect_features (hb_collect_features_context_t *c,
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700743 const OT::Script &s,
744 const hb_tag_t *languages,
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700745 const hb_tag_t *features)
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500746{
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700747 if (c->visited (s)) return;
748
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500749 if (!languages)
750 {
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700751 /* All languages. */
Behdad Esfahbodeb44bfc2018-10-25 21:42:19 -0700752 if (s.has_default_lang_sys ())
753 langsys_collect_features (c,
754 s.get_default_lang_sys (),
755 features);
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700756
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700757 unsigned int count = s.get_lang_sys_count ();
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500758 for (unsigned int language_index = 0; language_index < count; language_index++)
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700759 langsys_collect_features (c,
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700760 s.get_lang_sys (language_index),
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700761 features);
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600762 }
763 else
764 {
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500765 for (; *languages; languages++)
766 {
767 unsigned int language_index;
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700768 if (s.find_lang_sys_index (*languages, &language_index))
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700769 langsys_collect_features (c,
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700770 s.get_lang_sys (language_index),
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700771 features);
Garret Rieger7d92bef2018-07-30 17:17:43 -0700772 }
773 }
774}
775
776/**
Garret Rieger89733752018-07-30 18:10:43 -0700777 * hb_ot_layout_collect_features:
Garret Rieger7d92bef2018-07-30 17:17:43 -0700778 *
Behdad Esfahbod44d1fb32018-08-01 14:51:51 -0700779 * Since: 1.8.5
Garret Rieger7d92bef2018-07-30 17:17:43 -0700780 **/
781void
782hb_ot_layout_collect_features (hb_face_t *face,
783 hb_tag_t table_tag,
784 const hb_tag_t *scripts,
785 const hb_tag_t *languages,
786 const hb_tag_t *features,
787 hb_set_t *feature_indexes /* OUT */)
788{
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700789 hb_collect_features_context_t c (face, table_tag, feature_indexes);
Garret Rieger7d92bef2018-07-30 17:17:43 -0700790 if (!scripts)
791 {
Behdad Esfahbode8e67502018-10-25 20:48:20 -0700792 /* All scripts. */
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700793 unsigned int count = c.g.get_script_count ();
Garret Rieger7d92bef2018-07-30 17:17:43 -0700794 for (unsigned int script_index = 0; script_index < count; script_index++)
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700795 script_collect_features (&c,
796 c.g.get_script (script_index),
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700797 languages,
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700798 features);
Garret Rieger7d92bef2018-07-30 17:17:43 -0700799 }
800 else
801 {
802 for (; *scripts; scripts++)
803 {
804 unsigned int script_index;
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700805 if (c.g.find_script_index (*scripts, &script_index))
806 script_collect_features (&c,
807 c.g.get_script (script_index),
Behdad Esfahbodfe5520d2018-10-25 20:58:34 -0700808 languages,
Behdad Esfahbod941600a2018-10-25 21:26:08 -0700809 features);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500810 }
811 }
812}
813
Sascha Brawer01c3a882015-06-01 13:22:01 +0200814/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400815 * hb_ot_layout_collect_lookups:
816 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200817 * Since: 0.9.8
818 **/
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500819void
820hb_ot_layout_collect_lookups (hb_face_t *face,
821 hb_tag_t table_tag,
822 const hb_tag_t *scripts,
823 const hb_tag_t *languages,
824 const hb_tag_t *features,
825 hb_set_t *lookup_indexes /* OUT */)
826{
Behdad Esfahbode98af6d2018-10-25 22:25:29 -0700827 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
828
Garret Rieger7d92bef2018-07-30 17:17:43 -0700829 hb_auto_t<hb_set_t> feature_indexes;
830 hb_ot_layout_collect_features (face, table_tag, scripts, languages, features, &feature_indexes);
Behdad Esfahbode98af6d2018-10-25 22:25:29 -0700831
Behdad Esfahbodc237cdf2018-10-25 21:17:30 -0700832 for (hb_codepoint_t feature_index = HB_SET_VALUE_INVALID;
833 hb_set_next (&feature_indexes, &feature_index);)
Behdad Esfahbode98af6d2018-10-25 22:25:29 -0700834 g.get_feature (feature_index).add_lookup_indexes_to (lookup_indexes);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500835}
836
Sascha Brawer01c3a882015-06-01 13:22:01 +0200837/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400838 * hb_ot_layout_lookup_collect_glyphs:
839 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200840 * Since: 0.9.7
841 **/
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800842void
843hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
844 hb_tag_t table_tag,
845 unsigned int lookup_index,
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330846 hb_set_t *glyphs_before, /* OUT. May be NULL */
847 hb_set_t *glyphs_input, /* OUT. May be NULL */
848 hb_set_t *glyphs_after, /* OUT. May be NULL */
849 hb_set_t *glyphs_output /* OUT. May be NULL */)
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800850{
851 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
852
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600853 OT::hb_collect_glyphs_context_t c (face,
854 glyphs_before,
855 glyphs_input,
856 glyphs_after,
857 glyphs_output);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800858
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600859 switch (table_tag)
860 {
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800861 case HB_OT_TAG_GSUB:
862 {
Behdad Esfahboda2680682018-08-26 15:18:46 -0700863 const OT::SubstLookup& l = hb_ot_face_data (face)->GSUB->table->get_lookup (lookup_index);
Behdad Esfahbod780cd932013-05-03 17:33:16 -0400864 l.collect_glyphs (&c);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800865 return;
866 }
867 case HB_OT_TAG_GPOS:
868 {
Behdad Esfahboda2680682018-08-26 15:18:46 -0700869 const OT::PosLookup& l = hb_ot_face_data (face)->GPOS->table->get_lookup (lookup_index);
Behdad Esfahbod780cd932013-05-03 17:33:16 -0400870 l.collect_glyphs (&c);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800871 return;
872 }
873 }
874}
875
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400876
Behdad Esfahbod30c42b62016-09-10 03:32:39 -0700877/* Variations support */
878
879hb_bool_t
880hb_ot_layout_table_find_feature_variations (hb_face_t *face,
881 hb_tag_t table_tag,
882 const int *coords,
883 unsigned int num_coords,
884 unsigned int *variations_index /* out */)
885{
886 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
887
888 return g.find_variations_index (coords, num_coords, variations_index);
889}
890
Behdad Esfahbodec87ba92016-09-10 03:53:11 -0700891unsigned int
892hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face,
893 hb_tag_t table_tag,
894 unsigned int feature_index,
895 unsigned int variations_index,
896 unsigned int start_offset,
897 unsigned int *lookup_count /* IN/OUT */,
898 unsigned int *lookup_indexes /* OUT */)
899{
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200900 static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
Behdad Esfahbodec87ba92016-09-10 03:53:11 -0700901 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
902
903 const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
904
905 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
906}
907
Behdad Esfahbod30c42b62016-09-10 03:32:39 -0700908
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400909/*
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400910 * OT::GSUB
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400911 */
912
913hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400914hb_ot_layout_has_substitution (hb_face_t *face)
915{
Behdad Esfahbodb912fbe2018-08-06 06:30:12 -0700916 return _get_gsub (face).has_data ();
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400917}
918
Sascha Brawer01c3a882015-06-01 13:22:01 +0200919/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400920 * hb_ot_layout_lookup_would_substitute:
921 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200922 * Since: 0.9.7
923 **/
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400924hb_bool_t
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800925hb_ot_layout_lookup_would_substitute (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400926 unsigned int lookup_index,
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400927 const hb_codepoint_t *glyphs,
928 unsigned int glyphs_length,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400929 hb_bool_t zero_context)
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400930{
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400931 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800932 return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400933}
934
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400935hb_bool_t
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800936hb_ot_layout_lookup_would_substitute_fast (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400937 unsigned int lookup_index,
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400938 const hb_codepoint_t *glyphs,
939 unsigned int glyphs_length,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400940 hb_bool_t zero_context)
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400941{
Behdad Esfahboda2680682018-08-26 15:18:46 -0700942 if (unlikely (lookup_index >= hb_ot_face_data (face)->GSUB->lookup_count)) return false;
Behdad Esfahbodea512f72015-11-26 19:22:22 -0500943 OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400944
Behdad Esfahboda2680682018-08-26 15:18:46 -0700945 const OT::SubstLookup& l = hb_ot_face_data (face)->GSUB->table->get_lookup (lookup_index);
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400946
Behdad Esfahboda2680682018-08-26 15:18:46 -0700947 return l.would_apply (&c, &hb_ot_face_data (face)->GSUB->accels[lookup_index]);
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400948}
949
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400950void
Behdad Esfahbodc624e182018-08-26 09:19:20 -0700951hb_ot_layout_substitute_start (hb_font_t *font,
952 hb_buffer_t *buffer)
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400953{
Behdad Esfahbodc624e182018-08-26 09:19:20 -0700954_hb_ot_layout_set_glyph_props (font, buffer);
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400955}
956
Sascha Brawer01c3a882015-06-01 13:22:01 +0200957/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400958 * hb_ot_layout_lookup_substitute_closure:
959 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200960 * Since: 0.9.7
961 **/
Behdad Esfahbod5caece62012-04-23 23:03:12 -0400962void
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800963hb_ot_layout_lookup_substitute_closure (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400964 unsigned int lookup_index,
965 hb_set_t *glyphs)
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400966{
Garret Rieger45186b92018-06-05 17:14:42 -0700967 hb_auto_t<hb_map_t> done_lookups;
968 OT::hb_closure_context_t c (face, glyphs, &done_lookups);
Behdad Esfahbod9b346772012-11-23 17:55:40 -0500969
970 const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
971
Garret Rieger45186b92018-06-05 17:14:42 -0700972 l.closure (&c, lookup_index);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400973}
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400974
Garret Rieger57badad2018-06-06 16:02:51 -0700975/**
976 * hb_ot_layout_lookups_substitute_closure:
977 *
978 * Compute the transitive closure of glyphs needed for all of the
979 * provided lookups.
980 *
981 * Since: 1.8.1
982 **/
983void
984hb_ot_layout_lookups_substitute_closure (hb_face_t *face,
985 const hb_set_t *lookups,
986 hb_set_t *glyphs)
987{
988 hb_auto_t<hb_map_t> done_lookups;
989 OT::hb_closure_context_t c (face, glyphs, &done_lookups);
990 const OT::GSUB& gsub = _get_gsub (face);
991
Garret Rieger85646fd2018-07-23 15:37:18 -0700992 unsigned int iteration_count = 0;
Garret Rieger57badad2018-06-06 16:02:51 -0700993 unsigned int glyphs_length;
994 do
995 {
996 glyphs_length = glyphs->get_population ();
Garret Riegerfeb23892018-06-07 14:32:34 -0700997 if (lookups != nullptr)
998 {
999 for (hb_codepoint_t lookup_index = HB_SET_VALUE_INVALID; hb_set_next (lookups, &lookup_index);)
1000 gsub.get_lookup (lookup_index).closure (&c, lookup_index);
Behdad Esfahbodf56cd9d2018-06-11 22:02:38 -04001001 }
1002 else
1003 {
Garret Riegerfeb23892018-06-07 14:32:34 -07001004 for (unsigned int i = 0; i < gsub.get_lookup_count (); i++)
1005 gsub.get_lookup (i).closure (&c, i);
1006 }
Garret Rieger85646fd2018-07-23 15:37:18 -07001007 iteration_count++;
1008 } while (iteration_count <= HB_CLOSURE_MAX_STAGES
1009 && glyphs_length != glyphs->get_population ());
Garret Rieger57badad2018-06-06 16:02:51 -07001010}
1011
Behdad Esfahbod9c42f052009-05-18 17:43:49 -04001012/*
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001013 * OT::GPOS
Behdad Esfahbod9c42f052009-05-18 17:43:49 -04001014 */
1015
1016hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -04001017hb_ot_layout_has_positioning (hb_face_t *face)
1018{
Behdad Esfahbodb912fbe2018-08-06 06:30:12 -07001019 return _get_gpos (face).has_data ();
Behdad Esfahbod0ead4812009-08-02 17:41:36 -04001020}
1021
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -04001022void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001023hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -04001024{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001025 OT::GPOS::position_start (font, buffer);
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -04001026}
1027
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -05001028void
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001029hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -05001030{
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001031 OT::GPOS::position_finish_advances (font, buffer);
1032}
1033
1034void
1035hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
1036{
1037 OT::GPOS::position_finish_offsets (font, buffer);
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -05001038}
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001039
Sascha Brawer01c3a882015-06-01 13:22:01 +02001040/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +04001041 * hb_ot_layout_get_size_params:
1042 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +04301043 * Since: 0.9.10
Sascha Brawer01c3a882015-06-01 13:22:01 +02001044 **/
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001045hb_bool_t
Behdad Esfahbod875a5cb2012-12-11 14:17:01 -05001046hb_ot_layout_get_size_params (hb_face_t *face,
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301047 unsigned int *design_size, /* OUT. May be NULL */
1048 unsigned int *subfamily_id, /* OUT. May be NULL */
1049 unsigned int *subfamily_name_id, /* OUT. May be NULL */
1050 unsigned int *range_start, /* OUT. May be NULL */
1051 unsigned int *range_end /* OUT. May be NULL */)
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001052{
1053 const OT::GPOS &gpos = _get_gpos (face);
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001054 const hb_tag_t tag = HB_TAG ('s','i','z','e');
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001055
Behdad Esfahbod0dff11f2012-11-30 08:14:20 +02001056 unsigned int num_features = gpos.get_feature_count ();
1057 for (unsigned int i = 0; i < num_features; i++)
1058 {
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001059 if (tag == gpos.get_feature_tag (i))
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001060 {
Behdad Esfahbod0dff11f2012-11-30 08:14:20 +02001061 const OT::Feature &f = gpos.get_feature (i);
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001062 const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001063
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001064 if (params.designSize)
Behdad Esfahbod85bc44b2012-12-12 11:38:49 -05001065 {
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301066 if (design_size) *design_size = params.designSize;
1067 if (subfamily_id) *subfamily_id = params.subfamilyID;
1068 if (subfamily_name_id) *subfamily_name_id = params.subfamilyNameID;
1069 if (range_start) *range_start = params.rangeStart;
1070 if (range_end) *range_end = params.rangeEnd;
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001071
1072 return true;
1073 }
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001074 }
1075 }
1076
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301077 if (design_size) *design_size = 0;
1078 if (subfamily_id) *subfamily_id = 0;
1079 if (subfamily_name_id) *subfamily_name_id = 0;
1080 if (range_start) *range_start = 0;
1081 if (range_end) *range_end = 0;
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001082
Behdad Esfahbodefe252e2012-12-17 23:21:05 -05001083 return false;
Behdad Esfahbodf54cce32012-11-26 14:02:31 +02001084}
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001085
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301086/**
1087 * hb_ot_layout_feature_get_name_ids:
1088 * @face: #hb_face_t to work upon
Behdad Esfahboda5ad8c62018-10-20 16:52:55 -07001089 * @table_tag: table tag to query, "GSUB" or "GPOS".
1090 * @feature_index: index of feature to query.
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301091 * @label_id: (out) (allow-none): The ‘name’ table name ID that specifies a string
1092 * for a user-interface label for this feature. (May be NULL.)
1093 * @tooltip_id: (out) (allow-none): The ‘name’ table name ID that specifies a string
1094 * that an application can use for tooltip text for this
1095 * feature. (May be NULL.)
1096 * @sample_id: (out) (allow-none): The ‘name’ table name ID that specifies sample text
1097 * that illustrates the effect of this feature. (May be NULL.)
1098 * @num_named_parameters: (out) (allow-none): Number of named parameters. (May be zero.)
1099 * @first_param_id: (out) (allow-none): The first ‘name’ table name ID used to specify
1100 * strings for user-interface labels for the feature
1101 * parameters. (Must be zero if numParameters is zero.)
1102 *
Behdad Esfahbodc0a68142018-10-12 16:05:56 -04001103 * Fetches name indices from feature parameters for "Stylistic Set" ('ssXX') or
1104 * "Character Variant" ('cvXX') features.
1105 *
1106 * Return value: true if data found, false otherwise
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301107 *
Behdad Esfahbod3d9a0302018-10-18 05:58:17 -07001108 * Since: 2.0.0
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301109 **/
1110hb_bool_t
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301111hb_ot_layout_feature_get_name_ids (hb_face_t *face,
1112 hb_tag_t table_tag,
1113 unsigned int feature_index,
1114 hb_name_id_t *label_id, /* OUT. May be NULL */
1115 hb_name_id_t *tooltip_id, /* OUT. May be NULL */
1116 hb_name_id_t *sample_id, /* OUT. May be NULL */
1117 unsigned int *num_named_parameters, /* OUT. May be NULL */
1118 hb_name_id_t *first_param_id /* OUT. May be NULL */)
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301119{
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301120 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1121
Behdad Esfahbodc0a68142018-10-12 16:05:56 -04001122 hb_tag_t feature_tag = g.get_feature_tag (feature_index);
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301123 const OT::Feature &f = g.get_feature (feature_index);
1124
1125 const OT::FeatureParams &feature_params = f.get_feature_params ();
1126 if (&feature_params != &Null (OT::FeatureParams))
1127 {
1128 const OT::FeatureParamsStylisticSet& ss_params =
1129 feature_params.get_stylistic_set_params (feature_tag);
1130 if (&ss_params != &Null (OT::FeatureParamsStylisticSet)) /* ssXX */
1131 {
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301132 if (label_id) *label_id = ss_params.uiNameID;
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301133 // ssXX features don't have the rest
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301134 if (tooltip_id) *tooltip_id = HB_NAME_ID_INVALID;
1135 if (sample_id) *sample_id = HB_NAME_ID_INVALID;
1136 if (num_named_parameters) *num_named_parameters = 0;
1137 if (first_param_id) *first_param_id = HB_NAME_ID_INVALID;
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301138 return true;
1139 }
1140 const OT::FeatureParamsCharacterVariants& cv_params =
1141 feature_params.get_character_variants_params (feature_tag);
1142 if (&cv_params != &Null (OT::FeatureParamsCharacterVariants)) /* cvXX */
1143 {
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301144 if (label_id) *label_id = cv_params.featUILableNameID;
1145 if (tooltip_id) *tooltip_id = cv_params.featUITooltipTextNameID;
1146 if (sample_id) *sample_id = cv_params.sampleTextNameID;
1147 if (num_named_parameters) *num_named_parameters = cv_params.numNamedParameters;
1148 if (first_param_id) *first_param_id = cv_params.firstParamUILabelNameID;
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301149 return true;
1150 }
1151 }
1152
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301153 if (label_id) *label_id = HB_NAME_ID_INVALID;
1154 if (tooltip_id) *tooltip_id = HB_NAME_ID_INVALID;
1155 if (sample_id) *sample_id = HB_NAME_ID_INVALID;
1156 if (num_named_parameters) *num_named_parameters = 0;
1157 if (first_param_id) *first_param_id = HB_NAME_ID_INVALID;
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301158 return false;
1159}
1160
1161/**
Behdad Esfahboda5ad8c62018-10-20 16:52:55 -07001162 * hb_ot_layout_feature_get_characters:
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301163 * @face: #hb_face_t to work upon
Behdad Esfahboda5ad8c62018-10-20 16:52:55 -07001164 * @table_tag: table tag to query, "GSUB" or "GPOS".
1165 * @feature_index: index of feature to query.
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301166 * @start_offset: In case the resulting char_count was equal to its input value, there
1167 * is a chance there were more characters on the tag so this API can be
1168 * called with an offset till resulting char_count gets to a number
1169 * lower than input buffer (or consider using just a bigger buffer for
1170 * one shot copying).
HinTak49bdb542018-10-17 00:36:04 +01001171 * @char_count: (inout) (allow-none): The count of characters for which this feature
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301172 * provides glyph variants. (May be zero.)
1173 * @characters: (out) (allow-none): A buffer pointer. The Unicode Scalar Value
1174 * of the characters for which this feature provides glyph variants.
1175 *
Behdad Esfahbodc0a68142018-10-12 16:05:56 -04001176 * Fetches characters listed by designer under feature parameters for "Character
1177 * Variant" ("cvXX") features.
1178 *
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301179 * Return value: Number of total sample characters in the cvXX feature.
1180 *
Behdad Esfahbod3d9a0302018-10-18 05:58:17 -07001181 * Since: 2.0.0
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301182 **/
1183unsigned int
1184hb_ot_layout_feature_get_characters (hb_face_t *face,
1185 hb_tag_t table_tag,
1186 unsigned int feature_index,
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301187 unsigned int start_offset,
1188 unsigned int *char_count, /* IN/OUT. May be NULL */
1189 hb_codepoint_t *characters /* OUT. May be NULL */)
1190{
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301191 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
1192
Behdad Esfahbodc0a68142018-10-12 16:05:56 -04001193 hb_tag_t feature_tag = g.get_feature_tag (feature_index);
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301194 const OT::Feature &f = g.get_feature (feature_index);
1195
1196 const OT::FeatureParams &feature_params = f.get_feature_params ();
1197
1198 const OT::FeatureParamsCharacterVariants& cv_params =
1199 feature_params.get_character_variants_params(feature_tag);
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301200
1201 unsigned int len = 0;
1202 if (char_count && characters && start_offset < cv_params.characters.len)
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301203 {
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301204 len = MIN (cv_params.characters.len - start_offset, *char_count);
1205 for (unsigned int i = 0; i < len; ++i)
1206 characters[i] = cv_params.characters[start_offset + i];
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301207 }
Ebrahim Byagowi63109432018-10-13 14:00:05 +03301208 if (char_count) *char_count = len;
1209 return cv_params.characters.len;
Ebrahim Byagowidc49bd82018-10-12 03:00:59 +03301210}
1211
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001212
1213/*
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001214 * Parts of different types are implemented here such that they have direct
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001215 * access to GSUB/GPOS lookups.
1216 */
1217
1218
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001219struct GSUBProxy
1220{
Behdad Esfahbodfda994e2018-09-07 15:02:57 -04001221 enum { table_index = 0 };
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001222 static const bool inplace = false;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001223 typedef OT::SubstLookup Lookup;
1224
1225 GSUBProxy (hb_face_t *face) :
Behdad Esfahboda2680682018-08-26 15:18:46 -07001226 table (*hb_ot_face_data (face)->GSUB->table),
1227 accels (hb_ot_face_data (face)->GSUB->accels) {}
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001228
1229 const OT::GSUB &table;
Behdad Esfahbod97e59132018-10-10 11:41:05 -04001230 const OT::hb_ot_layout_lookup_accelerator_t *accels;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001231};
1232
1233struct GPOSProxy
1234{
Behdad Esfahbodfda994e2018-09-07 15:02:57 -04001235 enum { table_index = 1 };
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001236 static const bool inplace = true;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001237 typedef OT::PosLookup Lookup;
1238
1239 GPOSProxy (hb_face_t *face) :
Behdad Esfahboda2680682018-08-26 15:18:46 -07001240 table (*hb_ot_face_data (face)->GPOS->table),
1241 accels (hb_ot_face_data (face)->GPOS->accels) {}
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001242
1243 const OT::GPOS &table;
Behdad Esfahbod97e59132018-10-10 11:41:05 -04001244 const OT::hb_ot_layout_lookup_accelerator_t *accels;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001245};
1246
1247
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001248static inline bool
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001249apply_forward (OT::hb_ot_apply_context_t *c,
Behdad Esfahbod78c09bf2018-10-10 11:50:46 -04001250 const OT::hb_ot_layout_lookup_accelerator_t &accel)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001251{
1252 bool ret = false;
1253 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod7185b272018-05-31 20:03:00 -07001254 while (buffer->idx < buffer->len && buffer->successful)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001255 {
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001256 bool applied = false;
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001257 if (accel.may_have (buffer->cur().codepoint) &&
1258 (buffer->cur().mask & c->lookup_mask) &&
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001259 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1260 {
Behdad Esfahbode78549e2018-10-10 11:54:48 -04001261 applied = accel.apply (c);
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001262 }
1263
1264 if (applied)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001265 ret = true;
1266 else
1267 buffer->next_glyph ();
1268 }
1269 return ret;
1270}
1271
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001272static inline bool
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001273apply_backward (OT::hb_ot_apply_context_t *c,
Behdad Esfahbod78c09bf2018-10-10 11:50:46 -04001274 const OT::hb_ot_layout_lookup_accelerator_t &accel)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001275{
1276 bool ret = false;
1277 hb_buffer_t *buffer = c->buffer;
1278 do
1279 {
1280 if (accel.may_have (buffer->cur().codepoint) &&
1281 (buffer->cur().mask & c->lookup_mask) &&
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001282 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1283 {
Behdad Esfahbode78549e2018-10-10 11:54:48 -04001284 if (accel.apply (c))
1285 ret = true;
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001286 }
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001287 /* The reverse lookup doesn't "advance" cursor (for good reason). */
1288 buffer->idx--;
1289
1290 }
1291 while ((int) buffer->idx >= 0);
1292 return ret;
1293}
1294
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001295template <typename Proxy>
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001296static inline void
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001297apply_string (OT::hb_ot_apply_context_t *c,
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001298 const typename Proxy::Lookup &lookup,
Behdad Esfahbod97e59132018-10-10 11:41:05 -04001299 const OT::hb_ot_layout_lookup_accelerator_t &accel)
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001300{
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001301 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001302
Behdad Esfahbod5337db22015-11-06 16:18:09 -08001303 if (unlikely (!buffer->len || !c->lookup_mask))
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001304 return;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001305
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001306 c->set_lookup_props (lookup.get_props ());
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001307
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001308 if (likely (!lookup.is_reverse ()))
1309 {
1310 /* in/out forward substitution/positioning */
1311 if (Proxy::table_index == 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001312 buffer->clear_output ();
1313 buffer->idx = 0;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001314
Behdad Esfahbod640b66c2015-02-19 17:30:05 +03001315 bool ret;
Behdad Esfahbod78c09bf2018-10-10 11:50:46 -04001316 ret = apply_forward (c, accel);
Behdad Esfahbod640b66c2015-02-19 17:30:05 +03001317 if (ret)
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001318 {
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001319 if (!Proxy::inplace)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001320 buffer->swap_buffers ();
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001321 else
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001322 assert (!buffer->has_separate_output ());
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001323 }
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001324 }
1325 else
1326 {
1327 /* in-place backward substitution/positioning */
1328 if (Proxy::table_index == 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001329 buffer->remove_output ();
1330 buffer->idx = buffer->len - 1;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001331
Behdad Esfahbod78c09bf2018-10-10 11:50:46 -04001332 apply_backward (c, accel);
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001333 }
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001334}
1335
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001336template <typename Proxy>
1337inline void hb_ot_map_t::apply (const Proxy &proxy,
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001338 const hb_ot_shape_plan_t *plan,
1339 hb_font_t *font,
1340 hb_buffer_t *buffer) const
1341{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001342 const unsigned int table_index = proxy.table_index;
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001343 unsigned int i = 0;
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001344 OT::hb_ot_apply_context_t c (table_index, font, buffer);
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001345 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001346
1347 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1348 const stage_map_t *stage = &stages[table_index][stage_index];
1349 for (; i < stage->last_lookup; i++)
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001350 {
1351 unsigned int lookup_index = lookups[table_index][i].index;
Behdad Esfahbod0475ef22015-12-18 18:17:07 +00001352 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001353 c.set_lookup_index (lookup_index);
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001354 c.set_lookup_mask (lookups[table_index][i].mask);
1355 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
Behdad Esfahbodcdf1fd02017-07-14 12:43:34 +01001356 c.set_auto_zwnj (lookups[table_index][i].auto_zwnj);
David Corbettc2a75e02018-01-25 14:22:03 -05001357 if (lookups[table_index][i].random)
1358 {
David Corbettb545e272018-02-23 12:22:32 -05001359 c.set_random (true);
David Corbettc2a75e02018-01-25 14:22:03 -05001360 buffer->unsafe_to_break_all ();
1361 }
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001362 apply_string<Proxy> (&c,
1363 proxy.table.get_lookup (lookup_index),
1364 proxy.accels[lookup_index]);
Behdad Esfahbod0475ef22015-12-18 18:17:07 +00001365 (void) buffer->message (font, "end lookup %d", lookup_index);
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001366 }
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001367
1368 if (stage->pause_func)
1369 {
1370 buffer->clear_output ();
1371 stage->pause_func (plan, font, buffer);
1372 }
1373 }
1374}
1375
1376void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1377{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001378 GSUBProxy proxy (font->face);
1379 apply (proxy, plan, font, buffer);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001380}
1381
1382void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1383{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001384 GPOSProxy proxy (font->face);
1385 apply (proxy, plan, font, buffer);
1386}
1387
Behdad Esfahbodcdab20d2018-02-10 15:45:17 -06001388void
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001389hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001390 const OT::SubstLookup &lookup,
Behdad Esfahbod97e59132018-10-10 11:41:05 -04001391 const OT::hb_ot_layout_lookup_accelerator_t &accel)
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001392{
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001393 apply_string<GSUBProxy> (c, lookup, accel);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001394}