hb-ot-shape.cc revision 502f4cba3e0bcd625d31f8fd295b8b18e2d02a5a
12014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod/*
20e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod * Copyright (C) 2009,2010  Red Hat, Inc.
3476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbod * Copyright (C) 2010  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"
3013403bc67a01e0d4908fb964093fd02ddd11c580Behdad Esfahbod#include "hb-ot-shape-complex-private.hh"
312014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
32acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
33acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
34acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
3575f3469ca6d626b08eb411984a2ba7fd48ca5b5fBehdad Esfahbod/* XXX vertical */
362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodhb_tag_t default_features[] = {
37bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','a','l','t'),
382014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
40bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','s','w','h'),
41bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
43bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','i','g','a'),
44bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','o','c','l'),
452014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','a','r','k'),
462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','k','m','k'),
47bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('r','l','i','g')
482014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
51605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_plan_t       *plan,
5249baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			      const hb_segment_properties_t  *props,
53895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod			      const hb_feature_t       *user_features,
54895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod			      unsigned int              num_user_features)
552014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
56895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  switch (props->direction) {
570e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
58605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
59605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
62605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
63605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
660e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
670e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
680e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
690e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
700e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
71d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
72605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod    plan->map.add_bool_feature (default_features[i]);
73e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
7449baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod  hb_ot_shape_complex_collect_features (plan, props);
75e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
76895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  for (unsigned int i = 0; i < num_user_features; i++) {
77895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod    const hb_feature_t *feature = &user_features[i];
78605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod    plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
79e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
80e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
81e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
82e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
83e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
8452601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbodhb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
85e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
86605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_mask_t global_mask = c->plan->map.get_global_mask ();
8757ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod  c->buffer->reset_masks (global_mask);
885b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
892989be4919242670c94825bded96db20a7b2035bBehdad Esfahbod  hb_ot_shape_complex_setup_masks (c);
902989be4919242670c94825bded96db20a7b2035bBehdad Esfahbod
91bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod  c->buffer->reset_masks (global_mask);
92bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod
93d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  for (unsigned int i = 0; i < c->num_user_features; i++)
94efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
95d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod    const hb_feature_t *feature = &c->user_features[i];
964924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
974924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      unsigned int shift;
98605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
99bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
1004924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    }
101efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
1022014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1032014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
1042014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
105967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
10636925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
1072014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
108967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
109967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
111605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  c->plan->map.substitute (c->face, c->buffer);
1122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
113967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
114967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1152014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1162014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
117967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
11836925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
1192014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
1202014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
121967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
122967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
124605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  c->plan->map.position (c->font, c->face, c->buffer);
1252014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
126967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
1272014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
128967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
129967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
131ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
132ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
133ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
134ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
135ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
136ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
137ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
138ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
139ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
140ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
141ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
142ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
143ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
144ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
145ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
1466b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_form_clusters (hb_ot_shape_context_t *c)
147ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1486b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  unsigned int count = c->buffer->len;
149ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1506b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    if (c->buffer->unicode->v.get_general_category (c->buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
1516b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
152ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
153ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
154967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
1556b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_ensure_native_direction (hb_ot_shape_context_t *c)
156ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1576b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_direction_t direction = c->buffer->props.direction;
158ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
159ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
160967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
1616b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
162ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
1636b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_buffer_reverse_clusters (c->buffer);
1646b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
165ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
166ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
167ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
168ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
169ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
170ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
171ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
172aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
173ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
174aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = c->buffer->unicode->v.get_mirroring;
175ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
176aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->buffer->props.direction))
177ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
178ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
17939dede9ffffe732f78cbd092ccb3b48d77ddd66dBehdad Esfahbod  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
1804e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
181aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
182ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
183aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (c->buffer->info[i].codepoint);
184aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
185bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
1861094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
187aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
188ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
189ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
190ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
191ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
192ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
193ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
194ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
195ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
196ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
197ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
198ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
199a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
200ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
201a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
202a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
203a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
204a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
205ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
206a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
207ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
208ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
209f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
210f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
211a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
212ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
213ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
214ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
215967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
216ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
217967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
218ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
219ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
220ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
221967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
222ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
223ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
224ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
225ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
226ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
227ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
228ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
229ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
230967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
231ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
232967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
233ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
234967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
235ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
236502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod    hb_font_get_glyph_advance (c->font, c->face, c->buffer->info[i].codepoint,
237502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod			       &c->buffer->pos[i].x_advance,
238502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod			       &c->buffer->pos[i].y_advance);
239ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
240ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
241ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
242ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
243967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
244ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
245ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
246ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
247ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
248ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
249967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
250ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
251ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
252967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
253ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
254ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
255967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
256ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
257ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
258967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
259967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
260967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
261ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
262ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
263ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
264ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
265967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
266ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
267967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
268ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
269ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
270ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
271ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
272ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
273967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
274fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
275ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
276d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  /* Save the original direction, we use it later. */
277d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  c->original_direction = c->buffer->props.direction;
278d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
2796b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_form_clusters (c);
280ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
281fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod  hb_ot_shape_setup_masks (c);
282fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod
2831094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
284fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
285fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
286aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_mirror_chars (c);
287ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
2886b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_ensure_native_direction (c);
289ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
290967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
291bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
29236925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
293ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
294967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
295967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
296fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
2971094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
2981094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
299fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
300967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
301ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
30236925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
303ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
304967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
305fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
306967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
307ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
308967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
309967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
310ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
311fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
312967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
313fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
314ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
315967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
316967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
317967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
318967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
319605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
320d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   hb_face_t                *face,
32149baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			   const hb_segment_properties_t  *props,
322d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   const hb_feature_t       *user_features,
323d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   unsigned int              num_user_features)
324d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod{
32549baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod  plan->shaper = hb_ot_shape_complex_categorize (props);
32649baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod
327605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
328d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
329605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  plan->map.compile (face, props);
330d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod}
331d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
332d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbodvoid
333fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute (hb_ot_shape_plan_t *plan,
334fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_font_t          *font,
335fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_face_t          *face,
336fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_buffer_t        *buffer,
337fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     const hb_feature_t *user_features,
338fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     unsigned int        num_user_features)
339fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod{
340fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_context_t c = {plan, font, face, buffer, user_features, num_user_features};
341fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute_internal (&c);
342fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod}
343fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod
344fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodvoid
345967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
346967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
347967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
348d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod	     const hb_feature_t *user_features,
349d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod	     unsigned int        num_user_features)
350967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
351605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_plan_t plan;
352967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
353605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_plan_internal (&plan, face, &buffer->props, user_features, num_user_features);
354fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute (&plan, font, face, buffer, user_features, num_user_features);
355ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
356acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
357acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
358acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
359