hb-ot-shape.cc revision bbbbe80ec9bc45c5b685bc09c8f993e98496555c
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
893a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
902989be4919242670c94825bded96db20a7b2035bBehdad Esfahbod
91d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  for (unsigned int i = 0; i < c->num_user_features; i++)
92efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
93d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod    const hb_feature_t *feature = &c->user_features[i];
944924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
954924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      unsigned int shift;
96605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
97bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
984924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    }
99efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
1002014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1012014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
1022014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
103967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
10436925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
1052014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
106967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
107967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1082014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
109605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  c->plan->map.substitute (c->face, c->buffer);
1102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
111967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
112967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1142014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
115967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
11636925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
1172014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
1182014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
119967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
120967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1212014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
122605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  c->plan->map.position (c->font, c->face, c->buffer);
1232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
1241e7c1fcbc33599faefc32d4a28e5d8506d2c56faBehdad Esfahbod  hb_ot_layout_position_finish (c->buffer);
1252014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
126967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
127967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1282014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
129ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
130ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
131ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
132ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
133ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
134ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
135ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
136ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
137ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
138ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
139ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
140ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
141ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
142ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
143ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
1443a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodhb_set_unicode_props (hb_ot_shape_context_t *c)
1453a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod{
1463a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_unicode_get_general_category_func_t get_general_category = c->buffer->unicode->v.get_general_category;
1473a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_unicode_get_combining_class_func_t get_combining_class = c->buffer->unicode->v.get_combining_class;
1483a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_glyph_info_t *info = c->buffer->info;
1493a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
1503a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  unsigned int count = c->buffer->len;
1513a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
1523a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod    info[i].general_category() = get_general_category (info[i].codepoint);
1533a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod    info[i].combining_class() = get_combining_class (info[i].codepoint);
1543a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  }
1553a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod}
1563a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
1573a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbodstatic void
1586b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_form_clusters (hb_ot_shape_context_t *c)
159ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1606b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  unsigned int count = c->buffer->len;
161ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1623a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod    if (c->buffer->info[i].general_category() == HB_CATEGORY_NON_SPACING_MARK)
1636b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
164ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
165ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
166967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
1676b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_ensure_native_direction (hb_ot_shape_context_t *c)
168ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1696b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_direction_t direction = c->buffer->props.direction;
170ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
171ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
172967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
1736b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
174ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
1756b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_buffer_reverse_clusters (c->buffer);
1766b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
177ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
178ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
179ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
18098370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbodstatic void
18198370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbodhb_reset_glyph_infos (hb_ot_shape_context_t *c)
18298370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod{
18398370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod  unsigned int count = c->buffer->len;
18498370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
18598370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod    c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
18698370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod}
18798370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
188ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
189ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
190ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
191ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
192aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
193ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
194aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = c->buffer->unicode->v.get_mirroring;
195ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
196bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
197ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
198ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
19939dede9ffffe732f78cbd092ccb3b48d77ddd66dBehdad Esfahbod  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
2004e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
201aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
202ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
203aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (c->buffer->info[i].codepoint);
204aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
205bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
2061094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
207aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
208ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
209ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
210ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
211ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
212ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
213ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
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))) {
223dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod      buffer->replace_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
224a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
225ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
226dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod      buffer->replace_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
227ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
228ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
229f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
230dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod    buffer->replace_glyph (hb_font_get_glyph (font, face, 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{
237967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, 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{
252967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
253ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
254967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
255ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
256502f4cba3e0bcd625d31f8fd295b8b18e2d02a5aBehdad Esfahbod    hb_font_get_glyph_advance (c->font, c->face, 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++) {
274ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
275967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
276ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
277ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
278967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
279967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
280967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
281ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
282ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
283ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
284ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
285967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
286ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
287967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
288ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
289ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
290ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
291ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
292ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
293967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
294fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
295ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
296d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod  /* Save the original direction, we use it later. */
297bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->target_direction = c->buffer->props.direction;
298d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
2993a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
300ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
3013a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
3023a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod
303ee8aaf976a6eb42be49b63b4c51c7a0a338e0298Behdad Esfahbod  hb_ensure_native_direction (c);
304ee8aaf976a6eb42be49b63b4c51c7a0a338e0298Behdad Esfahbod
3053a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_form_clusters (c);
306fc96596b7c1c4e62491e951a3c256fb00dcde550Behdad Esfahbod
3073a852ae7fe6edfaadd75625d27515a3689503395Behdad Esfahbod  hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
30898370e89d1bff248737b482d129c2a4deb8bfd95Behdad Esfahbod
3091094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
310fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
311fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
312aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_mirror_chars (c);
313ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
314967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
315bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
31636925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
317ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
318967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
319967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
320fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
3211094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
3221094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
323fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
324967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
325ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
32636925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
327ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
328967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
329fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
330967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
331ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
332967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
333967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
334ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
335fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
336967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
337fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
338ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
339bbbbe80ec9bc45c5b685bc09c8f993e98496555cBehdad Esfahbod  c->buffer->props.direction = c->target_direction;
340967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
341967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
342967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
343605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbodhb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
344d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   hb_face_t                *face,
34549baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod			   const hb_segment_properties_t  *props,
346d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   const hb_feature_t       *user_features,
347d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod			   unsigned int              num_user_features)
348d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod{
34949baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod  plan->shaper = hb_ot_shape_complex_categorize (props);
35049baa1f69efb0e3c62e45bd59dd88459a84bf390Behdad Esfahbod
351605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
352d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
353605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  plan->map.compile (face, props);
354d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod}
355d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod
356d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbodvoid
357fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodhb_ot_shape_execute (hb_ot_shape_plan_t *plan,
358fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_font_t          *font,
359fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_face_t          *face,
360fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     hb_buffer_t        *buffer,
361fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     const hb_feature_t *user_features,
362fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod		     unsigned int        num_user_features)
363fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod{
364fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_context_t c = {plan, font, face, buffer, user_features, num_user_features};
365fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute_internal (&c);
366fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod}
367fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod
368fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbodvoid
369967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
370967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
371967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
372d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod	     const hb_feature_t *user_features,
373d2ba016ca1ba7489537768b619980d5159b5870cBehdad Esfahbod	     unsigned int        num_user_features)
374967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
375605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_plan_t plan;
376967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
377605ed468f380f86d642031f6451447d270cb6de1Behdad Esfahbod  hb_ot_shape_plan_internal (&plan, face, &buffer->props, user_features, num_user_features);
378fd3d32d31cb6d74a9994b6850d539fd0b707d941Behdad Esfahbod  hb_ot_shape_execute (&plan, font, face, buffer, user_features, num_user_features);
379ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
380acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
381acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
382acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
383