hb-ot-shape.cc revision 5b88908f12ad1d828dd6075fb8fc0036c2d6af3a
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
29967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod#include "hb-ot-shape-private.h"
302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
310e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod#include "hb-open-type-private.hh"
320e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
332014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod#include "hb-ot-layout.h"
342014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
35acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
36acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
37acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
3875f3469ca6d626b08eb411984a2ba7fd48ca5b5fBehdad Esfahbod/* XXX vertical */
392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodhb_tag_t default_features[] = {
40bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','a','l','t'),
412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
43bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','s','w','h'),
44bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
452014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
46bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','i','g','a'),
47bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','o','c','l'),
482014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','a','r','k'),
492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','k','m','k'),
50bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('r','l','i','g')
512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
522014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
536b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbodstruct lookup_map {
546b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  unsigned int index;
556b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  hb_mask_t mask;
566b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod};
576b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
582014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
592014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
60476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbodadd_lookups (hb_face_t    *face,
612014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     hb_tag_t      table_tag,
622014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int  feature_index,
636b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod	     hb_mask_t     mask,
646b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod	     lookup_map   *lookups,
652014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int *num_lookups,
662014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int  room_lookups)
672014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
682014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i = room_lookups - *num_lookups;
696b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  lookups += *num_lookups;
706b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
716b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  unsigned int *lookup_indices = (unsigned int *) lookups;
726b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
732014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
742014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod					   &i,
756b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod					   lookup_indices);
766b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
772014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  *num_lookups += i;
786b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
796b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  while (i--) {
806b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    lookups[i].mask = mask;
816b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    lookups[i].index = lookup_indices[i];
826b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  }
832014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
842014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
852014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic int
862014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodcmp_lookups (const void *p1, const void *p2)
872014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
886b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  const lookup_map *a = (const lookup_map *) p1;
896b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  const lookup_map *b = (const lookup_map *) p2;
902014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
916b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  return a->index - b->index;
922014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
932014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
9481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
952422c4b96d32037a5cdaef4c427ed1d25db5f647Behdad Esfahbod#define MAX_FEATURES 100 /* FIXME */
9681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
970e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbodstruct hb_mask_allocator_t {
9881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
990e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  struct feature_info_t {
1000e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_tag_t tag;
1010e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int value;
10281a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod    unsigned int seq;
1030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    bool global;
10481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1050e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static int
1060e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    cmp (const void *p1, const void *p2)
1070e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    {
1084e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_info_t *a = reinterpret_cast<const feature_info_t *>(p1);
1094e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_info_t *b = reinterpret_cast<const feature_info_t *>(p2);
11081c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1110e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (a->tag != b->tag)
1120e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        return a->tag < b->tag ? -1 : 1;
11381c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
11481a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod      return a->seq < b->seq ? -1 : 1;
1150e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
1160e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  };
11781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1180e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  struct feature_map_t {
1190e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_tag_t tag; /* should be first */
1200e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int index;
1210e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int shift;
1220e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_mask_t mask;
1230e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1240e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static int
1250e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    cmp (const void *p1, const void *p2)
12681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod    {
1274e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_map_t *a = reinterpret_cast<const feature_map_t *>(p1);
1284e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_map_t *b = reinterpret_cast<const feature_map_t *>(p2);
1290e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1300e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
1310e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
1320e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  };
1330e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
134f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t (void) : count (0) {}
1350e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
136e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  void add_feature (hb_tag_t tag,
137e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod		    unsigned int value,
138e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod		    bool global)
13981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
1400e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    feature_info_t *info = &infos[count++];
1410e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    info->tag = tag;
142e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    info->value = value;
14381a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod    info->seq = count;
1440e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    info->global = global;
14581c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
14681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
147f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  void compile (hb_face_t *face,
148f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		hb_tag_t table_tag,
149f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		unsigned int script_index,
150f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		unsigned int language_index)
15181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
1520e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    global_mask = 0;
1534e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    unsigned int next_bit = 1;
1540e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    if (!count)
1560e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      return;
1570e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
1590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int j = 0;
1610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    for (unsigned int i = 1; i < count; i++)
1620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (infos[i].tag != infos[j].tag)
1630e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	infos[++j] = infos[i];
1640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      else {
1658af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	if (infos[i].global)
1668af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	  infos[j] = infos[i];
1678af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	else {
1688af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	  infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
1690e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	  infos[j].value = MAX (infos[j].value, infos[i].value);
1708af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	}
1710e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      }
1720e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    count = j + 1;
1730e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1740e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    /* Allocate bits now */
1750e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    j = 0;
1760e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
1770e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      const feature_info_t *info = &infos[i];
1780e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      unsigned int bits_needed;
1800e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1810e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && info->value == 1)
1820e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        /* Uses the global bit */
1830e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        bits_needed = 0;
1840e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      else
1850e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        bits_needed = _hb_bit_storage (info->value);
1860e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1870e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
1880e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        continue; /* Feature disabled, or not enough bits. */
1890e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1900e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      unsigned int feature_index;
1910e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
1920e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod					       info->tag, &feature_index))
1930e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        continue;
1940e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      feature_map_t *map = &maps[j++];
1960e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1970e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map->tag = info->tag;
1980e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map->index = feature_index;
1990e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && info->value == 1) {
2006cf63132dc771e1bcd5627720daf4bd2ea0800a5Behdad Esfahbod        /* Use the global bit */
2010e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        map->shift = 0;
2020e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->mask = 1;
2030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      } else {
2040e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->shift = next_bit;
2050e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
2060e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	next_bit += bits_needed;
2070e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      }
2080e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && map->mask != 1)
2100e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        global_mask |= map->mask;
2110e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
2120e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    count = j;
21381c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
21481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2150e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t get_global_mask (void) { return global_mask; }
216efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
217efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  const feature_map_t *get_features (unsigned int *num_features) const {
218efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    *num_features = count;
219efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    return maps;
220efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
221efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
222efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
2230e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const feature_map_t *find_feature (hb_tag_t tag) const {
2240e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
2250e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
2260e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    return map ? map : &off_map;
22781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
22881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2290e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2300e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  private:
2310e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2320e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  unsigned int count;
2330e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  feature_info_t infos[MAX_FEATURES];
2340e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2354e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  feature_map_t maps[MAX_FEATURES];
2360e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t global_mask;
2370e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod};
23881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
240f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbodhb_ot_shape_setup_lookups (hb_ot_shape_context_t *c,
241f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod			   lookup_map            *lookups,
242f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod			   unsigned int          *num_lookups)
2432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
244f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t allocator;
2450e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
246967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
2470e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
248e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
249e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
2500e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2510e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
252e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
2534e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
2540e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
2560e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
2570e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
2580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
2600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
261d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
262e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (default_features[i], 1, true);
263e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
2645b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
265e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
266967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
267967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
268e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
269e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
2700e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2710e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2720e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Compile features */
2735360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int script_index, language_index, feature_index;
2745360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int room_lookups;
2755360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
2765360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_table_choose_script (c->face, c->table_tag,
2775360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    hb_ot_tags_from_script (c->buffer->props.script),
2785360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    &script_index);
2795360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_script_find_language (c->face, c->table_tag, script_index,
2805360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     hb_ot_tag_from_language (c->buffer->props.language),
2815360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     &language_index);
2825360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
283affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  allocator.compile (c->face, c->table_tag, script_index, language_index);
2840e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2850e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
286efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Gather lookup indices for features */
2870e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2885360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  room_lookups = *num_lookups;
2895360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  *num_lookups = 0;
2905360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
291affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  if (hb_ot_layout_language_get_required_feature_index (c->face, c->table_tag, script_index, language_index,
29227f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod							&feature_index))
293476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbod    add_lookups (c->face, c->table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
29427f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod
2950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const hb_mask_allocator_t::feature_map_t *map;
296efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  unsigned int num_features;
29781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
298efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  map = allocator.get_features (&num_features);
299d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned i = 0; i < num_features; i++)
300efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    add_lookups (c->face, c->table_tag, map[i].index, map[i].mask, lookups, num_lookups, room_lookups);
30181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
3020e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Sort lookups and merge duplicates */
303e53d77142ac4ecbe38ab3235491fa93cb7ff16abBehdad Esfahbod
3042014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
3052014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3062014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  if (*num_lookups)
3072014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  {
308d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    unsigned int j = 0;
309d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    for (unsigned int i = 1; i < *num_lookups; i++)
3106b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      if (lookups[i].index != lookups[j].index)
3112014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	lookups[++j] = lookups[i];
3126b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      else
3136b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod        lookups[j].mask |= lookups[i].mask;
3146b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    j++;
3152014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    *num_lookups = j;
3162014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
317efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
318efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
319efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Set masks in buffer */
320efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
3215b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  hb_mask_t global_mask = allocator.get_global_mask ();
3225b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  if (global_mask)
3235b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
3245b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
325d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++)
326efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
327efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    hb_feature_t *feature = &c->features[i];
328efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    map = allocator.find_feature (feature->tag);
329efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
330efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod      c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
331efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
332efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
3335b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
3342014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3352014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
337967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
338967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
3392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
34081c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
343967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
344967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3452014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
346affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GSUB;
347affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
348f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
350d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
351967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
3522014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
353967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
354967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3552014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3562014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
357967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
358967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
3592014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
360967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3612014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3622014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
363967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
364967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3652014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
366affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GPOS;
367affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
368f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3692014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
370d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
371967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
3722014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
373967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
3742014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
375967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
376967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3772014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
378ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
379ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
380ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
381ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
382ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
383ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
384ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
385ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
386ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
387ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
388ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
389ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
390ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
391ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
392ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
393ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
394ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
395ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
396ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
397bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod    if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
398ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod      buffer->info[i].cluster = buffer->info[i - 1].cluster;
399ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
400ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
401967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
402ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
403ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
404967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_direction_t direction = buffer->props.direction;
405ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
406ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
407967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
408967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (buffer->props.script))
409ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
410ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
4114e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
412ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
413ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
414ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
415ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
416ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
417ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
418ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
419ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_mirror_chars (hb_buffer_t *buffer)
420ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
421bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
422ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4234e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
424ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
425ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4264e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod//  map = allocator.find_feature (HB_TAG ('r','t','l','m'));
4274e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
428ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
429ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
4301094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
4311094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    if (likely (codepoint == buffer->info[i].codepoint))
4324e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod;//      buffer->info[i].mask |= map->mask;
4331094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
4341094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod      buffer->info[i].codepoint = codepoint;
435ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
436ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
437ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
438ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
439ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
440ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
441ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
442ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
443ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
444ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
445ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
446a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
447ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
448a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
449a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
450a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
451a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
452ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
453a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
454ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
455ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
456f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
457f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
458a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
459ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
460ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
461ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
462967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
463ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
464967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
465ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
466ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
467ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
468967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
469ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
470ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
471ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
472ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
473ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
474ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
475ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
476ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
477967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
478ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
479967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
480ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
481967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
482ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
483ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_glyph_metrics_t metrics;
484967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
485967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance = metrics.x_advance;
486967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].y_advance = metrics.y_advance;
487ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
488ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
489ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
490ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
491967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
492ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
493ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
494ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
495ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
496ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
497967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
498ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
499ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
500967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
501ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
502ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
503967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
504ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
505ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
506967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
507967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
508967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
509ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
510ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
511ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
512ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
513967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
514ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
515967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
516ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
517ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
518ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
519ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
520ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
521967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
522967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
523ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
524967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_form_clusters (c->buffer);
525ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
5261094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
527fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
528967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5291094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
530fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
531967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_mirror_chars (c->buffer);
532ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
533967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ensure_native_direction (c->buffer);
534ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
535967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
536bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
537967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_substitute_complex (c);
538ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
539967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
540967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
541fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
5421094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
5431094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
544fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
545967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5461094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
547967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
548ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
549967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_position_complex (c);
550ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
551967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
552fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
553967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
554ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
555967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
556967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
557ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
558fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
559967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
560fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
561ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
562967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
563967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
564967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
565967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
566967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
567967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
568967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
569967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_feature_t *features,
570967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     unsigned int  num_features)
571967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
572967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
573967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
574967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  /* Setup transient context members */
575967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c.original_direction = buffer->props.direction;
576967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
577967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_internal (&c);
578ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
579acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
580acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
581acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
582