hb-ot-shape.cc revision e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1
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
240e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodhb_ot_shape_collect_features (hb_ot_shape_context_t *c,
241e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod			      hb_mask_allocator_t   *allocator)
2422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
243967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
2440e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
245e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod      allocator->add_feature (HB_TAG ('l','t','r','a'), 1, true);
246e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod      allocator->add_feature (HB_TAG ('l','t','r','m'), 1, true);
2470e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2480e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
249e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod      allocator->add_feature (HB_TAG ('r','t','l','a'), 1, true);
250e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod      allocator->add_feature (HB_TAG ('r','t','l','m'), 1, false);
2510e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2520e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
2530e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
2540e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
2550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2560e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
2570e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
258d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
259e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod    allocator->add_feature (default_features[i], 1, true);
260e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
2615b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
262e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
263967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
264967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
265e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod    allocator->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
266e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
267e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod}
268e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
269e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod
270e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodstatic void
271e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbodhb_ot_shape_setup_lookups (hb_ot_shape_context_t *c,
272e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod			   lookup_map            *lookups,
273e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod			   unsigned int          *num_lookups)
274e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod{
275e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod  hb_mask_allocator_t allocator;
2760e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
277e89b7d2a61b7f58e6c7cec00d5ce2246dee1e8a1Behdad Esfahbod  hb_ot_shape_collect_features (c, &allocator);
2780e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Compile features */
2805360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int script_index, language_index, feature_index;
2815360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  unsigned int room_lookups;
2825360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
2835360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_table_choose_script (c->face, c->table_tag,
2845360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    hb_ot_tags_from_script (c->buffer->props.script),
2855360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				    &script_index);
2865360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  hb_ot_layout_script_find_language (c->face, c->table_tag, script_index,
2875360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     hb_ot_tag_from_language (c->buffer->props.language),
2885360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod				     &language_index);
2895360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
290affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  allocator.compile (c->face, c->table_tag, script_index, language_index);
2910e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2920e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
293efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Gather lookup indices for features */
2940e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2955360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  room_lookups = *num_lookups;
2965360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod  *num_lookups = 0;
2975360ce0c5c33f921b3f9ad3f42529a19df5ad0feBehdad Esfahbod
298affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  if (hb_ot_layout_language_get_required_feature_index (c->face, c->table_tag, script_index, language_index,
29927f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod							&feature_index))
300476c94218b4f5b8e119e82b0e10b641e0c10bf56Behdad Esfahbod    add_lookups (c->face, c->table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
30127f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod
3020e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const hb_mask_allocator_t::feature_map_t *map;
303efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  unsigned int num_features;
30481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
305efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  map = allocator.get_features (&num_features);
306d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned i = 0; i < num_features; i++)
307efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    add_lookups (c->face, c->table_tag, map[i].index, map[i].mask, lookups, num_lookups, room_lookups);
30881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
3090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Sort lookups and merge duplicates */
310e53d77142ac4ecbe38ab3235491fa93cb7ff16abBehdad Esfahbod
3112014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
3122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  if (*num_lookups)
3142014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  {
315d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    unsigned int j = 0;
316d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod    for (unsigned int i = 1; i < *num_lookups; i++)
3176b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      if (lookups[i].index != lookups[j].index)
3182014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	lookups[++j] = lookups[i];
3196b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      else
3206b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod        lookups[j].mask |= lookups[i].mask;
3216b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    j++;
3222014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    *num_lookups = j;
3232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
324efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
325efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
326efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  /* Set masks in buffer */
327efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
3285b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  hb_mask_t global_mask = allocator.get_global_mask ();
3295b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  if (global_mask)
3305b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
3315b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod
332d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++)
333efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  {
334efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    hb_feature_t *feature = &c->features[i];
335efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    map = allocator.find_feature (feature->tag);
336efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
337efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod      c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
338efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod  }
339efe0d682e860ffd23a1d17c68c8273f17d51c1c9Behdad Esfahbod
3405b88908f12ad1d828dd6075fb8fc0036c2d6af3aBehdad Esfahbod  /* complex */
3412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
344967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
345967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
3462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
34781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3482014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
350967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
351967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3522014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
353affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GSUB;
354affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
355f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3562014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
357d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
358967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
3592014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
360967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
361967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3622014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3632014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
364967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
365967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
3662014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
367967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3682014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3692014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
370967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
371967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3722014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
373affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GPOS;
374affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
375f1d07885dc358e79c237e824c94b3320c0a9c17dBehdad Esfahbod  hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
3762014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
377d9c726078828d50db62e05407a3f38f2e7607533Behdad Esfahbod  for (unsigned int i = 0; i < num_lookups; i++)
378967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
3792014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
380967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
3812014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
382967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
383967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3842014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
385ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
386ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
387ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
388ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
389ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
390ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
391ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
392ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
393ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
394ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
395ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
396ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
397ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
398ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
399ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
400ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
401ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
402ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
403ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
404bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod    if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
405ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod      buffer->info[i].cluster = buffer->info[i - 1].cluster;
406ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
407ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
408967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
409ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
410ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
411967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_direction_t direction = buffer->props.direction;
412ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
413ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
414967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
415967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (buffer->props.script))
416ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
417ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
4184e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
419ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
420ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
421ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
422ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
423ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
424ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
425ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
426ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_mirror_chars (hb_buffer_t *buffer)
427ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
428bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
429ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4304e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
431ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
432ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4334e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod//  map = allocator.find_feature (HB_TAG ('r','t','l','m'));
4344e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
435ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
436ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
4371094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
4381094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    if (likely (codepoint == buffer->info[i].codepoint))
4394e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod;//      buffer->info[i].mask |= map->mask;
4401094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
4411094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod      buffer->info[i].codepoint = codepoint;
442ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
443ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
444ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
445ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
446ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
447ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
448ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
449ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
450ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
451ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
452ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
453a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
454ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
455a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
456a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
457a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
458a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
459ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
460a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
461ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
462ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
463f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
464f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
465a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
466ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
467ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
468ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
469967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
470ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
471967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
472ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
473ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
474ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
475967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
476ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
477ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
478ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
479ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
480ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
481ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
482ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
483ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
484967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
485ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
486967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
487ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
488967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
489ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
490ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_glyph_metrics_t metrics;
491967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
492967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance = metrics.x_advance;
493967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].y_advance = metrics.y_advance;
494ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
495ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
496ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
497ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
498967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
499ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
500ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
501ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
502ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
503ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
504967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
505ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
506ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
507967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
508ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
509ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
510967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
511ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
512ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
513967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
514967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
515967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
516ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
517ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
518ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
519ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
520967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
521ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
522967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
523ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
524ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
525ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
526ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
527ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
528967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
529967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
530ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
531967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_form_clusters (c->buffer);
532ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
5331094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
534fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
535967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5361094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
537fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
538967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_mirror_chars (c->buffer);
539ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
540967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ensure_native_direction (c->buffer);
541ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
542967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
543bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
544967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_substitute_complex (c);
545ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
546967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
547967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
548fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
5491094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
5501094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
551fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
552967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5531094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
554967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
555ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
556967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_position_complex (c);
557ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
558967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
559fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
560967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
561ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
562967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
563967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
564ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
565fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
566967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
567fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
568ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
569967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
570967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
571967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
572967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
573967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
574967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
575967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
576967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_feature_t *features,
577967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     unsigned int  num_features)
578967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
579967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
580967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
581967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  /* Setup transient context members */
582967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c.original_direction = buffer->props.direction;
583967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
584967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_internal (&c);
585ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
586acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
587acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
588acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
589