hb-ot-shape.cc revision 52601275d5e4000dada4f925fb78723eeeee7bd4
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"
302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
314924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod#include "hb-ot-map-private.hh"
322014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
33acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
34acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
35acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
3675f3469ca6d626b08eb411984a2ba7fd48ca5b5fBehdad Esfahbod/* XXX vertical */
372014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodhb_tag_t default_features[] = {
38bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','a','l','t'),
392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
41bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','s','w','h'),
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
5236925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_context_t *c)
532014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
54967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
56aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->map->add_bool_feature (HB_TAG ('l','t','r','a'));
57aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->map->add_bool_feature (HB_TAG ('l','t','r','m'));
580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
60aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->map->add_bool_feature (HB_TAG ('r','t','l','a'));
61aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->map->add_bool_feature (HB_TAG ('r','t','l','m'), false);
620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
630e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
660e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
670e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
680e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
69d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
70aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    c->map->add_bool_feature (default_features[i]);
71e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
725b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
73e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
74967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
75967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
7636925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    c->map->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
77e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
78e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
79e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
80e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
81e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
8252601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbodhb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
83e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
8436925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod  hb_mask_t global_mask = c->map->get_global_mask ();
855b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  if (global_mask)
865b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
875b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
88d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++)
89efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
90efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    hb_feature_t *feature = &c->features[i];
914924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
924924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      unsigned int shift;
9336925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod      hb_mask_t mask = c->map->get_mask (feature->tag, &shift);
944924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
954924affe0f0adf75f2a0e2137a71206b0576d63fBehdad Esfahbod    }
96efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
97efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
985b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
992014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1002014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
1012014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
102967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
10336925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
1042014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
105967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
106967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1072014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
10836925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod  c->map->substitute (c);
1092014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
110967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
111967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
1132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
114967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
11536925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
1162014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
1172014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
118967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
119967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
1202014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
12136925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod  c->map->position (c);
1222014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
123967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
1242014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
125967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
126967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
1272014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
128ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
129ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
130ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
131ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
132ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
133ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
134ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
135ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
136ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
137ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
138ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
139ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
140ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
141ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
142ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
1436b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_form_clusters (hb_ot_shape_context_t *c)
144ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1456b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  unsigned int count = c->buffer->len;
146ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1476b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    if (c->buffer->unicode->v.get_general_category (c->buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
1486b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
149ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
150ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
151967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
1526b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbodhb_ensure_native_direction (hb_ot_shape_context_t *c)
153ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
1546b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_direction_t direction = c->buffer->props.direction;
155ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
156ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
157967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
1586b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
159ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
1606b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_buffer_reverse_clusters (c->buffer);
1616b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
162ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
163ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
164ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
165ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
166ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
167ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
168ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
169aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbodhb_mirror_chars (hb_ot_shape_context_t *c)
170ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
171aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = c->buffer->unicode->v.get_mirroring;
172ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
173aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (c->buffer->props.direction))
174ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
175ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
176aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  hb_mask_t rtlm_mask = c->map->get_mask (HB_TAG ('r','t','l','m'));
1774e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
178aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod  unsigned int count = c->buffer->len;
179ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
180aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (c->buffer->info[i].codepoint);
181aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    if (likely (codepoint == c->buffer->info[i].codepoint))
182aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].mask |= rtlm_mask;
1831094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
184aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod      c->buffer->info[i].codepoint = codepoint;
185ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
186ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
187ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
188ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
189ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
190ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
191ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
192ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
193ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
194ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
195ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
196a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
197ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
198a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
199a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
200a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
201a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
202ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
203a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
204ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
205ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
206f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
207f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
208a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
209ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
210ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
211ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
212967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
213ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
214967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
215ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
216ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
217ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
218967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
219ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
220ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
221ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
222ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
223ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
224ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
225ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
226ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
227967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
228ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
229967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
230ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
231967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
232ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
233ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_glyph_metrics_t metrics;
234967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
235967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance = metrics.x_advance;
236967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].y_advance = metrics.y_advance;
237ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
238ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
239ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
240ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
241967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
242ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
243ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
244ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
245ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
246ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
247967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
248ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
249ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
250967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
251ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
252ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
253967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
254ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
255ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
256967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
257967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
258967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
259ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
260ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
261ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
262ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
263967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
264ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
265967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
266ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
267ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
268ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
269ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
270ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
271967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
272967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
273ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
27452601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbod  hb_ot_shape_setup_masks (c);
275b897c607d91d569f4eaa681d1f5b3d9f3d2bb093Behdad Esfahbod
2766b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod  hb_form_clusters (c);
277ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
2781094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
279fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
280967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
2811094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
282fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
283aa9c450bb2d7c3c9e36ea32e3558250391a0582dBehdad Esfahbod    hb_mirror_chars (c);
284ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
2856b7e6758626268ba1c7c266128e618ec73ae2c0fBehdad Esfahbod    hb_ensure_native_direction (c);
286ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
287967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
288bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
28936925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_substitute_complex (c);
290ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
291967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
292967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
293fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
2941094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
2951094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
296fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
297967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
2981094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
299967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
300ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
30136925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod    hb_ot_position_complex (c);
302ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
303967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
304fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
305967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
306ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
307967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
308967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
309ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
310fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
311967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
312fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
313ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
314967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
315967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
316967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
317967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
318967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
319967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
320967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
321967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_feature_t *features,
322967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     unsigned int  num_features)
323967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
324967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
32536925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod  hb_ot_map_t map;
326967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
327967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  /* Setup transient context members */
328967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c.original_direction = buffer->props.direction;
32936925f695d349a53d52ecc3a58f18240a6977463Behdad Esfahbod  c.map = &map;
330967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
33152601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbod  hb_ot_shape_collect_features (&c);
33252601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbod  c.map->compile (&c);
33352601275d5e4000dada4f925fb78723eeeee7bd4Behdad Esfahbod
334967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_internal (&c);
335ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
336acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
337acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
338acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
339