hb-ot-shape.cc revision 967240dd8b96802345ef273e75427066e91ea8fb
12014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod/*
20e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod * Copyright (C) 2009,2010  Red Hat, Inc.
32014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
4c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
52014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
62014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * Permission is hereby granted, without written agreement and without
72014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
82014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * software and its documentation for any purpose, provided that the
92014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * all copies of this software.
112014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
122014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
132014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
142014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
152014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
162014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * DAMAGE.
172014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
182014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
192014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
202014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
212014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
222014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
232014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod *
242014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
252014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod */
262014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
27967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod#include "hb-ot-shape-private.h"
282014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
290e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod#include "hb-open-type-private.hh"
300e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
312014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod#include "hb-ot-layout.h"
322014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
33acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
34acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
35acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
3675f3469ca6d626b08eb411984a2ba7fd48ca5b5fBehdad Esfahbod/* XXX vertical */
372014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodhb_tag_t default_features[] = {
38bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','a','l','t'),
392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','c','m','p'),
402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('c','l','i','g'),
41bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','s','w','h'),
42bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('c','u','r','s'),
432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('k','e','r','n'),
44bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','i','g','a'),
45bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('l','o','c','l'),
462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','a','r','k'),
472014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  HB_TAG('m','k','m','k'),
48bd0987386b3a4dddf208ccf1a70ebfff6242ba73Behdad Esfahbod  HB_TAG('r','l','i','g')
492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod};
502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
516b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbodstruct lookup_map {
526b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  unsigned int index;
536b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  hb_mask_t mask;
546b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod};
556b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
562014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
572014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
582014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodadd_feature (hb_face_t    *face,
592014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     hb_tag_t      table_tag,
602014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int  feature_index,
616b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod	     hb_mask_t     mask,
626b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod	     lookup_map   *lookups,
632014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int *num_lookups,
642014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	     unsigned int  room_lookups)
652014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
662014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i = room_lookups - *num_lookups;
676b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  lookups += *num_lookups;
686b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
696b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  unsigned int *lookup_indices = (unsigned int *) lookups;
706b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
712014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
722014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod					   &i,
736b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod					   lookup_indices);
746b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
752014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  *num_lookups += i;
766b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod
776b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  while (i--) {
786b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    lookups[i].mask = mask;
796b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    lookups[i].index = lookup_indices[i];
806b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  }
812014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
822014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
832014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic int
842014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodcmp_lookups (const void *p1, const void *p2)
852014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
866b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  const lookup_map *a = (const lookup_map *) p1;
876b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  const lookup_map *b = (const lookup_map *) p2;
882014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
896b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod  return a->index - b->index;
902014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
912014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
9281c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
932422c4b96d32037a5cdaef4c427ed1d25db5f647Behdad Esfahbod#define MAX_FEATURES 100 /* FIXME */
9481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbodstruct hb_mask_allocator_t {
9681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
970e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  struct feature_info_t {
980e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_tag_t tag;
990e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int value;
10081a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod    unsigned int seq;
1010e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    bool global;
10281c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static int
1040e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    cmp (const void *p1, const void *p2)
1050e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    {
1064e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_info_t *a = reinterpret_cast<const feature_info_t *>(p1);
1074e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_info_t *b = reinterpret_cast<const feature_info_t *>(p2);
10881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (a->tag != b->tag)
1100e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        return a->tag < b->tag ? -1 : 1;
11181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
11281a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod      return a->seq < b->seq ? -1 : 1;
1130e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
1140e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  };
11581c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
1160e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  struct feature_map_t {
1170e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_tag_t tag; /* should be first */
1180e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int index;
1190e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int shift;
1200e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    hb_mask_t mask;
1210e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1220e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static int
1230e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    cmp (const void *p1, const void *p2)
12481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod    {
1254e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_map_t *a = reinterpret_cast<const feature_map_t *>(p1);
1264e573715ae5f5ed486ad66382bb44c47a86591ffBehdad Esfahbod      const feature_map_t *b = reinterpret_cast<const feature_map_t *>(p2);
1270e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1280e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
1290e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
1300e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  };
1310e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
132f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t (void) : count (0) {}
1330e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
134e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  void add_feature (hb_tag_t tag,
135e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod		    unsigned int value,
136e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod		    bool global)
13781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
1380e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    feature_info_t *info = &infos[count++];
1390e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    info->tag = tag;
140e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    info->value = value;
14181a77b115db401f69e869690f24b9047370bdfdeBehdad Esfahbod    info->seq = count;
1420e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    info->global = global;
14381c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
14481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
145f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  void compile (hb_face_t *face,
146f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		hb_tag_t table_tag,
147f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		unsigned int script_index,
148f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod		unsigned int language_index)
14981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
1500e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    global_mask = 0;
1514e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    unsigned int next_bit = 1;
1520e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1530e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    if (!count)
1540e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      return;
1550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1560e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
1570e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    unsigned int j = 0;
1590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    for (unsigned int i = 1; i < count; i++)
1600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (infos[i].tag != infos[j].tag)
1610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	infos[++j] = infos[i];
1620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      else {
1638af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	if (infos[i].global)
1648af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	  infos[j] = infos[i];
1658af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	else {
1668af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	  infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
1670e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	  infos[j].value = MAX (infos[j].value, infos[i].value);
1688af45fda475d075c5a285002463a00a0423d3926Behdad Esfahbod	}
1690e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      }
1700e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    count = j + 1;
1710e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1720e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    /* Allocate bits now */
1730e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    j = 0;
1740e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    for (unsigned int i = 0; i < count; i++) {
1750e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      const feature_info_t *info = &infos[i];
1760e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1770e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      unsigned int bits_needed;
1780e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && info->value == 1)
1800e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        /* Uses the global bit */
1810e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        bits_needed = 0;
1820e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      else
1830e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        bits_needed = _hb_bit_storage (info->value);
1840e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1850e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
1860e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        continue; /* Feature disabled, or not enough bits. */
1870e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1880e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      unsigned int feature_index;
1890e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
1900e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod					       info->tag, &feature_index))
1910e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        continue;
1920e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1930e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      feature_map_t *map = &maps[j++];
1940e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
1950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map->tag = info->tag;
1960e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map->index = feature_index;
1970e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && info->value == 1) {
1986cf63132dc771e1bcd5627720daf4bd2ea0800a5Behdad Esfahbod        /* Use the global bit */
1990e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        map->shift = 0;
2000e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->mask = 1;
2010e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      } else {
2020e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->shift = next_bit;
2030e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
2040e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod	next_bit += bits_needed;
2050e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      }
2060e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2070e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      if (info->global && map->mask != 1)
2080e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod        global_mask |= map->mask;
2090e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    }
2100e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    count = j;
21181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
21281c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2130e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t get_global_mask (void) { return global_mask; }
2140e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const feature_map_t *find_feature (hb_tag_t tag) const {
2150e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
2160e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
2170e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    return map ? map : &off_map;
21881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
21981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2200e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2210e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  private:
2220e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2230e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  unsigned int count;
2240e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  feature_info_t infos[MAX_FEATURES];
2250e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2264e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  feature_map_t maps[MAX_FEATURES];
2270e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t global_mask;
2280e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod};
22981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbodstatic void
231967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodsetup_lookups (hb_ot_shape_context_t *c,
232967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	       hb_tag_t               table_tag,
233967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	       lookup_map            *lookups,
234967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	       unsigned int          *num_lookups)
2352014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
2362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i, j, script_index, language_index, feature_index, room_lookups;
2372014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
2382014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  room_lookups = *num_lookups;
2392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  *num_lookups = 0;
2402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
241967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_table_choose_script (c->face, table_tag,
242967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod				    hb_ot_tags_from_script (c->buffer->props.script),
2432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod				    &script_index);
244967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_script_find_language (c->face, table_tag, script_index,
245967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod				     hb_ot_tag_from_language (c->buffer->props.language),
2462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod				     &language_index);
2472014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
24881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
249f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t allocator;
2500e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
251967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
2520e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
253e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
254e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
2550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2560e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
257e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
2584e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
2590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
2610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
2620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
2630e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
2650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2660e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
267e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (default_features[i], 1, true);
268e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
269e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  /* XXX complex-shaper features go here */
270e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
271967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
272967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
273e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
274e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
2750e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2760e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2770e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Compile features */
278967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  allocator.compile (c->face, table_tag, script_index, language_index);
2790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2800e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2810e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Gather lookup indices for features and set buffer masks at the same time */
2820e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
283967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (hb_ot_layout_language_get_required_feature_index (c->face, table_tag, script_index, language_index,
28427f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod							&feature_index))
285967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    add_feature (c->face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
28627f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod
2870e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const hb_mask_allocator_t::feature_map_t *map;
28881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2890e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t global_mask = allocator.get_global_mask ();
2900e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  if (global_mask)
291967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
292074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod
293967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
294074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_LTR:
2950e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('l','t','r','a'));
296967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
2970e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('l','t','r','m'));
298967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
299074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
300074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_RTL:
3010e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('r','t','l','a'));
302967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
3034e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      map = allocator.find_feature (HB_TAG ('r','t','l','m'));
304967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
305074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
306074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_TTB:
307074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_BTT:
308074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    default:
309074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
3102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
3112014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
31281c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
3137f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod  {
3140e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    map = allocator.find_feature (default_features[i]);
315967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
31681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
3176774463883978b00b4d8c719ed75edfc4537c77fBehdad Esfahbod
318967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (i = 0; i < c->num_features; i++)
31981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
320967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_feature_t *feature = &c->features[i];
3210e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    map = allocator.find_feature (feature->tag);
322967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
3230e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
324967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
3257f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod  }
3267f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod
32781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
3280e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Sort lookups and merge duplicates */
329e53d77142ac4ecbe38ab3235491fa93cb7ff16abBehdad Esfahbod
3302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
3312014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3322014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  if (*num_lookups)
3332014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  {
3342014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    for (i = 1, j = 0; i < *num_lookups; i++)
3356b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      if (lookups[i].index != lookups[j].index)
3362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	lookups[++j] = lookups[i];
3376b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      else
3386b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod        lookups[j].mask |= lookups[i].mask;
3396b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    j++;
3402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    *num_lookups = j;
3412014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
3422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3432014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3442014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
345967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
346967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_substitute_complex (hb_ot_shape_context_t *c)
3472014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
34881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3492014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i;
3512014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
352967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
353967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3542014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
355967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  setup_lookups (c, HB_OT_TAG_GSUB, lookups, &num_lookups);
3562014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3572014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  for (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  unsigned int i;
3702014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
371967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
372967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3732014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
374967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  setup_lookups (c, HB_OT_TAG_GPOS, lookups, &num_lookups);
3752014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3762014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  for (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