hb-ot-shape.cc revision 1e7d860613032e40a3f90e2caa2ee5ac44ab8c8c
12014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod/*
22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2009,2010  Red Hat, Inc.
3359dcaa0d33271025ca42a5f54ecbac7ae3b56d3Behdad Esfahbod * Copyright © 2010,2011  Google, Inc.
42014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
5c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
62014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
72014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * Permission is hereby granted, without written agreement and without
82014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
92014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * software and its documentation for any purpose, provided that the
102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
112014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * all copies of this software.
122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
142014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
152014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
162014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
172014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * DAMAGE.
182014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
192014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
202014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
212014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
222014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
242014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
252014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
26476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbod * Google Author(s): Behdad Esfahbod
272014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod */
282014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
29027857d0412477fb4427dcb8a8c45287c272e143Behdad Esfahbod#define HB_SHAPER ot
30027857d0412477fb4427dcb8a8c45287c272e143Behdad Esfahbod#define hb_ot_shaper_face_data_t hb_ot_layout_t
31e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod#define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
32027857d0412477fb4427dcb8a8c45287c272e143Behdad Esfahbod#include "hb-shaper-impl-private.hh"
33027857d0412477fb4427dcb8a8c45287c272e143Behdad Esfahbod
344924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod#include "hb-ot-shape-private.hh"
35b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod#include "hb-ot-shape-normalize-private.hh"
362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
37b70021f7c81a0ed08475b14b07291f662cd9f905Behdad Esfahbod#include "hb-ot-layout-private.hh"
381827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod#include "hb-set-private.hh"
3972657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod
40acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
41f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodhb_tag_t common_features[] = {
422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
43a5f1834f57ea3fb254f5c7d372747de316fcc8f1Behdad Esfahbod  HB_TAG('l','i','g','a'),
4489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('l','o','c','l'),
4589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('m','a','r','k'),
4689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('m','k','m','k'),
4789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('r','l','i','g'),
4889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod};
4989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
50d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
5189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbodhb_tag_t horizontal_features[] = {
5289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('c','a','l','t'),
532014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
54bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
552014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
5689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod};
5789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
5889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod/* Note:
5989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * Technically speaking, vrt2 and vert are mutually exclusive.
6089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * According to the spec, valt and vpal are also mutually exclusive.
6189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * But we apply them all for now.
6289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod */
6389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbodhb_tag_t vertical_features[] = {
6489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','a','l','t'),
6589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','e','r','t'),
6689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','k','r','n'),
6789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','p','a','l'),
6889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','r','t','2'),
692014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
702014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
71b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
72b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
73b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstruct hb_ot_shape_planner_t
74b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod{
75b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_map_builder_t map;
76693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  const hb_ot_complex_shaper_t *shaper;
77b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
78b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_shape_planner_t (void) : map () {}
79b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  ~hb_ot_shape_planner_t (void) { map.finish (); }
80b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
81b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  inline void compile (hb_face_t *face,
82b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod		       const hb_segment_properties_t *props,
83e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod		       hb_ot_shape_plan_t &plan)
84b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  {
85b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod    plan.shaper = shaper;
86b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod    map.compile (face, props, plan.map);
87b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  }
88b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
89b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  private:
90b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  NO_COPY (hb_ot_shape_planner_t);
91b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod};
92b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
932014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
9490645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
9549baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			      const hb_segment_properties_t  *props,
9690645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod			      const hb_feature_t             *user_features,
9790645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod			      unsigned int                    num_user_features)
982014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
99895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  switch (props->direction) {
1000e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
10190645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
10290645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
1030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1040e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
10590645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
10690645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
1070e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1080e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
1090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
1103286fc0e9adc3f2874c9409e7fdb09e4d2b7dda1Behdad Esfahbod    case HB_DIRECTION_INVALID:
1110e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
1120e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1130e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
1140e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
11589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod#define ADD_FEATURES(array) \
11689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_STMT_START { \
11789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
11889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod      planner->map.add_bool_feature (array[i]); \
11989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  } HB_STMT_END
12089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
121693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  if (planner->shaper->collect_features)
122693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod    planner->shaper->collect_features (planner->shaper, &planner->map, props);
123e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
12489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  ADD_FEATURES (common_features);
12589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
12689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
12789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    ADD_FEATURES (horizontal_features);
12889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  else
12989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    ADD_FEATURES (vertical_features);
13089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
131693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  if (planner->shaper->override_features)
132693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod    planner->shaper->override_features (planner->shaper, &planner->map, props);
133d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod
13489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod#undef ADD_FEATURES
13589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
136895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  for (unsigned int i = 0; i < num_user_features; i++) {
137895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod    const hb_feature_t *feature = &user_features[i];
13890645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod    planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
139e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
140e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
141e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
142e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
143e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod/*
144e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod * shaper face data
145e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod */
146e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
147e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodhb_ot_shaper_face_data_t *
148e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_face_data_create (hb_face_t *face)
149e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
150e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  return _hb_ot_layout_create (face);
151e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
152e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
153e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodvoid
154e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
155e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
156e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  _hb_ot_layout_destroy (data);
157e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
158e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
159e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
160e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod/*
161e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod * shaper font data
162e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod */
163e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
164e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodstruct hb_ot_shaper_font_data_t {};
165e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
166e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodhb_ot_shaper_font_data_t *
167e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_font_data_create (hb_font_t *font)
168e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
169e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
170e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
171e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
172e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodvoid
173e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
174e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
175e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
176e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
177e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
178e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod/*
179e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod * shaper shape_plan data
180e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod */
181e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
182e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodhb_ot_shaper_shape_plan_data_t *
183e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
184e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod				      const hb_feature_t *user_features,
185e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod				      unsigned int        num_user_features)
186e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
187e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_ot_shape_plan_t *data = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
188e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  if (unlikely (!data))
189e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod    return NULL;
190e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
191e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_ot_shape_planner_t planner;
192e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
193e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  assert (HB_DIRECTION_IS_VALID (shape_plan->props.direction));
194e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
195e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  planner.shaper = hb_ot_shape_complex_categorize (&shape_plan->props);
196e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
197e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
198e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
199e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  planner.compile (shape_plan->face, &shape_plan->props, *data);
200e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
201e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  return data;
202e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
203e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
204e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodvoid
205e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *data)
206e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod{
207e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  data->map.finish ();
2084ba647eecf0f70917ac4229af1f2dd3c62fcb7d5Behdad Esfahbod
2094ba647eecf0f70917ac4229af1f2dd3c62fcb7d5Behdad Esfahbod  free (data);
210e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod}
211e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
212e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
213e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod/*
214e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod * shaper
215e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod */
216e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
217b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstruct hb_ot_shape_context_t
218b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod{
219e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  /* Input to hb_ot_shape_internal() */
220b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_shape_plan_t *plan;
221b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_font_t *font;
222b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_face_t *face;
223b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_buffer_t  *buffer;
224b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  const hb_feature_t *user_features;
225b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  unsigned int        num_user_features;
226b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
227b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  /* Transient stuff */
228b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_direction_t target_direction;
229b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_bool_t applied_position_complex;
230b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod};
231b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
232e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
23352601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbodhb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
234e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
235605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_mask_t global_mask = c->plan->map.get_global_mask ();
23657ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod  c->buffer->reset_masks (global_mask);
2375b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
238693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  if (c->plan->shaper->setup_masks)
239693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod    c->plan->shaper->setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
2402989be4919242670c94825bded96db20a7b2035bBehdad Esfahbod
241d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  for (unsigned int i = 0; i < c->num_user_features; i++)
242efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
243d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod    const hb_feature_t *feature = &c->user_features[i];
2444924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
2454924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      unsigned int shift;
246605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
247bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
2484924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    }
249efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
2502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
2512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
2522014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
253ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
254ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
255ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
256ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
257b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstatic void
258b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodhb_set_unicode_props (hb_buffer_t *buffer)
2593a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod{
260aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  unsigned int count = buffer->len;
2610e3361464b00b76aa7375515163e0710a691db0cBehdad Esfahbod  for (unsigned int i = 0; i < count; i++)
262d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
2633a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod}
2643a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
2653a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodstatic void
266aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
267ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
268aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  unsigned int count = buffer->len;
269ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
270d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
2714a68684654e645882095c1189477146287ce9437Behdad Esfahbod	(FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
2724a68684654e645882095c1189477146287ce9437Behdad Esfahbod	 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
273d63adfc7d09b26764d9166da97372b21257e7611Behdad Esfahbod	 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
274330a2af3ff0e12c01b3b451357b8bdc83b2e9b47Behdad Esfahbod      buffer->merge_clusters (i - 1, i + 1);
275ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
276ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
277967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
278aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
279ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
280aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  hb_direction_t direction = buffer->props.direction;
281ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
2827403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod  /* TODO vertical:
2837403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
2847403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
2857403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * first. */
286aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
2877403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod      (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
288ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
289aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
290aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
291ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
292ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
293ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
294ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
295ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
296ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
297ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
298aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
299ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
300fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod  hb_unicode_funcs_t *unicode = c->buffer->unicode;
301ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
302bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
303ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
304ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
30539dede9ffffe732f78cbd092ccb3b48d77ddd66dBehdad Esfahbod  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
3064e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
307aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
308ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
309c4641723fbf6532b2e80a662e15573b31276bc73Behdad Esfahbod    hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
310aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
311bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
3121094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
313aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
314ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
315ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
316ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
317ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
318ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
319ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
320ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
3215d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod  hb_codepoint_t glyph;
3225d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod
323ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
324ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
325ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
326a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
3275d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod
328ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
329468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  for (buffer->idx = 0; buffer->idx < count;) {
33099c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod    if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
33199c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod      hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
332a4385f0b0a6949e2ce49e6a147ad4beaa724f6c3Behdad Esfahbod      buffer->replace_glyphs (2, 1, &glyph);
333ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
33499c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod      hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
3350fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod      buffer->replace_glyph (glyph);
336ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
337ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
338468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  if (likely (buffer->idx < buffer->len)) {
33999c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod    hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
3400fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod    buffer->replace_glyph (glyph);
3410fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  }
342468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  buffer->swap_buffers ();
343ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
344ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
345ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
346967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
347ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
348b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_mirror_chars (c);
349b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
35072657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_map_glyphs (c->font, c->buffer);
351ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
352ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
353ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
3545fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbodhb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
3555fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod{
3565fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod  unsigned int count = c->buffer->len;
3575fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
3585fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod    c->buffer->info[i].glyph_props() = FLAG (_hb_glyph_info_get_general_category (&c->buffer->info[i])) &
3595fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod				       (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
3605fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod					FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
3615fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod					FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)) ?
3625fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod				       HB_OT_LAYOUT_GLYPH_CLASS_MARK :
3635fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod				       HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
3645fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod}
3655fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod
3665fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod
3675fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbodstatic void
368c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
369c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod{
37005bd1b63426e07d1df7a1b40bf845dc94ab995a8Behdad Esfahbod  hb_ot_layout_substitute_start (c->face, c->buffer);
37105bd1b63426e07d1df7a1b40bf845dc94ab995a8Behdad Esfahbod
3725fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod  if (!hb_ot_layout_has_glyph_classes (c->face))
3735fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod    hb_synthesize_glyph_classes (c);
3745fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod
3755fecd8b0355894ceda14b3d3c654f20c3d5e77f4Behdad Esfahbod  if (hb_ot_layout_has_substitution (c->face))
376b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    c->plan->map.substitute (c->face, c->buffer);
377c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
37805bd1b63426e07d1df7a1b40bf845dc94ab995a8Behdad Esfahbod  hb_ot_layout_substitute_finish (c->face, c->buffer);
379c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
380c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  return;
381c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod}
382c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
383ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
384ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
385ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
386ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
387967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
388ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
38905bd1b63426e07d1df7a1b40bf845dc94ab995a8Behdad Esfahbod  hb_ot_layout_position_start (c->font, c->buffer);
390ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
391967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
392ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
393744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod    hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
394744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     c->buffer->props.direction,
395744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     &c->buffer->pos[i].x_advance,
396744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     &c->buffer->pos[i].y_advance);
3978b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod    hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
3988b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 c->buffer->props.direction,
3998b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 &c->buffer->pos[i].x_offset,
4008b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 &c->buffer->pos[i].y_offset);
401ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
402ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
403ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
404ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
4052bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbodhb_zero_mark_advances (hb_ot_shape_context_t *c)
4062bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod{
4072bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod  unsigned int count = c->buffer->len;
4082bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
4092bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod    if (c->buffer->info[i].glyph_props() & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
4102bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod    {
4112bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod      c->buffer->pos[i].x_advance = 0;
4122bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod      c->buffer->pos[i].y_advance = 0;
4132bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod    }
4142bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod}
4152bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod
4162bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbodstatic void
417c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
418c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod{
419c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
420b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  if (hb_ot_layout_has_positioning (c->face))
421b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  {
422b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
423b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
424b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    unsigned int count = c->buffer->len;
425b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
426b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod      hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
427b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      HB_DIRECTION_LTR,
428b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      &c->buffer->pos[i].x_offset,
429b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      &c->buffer->pos[i].y_offset);
430b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    }
431c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
432b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    c->plan->map.position (c->font, c->buffer);
433c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
434b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
435b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod      hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
436b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   HB_DIRECTION_LTR,
437b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   &c->buffer->pos[i].x_offset,
438b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   &c->buffer->pos[i].y_offset);
439b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    }
4402118fdb9f584e6735e904638e48bae48314372faBehdad Esfahbod
4410594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    c->applied_position_complex = true;
4421e7d860613032e40a3f90e2caa2ee5ac44ab8c8cBehdad Esfahbod  } else
4432bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod    hb_zero_mark_advances (c);
4442bc3b9a616cedbc56ff4a915f9e3439ff3a6bf13Behdad Esfahbod
4451e7d860613032e40a3f90e2caa2ee5ac44ab8c8cBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->buffer, c->plan->shaper->zero_width_attached_marks);
446c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
447c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  return;
448c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod}
449c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
450c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodstatic void
451967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
452ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
453ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
454ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
455ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
456ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
457967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
458ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
459ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
460967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
461ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
462a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    hb_position_t x_kern, y_kern, kern1, kern2;
4637e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod    hb_font_get_glyph_kerning_for_direction (c->font,
4647e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
4657e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     c->buffer->props.direction,
4667e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     &x_kern, &y_kern);
467a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
468a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = x_kern >> 1;
469a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = x_kern - kern1;
470967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
471967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
472967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
473a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
474a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = y_kern >> 1;
475a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = y_kern - kern1;
476a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i - 1].y_advance += kern1;
477a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_advance += kern2;
478a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_offset += kern2;
479ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
480ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
481ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
482ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
483967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
484ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
485967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
486ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
487ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
488d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbodstatic void
489d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbodhb_hide_zerowidth (hb_ot_shape_context_t *c)
490d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod{
491d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_codepoint_t space;
492d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  if (!hb_font_get_glyph (c->font, ' ', 0, &space))
493d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    return; /* No point! */
494d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
495d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  unsigned int count = c->buffer->len;
496d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  for (unsigned int i = 0; i < count; i++)
497b70021f7c81a0ed08475b14b07291f662cd9f905Behdad Esfahbod    if (unlikely (!is_a_ligature (c->buffer->info[i]) &&
498b70021f7c81a0ed08475b14b07291f662cd9f905Behdad Esfahbod		  _hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
499d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->info[i].codepoint = space;
500d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->pos[i].x_advance = 0;
501d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->pos[i].y_advance = 0;
502d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    }
503d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod}
504d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
505ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
506ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
507ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
508967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
509e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
510ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
511b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  c->buffer->deallocate_var_all ();
512b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
513d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  /* Save the original direction, we use it later. */
514bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->target_direction = c->buffer->props.direction;
515d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
516d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
517d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
518b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
519330a2af3ff0e12c01b3b451357b8bdc83b2e9b47Behdad Esfahbod  c->buffer->clear_output ();
520330a2af3ff0e12c01b3b451357b8bdc83b2e9b47Behdad Esfahbod
521d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_set_unicode_props (c->buffer);
522fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod
5234ff0d2d9dfc4f7e4880a4e964ca9872624508ea0Behdad Esfahbod  hb_form_clusters (c->buffer);
5244ff0d2d9dfc4f7e4880a4e964ca9872624508ea0Behdad Esfahbod
525aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  hb_ensure_native_direction (c->buffer);
52654d1a0d2b2c4ffe15494967122c6422ecb1fc80bBehdad Esfahbod
5277e34601dededd521bcef15111e39293df3d0d13dBehdad Esfahbod  _hb_ot_shape_normalize (c->font, c->buffer,
528693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			  c->plan->shaper->normalization_preference ?
529693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			  c->plan->shaper->normalization_preference (c->plan->shaper, &c->buffer->props) :
530693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			  HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT);
531655586fe5e1fadf2a2ef7826e61ee9a445ffa37aBehdad Esfahbod
532b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_ot_shape_setup_masks (c);
53398370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
5341094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
535fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
536967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
537bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
53836925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
539fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
5401094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
5411094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
542fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
543967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
544ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
54536925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
546ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
547967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
548fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
549967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
550ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
551967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
552967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
553ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
554fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
555967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
556fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
557ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
558d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_hide_zerowidth (c);
559d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
560d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
561d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
562b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
563bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->buffer->props.direction = c->target_direction;
5649f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
5659f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  c->buffer->deallocate_var_all ();
566967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
567967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
568fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod
56902aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbodhb_bool_t
570bd26b4d21f59312805d294f46f15182adbcc47daBehdad Esfahbod_hb_ot_shape (hb_shape_plan_t    *shape_plan,
571bd26b4d21f59312805d294f46f15182adbcc47daBehdad Esfahbod	      hb_font_t          *font,
5726bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      hb_buffer_t        *buffer,
5736bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      const hb_feature_t *features,
5746bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      unsigned int        num_features)
575967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
576e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
577e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_ot_shape_internal (&c);
57802aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbod
5790594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
580ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
5811827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5821827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5831827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbodvoid
5841827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbodhb_ot_shape_glyphs_closure (hb_font_t          *font,
5851827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    hb_buffer_t        *buffer,
5861827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    const hb_feature_t *features,
5871827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    unsigned int        num_features,
5881827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    hb_set_t           *glyphs)
5891827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod{
5901827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_ot_shape_plan_t plan;
5911827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5921827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  buffer->guess_properties ();
5931827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
594e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  /* TODO cache / ensure correct backend, etc. */
595e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_shape_plan_t *shape_plan = hb_shape_plan_create (font->face, &buffer->props, features, num_features, NULL);
5961827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5971827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* TODO: normalization? have shapers do closure()? */
5981827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* TODO: Deal with mirrored chars? */
5991827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_map_glyphs (font, buffer);
6001827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
6011827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* Seed it.  It's user's responsibility to have cleard glyphs
6021827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod   * if that's what they desire. */
6031827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  unsigned int count = buffer->len;
6041827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
6051827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod    hb_set_add (glyphs, buffer->info[i].codepoint);
6061827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
6071827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* And find transitive closure. */
6081827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_set_t copy;
6091827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  copy.init ();
6101827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
6111827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  do {
6121827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod    copy.set (glyphs);
613e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod    HB_SHAPER_DATA_GET (shape_plan)->map.substitute_closure (font->face, glyphs);
6141827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  } while (!copy.equal (glyphs));
615e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod
616e82061e8db922f0ddbefd5a184ee2f9f967b9a05Behdad Esfahbod  hb_shape_plan_destroy (shape_plan);
6171827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod}
618