Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
2  * Copyright © 1998-2004  David Turner and Werner Lemberg
3  * Copyright © 2006  Behdad Esfahbod
4  * Copyright © 2007,2008,2009  Red Hat, Inc.
5  * Copyright © 2012,2013  Google, Inc.
6  *
7  *  This is part of HarfBuzz, a text shaping library.
8  *
9  * Permission is hereby granted, without written agreement and without
10  * license or royalty fees, to use, copy, modify, and distribute this
11  * software and its documentation for any purpose, provided that the
12  * above copyright notice and the following two paragraphs appear in
13  * all copies of this software.
14  *
15  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
16  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
17  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19  * DAMAGE.
20  *
21  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
24  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26  *
27  * Red Hat Author(s): Behdad Esfahbod
28  * Google Author(s): Behdad Esfahbod
29  */
30
31 #include "hb-open-type-private.hh"
32 #include "hb-ot-layout-private.hh"
33
34 #include "hb-ot-layout-base-table.hh"
35 #include "hb-ot-layout-gdef-table.hh"
36 #include "hb-ot-layout-gsub-table.hh"
37 #include "hb-ot-layout-gpos-table.hh"
38 #include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise.
39 #include "hb-ot-name-table.hh" // Just so we compile it; unused otherwise.
40 #include "hb-ot-color-colr-table.hh"
41 #include "hb-ot-color-cpal-table.hh"
42
43 #include "hb-ot-map-private.hh"
44
45
46 #ifndef HB_NO_VISIBILITY
47 const void * const OT::_hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] = {};
48 #endif
49
50
51 hb_ot_layout_t *
52 _hb_ot_layout_create (hb_face_t *face)
53 {
54   hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
55   if (unlikely (!layout))
56     return nullptr;
57
58   layout->gdef_blob = OT::Sanitizer<OT::GDEF>().sanitize (face->reference_table (HB_OT_TAG_GDEF));
59   layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
60
61   layout->gsub_blob = OT::Sanitizer<OT::GSUB>().sanitize (face->reference_table (HB_OT_TAG_GSUB));
62   layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
63
64   layout->gpos_blob = OT::Sanitizer<OT::GPOS>().sanitize (face->reference_table (HB_OT_TAG_GPOS));
65   layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
66
67   layout->base.init (face);
68   layout->colr.init (face);
69   layout->cpal.init (face);
70   layout->math.init (face);
71   layout->fvar.init (face);
72   layout->avar.init (face);
73   layout->ankr.init (face);
74   layout->kerx.init (face);
75   layout->morx.init (face);
76   layout->trak.init (face);
77
78   {
79     /*
80      * The ugly business of blacklisting individual fonts' tables happen here!
81      * See this thread for why we finally had to bend in and do this:
82      * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
83      */
84     unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
85     unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
86     unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
87     if (0
88       /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
89       || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
90       /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
91       || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
92       /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
93       || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
94       /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
95       || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
96       /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
97       || (490 == gdef_len && 41638 == gpos_len && 3046 == gsub_len)
98       /* sha1sum:ec0f5a8751845355b7c3271d11f9918a966cb8c9 OS X 10.11.3 Times New Roman Bold Italic.ttf */
99       || (478 == gdef_len && 41902 == gpos_len && 3046 == gsub_len)
100     )
101     {
102       /* In certain versions of Times New Roman Italic and Bold Italic,
103        * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
104        * glyph class 3 (mark) in GDEF.  Nuke the GDEF to avoid zero-width
105        * double-quote.  See:
106        * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
107        */
108      if (3 == layout->gdef->get_glyph_class (5))
109        layout->gdef = &OT::Null(OT::GDEF);
110     }
111     else if (0
112       /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c  tahoma.ttf from Windows 8 */
113       || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
114       /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc  tahomabd.ttf from Windows 8 */
115       || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
116       /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e  tahoma.ttf from Windows 8.1 */
117       || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
118       /* sha1sum:6d400781948517c3c0441ba42acb309584b73033  tahomabd.ttf from Windows 8.1 */
119       || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
120       /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
121       || (964 == gdef_len && 60072 == gpos_len && 23836 == gsub_len)
122       /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
123       || (976 == gdef_len && 61456 == gpos_len && 23832 == gsub_len)
124       /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846  tahoma.ttf from Windows 10 */
125       || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
126       /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343  tahomabd.ttf from Windows 10 */
127       || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
128       /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
129       || (1006 == gdef_len && 61346 == gpos_len && 24576 == gsub_len)
130       /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
131       || (1018 == gdef_len && 62828 == gpos_len && 24572 == gsub_len)
132       /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5  tahoma.ttf from Windows 10 AU */
133       || (1006 == gdef_len && 61352 == gpos_len && 24576 == gsub_len)
134       /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2  tahomabd.ttf from Windows 10 AU */
135       || (1018 == gdef_len && 62834 == gpos_len && 24572 == gsub_len)
136       /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7  Tahoma.ttf from Mac OS X 10.9 */
137       || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
138       /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba  Tahoma Bold.ttf from Mac OS X 10.9 */
139       || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
140       /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc  himalaya.ttf from Windows 7 */
141       || (180 == gdef_len && 7254 == gpos_len && 13054 == gsub_len)
142       /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0  himalaya.ttf from Windows 8 */
143       || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
144       /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427  himalaya.ttf from Windows 8.1 */
145       || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
146       /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44  cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
147       /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371  cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
148       || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
149       /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f  cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
150       /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b  cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
151       || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
152       /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
153       || (1058 == gdef_len && 11818 == gpos_len && 47032 == gsub_len)
154       /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
155       || (1046 == gdef_len && 12600 == gpos_len && 47030 == gsub_len)
156       /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
157       || (1058 == gdef_len && 16770 == gpos_len && 71796 == gsub_len)
158       /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
159       || (1046 == gdef_len && 17862 == gpos_len && 71790 == gsub_len)
160       /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
161       || (1046 == gdef_len && 17112 == gpos_len && 71788 == gsub_len)
162       /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
163       || (1058 == gdef_len && 17514 == gpos_len && 71794 == gsub_len)
164       /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
165       || (1330 == gdef_len && 57938 == gpos_len && 109904 == gsub_len)
166       /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
167       || (1330 == gdef_len && 58972 == gpos_len && 109904 == gsub_len)
168       /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85  Padauk.ttf
169        *  "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
170       || (1004 == gdef_len && 14836 == gpos_len && 59092 == gsub_len)
171     )
172     {
173       /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
174        * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
175        * and the version of Cantarell shipped by Ubuntu 16.04.
176        * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
177        * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
178        *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
179        *     https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
180        */
181       layout->gdef = &OT::Null(OT::GDEF);
182     }
183   }
184
185   layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
186   layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
187
188   layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
189   layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
190
191   if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
192                 (layout->gpos_lookup_count && !layout->gpos_accels)))
193   {
194     _hb_ot_layout_destroy (layout);
195     return nullptr;
196   }
197
198   for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
199     layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
200   for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
201     layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
202
203   return layout;
204 }
205
206 void
207 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
208 {
209   if (layout->gsub_accels)
210     for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
211       layout->gsub_accels[i].fini ();
212   if (layout->gpos_accels)
213     for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
214       layout->gpos_accels[i].fini ();
215
216   free (layout->gsub_accels);
217   free (layout->gpos_accels);
218
219   hb_blob_destroy (layout->gdef_blob);
220   hb_blob_destroy (layout->gsub_blob);
221   hb_blob_destroy (layout->gpos_blob);
222
223   layout->base.fini ();
224   layout->colr.fini ();
225   layout->cpal.fini ();
226   layout->math.fini ();
227   layout->fvar.fini ();
228   layout->avar.fini ();
229   layout->ankr.fini ();
230   layout->kerx.fini ();
231   layout->morx.fini ();
232   layout->trak.fini ();
233
234   free (layout);
235 }
236
237 // static inline const OT::BASE&
238 // _get_base (hb_face_t *face)
239 // {
240 //   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::BASE);
241 //   hb_ot_layout_t * layout = hb_ot_layout_from_face (face);
242 //   return *(layout->base.get ());
243 // }
244
245 static inline const OT::GDEF&
246 _get_gdef (hb_face_t *face)
247 {
248   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
249   return *hb_ot_layout_from_face (face)->gdef;
250 }
251 static inline const OT::GSUB&
252 _get_gsub (hb_face_t *face)
253 {
254   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
255   return *hb_ot_layout_from_face (face)->gsub;
256 }
257 static inline const OT::GPOS&
258 _get_gpos (hb_face_t *face)
259 {
260   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
261   return *hb_ot_layout_from_face (face)->gpos;
262 }
263
264 /*
265  * GDEF
266  */
267
268 hb_bool_t
269 hb_ot_layout_has_glyph_classes (hb_face_t *face)
270 {
271   return _get_gdef (face).has_glyph_classes ();
272 }
273
274 /**
275  * hb_ot_layout_get_glyph_class:
276  *
277  * Since: 0.9.7
278  **/
279 hb_ot_layout_glyph_class_t
280 hb_ot_layout_get_glyph_class (hb_face_t      *face,
281                               hb_codepoint_t  glyph)
282 {
283   return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
284 }
285
286 /**
287  * hb_ot_layout_get_glyphs_in_class:
288  *
289  * Since: 0.9.7
290  **/
291 void
292 hb_ot_layout_get_glyphs_in_class (hb_face_t                  *face,
293                                   hb_ot_layout_glyph_class_t  klass,
294                                   hb_set_t                   *glyphs /* OUT */)
295 {
296   return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
297 }
298
299 unsigned int
300 hb_ot_layout_get_attach_points (hb_face_t      *face,
301                                 hb_codepoint_t  glyph,
302                                 unsigned int    start_offset,
303                                 unsigned int   *point_count /* IN/OUT */,
304                                 unsigned int   *point_array /* OUT */)
305 {
306   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
307 }
308
309 unsigned int
310 hb_ot_layout_get_ligature_carets (hb_font_t      *font,
311                                   hb_direction_t  direction,
312                                   hb_codepoint_t  glyph,
313                                   unsigned int    start_offset,
314                                   unsigned int   *caret_count /* IN/OUT */,
315                                   hb_position_t  *caret_array /* OUT */)
316 {
317   return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
318 }
319
320
321 /*
322  * GSUB/GPOS
323  */
324
325 static const OT::GSUBGPOS&
326 get_gsubgpos_table (hb_face_t *face,
327                     hb_tag_t   table_tag)
328 {
329   switch (table_tag) {
330     case HB_OT_TAG_GSUB: return _get_gsub (face);
331     case HB_OT_TAG_GPOS: return _get_gpos (face);
332     default:             return OT::Null(OT::GSUBGPOS);
333   }
334 }
335
336
337 unsigned int
338 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
339                                     hb_tag_t      table_tag,
340                                     unsigned int  start_offset,
341                                     unsigned int *script_count /* IN/OUT */,
342                                     hb_tag_t     *script_tags /* OUT */)
343 {
344   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
345
346   return g.get_script_tags (start_offset, script_count, script_tags);
347 }
348
349 #define HB_OT_TAG_LATIN_SCRIPT          HB_TAG ('l', 'a', 't', 'n')
350
351 hb_bool_t
352 hb_ot_layout_table_find_script (hb_face_t    *face,
353                                 hb_tag_t      table_tag,
354                                 hb_tag_t      script_tag,
355                                 unsigned int *script_index)
356 {
357   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
358   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
359
360   if (g.find_script_index (script_tag, script_index))
361     return true;
362
363   /* try finding 'DFLT' */
364   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
365     return false;
366
367   /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
368    * including many versions of DejaVu Sans Mono! */
369   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
370     return false;
371
372   /* try with 'latn'; some old fonts put their features there even though
373      they're really trying to support Thai, for example :( */
374   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
375     return false;
376
377   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
378   return false;
379 }
380
381 hb_bool_t
382 hb_ot_layout_table_choose_script (hb_face_t      *face,
383                                   hb_tag_t        table_tag,
384                                   const hb_tag_t *script_tags,
385                                   unsigned int   *script_index,
386                                   hb_tag_t       *chosen_script)
387 {
388   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
389   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
390
391   while (*script_tags)
392   {
393     if (g.find_script_index (*script_tags, script_index)) {
394       if (chosen_script)
395         *chosen_script = *script_tags;
396       return true;
397     }
398     script_tags++;
399   }
400
401   /* try finding 'DFLT' */
402   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
403     if (chosen_script)
404       *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
405     return false;
406   }
407
408   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
409   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
410     if (chosen_script)
411       *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
412     return false;
413   }
414
415   /* try with 'latn'; some old fonts put their features there even though
416      they're really trying to support Thai, for example :( */
417   if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
418     if (chosen_script)
419       *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
420     return false;
421   }
422
423   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
424   if (chosen_script)
425     *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
426   return false;
427 }
428
429 unsigned int
430 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
431                                      hb_tag_t      table_tag,
432                                      unsigned int  start_offset,
433                                      unsigned int *feature_count /* IN/OUT */,
434                                      hb_tag_t     *feature_tags /* OUT */)
435 {
436   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
437
438   return g.get_feature_tags (start_offset, feature_count, feature_tags);
439 }
440
441 hb_bool_t
442 hb_ot_layout_table_find_feature (hb_face_t    *face,
443                                  hb_tag_t      table_tag,
444                                  hb_tag_t      feature_tag,
445                                  unsigned int *feature_index)
446 {
447   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
448   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
449
450   unsigned int num_features = g.get_feature_count ();
451   for (unsigned int i = 0; i < num_features; i++)
452   {
453     if (feature_tag == g.get_feature_tag (i)) {
454       if (feature_index) *feature_index = i;
455       return true;
456     }
457   }
458
459   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
460   return false;
461 }
462
463
464 unsigned int
465 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
466                                        hb_tag_t      table_tag,
467                                        unsigned int  script_index,
468                                        unsigned int  start_offset,
469                                        unsigned int *language_count /* IN/OUT */,
470                                        hb_tag_t     *language_tags /* OUT */)
471 {
472   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
473
474   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
475 }
476
477 hb_bool_t
478 hb_ot_layout_script_find_language (hb_face_t    *face,
479                                    hb_tag_t      table_tag,
480                                    unsigned int  script_index,
481                                    hb_tag_t      language_tag,
482                                    unsigned int *language_index)
483 {
484   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
485   const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
486
487   if (s.find_lang_sys_index (language_tag, language_index))
488     return true;
489
490   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
491   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
492     return false;
493
494   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
495   return false;
496 }
497
498 hb_bool_t
499 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
500                                                   hb_tag_t      table_tag,
501                                                   unsigned int  script_index,
502                                                   unsigned int  language_index,
503                                                   unsigned int *feature_index)
504 {
505   return hb_ot_layout_language_get_required_feature (face,
506                                                      table_tag,
507                                                      script_index,
508                                                      language_index,
509                                                      feature_index,
510                                                      nullptr);
511 }
512
513 /**
514  * hb_ot_layout_language_get_required_feature:
515  *
516  * Since: 0.9.30
517  **/
518 hb_bool_t
519 hb_ot_layout_language_get_required_feature (hb_face_t    *face,
520                                             hb_tag_t      table_tag,
521                                             unsigned int  script_index,
522                                             unsigned int  language_index,
523                                             unsigned int *feature_index,
524                                             hb_tag_t     *feature_tag)
525 {
526   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
527   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
528
529   unsigned int index = l.get_required_feature_index ();
530   if (feature_index) *feature_index = index;
531   if (feature_tag) *feature_tag = g.get_feature_tag (index);
532
533   return l.has_required_feature ();
534 }
535
536 unsigned int
537 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
538                                            hb_tag_t      table_tag,
539                                            unsigned int  script_index,
540                                            unsigned int  language_index,
541                                            unsigned int  start_offset,
542                                            unsigned int *feature_count /* IN/OUT */,
543                                            unsigned int *feature_indexes /* OUT */)
544 {
545   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
546   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
547
548   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
549 }
550
551 unsigned int
552 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
553                                         hb_tag_t      table_tag,
554                                         unsigned int  script_index,
555                                         unsigned int  language_index,
556                                         unsigned int  start_offset,
557                                         unsigned int *feature_count /* IN/OUT */,
558                                         hb_tag_t     *feature_tags /* OUT */)
559 {
560   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
561   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
562
563   static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
564   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
565
566   if (feature_tags) {
567     unsigned int count = *feature_count;
568     for (unsigned int i = 0; i < count; i++)
569       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
570   }
571
572   return ret;
573 }
574
575
576 hb_bool_t
577 hb_ot_layout_language_find_feature (hb_face_t    *face,
578                                     hb_tag_t      table_tag,
579                                     unsigned int  script_index,
580                                     unsigned int  language_index,
581                                     hb_tag_t      feature_tag,
582                                     unsigned int *feature_index)
583 {
584   static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
585   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
586   const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
587
588   unsigned int num_features = l.get_feature_count ();
589   for (unsigned int i = 0; i < num_features; i++) {
590     unsigned int f_index = l.get_feature_index (i);
591
592     if (feature_tag == g.get_feature_tag (f_index)) {
593       if (feature_index) *feature_index = f_index;
594       return true;
595     }
596   }
597
598   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
599   return false;
600 }
601
602 /**
603  * hb_ot_layout_feature_get_lookups:
604  *
605  * Since: 0.9.7
606  **/
607 unsigned int
608 hb_ot_layout_feature_get_lookups (hb_face_t    *face,
609                                   hb_tag_t      table_tag,
610                                   unsigned int  feature_index,
611                                   unsigned int  start_offset,
612                                   unsigned int *lookup_count /* IN/OUT */,
613                                   unsigned int *lookup_indexes /* OUT */)
614 {
615   return hb_ot_layout_feature_with_variations_get_lookups (face,
616                                                            table_tag,
617                                                            feature_index,
618                                                            HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
619                                                            start_offset,
620                                                            lookup_count,
621                                                            lookup_indexes);
622 }
623
624 /**
625  * hb_ot_layout_table_get_lookup_count:
626  *
627  * Since: 0.9.22
628  **/
629 unsigned int
630 hb_ot_layout_table_get_lookup_count (hb_face_t    *face,
631                                      hb_tag_t      table_tag)
632 {
633   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return 0;
634   switch (table_tag)
635   {
636     case HB_OT_TAG_GSUB:
637     {
638       return hb_ot_layout_from_face (face)->gsub_lookup_count;
639     }
640     case HB_OT_TAG_GPOS:
641     {
642       return hb_ot_layout_from_face (face)->gpos_lookup_count;
643     }
644   }
645   return 0;
646 }
647
648 static void
649 _hb_ot_layout_collect_lookups_lookups (hb_face_t      *face,
650                                        hb_tag_t        table_tag,
651                                        unsigned int    feature_index,
652                                        hb_set_t       *lookup_indexes /* OUT */)
653 {
654   unsigned int lookup_indices[32];
655   unsigned int offset, len;
656
657   offset = 0;
658   do {
659     len = ARRAY_LENGTH (lookup_indices);
660     hb_ot_layout_feature_get_lookups (face,
661                                       table_tag,
662                                       feature_index,
663                                       offset, &len,
664                                       lookup_indices);
665
666     for (unsigned int i = 0; i < len; i++)
667       lookup_indexes->add (lookup_indices[i]);
668
669     offset += len;
670   } while (len == ARRAY_LENGTH (lookup_indices));
671 }
672
673 static void
674 _hb_ot_layout_collect_lookups_features (hb_face_t      *face,
675                                         hb_tag_t        table_tag,
676                                         unsigned int    script_index,
677                                         unsigned int    language_index,
678                                         const hb_tag_t *features,
679                                         hb_set_t       *lookup_indexes /* OUT */)
680 {
681   if (!features)
682   {
683     unsigned int required_feature_index;
684     if (hb_ot_layout_language_get_required_feature (face,
685                                                     table_tag,
686                                                     script_index,
687                                                     language_index,
688                                                     &required_feature_index,
689                                                     nullptr))
690       _hb_ot_layout_collect_lookups_lookups (face,
691                                              table_tag,
692                                              required_feature_index,
693                                              lookup_indexes);
694
695     /* All features */
696     unsigned int feature_indices[32];
697     unsigned int offset, len;
698
699     offset = 0;
700     do {
701       len = ARRAY_LENGTH (feature_indices);
702       hb_ot_layout_language_get_feature_indexes (face,
703                                                  table_tag,
704                                                  script_index,
705                                                  language_index,
706                                                  offset, &len,
707                                                  feature_indices);
708
709       for (unsigned int i = 0; i < len; i++)
710         _hb_ot_layout_collect_lookups_lookups (face,
711                                                table_tag,
712                                                feature_indices[i],
713                                                lookup_indexes);
714
715       offset += len;
716     } while (len == ARRAY_LENGTH (feature_indices));
717   }
718   else
719   {
720     for (; *features; features++)
721     {
722       unsigned int feature_index;
723       if (hb_ot_layout_language_find_feature (face,
724                                               table_tag,
725                                               script_index,
726                                               language_index,
727                                               *features,
728                                               &feature_index))
729         _hb_ot_layout_collect_lookups_lookups (face,
730                                                table_tag,
731                                                feature_index,
732                                                lookup_indexes);
733     }
734   }
735 }
736
737 static void
738 _hb_ot_layout_collect_lookups_languages (hb_face_t      *face,
739                                          hb_tag_t        table_tag,
740                                          unsigned int    script_index,
741                                          const hb_tag_t *languages,
742                                          const hb_tag_t *features,
743                                          hb_set_t       *lookup_indexes /* OUT */)
744 {
745   _hb_ot_layout_collect_lookups_features (face,
746                                           table_tag,
747                                           script_index,
748                                           HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
749                                           features,
750                                           lookup_indexes);
751
752   if (!languages)
753   {
754     /* All languages */
755     unsigned int count = hb_ot_layout_script_get_language_tags (face,
756                                                                 table_tag,
757                                                                 script_index,
758                                                                 0, nullptr, nullptr);
759     for (unsigned int language_index = 0; language_index < count; language_index++)
760       _hb_ot_layout_collect_lookups_features (face,
761                                               table_tag,
762                                               script_index,
763                                               language_index,
764                                               features,
765                                               lookup_indexes);
766   }
767   else
768   {
769     for (; *languages; languages++)
770     {
771       unsigned int language_index;
772       if (hb_ot_layout_script_find_language (face,
773                                              table_tag,
774                                              script_index,
775                                              *languages,
776                                              &language_index))
777         _hb_ot_layout_collect_lookups_features (face,
778                                                 table_tag,
779                                                 script_index,
780                                                 language_index,
781                                                 features,
782                                                 lookup_indexes);
783     }
784   }
785 }
786
787 /**
788  * hb_ot_layout_collect_lookups:
789  *
790  * Since: 0.9.8
791  **/
792 void
793 hb_ot_layout_collect_lookups (hb_face_t      *face,
794                               hb_tag_t        table_tag,
795                               const hb_tag_t *scripts,
796                               const hb_tag_t *languages,
797                               const hb_tag_t *features,
798                               hb_set_t       *lookup_indexes /* OUT */)
799 {
800   if (!scripts)
801   {
802     /* All scripts */
803     unsigned int count = hb_ot_layout_table_get_script_tags (face,
804                                                              table_tag,
805                                                              0, nullptr, nullptr);
806     for (unsigned int script_index = 0; script_index < count; script_index++)
807       _hb_ot_layout_collect_lookups_languages (face,
808                                                table_tag,
809                                                script_index,
810                                                languages,
811                                                features,
812                                                lookup_indexes);
813   }
814   else
815   {
816     for (; *scripts; scripts++)
817     {
818       unsigned int script_index;
819       if (hb_ot_layout_table_find_script (face,
820                                           table_tag,
821                                           *scripts,
822                                           &script_index))
823         _hb_ot_layout_collect_lookups_languages (face,
824                                                  table_tag,
825                                                  script_index,
826                                                  languages,
827                                                  features,
828                                                  lookup_indexes);
829     }
830   }
831 }
832
833 /**
834  * hb_ot_layout_lookup_collect_glyphs:
835  *
836  * Since: 0.9.7
837  **/
838 void
839 hb_ot_layout_lookup_collect_glyphs (hb_face_t    *face,
840                                     hb_tag_t      table_tag,
841                                     unsigned int  lookup_index,
842                                     hb_set_t     *glyphs_before, /* OUT. May be nullptr */
843                                     hb_set_t     *glyphs_input,  /* OUT. May be nullptr */
844                                     hb_set_t     *glyphs_after,  /* OUT. May be nullptr */
845                                     hb_set_t     *glyphs_output  /* OUT. May be nullptr */)
846 {
847   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
848
849   OT::hb_collect_glyphs_context_t c (face,
850                                      glyphs_before,
851                                      glyphs_input,
852                                      glyphs_after,
853                                      glyphs_output);
854
855   switch (table_tag)
856   {
857     case HB_OT_TAG_GSUB:
858     {
859       const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
860       l.collect_glyphs (&c);
861       return;
862     }
863     case HB_OT_TAG_GPOS:
864     {
865       const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
866       l.collect_glyphs (&c);
867       return;
868     }
869   }
870 }
871
872
873 /* Variations support */
874
875 hb_bool_t
876 hb_ot_layout_table_find_feature_variations (hb_face_t    *face,
877                                             hb_tag_t      table_tag,
878                                             const int    *coords,
879                                             unsigned int  num_coords,
880                                             unsigned int *variations_index /* out */)
881 {
882   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
883
884   return g.find_variations_index (coords, num_coords, variations_index);
885 }
886
887 unsigned int
888 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t    *face,
889                                                   hb_tag_t      table_tag,
890                                                   unsigned int  feature_index,
891                                                   unsigned int  variations_index,
892                                                   unsigned int  start_offset,
893                                                   unsigned int *lookup_count /* IN/OUT */,
894                                                   unsigned int *lookup_indexes /* OUT */)
895 {
896   static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
897   const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
898
899   const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
900
901   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
902 }
903
904
905 /*
906  * OT::GSUB
907  */
908
909 hb_bool_t
910 hb_ot_layout_has_substitution (hb_face_t *face)
911 {
912   return &_get_gsub (face) != &OT::Null(OT::GSUB);
913 }
914
915 /**
916  * hb_ot_layout_lookup_would_substitute:
917  *
918  * Since: 0.9.7
919  **/
920 hb_bool_t
921 hb_ot_layout_lookup_would_substitute (hb_face_t            *face,
922                                       unsigned int          lookup_index,
923                                       const hb_codepoint_t *glyphs,
924                                       unsigned int          glyphs_length,
925                                       hb_bool_t             zero_context)
926 {
927   if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
928   return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
929 }
930
931 hb_bool_t
932 hb_ot_layout_lookup_would_substitute_fast (hb_face_t            *face,
933                                            unsigned int          lookup_index,
934                                            const hb_codepoint_t *glyphs,
935                                            unsigned int          glyphs_length,
936                                            hb_bool_t             zero_context)
937 {
938   if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
939   OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
940
941   const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
942
943   return l.would_apply (&c, &hb_ot_layout_from_face (face)->gsub_accels[lookup_index]);
944 }
945
946 void
947 hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
948 {
949   OT::GSUB::substitute_start (font, buffer);
950 }
951
952 /**
953  * hb_ot_layout_lookup_substitute_closure:
954  *
955  * Since: 0.9.7
956  **/
957 void
958 hb_ot_layout_lookup_substitute_closure (hb_face_t    *face,
959                                         unsigned int  lookup_index,
960                                         hb_set_t     *glyphs)
961 {
962   OT::hb_closure_context_t c (face, glyphs);
963
964   const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
965
966   l.closure (&c);
967 }
968
969 /*
970  * OT::GPOS
971  */
972
973 hb_bool_t
974 hb_ot_layout_has_positioning (hb_face_t *face)
975 {
976   return &_get_gpos (face) != &OT::Null(OT::GPOS);
977 }
978
979 void
980 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
981 {
982   OT::GPOS::position_start (font, buffer);
983 }
984
985 void
986 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
987 {
988   OT::GPOS::position_finish_advances (font, buffer);
989 }
990
991 void
992 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
993 {
994   OT::GPOS::position_finish_offsets (font, buffer);
995 }
996
997 /**
998  * hb_ot_layout_get_size_params:
999  *
1000  * Since: 0.9.10
1001  **/
1002 hb_bool_t
1003 hb_ot_layout_get_size_params (hb_face_t    *face,
1004                               unsigned int *design_size,       /* OUT.  May be nullptr */
1005                               unsigned int *subfamily_id,      /* OUT.  May be nullptr */
1006                               unsigned int *subfamily_name_id, /* OUT.  May be nullptr */
1007                               unsigned int *range_start,       /* OUT.  May be nullptr */
1008                               unsigned int *range_end          /* OUT.  May be nullptr */)
1009 {
1010   const OT::GPOS &gpos = _get_gpos (face);
1011   const hb_tag_t tag = HB_TAG ('s','i','z','e');
1012
1013   unsigned int num_features = gpos.get_feature_count ();
1014   for (unsigned int i = 0; i < num_features; i++)
1015   {
1016     if (tag == gpos.get_feature_tag (i))
1017     {
1018       const OT::Feature &f = gpos.get_feature (i);
1019       const OT::FeatureParamsSize &params = f.get_feature_params ().get_size_params (tag);
1020
1021       if (params.designSize)
1022       {
1023 #define PARAM(a, A) if (a) *a = params.A
1024         PARAM (design_size, designSize);
1025         PARAM (subfamily_id, subfamilyID);
1026         PARAM (subfamily_name_id, subfamilyNameID);
1027         PARAM (range_start, rangeStart);
1028         PARAM (range_end, rangeEnd);
1029 #undef PARAM
1030
1031         return true;
1032       }
1033     }
1034   }
1035
1036 #define PARAM(a, A) if (a) *a = 0
1037   PARAM (design_size, designSize);
1038   PARAM (subfamily_id, subfamilyID);
1039   PARAM (subfamily_name_id, subfamilyNameID);
1040   PARAM (range_start, rangeStart);
1041   PARAM (range_end, rangeEnd);
1042 #undef PARAM
1043
1044   return false;
1045 }
1046
1047
1048 /*
1049  * Parts of different types are implemented here such that they have direct
1050  * access to GSUB/GPOS lookups.
1051  */
1052
1053
1054 struct GSUBProxy
1055 {
1056   static const unsigned int table_index = 0;
1057   static const bool inplace = false;
1058   typedef OT::SubstLookup Lookup;
1059
1060   GSUBProxy (hb_face_t *face) :
1061     table (*hb_ot_layout_from_face (face)->gsub),
1062     accels (hb_ot_layout_from_face (face)->gsub_accels) {}
1063
1064   const OT::GSUB &table;
1065   const hb_ot_layout_lookup_accelerator_t *accels;
1066 };
1067
1068 struct GPOSProxy
1069 {
1070   static const unsigned int table_index = 1;
1071   static const bool inplace = true;
1072   typedef OT::PosLookup Lookup;
1073
1074   GPOSProxy (hb_face_t *face) :
1075     table (*hb_ot_layout_from_face (face)->gpos),
1076     accels (hb_ot_layout_from_face (face)->gpos_accels) {}
1077
1078   const OT::GPOS &table;
1079   const hb_ot_layout_lookup_accelerator_t *accels;
1080 };
1081
1082
1083 struct hb_get_subtables_context_t :
1084        OT::hb_dispatch_context_t<hb_get_subtables_context_t, hb_void_t, HB_DEBUG_APPLY>
1085 {
1086   template <typename Type>
1087   static inline bool apply_to (const void *obj, OT::hb_ot_apply_context_t *c)
1088   {
1089     const Type *typed_obj = (const Type *) obj;
1090     return typed_obj->apply (c);
1091   }
1092
1093   typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_ot_apply_context_t *c);
1094
1095   struct hb_applicable_t
1096   {
1097     inline void init (const void *obj_, hb_apply_func_t apply_func_)
1098     {
1099       obj = obj_;
1100       apply_func = apply_func_;
1101     }
1102
1103     inline bool apply (OT::hb_ot_apply_context_t *c) const { return apply_func (obj, c); }
1104
1105     private:
1106     const void *obj;
1107     hb_apply_func_t apply_func;
1108   };
1109
1110   typedef hb_auto_array_t<hb_applicable_t> array_t;
1111
1112   /* Dispatch interface. */
1113   inline const char *get_name (void) { return "GET_SUBTABLES"; }
1114   template <typename T>
1115   inline return_t dispatch (const T &obj)
1116   {
1117     hb_applicable_t *entry = array.push();
1118     if (likely (entry))
1119       entry->init (&obj, apply_to<T>);
1120     return HB_VOID;
1121   }
1122   static return_t default_return_value (void) { return HB_VOID; }
1123   bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
1124
1125   hb_get_subtables_context_t (array_t &array_) :
1126                               array (array_),
1127                               debug_depth (0) {}
1128
1129   array_t &array;
1130   unsigned int debug_depth;
1131 };
1132
1133 static inline bool
1134 apply_forward (OT::hb_ot_apply_context_t *c,
1135                const hb_ot_layout_lookup_accelerator_t &accel,
1136                const hb_get_subtables_context_t::array_t &subtables)
1137 {
1138   bool ret = false;
1139   hb_buffer_t *buffer = c->buffer;
1140   while (buffer->idx < buffer->len && !buffer->in_error)
1141   {
1142     bool applied = false;
1143     if (accel.may_have (buffer->cur().codepoint) &&
1144         (buffer->cur().mask & c->lookup_mask) &&
1145         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1146      {
1147        for (unsigned int i = 0; i < subtables.len; i++)
1148          if (subtables[i].apply (c))
1149          {
1150            applied = true;
1151            break;
1152          }
1153      }
1154
1155     if (applied)
1156       ret = true;
1157     else
1158       buffer->next_glyph ();
1159   }
1160   return ret;
1161 }
1162
1163 static inline bool
1164 apply_backward (OT::hb_ot_apply_context_t *c,
1165                const hb_ot_layout_lookup_accelerator_t &accel,
1166                const hb_get_subtables_context_t::array_t &subtables)
1167 {
1168   bool ret = false;
1169   hb_buffer_t *buffer = c->buffer;
1170   do
1171   {
1172     if (accel.may_have (buffer->cur().codepoint) &&
1173         (buffer->cur().mask & c->lookup_mask) &&
1174         c->check_glyph_property (&buffer->cur(), c->lookup_props))
1175     {
1176      for (unsigned int i = 0; i < subtables.len; i++)
1177        if (subtables[i].apply (c))
1178        {
1179          ret = true;
1180          break;
1181        }
1182     }
1183     /* The reverse lookup doesn't "advance" cursor (for good reason). */
1184     buffer->idx--;
1185
1186   }
1187   while ((int) buffer->idx >= 0);
1188   return ret;
1189 }
1190
1191 template <typename Proxy>
1192 static inline void
1193 apply_string (OT::hb_ot_apply_context_t *c,
1194               const typename Proxy::Lookup &lookup,
1195               const hb_ot_layout_lookup_accelerator_t &accel)
1196 {
1197   hb_buffer_t *buffer = c->buffer;
1198
1199   if (unlikely (!buffer->len || !c->lookup_mask))
1200     return;
1201
1202   c->set_lookup_props (lookup.get_props ());
1203
1204   hb_get_subtables_context_t::array_t subtables;
1205   hb_get_subtables_context_t c_get_subtables (subtables);
1206   lookup.dispatch (&c_get_subtables);
1207
1208   if (likely (!lookup.is_reverse ()))
1209   {
1210     /* in/out forward substitution/positioning */
1211     if (Proxy::table_index == 0)
1212       buffer->clear_output ();
1213     buffer->idx = 0;
1214
1215     bool ret;
1216     ret = apply_forward (c, accel, subtables);
1217     if (ret)
1218     {
1219       if (!Proxy::inplace)
1220         buffer->swap_buffers ();
1221       else
1222         assert (!buffer->has_separate_output ());
1223     }
1224   }
1225   else
1226   {
1227     /* in-place backward substitution/positioning */
1228     if (Proxy::table_index == 0)
1229       buffer->remove_output ();
1230     buffer->idx = buffer->len - 1;
1231
1232     apply_backward (c, accel, subtables);
1233   }
1234 }
1235
1236 template <typename Proxy>
1237 inline void hb_ot_map_t::apply (const Proxy &proxy,
1238                                 const hb_ot_shape_plan_t *plan,
1239                                 hb_font_t *font,
1240                                 hb_buffer_t *buffer) const
1241 {
1242   const unsigned int table_index = proxy.table_index;
1243   unsigned int i = 0;
1244   OT::hb_ot_apply_context_t c (table_index, font, buffer);
1245   c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1246
1247   for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1248     const stage_map_t *stage = &stages[table_index][stage_index];
1249     for (; i < stage->last_lookup; i++)
1250     {
1251       unsigned int lookup_index = lookups[table_index][i].index;
1252       if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1253       c.set_lookup_index (lookup_index);
1254       c.set_lookup_mask (lookups[table_index][i].mask);
1255       c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1256       c.set_auto_zwnj (lookups[table_index][i].auto_zwnj);
1257       apply_string<Proxy> (&c,
1258                            proxy.table.get_lookup (lookup_index),
1259                            proxy.accels[lookup_index]);
1260       (void) buffer->message (font, "end lookup %d", lookup_index);
1261     }
1262
1263     if (stage->pause_func)
1264     {
1265       buffer->clear_output ();
1266       stage->pause_func (plan, font, buffer);
1267     }
1268   }
1269 }
1270
1271 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1272 {
1273   GSUBProxy proxy (font->face);
1274   apply (proxy, plan, font, buffer);
1275 }
1276
1277 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1278 {
1279   GPOSProxy proxy (font->face);
1280   apply (proxy, plan, font, buffer);
1281 }
1282
1283 void
1284 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
1285                                 const OT::SubstLookup &lookup,
1286                                 const hb_ot_layout_lookup_accelerator_t &accel)
1287 {
1288   apply_string<GSUBProxy> (c, lookup, accel);
1289 }
1290
1291
1292
1293
1294 /*
1295  * OT::BASE
1296  */
1297
1298 // /**
1299 //  * hb_ot_base_has_data:
1300 //  * @face: #hb_face_t to test
1301 //  *
1302 //  * This function allows to verify the presence of an OpenType BASE table on the
1303 //  * face.
1304 //  *
1305 //  * Return value: true if face has a BASE table, false otherwise
1306 //  *
1307 //  * Since: XXX
1308 //  **/
1309 // hb_bool_t
1310 // hb_ot_base_has_data (hb_face_t *face)
1311 // {
1312 //   return &_get_base (face) != &OT::Null(OT::BASE);
1313 // }