hb-ot-shape.cc revision 0594a2448440208efa0acac9a5d8d52d43108289
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
294924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod#include "hb-ot-shape-private.hh"
30b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod#include "hb-ot-shape-normalize-private.hh"
312014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3272657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod#include "hb-font-private.hh"
331827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod#include "hb-set-private.hh"
3472657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod
35acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
36acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
37f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodhb_tag_t common_features[] = {
382014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
39a5f1834f57ea3fb254f5c7d372747de316fcc8f1Behdad Esfahbod  HB_TAG('l','i','g','a'),
4089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('l','o','c','l'),
4189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('m','a','r','k'),
4289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('m','k','m','k'),
4389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('r','l','i','g'),
4489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod};
4589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
46d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
4789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbodhb_tag_t horizontal_features[] = {
4889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('c','a','l','t'),
492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
50bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
5289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod};
5389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
5489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod/* Note:
5589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * Technically speaking, vrt2 and vert are mutually exclusive.
5689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * According to the spec, valt and vpal are also mutually exclusive.
5789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod * But we apply them all for now.
5889a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod */
5989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbodhb_tag_t vertical_features[] = {
6089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','a','l','t'),
6189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','e','r','t'),
6289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','k','r','n'),
6389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','p','a','l'),
6489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_TAG('v','r','t','2'),
652014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
662014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
67b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
68b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
69b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstruct hb_ot_shape_planner_t
70b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod{
71b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_map_builder_t map;
72b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_complex_shaper_t shaper;
73b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
74b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_shape_planner_t (void) : map () {}
75b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  ~hb_ot_shape_planner_t (void) { map.finish (); }
76b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
77b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  inline void compile (hb_face_t *face,
78b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod		       const hb_segment_properties_t *props,
79b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod		       struct hb_ot_shape_plan_t &plan)
80b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  {
81b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod    plan.shaper = shaper;
82b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod    map.compile (face, props, plan.map);
83b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  }
84b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
85b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  private:
86b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  NO_COPY (hb_ot_shape_planner_t);
87b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod};
88b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
892014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
9090645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
9149baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			      const hb_segment_properties_t  *props,
9290645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod			      const hb_feature_t             *user_features,
9390645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod			      unsigned int                    num_user_features)
942014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
95895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  switch (props->direction) {
960e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
9790645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
9890645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
990e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1000e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
10190645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
10290645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod      planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
1030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1040e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
1050e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
1063286fc0e9adc3f2874c9409e7fdb09e4d2b7dda1Behdad Esfahbod    case HB_DIRECTION_INVALID:
1070e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
1080e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
1090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
1100e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
11189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod#define ADD_FEATURES(array) \
11289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  HB_STMT_START { \
11389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
11489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod      planner->map.add_bool_feature (array[i]); \
11589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  } HB_STMT_END
11689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
11776f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod  hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props);
118e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
11989a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  ADD_FEATURES (common_features);
12089a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
12189a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
12289a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    ADD_FEATURES (horizontal_features);
12389a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod  else
12489a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod    ADD_FEATURES (vertical_features);
12589a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
12689a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod#undef ADD_FEATURES
12789a2bc9ba674e7e57fec1fd8ce7648a44f3aab63Behdad Esfahbod
128895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  for (unsigned int i = 0; i < num_user_features; i++) {
129895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod    const hb_feature_t *feature = &user_features[i];
13090645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod    planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
131e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
132e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
133e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
134e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
135b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstruct hb_ot_shape_context_t
136b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod{
137b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  /* Input to hb_ot_shape_execute() */
138b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_ot_shape_plan_t *plan;
139b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_font_t *font;
140b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_face_t *face;
141b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_buffer_t  *buffer;
142b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  const hb_feature_t *user_features;
143b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  unsigned int        num_user_features;
144b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
145b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  /* Transient stuff */
146b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_direction_t target_direction;
147b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod  hb_bool_t applied_position_complex;
148b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod};
149b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbod
150e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
15152601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbodhb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
152e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
153605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_mask_t global_mask = c->plan->map.get_global_mask ();
15457ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod  c->buffer->reset_masks (global_mask);
1555b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
156acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod  hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
1572989be4919242670c94825bded96db20a7b2035bBehdad Esfahbod
158d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  for (unsigned int i = 0; i < c->num_user_features; i++)
159efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
160d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod    const hb_feature_t *feature = &c->user_features[i];
1614924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
1624924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      unsigned int shift;
163605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
164bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
1654924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    }
166efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
1672014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1682014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
1692014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
170ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
171ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
172ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
173ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
174b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodstatic void
175b9f199c8e38cc5ed0d73845568630f3bcbdd4374Behdad Esfahbodhb_set_unicode_props (hb_buffer_t *buffer)
1763a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod{
177aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  unsigned int count = buffer->len;
1780e3361464b00b76aa7375515163e0710a691db0cBehdad Esfahbod  for (unsigned int i = 0; i < count; i++)
179d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
1803a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod}
1813a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
1823a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodstatic void
183aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
184ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
185aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  unsigned int count = buffer->len;
186ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
187d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
1884a68684654e645882095c1189477146287ce9437Behdad Esfahbod	(FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
1894a68684654e645882095c1189477146287ce9437Behdad Esfahbod	 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
190d63adfc7d09b26764d9166da97372b21257e7611Behdad Esfahbod	 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
191a4385f0b0a6949e2ce49e6a147ad4beaa724f6c3Behdad Esfahbod      buffer->info[i].cluster = buffer->info[i - 1].cluster; /* XXX do the min() here */
192ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
193ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
194967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
195aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
196ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
197aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  hb_direction_t direction = buffer->props.direction;
198ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
1997403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod  /* TODO vertical:
2007403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
2017403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
2027403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod   * first. */
203aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
2047403e055cd1463f38215ad9faedd61c3e1b66ac5Behdad Esfahbod      (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
205ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
206aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
207aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
208ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
209ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
210ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
211ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
212ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
213ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
214ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
215aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
216ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
217fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod  hb_unicode_funcs_t *unicode = c->buffer->unicode;
218ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
219bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
220ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
221ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
22239dede9ffffe732f78cbd092ccb3b48d77ddd66dBehdad Esfahbod  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
2234e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
224aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
225ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
226c4641723fbf6532b2e80a662e15573b31276bc73Behdad Esfahbod    hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
227aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
228bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
2291094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
230aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
231ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
232ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
233ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
234ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
235ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
236ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
237ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
2385d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod  hb_codepoint_t glyph;
2395d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod
240ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
241ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
242ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
243a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
2445d90a342e319068716429bf7af76c3896b61a0e5Behdad Esfahbod
245ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
246468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  for (buffer->idx = 0; buffer->idx < count;) {
24799c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod    if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
24899c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod      hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
249a4385f0b0a6949e2ce49e6a147ad4beaa724f6c3Behdad Esfahbod      buffer->replace_glyphs (2, 1, &glyph);
250ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
25199c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod      hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
2520fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod      buffer->replace_glyph (glyph);
253ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
254ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
255468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  if (likely (buffer->idx < buffer->len)) {
25699c2695759a6af855d565f4994bbdf220570bb48Behdad Esfahbod    hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
2570fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod    buffer->replace_glyph (glyph);
2580fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  }
259468e9cb25c9bc14781b7013e447d763f93bf76a3Behdad Esfahbod  buffer->swap_buffers ();
260ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
261ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
262ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
263967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
264ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
265b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_ot_layout_substitute_start (c->buffer);
266b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
267b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_mirror_chars (c);
268b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
26972657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_map_glyphs (c->font, c->buffer);
270ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
271ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
272ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
273c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
274c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod{
2752118fdb9f584e6735e904638e48bae48314372faBehdad Esfahbod  if (hb_ot_layout_has_substitution (c->face)) {
276b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    c->plan->map.substitute (c->face, c->buffer);
2772118fdb9f584e6735e904638e48bae48314372faBehdad Esfahbod  }
278c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
279b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_ot_layout_substitute_finish (c->buffer);
280c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
281c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  return;
282c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod}
283c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
284ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
285ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
286ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
287ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
288967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
289ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
2908f0d7e0c3fd4b05c43ac449be4f374dc2dc56127Behdad Esfahbod  hb_ot_layout_position_start (c->buffer);
291ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
292967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
293ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
294744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod    hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
295744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     c->buffer->props.direction,
296744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     &c->buffer->pos[i].x_advance,
297744970af4d884cc87ffa645804578fec8df674a9Behdad Esfahbod					     &c->buffer->pos[i].y_advance);
2988b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod    hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
2998b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 c->buffer->props.direction,
3008b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 &c->buffer->pos[i].x_offset,
3018b38faeede41e64eb0f6ac2e12ce51dd7138d50aBehdad Esfahbod						 &c->buffer->pos[i].y_offset);
302ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
303ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
304ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
305ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
306c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
307c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod{
308c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
309b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  if (hb_ot_layout_has_positioning (c->face))
310b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  {
311b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
312b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
313b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    unsigned int count = c->buffer->len;
314b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
315b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod      hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
316b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      HB_DIRECTION_LTR,
317b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      &c->buffer->pos[i].x_offset,
318b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod					      &c->buffer->pos[i].y_offset);
319b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    }
320c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
321b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    c->plan->map.position (c->font, c->buffer);
322c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
323b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
324b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod      hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
325b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   HB_DIRECTION_LTR,
326b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   &c->buffer->pos[i].x_offset,
327b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod						   &c->buffer->pos[i].y_offset);
328b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod    }
3292118fdb9f584e6735e904638e48bae48314372faBehdad Esfahbod
3300594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    c->applied_position_complex = true;
331c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  }
332c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
333c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  hb_ot_layout_position_finish (c->buffer);
334c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
335c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod  return;
336c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod}
337c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbod
338c86f932015bdf5803572b0904d343d3bc033e009Behdad Esfahbodstatic void
339967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
340ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
341ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
342ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
343ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
344ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
345967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
346ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
347ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
348967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
349ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
350a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    hb_position_t x_kern, y_kern, kern1, kern2;
3517e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod    hb_font_get_glyph_kerning_for_direction (c->font,
3527e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
3537e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     c->buffer->props.direction,
3547e2c85de305be59e1a6afa7d2061e4b7dd00acf7Behdad Esfahbod					     &x_kern, &y_kern);
355a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
356a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = x_kern >> 1;
357a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = x_kern - kern1;
358967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
359967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
360967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
361a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
362a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = y_kern >> 1;
363a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = y_kern - kern1;
364a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i - 1].y_advance += kern1;
365a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_advance += kern2;
366a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_offset += kern2;
367ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
368ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
369ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
370ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
371967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
372ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
373967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
374ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
375ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
376d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbodstatic void
377d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbodhb_hide_zerowidth (hb_ot_shape_context_t *c)
378d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod{
379d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  /* TODO Save the space character in the font? */
380d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_codepoint_t space;
381d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  if (!hb_font_get_glyph (c->font, ' ', 0, &space))
382d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    return; /* No point! */
383d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
384d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  unsigned int count = c->buffer->len;
385d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  for (unsigned int i = 0; i < count; i++)
386d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    if (unlikely (_hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
387d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->info[i].codepoint = space;
388d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->pos[i].x_advance = 0;
389d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod      c->buffer->pos[i].y_advance = 0;
390d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod    }
391d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod}
392d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
393ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
394ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
395ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
396967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
397fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
398ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
399b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  c->buffer->deallocate_var_all ();
400b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
401d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  /* Save the original direction, we use it later. */
402bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->target_direction = c->buffer->props.direction;
403d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
404d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
405d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
406b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
407d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_set_unicode_props (c->buffer);
408fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod
4094ff0d2d9dfc4f7e4880a4e964ca9872624508ea0Behdad Esfahbod  hb_form_clusters (c->buffer);
4104ff0d2d9dfc4f7e4880a4e964ca9872624508ea0Behdad Esfahbod
411aa7264123a088936f2043b45d4d41ca7413fabe5Behdad Esfahbod  hb_ensure_native_direction (c->buffer);
41254d1a0d2b2c4ffe15494967122c6422ecb1fc80bBehdad Esfahbod
41311138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper));
414655586fe5e1fadf2a2ef7826e61ee9a445ffa37aBehdad Esfahbod
415b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod  hb_ot_shape_setup_masks (c);
41698370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
4171094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
418fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
419967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
420bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
42136925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
422fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
4231094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
4241094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
425fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
426967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
427ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
42836925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
429ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
430967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
431fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
432967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
433ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
434967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
435967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
436ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
437fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
438967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
439fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
440ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
441d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  hb_hide_zerowidth (c);
442d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod
443d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
444d1deaa2f5bd028e8076265cba92cffa4fa2834acBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
445b65c06025d2b54a44f716e030d4b10072c65bea8Behdad Esfahbod
446bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->buffer->props.direction = c->target_direction;
4479f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
4489f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  c->buffer->deallocate_var_all ();
449967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
450967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
451376dafa6ed414e368e9dc1d5a2e0bfc8e55f410dBehdad Esfahbodstatic void
452605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
453d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   hb_face_t                *face,
45449baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			   const hb_segment_properties_t  *props,
455d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   const hb_feature_t       *user_features,
456d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   unsigned int              num_user_features)
457d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod{
45890645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod  hb_ot_shape_planner_t planner;
45949baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod
460d4adade217a61007dd2da5cd9eccf889f79a019aBehdad Esfahbod  assert (HB_DIRECTION_IS_VALID (props->direction));
461d4adade217a61007dd2da5cd9eccf889f79a019aBehdad Esfahbod
46290645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod  planner.shaper = hb_ot_shape_complex_categorize (props);
463d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
46490645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod  hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
46590645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod
46690645fb24bcbb78183576d3641a99560d87e49f2Behdad Esfahbod  planner.compile (face, props, *plan);
467d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod}
468d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
469376dafa6ed414e368e9dc1d5a2e0bfc8e55f410dBehdad Esfahbodstatic void
470fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute (hb_ot_shape_plan_t *plan,
471fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_font_t          *font,
472fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_buffer_t        *buffer,
473fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     const hb_feature_t *user_features,
474fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     unsigned int        num_user_features)
475fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod{
47672657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
477fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute_internal (&c);
478fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod}
479fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod
48002aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbodhb_bool_t
4816bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod_hb_ot_shape (hb_font_t          *font,
4826bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      hb_buffer_t        *buffer,
4836bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      const hb_feature_t *features,
4846bd9b479b8b2befbb0847282e93beade197c8038Behdad Esfahbod	      unsigned int        num_features)
485967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
486e06d4eda7bbdb3a1be1f1ce8d98b059a0730f14dBehdad Esfahbod  hb_ot_shape_plan_t plan;
487967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
48802aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbod  buffer->guess_properties ();
48902aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbod
49072657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
49172657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_execute (&plan, font, buffer, features, num_features);
49202aeca985b570763342c35e99af90025bfa088d5Behdad Esfahbod
4930594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
494ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
4951827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
4961827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
4971827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbodvoid
4981827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbodhb_ot_shape_glyphs_closure (hb_font_t          *font,
4991827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    hb_buffer_t        *buffer,
5001827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    const hb_feature_t *features,
5011827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    unsigned int        num_features,
5021827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod			    hb_set_t           *glyphs)
5031827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod{
5041827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_ot_shape_plan_t plan;
5051827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5061827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  buffer->guess_properties ();
5071827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5081827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
5091827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5101827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* TODO: normalization? have shapers do closure()? */
5111827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* TODO: Deal with mirrored chars? */
5121827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_map_glyphs (font, buffer);
5131827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5141827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* Seed it.  It's user's responsibility to have cleard glyphs
5151827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod   * if that's what they desire. */
5161827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  unsigned int count = buffer->len;
5171827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
5181827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod    hb_set_add (glyphs, buffer->info[i].codepoint);
5191827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5201827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  /* And find transitive closure. */
5211827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  hb_set_t copy;
5221827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  copy.init ();
5231827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod
5241827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  do {
5251827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod    copy.set (glyphs);
5261827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod    plan.map.substitute_closure (font->face, glyphs);
5271827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod  } while (!copy.equal (glyphs));
5281827dc208c867e433a95237d1ed3fc7a73d1d9a7Behdad Esfahbod}
529