blob: d7ededd8db99aa92ab6abdb7f97fd57b25ffbf51 [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 Esfahbod77a1a2b2015-10-09 12:20:58 -040031#include "hb-open-type-private.hh"
Behdad Esfahbod22da7fd2010-05-12 18:23:21 -040032#include "hb-ot-layout-private.hh"
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050033
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020034#include "hb-ot-layout-gdef-table.hh"
35#include "hb-ot-layout-gsub-table.hh"
36#include "hb-ot-layout-gpos-table.hh"
Behdad Esfahbod113393e2017-01-21 15:12:03 -080037#include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise.
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050038
Behdad Esfahbodd2c96812013-05-02 18:18:24 -040039#include "hb-ot-map-private.hh"
40
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040041
Behdad Esfahbodcfe98822012-07-27 03:06:30 -040042HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
Behdad Esfahbod11f4c872012-07-30 02:36:46 -040043
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -040044hb_ot_layout_t *
Behdad Esfahbod266b3442011-05-03 00:35:53 -040045_hb_ot_layout_create (hb_face_t *face)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -050046{
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -040047 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
Behdad Esfahbod3dcbdc22012-07-30 19:31:17 -040048 if (unlikely (!layout))
49 return NULL;
Behdad Esfahbod555d1122010-01-26 12:58:59 -050050
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040051 layout->gdef_blob = OT::Sanitizer<OT::GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
52 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040053
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040054 layout->gsub_blob = OT::Sanitizer<OT::GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
55 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040056
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040057 layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
58 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -040059
Behdad Esfahbod1f810da2017-01-09 23:50:56 -080060 layout->math.init (face);
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -080061 layout->fvar.init (face);
62 layout->avar.init (face);
Frédéric Wang5fbcb992016-08-25 10:47:15 +020063
Behdad Esfahbode23cf902016-02-25 11:11:15 +090064 {
65 /*
66 * The ugly business of blacklisting individual fonts' tables happen here!
67 * See this thread for why we finally had to bend in and do this:
68 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
69 */
70 unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
71 unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
72 unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
73 if (0
Behdad Esfahbod29393882016-04-26 10:35:23 -070074 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
75 || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
76 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
77 || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
78 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
79 || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
80 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
81 || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
82 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
83 || (490 == gdef_len && 41638 == gpos_len && 3046 == gsub_len)
84 /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
85 || (478 == gdef_len && 41902 == gpos_len && 3046 == gsub_len)
Behdad Esfahbode23cf902016-02-25 11:11:15 +090086 )
87 {
88 /* In certain versions of Times New Roman Italic and Bold Italic,
89 * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
90 * glyph class 3 (mark) in GDEF. Nuke the GDEF to avoid zero-width
91 * double-quote. See:
92 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
93 */
94 if (3 == layout->gdef->get_glyph_class (5))
95 layout->gdef = &OT::Null(OT::GDEF);
96 }
jfkthamef3f6c1c2016-06-16 00:10:42 +010097 else if (0
98 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
99 || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
100 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
101 || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
102 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
103 || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
104 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
105 || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
106 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
107 || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
108 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
109 || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
110 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
111 || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
112 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
113 || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
114 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
115 || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
116 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
117 || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
118 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
119 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
120 || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
121 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
122 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
123 || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
Dominik Röttsches67e9fdf2016-09-05 11:27:25 +0300124 /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
125 || (1046 == gdef_len && 17112 == gpos_len && 71788 == gsub_len)
126 /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
127 || (1058 == gdef_len && 17514 == gpos_len && 71794 == gsub_len)
128 /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
129 || (1330 == gdef_len && 57938 == gpos_len && 109904 == gsub_len)
130 /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
131 || (1330 == gdef_len && 58972 == gpos_len && 109904 == gsub_len)
jfkthamef3f6c1c2016-06-16 00:10:42 +0100132 )
133 {
134 /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
135 * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
136 * and the version of Cantarell shipped by Ubuntu 16.04.
137 * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
138 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
139 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
140 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
141 */
142 layout->gdef = &OT::Null(OT::GDEF);
143 }
Behdad Esfahbode23cf902016-02-25 11:11:15 +0900144 }
145
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400146 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
147 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
148
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400149 layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
150 layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
Behdad Esfahbod1336ecd2012-08-01 21:46:36 -0400151
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400152 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
153 (layout->gpos_lookup_count && !layout->gpos_accels)))
Behdad Esfahbod1336ecd2012-08-01 21:46:36 -0400154 {
155 _hb_ot_layout_destroy (layout);
156 return NULL;
157 }
158
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400159 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400160 layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400161 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400162 layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
Behdad Esfahbod1336ecd2012-08-01 21:46:36 -0400163
Behdad Esfahbod0a4399c2010-05-19 15:45:06 -0400164 return layout;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500165}
166
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -0500167void
Behdad Esfahbod266b3442011-05-03 00:35:53 -0400168_hb_ot_layout_destroy (hb_ot_layout_t *layout)
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -0500169{
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400170 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
Behdad Esfahbod395b3592014-12-28 16:03:26 -0800171 layout->gsub_accels[i].fini ();
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400172 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
Behdad Esfahbod395b3592014-12-28 16:03:26 -0800173 layout->gpos_accels[i].fini ();
Behdad Esfahbod45fd9422013-05-02 18:06:51 -0400174
175 free (layout->gsub_accels);
176 free (layout->gpos_accels);
Behdad Esfahbod1336ecd2012-08-01 21:46:36 -0400177
Bradley Grainger89312b72013-06-21 15:02:18 -0700178 hb_blob_destroy (layout->gdef_blob);
179 hb_blob_destroy (layout->gsub_blob);
180 hb_blob_destroy (layout->gpos_blob);
Behdad Esfahbod1f810da2017-01-09 23:50:56 -0800181
182 layout->math.fini ();
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800183 layout->fvar.fini ();
184 layout->avar.fini ();
Frédéric Wang5fbcb992016-08-25 10:47:15 +0200185
Behdad Esfahbod9ea73682010-09-22 17:38:44 -0400186 free (layout);
Behdad Esfahbod2ebb89d2009-07-25 19:09:01 -0400187}
188
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400189static inline const OT::GDEF&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400190_get_gdef (hb_face_t *face)
191{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400192 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400193 return *hb_ot_layout_from_face (face)->gdef;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400194}
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400195static inline const OT::GSUB&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400196_get_gsub (hb_face_t *face)
197{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400198 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400199 return *hb_ot_layout_from_face (face)->gsub;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400200}
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400201static inline const OT::GPOS&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400202_get_gpos (hb_face_t *face)
203{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400204 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
Behdad Esfahbodea278d32012-07-27 02:12:28 -0400205 return *hb_ot_layout_from_face (face)->gpos;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400206}
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400207
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500208/*
209 * GDEF
210 */
211
212hb_bool_t
Behdad Esfahbod52ea4772009-11-06 17:45:38 -0500213hb_ot_layout_has_glyph_classes (hb_face_t *face)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500214{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400215 return _get_gdef (face).has_glyph_classes ();
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500216}
217
Sascha Brawer01c3a882015-06-01 13:22:01 +0200218/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500219 * hb_ot_layout_get_glyph_class:
220 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200221 * Since: 0.9.7
222 **/
Behdad Esfahbod5a08ecf2012-11-16 13:34:29 -0800223hb_ot_layout_glyph_class_t
224hb_ot_layout_get_glyph_class (hb_face_t *face,
225 hb_codepoint_t glyph)
226{
227 return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
228}
Behdad Esfahbod4a2d8442010-11-03 15:28:56 -0400229
Sascha Brawer01c3a882015-06-01 13:22:01 +0200230/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500231 * hb_ot_layout_get_glyphs_in_class:
232 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200233 * Since: 0.9.7
234 **/
Behdad Esfahbod89ca8ee2012-11-16 13:53:40 -0800235void
236hb_ot_layout_get_glyphs_in_class (hb_face_t *face,
237 hb_ot_layout_glyph_class_t klass,
238 hb_set_t *glyphs /* OUT */)
239{
240 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
241}
242
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500243unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400244hb_ot_layout_get_attach_points (hb_face_t *face,
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400245 hb_codepoint_t glyph,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500246 unsigned int start_offset,
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400247 unsigned int *point_count /* IN/OUT */,
248 unsigned int *point_array /* OUT */)
249{
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500250 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400251}
252
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500253unsigned int
Behdad Esfahbode2046742010-10-27 12:32:02 -0400254hb_ot_layout_get_ligature_carets (hb_font_t *font,
Behdad Esfahbode2046742010-10-27 12:32:02 -0400255 hb_direction_t direction,
256 hb_codepoint_t glyph,
257 unsigned int start_offset,
258 unsigned int *caret_count /* IN/OUT */,
259 int *caret_array /* OUT */)
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400260{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400261 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400262}
263
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400264
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500265/*
266 * GSUB/GPOS
267 */
268
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400269static const OT::GSUBGPOS&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400270get_gsubgpos_table (hb_face_t *face,
271 hb_tag_t table_tag)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500272{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400273 switch (table_tag) {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400274 case HB_OT_TAG_GSUB: return _get_gsub (face);
275 case HB_OT_TAG_GPOS: return _get_gpos (face);
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400276 default: return OT::Null(OT::GSUBGPOS);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500277 }
278}
279
280
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500281unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400282hb_ot_layout_table_get_script_tags (hb_face_t *face,
283 hb_tag_t table_tag,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500284 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400285 unsigned int *script_count /* IN/OUT */,
286 hb_tag_t *script_tags /* OUT */)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500287{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400288 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500289
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500290 return g.get_script_tags (start_offset, script_count, script_tags);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500291}
292
Behdad Esfahbodeb45c0a2013-01-16 22:07:50 -0600293#define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
294
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500295hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400296hb_ot_layout_table_find_script (hb_face_t *face,
297 hb_tag_t table_tag,
298 hb_tag_t script_tag,
299 unsigned int *script_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500300{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400301 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
302 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500303
304 if (g.find_script_index (script_tag, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400305 return true;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500306
307 /* try finding 'DFLT' */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500308 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400309 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500310
Behdad Esfahboddca8aff2010-09-28 16:25:23 -0400311 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
312 * including many versions of DejaVu Sans Mono! */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500313 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400314 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500315
Behdad Esfahbodeb45c0a2013-01-16 22:07:50 -0600316 /* try with 'latn'; some old fonts put their features there even though
317 they're really trying to support Thai, for example :( */
318 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
319 return false;
320
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500321 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400322 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500323}
324
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100325hb_bool_t
326hb_ot_layout_table_choose_script (hb_face_t *face,
327 hb_tag_t table_tag,
328 const hb_tag_t *script_tags,
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400329 unsigned int *script_index,
330 hb_tag_t *chosen_script)
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100331{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400332 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
333 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100334
335 while (*script_tags)
336 {
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400337 if (g.find_script_index (*script_tags, script_index)) {
338 if (chosen_script)
339 *chosen_script = *script_tags;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400340 return true;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400341 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100342 script_tags++;
343 }
344
345 /* try finding 'DFLT' */
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400346 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
347 if (chosen_script)
348 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400349 return false;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400350 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100351
352 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400353 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
354 if (chosen_script)
355 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400356 return false;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400357 }
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100358
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500359 /* try with 'latn'; some old fonts put their features there even though
360 they're really trying to support Thai, for example :( */
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500361 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
362 if (chosen_script)
363 *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400364 return false;
Behdad Esfahbod71632c92012-01-22 15:31:44 -0500365 }
366
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100367 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbodc47a31f2011-07-30 20:57:01 -0400368 if (chosen_script)
369 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400370 return false;
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100371}
372
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500373unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400374hb_ot_layout_table_get_feature_tags (hb_face_t *face,
375 hb_tag_t table_tag,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500376 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400377 unsigned int *feature_count /* IN/OUT */,
378 hb_tag_t *feature_tags /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500379{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400380 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500381
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500382 return g.get_feature_tags (start_offset, feature_count, feature_tags);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500383}
384
Behdad Esfahbod0f98fe82015-07-23 11:52:11 +0100385hb_bool_t
386hb_ot_layout_table_find_feature (hb_face_t *face,
387 hb_tag_t table_tag,
388 hb_tag_t feature_tag,
389 unsigned int *feature_index)
390{
391 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
392 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
393
394 unsigned int num_features = g.get_feature_count ();
395 for (unsigned int i = 0; i < num_features; i++)
396 {
397 if (feature_tag == g.get_feature_tag (i)) {
398 if (feature_index) *feature_index = i;
399 return true;
400 }
401 }
402
403 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
404 return false;
405}
406
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500407
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500408unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400409hb_ot_layout_script_get_language_tags (hb_face_t *face,
410 hb_tag_t table_tag,
411 unsigned int script_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500412 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400413 unsigned int *language_count /* IN/OUT */,
414 hb_tag_t *language_tags /* OUT */)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500415{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400416 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500417
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500418 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500419}
420
421hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400422hb_ot_layout_script_find_language (hb_face_t *face,
423 hb_tag_t table_tag,
424 unsigned int script_index,
425 hb_tag_t language_tag,
426 unsigned int *language_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500427{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400428 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
429 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500430
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500431 if (s.find_lang_sys_index (language_tag, language_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400432 return true;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500433
434 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500435 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400436 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500437
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500438 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400439 return false;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500440}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500441
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500442hb_bool_t
Behdad Esfahbod911ca382014-06-24 10:20:36 -0600443hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
444 hb_tag_t table_tag,
445 unsigned int script_index,
446 unsigned int language_index,
447 unsigned int *feature_index)
448{
449 return hb_ot_layout_language_get_required_feature (face,
450 table_tag,
451 script_index,
452 language_index,
453 feature_index,
454 NULL);
455}
456
Sascha Brawer01c3a882015-06-01 13:22:01 +0200457/**
Behdad Esfahbod35d18582015-11-26 19:30:37 -0500458 * hb_ot_layout_language_get_required_feature:
459 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200460 * Since: 0.9.30
461 **/
Behdad Esfahbod911ca382014-06-24 10:20:36 -0600462hb_bool_t
Jonathan Kewda132932014-04-27 14:05:24 +0100463hb_ot_layout_language_get_required_feature (hb_face_t *face,
464 hb_tag_t table_tag,
465 unsigned int script_index,
466 unsigned int language_index,
467 unsigned int *feature_index,
468 hb_tag_t *feature_tag)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500469{
Jonathan Kewda132932014-04-27 14:05:24 +0100470 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
471 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500472
Jonathan Kewda132932014-04-27 14:05:24 +0100473 unsigned int index = l.get_required_feature_index ();
474 if (feature_index) *feature_index = index;
475 if (feature_tag) *feature_tag = g.get_feature_tag (index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500476
477 return l.has_required_feature ();
478}
479
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500480unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400481hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
482 hb_tag_t table_tag,
483 unsigned int script_index,
484 unsigned int language_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500485 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400486 unsigned int *feature_count /* IN/OUT */,
487 unsigned int *feature_indexes /* OUT */)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500488{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400489 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
490 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500491
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500492 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500493}
494
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500495unsigned int
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400496hb_ot_layout_language_get_feature_tags (hb_face_t *face,
497 hb_tag_t table_tag,
498 unsigned int script_index,
499 unsigned int language_index,
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500500 unsigned int start_offset,
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400501 unsigned int *feature_count /* IN/OUT */,
502 hb_tag_t *feature_tags /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500503{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400504 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
505 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500506
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400507 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500508 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400509
Behdad Esfahbod98977492009-08-27 01:32:17 -0400510 if (feature_tags) {
511 unsigned int count = *feature_count;
512 for (unsigned int i = 0; i < count; i++)
513 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
514 }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400515
516 return ret;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500517}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500518
519
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500520hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400521hb_ot_layout_language_find_feature (hb_face_t *face,
522 hb_tag_t table_tag,
523 unsigned int script_index,
524 unsigned int language_index,
525 hb_tag_t feature_tag,
526 unsigned int *feature_index)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500527{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400528 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
529 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
530 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500531
Behdad Esfahbodf4c95142009-05-17 04:59:56 -0400532 unsigned int num_features = l.get_feature_count ();
533 for (unsigned int i = 0; i < num_features; i++) {
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500534 unsigned int f_index = l.get_feature_index (i);
535
536 if (feature_tag == g.get_feature_tag (f_index)) {
537 if (feature_index) *feature_index = f_index;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400538 return true;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500539 }
540 }
541
542 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400543 return false;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500544}
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500545
Sascha Brawer01c3a882015-06-01 13:22:01 +0200546/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400547 * hb_ot_layout_feature_get_lookups:
548 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200549 * Since: 0.9.7
550 **/
Behdad Esfahbode21899b2009-11-04 16:36:14 -0500551unsigned int
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800552hb_ot_layout_feature_get_lookups (hb_face_t *face,
553 hb_tag_t table_tag,
554 unsigned int feature_index,
555 unsigned int start_offset,
556 unsigned int *lookup_count /* IN/OUT */,
557 unsigned int *lookup_indexes /* OUT */)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500558{
Behdad Esfahbodec87ba92016-09-10 03:53:11 -0700559 return hb_ot_layout_feature_with_variations_get_lookups (face,
560 table_tag,
561 feature_index,
562 HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
563 start_offset,
564 lookup_count,
565 lookup_indexes);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500566}
567
Sascha Brawer01c3a882015-06-01 13:22:01 +0200568/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400569 * hb_ot_layout_table_get_lookup_count:
570 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200571 * Since: 0.9.22
572 **/
Behdad Esfahbod27674b42013-10-03 14:54:50 -0400573unsigned int
574hb_ot_layout_table_get_lookup_count (hb_face_t *face,
575 hb_tag_t table_tag)
576{
577 switch (table_tag)
578 {
579 case HB_OT_TAG_GSUB:
580 {
581 return hb_ot_layout_from_face (face)->gsub_lookup_count;
582 }
583 case HB_OT_TAG_GPOS:
584 {
585 return hb_ot_layout_from_face (face)->gpos_lookup_count;
586 }
587 }
Behdad Esfahbod4e6e53d2013-10-14 13:06:36 +0200588 return 0;
Behdad Esfahbod27674b42013-10-03 14:54:50 -0400589}
590
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500591static void
592_hb_ot_layout_collect_lookups_lookups (hb_face_t *face,
593 hb_tag_t table_tag,
594 unsigned int feature_index,
595 hb_set_t *lookup_indexes /* OUT */)
596{
597 unsigned int lookup_indices[32];
598 unsigned int offset, len;
599
600 offset = 0;
601 do {
602 len = ARRAY_LENGTH (lookup_indices);
603 hb_ot_layout_feature_get_lookups (face,
604 table_tag,
605 feature_index,
606 offset, &len,
607 lookup_indices);
608
609 for (unsigned int i = 0; i < len; i++)
610 lookup_indexes->add (lookup_indices[i]);
611
612 offset += len;
613 } while (len == ARRAY_LENGTH (lookup_indices));
614}
615
616static void
617_hb_ot_layout_collect_lookups_features (hb_face_t *face,
618 hb_tag_t table_tag,
619 unsigned int script_index,
620 unsigned int language_index,
621 const hb_tag_t *features,
622 hb_set_t *lookup_indexes /* OUT */)
623{
624 if (!features)
625 {
Behdad Esfahbod5f85c802013-06-26 20:14:18 -0400626 unsigned int required_feature_index;
Jonathan Kewda132932014-04-27 14:05:24 +0100627 if (hb_ot_layout_language_get_required_feature (face,
628 table_tag,
629 script_index,
630 language_index,
631 &required_feature_index,
632 NULL))
Behdad Esfahbod5f85c802013-06-26 20:14:18 -0400633 _hb_ot_layout_collect_lookups_lookups (face,
634 table_tag,
635 required_feature_index,
636 lookup_indexes);
637
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500638 /* All features */
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600639 unsigned int feature_indices[32];
640 unsigned int offset, len;
641
642 offset = 0;
643 do {
644 len = ARRAY_LENGTH (feature_indices);
645 hb_ot_layout_language_get_feature_indexes (face,
646 table_tag,
647 script_index,
648 language_index,
649 offset, &len,
650 feature_indices);
651
652 for (unsigned int i = 0; i < len; i++)
653 _hb_ot_layout_collect_lookups_lookups (face,
654 table_tag,
655 feature_indices[i],
656 lookup_indexes);
657
658 offset += len;
659 } while (len == ARRAY_LENGTH (feature_indices));
660 }
661 else
662 {
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500663 for (; *features; features++)
664 {
665 unsigned int feature_index;
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600666 if (hb_ot_layout_language_find_feature (face,
667 table_tag,
668 script_index,
669 language_index,
670 *features,
671 &feature_index))
672 _hb_ot_layout_collect_lookups_lookups (face,
673 table_tag,
674 feature_index,
675 lookup_indexes);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500676 }
677 }
678}
679
680static void
681_hb_ot_layout_collect_lookups_languages (hb_face_t *face,
682 hb_tag_t table_tag,
683 unsigned int script_index,
684 const hb_tag_t *languages,
685 const hb_tag_t *features,
686 hb_set_t *lookup_indexes /* OUT */)
687{
Behdad Esfahbodf0c82412013-01-03 00:07:16 -0600688 _hb_ot_layout_collect_lookups_features (face,
689 table_tag,
690 script_index,
691 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
692 features,
693 lookup_indexes);
694
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500695 if (!languages)
696 {
697 /* All languages */
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600698 unsigned int count = hb_ot_layout_script_get_language_tags (face,
699 table_tag,
700 script_index,
701 0, NULL, NULL);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500702 for (unsigned int language_index = 0; language_index < count; language_index++)
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600703 _hb_ot_layout_collect_lookups_features (face,
704 table_tag,
705 script_index,
706 language_index,
707 features,
708 lookup_indexes);
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600709 }
710 else
711 {
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500712 for (; *languages; languages++)
713 {
714 unsigned int language_index;
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600715 if (hb_ot_layout_script_find_language (face,
716 table_tag,
717 script_index,
718 *languages,
719 &language_index))
720 _hb_ot_layout_collect_lookups_features (face,
721 table_tag,
722 script_index,
723 language_index,
724 features,
725 lookup_indexes);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500726 }
727 }
728}
729
Sascha Brawer01c3a882015-06-01 13:22:01 +0200730/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400731 * hb_ot_layout_collect_lookups:
732 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200733 * Since: 0.9.8
734 **/
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500735void
736hb_ot_layout_collect_lookups (hb_face_t *face,
737 hb_tag_t table_tag,
738 const hb_tag_t *scripts,
739 const hb_tag_t *languages,
740 const hb_tag_t *features,
741 hb_set_t *lookup_indexes /* OUT */)
742{
743 if (!scripts)
744 {
745 /* All scripts */
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600746 unsigned int count = hb_ot_layout_table_get_script_tags (face,
747 table_tag,
748 0, NULL, NULL);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500749 for (unsigned int script_index = 0; script_index < count; script_index++)
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600750 _hb_ot_layout_collect_lookups_languages (face,
751 table_tag,
752 script_index,
753 languages,
754 features,
755 lookup_indexes);
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600756 }
757 else
758 {
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500759 for (; *scripts; scripts++)
760 {
761 unsigned int script_index;
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600762 if (hb_ot_layout_table_find_script (face,
763 table_tag,
764 *scripts,
765 &script_index))
766 _hb_ot_layout_collect_lookups_languages (face,
767 table_tag,
768 script_index,
769 languages,
770 features,
771 lookup_indexes);
Behdad Esfahboda88e7162012-11-24 02:31:02 -0500772 }
773 }
774}
775
Sascha Brawer01c3a882015-06-01 13:22:01 +0200776/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400777 * hb_ot_layout_lookup_collect_glyphs:
778 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200779 * Since: 0.9.7
780 **/
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800781void
782hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
783 hb_tag_t table_tag,
784 unsigned int lookup_index,
785 hb_set_t *glyphs_before, /* OUT. May be NULL */
786 hb_set_t *glyphs_input, /* OUT. May be NULL */
787 hb_set_t *glyphs_after, /* OUT. May be NULL */
788 hb_set_t *glyphs_output /* OUT. May be NULL */)
789{
790 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
791
Behdad Esfahbod733e8c02013-01-03 00:00:23 -0600792 OT::hb_collect_glyphs_context_t c (face,
793 glyphs_before,
794 glyphs_input,
795 glyphs_after,
796 glyphs_output);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800797
Behdad Esfahbod15e9e4e2013-01-03 00:04:40 -0600798 switch (table_tag)
799 {
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800800 case HB_OT_TAG_GSUB:
801 {
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500802 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
Behdad Esfahbod780cd932013-05-03 17:33:16 -0400803 l.collect_glyphs (&c);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800804 return;
805 }
806 case HB_OT_TAG_GPOS:
807 {
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500808 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
Behdad Esfahbod780cd932013-05-03 17:33:16 -0400809 l.collect_glyphs (&c);
Behdad Esfahbode8cfdd72012-11-16 19:07:06 -0800810 return;
811 }
812 }
813}
814
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400815
Behdad Esfahbod30c42b62016-09-10 03:32:39 -0700816/* Variations support */
817
818hb_bool_t
819hb_ot_layout_table_find_feature_variations (hb_face_t *face,
820 hb_tag_t table_tag,
821 const int *coords,
822 unsigned int num_coords,
823 unsigned int *variations_index /* out */)
824{
825 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
826
827 return g.find_variations_index (coords, num_coords, variations_index);
828}
829
Behdad Esfahbodec87ba92016-09-10 03:53:11 -0700830unsigned int
831hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face,
832 hb_tag_t table_tag,
833 unsigned int feature_index,
834 unsigned int variations_index,
835 unsigned int start_offset,
836 unsigned int *lookup_count /* IN/OUT */,
837 unsigned int *lookup_indexes /* OUT */)
838{
839 ASSERT_STATIC (OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX);
840 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
841
842 const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
843
844 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
845}
846
Behdad Esfahbod30c42b62016-09-10 03:32:39 -0700847
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400848/*
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400849 * OT::GSUB
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400850 */
851
852hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400853hb_ot_layout_has_substitution (hb_face_t *face)
854{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400855 return &_get_gsub (face) != &OT::Null(OT::GSUB);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400856}
857
Sascha Brawer01c3a882015-06-01 13:22:01 +0200858/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400859 * hb_ot_layout_lookup_would_substitute:
860 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200861 * Since: 0.9.7
862 **/
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400863hb_bool_t
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800864hb_ot_layout_lookup_would_substitute (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400865 unsigned int lookup_index,
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400866 const hb_codepoint_t *glyphs,
867 unsigned int glyphs_length,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400868 hb_bool_t zero_context)
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400869{
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400870 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800871 return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
Behdad Esfahbode72b3602012-07-19 14:35:23 -0400872}
873
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400874hb_bool_t
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800875hb_ot_layout_lookup_would_substitute_fast (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400876 unsigned int lookup_index,
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400877 const hb_codepoint_t *glyphs,
878 unsigned int glyphs_length,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400879 hb_bool_t zero_context)
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400880{
Behdad Esfahbod6f761132012-08-02 04:00:31 -0400881 if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
Behdad Esfahbodea512f72015-11-26 19:22:22 -0500882 OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
Behdad Esfahbod2bd9fe32012-09-04 15:15:19 -0400883
884 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
885
Behdad Esfahbod241eac92015-01-28 20:55:42 -0800886 return l.would_apply (&c, &hb_ot_layout_from_face (face)->gsub_accels[lookup_index]);
Behdad Esfahbodf8603662012-07-30 02:38:39 -0400887}
888
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400889void
Behdad Esfahbodafbcc242012-08-02 08:36:40 -0400890hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400891{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400892 OT::GSUB::substitute_start (font, buffer);
Behdad Esfahbod46d6a212011-05-11 22:33:13 -0400893}
894
Sascha Brawer01c3a882015-06-01 13:22:01 +0200895/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400896 * hb_ot_layout_lookup_substitute_closure:
897 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200898 * Since: 0.9.7
899 **/
Behdad Esfahbod5caece62012-04-23 23:03:12 -0400900void
Behdad Esfahbod362a9902012-11-15 14:57:31 -0800901hb_ot_layout_lookup_substitute_closure (hb_face_t *face,
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400902 unsigned int lookup_index,
903 hb_set_t *glyphs)
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400904{
Behdad Esfahbod9b346772012-11-23 17:55:40 -0500905 OT::hb_closure_context_t c (face, glyphs);
906
907 const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
908
909 l.closure (&c);
Behdad Esfahbodf94b0aa2012-04-23 13:04:38 -0400910}
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400911
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400912/*
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400913 * OT::GPOS
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400914 */
915
916hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400917hb_ot_layout_has_positioning (hb_face_t *face)
918{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400919 return &_get_gpos (face) != &OT::Null(OT::GPOS);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400920}
921
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -0400922void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400923hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -0400924{
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400925 OT::GPOS::position_start (font, buffer);
Behdad Esfahbod8f0d7e02011-04-15 18:59:56 -0400926}
927
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -0500928void
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +0700929hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -0500930{
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +0700931 OT::GPOS::position_finish_advances (font, buffer);
932}
933
934void
935hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
936{
937 OT::GPOS::position_finish_offsets (font, buffer);
Behdad Esfahbod9db8ad72009-11-06 16:47:31 -0500938}
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200939
Sascha Brawer01c3a882015-06-01 13:22:01 +0200940/**
Khaled Hosnyd7bf9d02015-12-29 02:23:24 +0400941 * hb_ot_layout_get_size_params:
942 *
Behdad Esfahbodb8811422015-09-03 15:53:22 +0430943 * Since: 0.9.10
Sascha Brawer01c3a882015-06-01 13:22:01 +0200944 **/
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200945hb_bool_t
Behdad Esfahbod875a5cb2012-12-11 14:17:01 -0500946hb_ot_layout_get_size_params (hb_face_t *face,
947 unsigned int *design_size, /* OUT. May be NULL */
948 unsigned int *subfamily_id, /* OUT. May be NULL */
949 unsigned int *subfamily_name_id, /* OUT. May be NULL */
950 unsigned int *range_start, /* OUT. May be NULL */
951 unsigned int *range_end /* OUT. May be NULL */)
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200952{
953 const OT::GPOS &gpos = _get_gpos (face);
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500954 const hb_tag_t tag = HB_TAG ('s','i','z','e');
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200955
Behdad Esfahbod0dff11f2012-11-30 08:14:20 +0200956 unsigned int num_features = gpos.get_feature_count ();
957 for (unsigned int i = 0; i < num_features; i++)
958 {
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500959 if (tag == gpos.get_feature_tag (i))
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200960 {
Behdad Esfahbod0dff11f2012-11-30 08:14:20 +0200961 const OT::Feature &f = gpos.get_feature (i);
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500962 const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200963
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500964 if (params.designSize)
Behdad Esfahbod85bc44b2012-12-12 11:38:49 -0500965 {
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500966#define PARAM(a, A) if (a) *a = params.A
Behdad Esfahbod85bc44b2012-12-12 11:38:49 -0500967 PARAM (design_size, designSize);
968 PARAM (subfamily_id, subfamilyID);
969 PARAM (subfamily_name_id, subfamilyNameID);
970 PARAM (range_start, rangeStart);
971 PARAM (range_end, rangeEnd);
Behdad Esfahbod85bc44b2012-12-12 11:38:49 -0500972#undef PARAM
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500973
974 return true;
975 }
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200976 }
977 }
978
Behdad Esfahbod875a5cb2012-12-11 14:17:01 -0500979#define PARAM(a, A) if (a) *a = 0
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500980 PARAM (design_size, designSize);
981 PARAM (subfamily_id, subfamilyID);
982 PARAM (subfamily_name_id, subfamilyNameID);
983 PARAM (range_start, rangeStart);
984 PARAM (range_end, rangeEnd);
Behdad Esfahbod875a5cb2012-12-11 14:17:01 -0500985#undef PARAM
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200986
Behdad Esfahbodefe252e2012-12-17 23:21:05 -0500987 return false;
Behdad Esfahbodf54cce32012-11-26 14:02:31 +0200988}
Behdad Esfahbodd2c96812013-05-02 18:18:24 -0400989
990
991/*
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400992 * Parts of different types are implemented here such that they have direct
Behdad Esfahbodd2c96812013-05-02 18:18:24 -0400993 * access to GSUB/GPOS lookups.
994 */
995
996
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400997struct GSUBProxy
998{
999 static const unsigned int table_index = 0;
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001000 static const bool inplace = false;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001001 typedef OT::SubstLookup Lookup;
1002
1003 GSUBProxy (hb_face_t *face) :
1004 table (*hb_ot_layout_from_face (face)->gsub),
1005 accels (hb_ot_layout_from_face (face)->gsub_accels) {}
1006
1007 const OT::GSUB &table;
1008 const hb_ot_layout_lookup_accelerator_t *accels;
1009};
1010
1011struct GPOSProxy
1012{
1013 static const unsigned int table_index = 1;
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001014 static const bool inplace = true;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001015 typedef OT::PosLookup Lookup;
1016
1017 GPOSProxy (hb_face_t *face) :
1018 table (*hb_ot_layout_from_face (face)->gpos),
1019 accels (hb_ot_layout_from_face (face)->gpos_accels) {}
1020
1021 const OT::GPOS &table;
1022 const hb_ot_layout_lookup_accelerator_t *accels;
1023};
1024
1025
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001026struct hb_get_subtables_context_t :
1027 OT::hb_dispatch_context_t<hb_get_subtables_context_t, hb_void_t, HB_DEBUG_APPLY>
1028{
1029 template <typename Type>
1030 static inline bool apply_to (const void *obj, OT::hb_apply_context_t *c)
1031 {
1032 const Type *typed_obj = (const Type *) obj;
1033 return typed_obj->apply (c);
1034 }
1035
1036 typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_apply_context_t *c);
1037
1038 struct hb_applicable_t
1039 {
1040 inline void init (const void *obj_, hb_apply_func_t apply_func_)
1041 {
1042 obj = obj_;
1043 apply_func = apply_func_;
1044 }
1045
1046 inline bool apply (OT::hb_apply_context_t *c) const { return apply_func (obj, c); }
1047
1048 private:
1049 const void *obj;
1050 hb_apply_func_t apply_func;
1051 };
1052
1053 typedef hb_auto_array_t<hb_applicable_t> array_t;
1054
1055 /* Dispatch interface. */
1056 inline const char *get_name (void) { return "GET_SUBTABLES"; }
1057 template <typename T>
1058 inline return_t dispatch (const T &obj)
1059 {
1060 hb_applicable_t *entry = array.push();
1061 if (likely (entry))
1062 entry->init (&obj, apply_to<T>);
1063 return HB_VOID;
1064 }
1065 static return_t default_return_value (void) { return HB_VOID; }
1066 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
1067
1068 hb_get_subtables_context_t (array_t &array_) :
1069 array (array_),
1070 debug_depth (0) {}
1071
1072 array_t &array;
1073 unsigned int debug_depth;
1074};
1075
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001076static inline bool
1077apply_forward (OT::hb_apply_context_t *c,
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001078 const hb_ot_layout_lookup_accelerator_t &accel,
1079 const hb_get_subtables_context_t::array_t &subtables)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001080{
1081 bool ret = false;
1082 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodabadc172015-11-18 17:52:08 -08001083 while (buffer->idx < buffer->len && !buffer->in_error)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001084 {
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001085 bool applied = false;
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001086 if (accel.may_have (buffer->cur().codepoint) &&
1087 (buffer->cur().mask & c->lookup_mask) &&
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001088 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1089 {
1090 for (unsigned int i = 0; i < subtables.len; i++)
1091 if (subtables[i].apply (c))
1092 {
1093 applied = true;
1094 break;
1095 }
1096 }
1097
1098 if (applied)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001099 ret = true;
1100 else
1101 buffer->next_glyph ();
1102 }
1103 return ret;
1104}
1105
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001106static inline bool
1107apply_backward (OT::hb_apply_context_t *c,
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001108 const hb_ot_layout_lookup_accelerator_t &accel,
1109 const hb_get_subtables_context_t::array_t &subtables)
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001110{
1111 bool ret = false;
1112 hb_buffer_t *buffer = c->buffer;
1113 do
1114 {
1115 if (accel.may_have (buffer->cur().codepoint) &&
1116 (buffer->cur().mask & c->lookup_mask) &&
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001117 c->check_glyph_property (&buffer->cur(), c->lookup_props))
1118 {
1119 for (unsigned int i = 0; i < subtables.len; i++)
1120 if (subtables[i].apply (c))
1121 {
1122 ret = true;
1123 break;
1124 }
1125 }
Behdad Esfahbode2f50f22015-02-19 17:15:05 +03001126 /* The reverse lookup doesn't "advance" cursor (for good reason). */
1127 buffer->idx--;
1128
1129 }
1130 while ((int) buffer->idx >= 0);
1131 return ret;
1132}
1133
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001134template <typename Proxy>
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001135static inline void
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001136apply_string (OT::hb_apply_context_t *c,
1137 const typename Proxy::Lookup &lookup,
1138 const hb_ot_layout_lookup_accelerator_t &accel)
1139{
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001140 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001141
Behdad Esfahbod5337db22015-11-06 16:18:09 -08001142 if (unlikely (!buffer->len || !c->lookup_mask))
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001143 return;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001144
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001145 c->set_lookup_props (lookup.get_props ());
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001146
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001147 hb_get_subtables_context_t::array_t subtables;
1148 hb_get_subtables_context_t c_get_subtables (subtables);
1149 lookup.dispatch (&c_get_subtables);
1150
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001151 if (likely (!lookup.is_reverse ()))
1152 {
1153 /* in/out forward substitution/positioning */
1154 if (Proxy::table_index == 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001155 buffer->clear_output ();
1156 buffer->idx = 0;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001157
Behdad Esfahbod640b66c2015-02-19 17:30:05 +03001158 bool ret;
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001159 ret = apply_forward (c, accel, subtables);
Behdad Esfahbod640b66c2015-02-19 17:30:05 +03001160 if (ret)
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001161 {
Behdad Esfahbod6ffc0072013-10-28 19:26:02 +01001162 if (!Proxy::inplace)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001163 buffer->swap_buffers ();
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001164 else
Behdad Esfahbod1d4a3282015-02-19 11:33:30 +03001165 assert (!buffer->has_separate_output ());
Behdad Esfahbod9d9e72e2013-05-03 18:10:10 -04001166 }
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001167 }
1168 else
1169 {
1170 /* in-place backward substitution/positioning */
1171 if (Proxy::table_index == 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001172 buffer->remove_output ();
1173 buffer->idx = buffer->len - 1;
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001174
Behdad Esfahbod3e704522016-01-11 17:38:41 +00001175 apply_backward (c, accel, subtables);
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001176 }
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001177}
1178
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001179template <typename Proxy>
1180inline void hb_ot_map_t::apply (const Proxy &proxy,
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001181 const hb_ot_shape_plan_t *plan,
1182 hb_font_t *font,
1183 hb_buffer_t *buffer) const
1184{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001185 const unsigned int table_index = proxy.table_index;
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001186 unsigned int i = 0;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001187 OT::hb_apply_context_t c (table_index, font, buffer);
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001188 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001189
1190 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1191 const stage_map_t *stage = &stages[table_index][stage_index];
1192 for (; i < stage->last_lookup; i++)
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001193 {
1194 unsigned int lookup_index = lookups[table_index][i].index;
Behdad Esfahbod0475ef22015-12-18 18:17:07 +00001195 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001196 c.set_lookup_index (lookup_index);
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001197 c.set_lookup_mask (lookups[table_index][i].mask);
1198 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001199 apply_string<Proxy> (&c,
1200 proxy.table.get_lookup (lookup_index),
1201 proxy.accels[lookup_index]);
Behdad Esfahbod0475ef22015-12-18 18:17:07 +00001202 (void) buffer->message (font, "end lookup %d", lookup_index);
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001203 }
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001204
1205 if (stage->pause_func)
1206 {
1207 buffer->clear_output ();
1208 stage->pause_func (plan, font, buffer);
1209 }
1210 }
1211}
1212
1213void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1214{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001215 GSUBProxy proxy (font->face);
1216 apply (proxy, plan, font, buffer);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001217}
1218
1219void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1220{
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -04001221 GPOSProxy proxy (font->face);
1222 apply (proxy, plan, font, buffer);
1223}
1224
1225HB_INTERNAL void
1226hb_ot_layout_substitute_lookup (OT::hb_apply_context_t *c,
1227 const OT::SubstLookup &lookup,
1228 const hb_ot_layout_lookup_accelerator_t &accel)
1229{
Behdad Esfahbod45f3d982013-05-03 17:49:44 -04001230 apply_string<GSUBProxy> (c, lookup, accel);
Behdad Esfahbodd2c96812013-05-02 18:18:24 -04001231}