hb-ot-shape.cc revision 5360ce0c5c33f921b3f9ad3f42529a19df5ad0fe
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
24098aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbodhb_ot_shape_setup_lookups_complex (hb_ot_shape_context_t *c,
24198aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod				   lookup_map            *lookups,
24298aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod				   unsigned int          *num_lookups)
24398aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod{
24498aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod  /* XXX */
24598aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod}
24698aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod
24798aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbodstatic void
248f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbodhb_ot_shape_setup_lookups (hb_ot_shape_context_t *c,
249f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod			   lookup_map            *lookups,
250f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod			   unsigned int          *num_lookups)
2512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
252f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t allocator;
2530e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
254967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
2550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
256e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
257e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
2580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
260e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
2614e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
2620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2630e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
2640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
2650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
2660e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2670e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
2680e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
269d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
270e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (default_features[i], 1, true);
271e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
27298aa3f65446496dc250d9b01d98cacfdf1157e06Behdad Esfahbod  hb_ot_shape_setup_lookups_complex (c, lookups, num_lookups);
273e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
274967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
275967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
276e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
277e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
2780e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2800e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Compile features */
2815360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int script_index, language_index, feature_index;
2825360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int room_lookups;
2835360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
2845360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_table_choose_script (c->face, c->table_tag,
2855360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    hb_ot_tags_from_script (c->buffer->props.script),
2865360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    &script_index);
2875360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_script_find_language (c->face, c->table_tag, script_index,
2885360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     hb_ot_tag_from_language (c->buffer->props.language),
2895360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     &language_index);
2905360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
291affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  allocator.compile (c->face, c->table_tag, script_index, language_index);
2920e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2930e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
294efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Gather lookup indices for features */
2950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2965360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  room_lookups = *num_lookups;
2975360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  *num_lookups = 0;
2985360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
299affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  if (hb_ot_layout_language_get_required_feature_index (c->face, c->table_tag, script_index, language_index,
30027f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod							&feature_index))
301476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbod    add_lookups (c->face, c->table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
30227f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod
3030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const hb_mask_allocator_t::feature_map_t *map;
304efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  unsigned int num_features;
30581c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
306efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  map = allocator.get_features (&num_features);
307d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned i = 0; i < num_features; i++)
308efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    add_lookups (c->face, c->table_tag, map[i].index, map[i].mask, lookups, num_lookups, room_lookups);
30981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
3100e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Sort lookups and merge duplicates */
311e53d77142ac4ecbe38ab3235491fa93cb7ff16abBehdad Esfahbod
3122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
3132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3142014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  if (*num_lookups)
3152014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  {
316d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    unsigned int j = 0;
317d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    for (unsigned int i = 1; i < *num_lookups; i++)
3186b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      if (lookups[i].index != lookups[j].index)
3192014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	lookups[++j] = lookups[i];
3206b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      else
3216b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod        lookups[j].mask |= lookups[i].mask;
3226b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    j++;
3232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    *num_lookups = j;
3242014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
325efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
326efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
327efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Set masks in buffer */
328efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
329d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++)
330efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
331efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    hb_feature_t *feature = &c->features[i];
332efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    map = allocator.find_feature (feature->tag);
333efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
334efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod      c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
335efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
336efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
337efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  hb_mask_t global_mask = allocator.get_global_mask ();
338efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  if (global_mask)
339efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
3402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
343967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
344967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
3452014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
34681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3472014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3482014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
349967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
350967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
352affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GSUB;
353affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
354f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3552014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
356d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
357967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
3582014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
359967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
360967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3612014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3622014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
363967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
364967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
3652014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
366967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3672014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3682014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
369967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
370967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3712014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
372affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GPOS;
373affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
374f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3752014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
376d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
377967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
3782014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
379967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
3802014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
381967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
382967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3832014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
384ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
385ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
386ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
387ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
388ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
389ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
390ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
391ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
392ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
393ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
394ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
395ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
396ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
397ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
398ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
399ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
400ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
401ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
402ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
403bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod    if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
404ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod      buffer->info[i].cluster = buffer->info[i - 1].cluster;
405ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
406ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
407967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
408ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
409ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
410967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_direction_t direction = buffer->props.direction;
411ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
412ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
413967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
414967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (buffer->props.script))
415ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
416ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
4174e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
418ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
419ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
420ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
421ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
422ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
423ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
424ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
425ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_mirror_chars (hb_buffer_t *buffer)
426ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
427bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
428ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4294e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
430ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
431ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4324e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod//  map = allocator.find_feature (HB_TAG ('r','t','l','m'));
4334e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
434ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
435ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
4361094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
4371094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    if (likely (codepoint == buffer->info[i].codepoint))
4384e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod;//      buffer->info[i].mask |= map->mask;
4391094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
4401094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod      buffer->info[i].codepoint = codepoint;
441ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
442ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
443ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
444ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
445ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
446ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
447ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
448ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
449ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
450ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
451ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
452a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
453ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
454a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
455a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
456a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
457a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
458ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
459a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
460ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
461ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
462f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
463f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
464a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
465ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
466ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
467ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
468967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
469ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
470967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
471ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
472ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
473ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
474967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
475ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
476ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
477ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
478ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
479ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
480ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
481ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
482ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
483967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
484ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
485967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
486ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
487967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
488ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
489ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_glyph_metrics_t metrics;
490967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
491967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance = metrics.x_advance;
492967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].y_advance = metrics.y_advance;
493ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
494ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
495ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
496ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
497967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
498ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
499ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
500ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
501ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
502ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
503967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
504ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
505ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
506967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
507ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
508ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
509967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
510ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
511ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
512967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
513967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
514967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
515ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
516ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
517ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
518ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
519967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
520ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
521967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
522ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
523ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
524ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
525ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
526ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
527967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
528967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
529ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
530967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_form_clusters (c->buffer);
531ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
5321094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
533fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
534967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5351094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
536fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
537967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_mirror_chars (c->buffer);
538ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
539967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ensure_native_direction (c->buffer);
540ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
541967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
542bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
543967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_substitute_complex (c);
544ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
545967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
546967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
547fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
5481094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
5491094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
550fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
551967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5521094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
553967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
554ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
555967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_position_complex (c);
556ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
557967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
558fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
559967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
560ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
561967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
562967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
563ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
564fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
565967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
566fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
567ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
568967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
569967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
570967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
571967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
572967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
573967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
574967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
575967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_feature_t *features,
576967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     unsigned int  num_features)
577967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
578967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
579967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
580967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  /* Setup transient context members */
581967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c.original_direction = buffer->props.direction;
582967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
583967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_internal (&c);
584ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
585acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
586acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
587acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
588