hb-ot-shape.cc revision a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9
12014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod/*
22409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 2009,2010  Red Hat, Inc.
32409d5f8d7dd8b535ce5ea29e933f7db27d33793Behdad Esfahbod * Copyright © 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
3272657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod#include "hb-font-private.hh"
3372657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod
34acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
35acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
36acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
3775f3469ca6d626b08eb411984a2ba7fd48ca5b5fBehdad Esfahbod/* XXX vertical */
382014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodhb_tag_t default_features[] = {
39bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','a','l','t'),
402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
42bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
44bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','i','g','a'),
45bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','o','c','l'),
462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','a','r','k'),
472014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','k','m','k'),
48bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('r','l','i','g')
492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
52605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_plan_t       *plan,
5349baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			      const hb_segment_properties_t  *props,
54895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod			      const hb_feature_t       *user_features,
55895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod			      unsigned int              num_user_features)
562014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
57895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  switch (props->direction) {
580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
59605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
60605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
63605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
64605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
660e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
670e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
683286fc0e9adc3f2874c9409e7fdb09e4d2b7dda1Behdad Esfahbod    case HB_DIRECTION_INVALID:
690e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
700e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
710e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
720e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
73d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
74605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod    plan->map.add_bool_feature (default_features[i]);
75e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
7649baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod  hb_ot_shape_complex_collect_features (plan, props);
77e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
78895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod  for (unsigned int i = 0; i < num_user_features; i++) {
79895fb5d364e7ae5d9d2e34b9f68b8651804369efBehdad Esfahbod    const hb_feature_t *feature = &user_features[i];
80605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod    plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
81e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
82e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
83e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
84e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
85e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
8652601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbodhb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
87e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
88605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_mask_t global_mask = c->plan->map.get_global_mask ();
8957ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod  c->buffer->reset_masks (global_mask);
905b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
913a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
922989be4919242670c94825bded96db20a7b2035bBehdad 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
1261e7c1fcbc33599faefc32d4a28e5d8506d2c56faBehdad Esfahbod  hb_ot_layout_position_finish (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
1463a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodhb_set_unicode_props (hb_ot_shape_context_t *c)
1473a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod{
148fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod  hb_unicode_funcs_t *unicode = c->buffer->unicode;
1493a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_glyph_info_t *info = c->buffer->info;
1503a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
1513a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  unsigned int count = c->buffer->len;
1523a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
153fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod    info[i].general_category() = unicode->get_general_category (info[i].codepoint);
154fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod    info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
1553a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  }
1563a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod}
1573a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
1583a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodstatic void
1596b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_form_clusters (hb_ot_shape_context_t *c)
160ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1616b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  unsigned int count = c->buffer->len;
162ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
16399b74760a4cddc798ab44b5ca897486bbb9c76d6Behdad Esfahbod    if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
1646b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
165ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
166ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
167967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
1686b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_ensure_native_direction (hb_ot_shape_context_t *c)
169ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1706b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_direction_t direction = c->buffer->props.direction;
171ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
172ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
173967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
1743286fc0e9adc3f2874c9409e7fdb09e4d2b7dda1Behdad Esfahbod      direction != hb_script_get_horizontal_direction (c->buffer->props.script))
175ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
1766b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_buffer_reverse_clusters (c->buffer);
1776b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
178ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
179ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
180ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
18198370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbodstatic void
18298370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbodhb_reset_glyph_infos (hb_ot_shape_context_t *c)
18398370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod{
18498370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod  unsigned int count = c->buffer->len;
18598370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
18698370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod    c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
18798370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod}
18898370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
189ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
190ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
191ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
192ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
193aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
194ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
195fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod  hb_unicode_funcs_t *unicode = c->buffer->unicode;
196ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
197bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
198ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
199ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
20039dede9ffffe732f78cbd092ccb3b48d77ddd66dBehdad Esfahbod  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
2014e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
202aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
203ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
204fb194b8794898f51eb596fa4092c26606889d376Behdad Esfahbod    hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
205aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
206bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
2071094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
208aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
209ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
210ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
211ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
212ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
213ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
214ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
215ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
216ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
217ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
218ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
219a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
220ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
221a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
222a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
22372657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod      buffer->replace_glyph (hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
224a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
225ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
22672657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod      buffer->replace_glyph (hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0));
227ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
228ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
229f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
23072657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod    buffer->replace_glyph (hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0));
231a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
232ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
233ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
234ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
235967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
236ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
23772657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_map_glyphs (c->font, c->buffer);
238ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
239ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
240ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
241967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
242ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
243ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
244ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
245ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
246ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
247ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
248ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
249ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
250967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
251ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
2528f0d7e0c3fd4b05c43ac449be4f374dc2dc56127Behdad Esfahbod  hb_ot_layout_position_start (c->buffer);
253ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
254967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
255ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
25672657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod    hb_font_get_glyph_advance (c->font, c->buffer->info[i].codepoint,
257502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod			       &c->buffer->pos[i].x_advance,
258502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod			       &c->buffer->pos[i].y_advance);
259ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
260ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
261ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
262ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
263967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
264ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
265ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
266ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
267ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
268ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
269967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
270ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
271ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
272967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
273ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
274a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    hb_position_t x_kern, y_kern, kern1, kern2;
275a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    hb_font_get_kerning (c->font, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint, &x_kern, &y_kern);
276a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
277a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = x_kern >> 1;
278a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = x_kern - kern1;
279967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
280967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
281967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
282a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod
283a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern1 = y_kern >> 1;
284a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    kern2 = y_kern - kern1;
285a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i - 1].y_advance += kern1;
286a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_advance += kern2;
287a513dbcf73ab1cc39a7c9653034904d0c6cd9fe9Behdad Esfahbod    c->buffer->pos[i].y_offset += kern2;
288ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
289ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
290ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
291ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
292967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
293ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
294967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
295ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
296ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
297ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
298ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
299ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
300967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
301fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
302ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
303d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  /* Save the original direction, we use it later. */
304bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->target_direction = c->buffer->props.direction;
305d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
3063a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
307ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
3083a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
3093a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
310ee8aaf976a6eb42be49b63b4c51c7a0a338e0298Behdad Esfahbod  hb_ensure_native_direction (c);
311ee8aaf976a6eb42be49b63b4c51c7a0a338e0298Behdad Esfahbod
3123a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_form_clusters (c);
313fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod
3143a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
31598370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
3161094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
317fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
318fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
319aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_mirror_chars (c);
320ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
321967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
322bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
32336925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
324ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
325967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
326967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
327fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
3281094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
3291094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
330fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
331967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
332ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
33336925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
334ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
335967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
336fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
337967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
338ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
339967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
340967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
341ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
342fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
343967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
344fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
345ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
346bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->buffer->props.direction = c->target_direction;
347967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
348967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
349967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
350605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
351d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   hb_face_t                *face,
35249baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			   const hb_segment_properties_t  *props,
353d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   const hb_feature_t       *user_features,
354d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   unsigned int              num_user_features)
355d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod{
35649baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod  plan->shaper = hb_ot_shape_complex_categorize (props);
35749baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod
358605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
359d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
360605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  plan->map.compile (face, props);
361d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod}
362d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
363d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbodvoid
364fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute (hb_ot_shape_plan_t *plan,
365fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_font_t          *font,
366fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_buffer_t        *buffer,
367fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     const hb_feature_t *user_features,
368fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     unsigned int        num_user_features)
369fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod{
37072657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
371fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute_internal (&c);
372fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod}
373fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod
374fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodvoid
3755814dfa3f5aa41bc3df06b78980d57d7bea0ba58Behdad Esfahbodhb_ot_shape (hb_font_t          *font,
3765814dfa3f5aa41bc3df06b78980d57d7bea0ba58Behdad Esfahbod	     hb_buffer_t        *buffer,
3775814dfa3f5aa41bc3df06b78980d57d7bea0ba58Behdad Esfahbod	     const hb_feature_t *features,
3785814dfa3f5aa41bc3df06b78980d57d7bea0ba58Behdad Esfahbod	     unsigned int        num_features)
379967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
3806843569d2c70c1771ce964e3d1a4cf91e14e7687Behdad Esfahbod  hb_ot_shape_plan_t plan = hb_ot_shape_plan_t ();
381967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
38272657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
38372657e4ce757dcb055a8db7291b68f96f0d34bfbBehdad Esfahbod  hb_ot_shape_execute (&plan, font, buffer, features, num_features);
384ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
385acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
386acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
387acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
388