hb-ot-shape.cc revision affc5abac7bdae51df85856a5478d34d96fda4fe
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	       lookup_map            *lookups,
233967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	       unsigned int          *num_lookups)
2342014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
2352014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i, j, script_index, language_index, feature_index, room_lookups;
2362014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
2372014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  room_lookups = *num_lookups;
2382014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  *num_lookups = 0;
2392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
240affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  hb_ot_layout_table_choose_script (c->face, c->table_tag,
241967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod				    hb_ot_tags_from_script (c->buffer->props.script),
2422014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod				    &script_index);
243affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  hb_ot_layout_script_find_language (c->face, c->table_tag, script_index,
244967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod				     hb_ot_tag_from_language (c->buffer->props.language),
2452014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod				     &language_index);
2462014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
24781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
248f062ec6bb24b1c21d37b12adc7e944a5fe53526aBehdad Esfahbod  hb_mask_allocator_t allocator;
2490e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
250967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
2510e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_LTR:
252e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
253e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
2540e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2550e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_RTL:
256e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
2574e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
2580e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2590e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_TTB:
2600e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    case HB_DIRECTION_BTT:
2610e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    default:
2620e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      break;
2630e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  }
2640e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2650e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
266e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (default_features[i], 1, true);
267e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
268e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  /* XXX complex-shaper features go here */
269e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod
270967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (unsigned int i = 0; i < c->num_features; i++) {
271967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    const hb_feature_t *feature = &c->features[i];
272e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod    allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
273e04685ee7be01695ec437fab50f919f1b7423c57Behdad Esfahbod  }
2740e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2750e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2760e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Compile features */
277affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  allocator.compile (c->face, c->table_tag, script_index, language_index);
2780e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2790e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
2800e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Gather lookup indices for features and set buffer masks at the same time */
2810e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod
282affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  if (hb_ot_layout_language_get_required_feature_index (c->face, c->table_tag, script_index, language_index,
28327f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod							&feature_index))
284affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod    add_feature (c->face, c->table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
28527f0b092a13344e8791c496c77a3c1e5de4f887cBehdad Esfahbod
2860e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  const hb_mask_allocator_t::feature_map_t *map;
28781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
2880e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  hb_mask_t global_mask = allocator.get_global_mask ();
2890e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  if (global_mask)
290967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
291074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod
292967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  switch (c->original_direction) {
293074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_LTR:
2940e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('l','t','r','a'));
295affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod      add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
2960e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('l','t','r','m'));
297affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod      add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
298074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
299074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_RTL:
3000e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod      map = allocator.find_feature (HB_TAG ('r','t','l','a'));
301affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod      add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
3024e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod      map = allocator.find_feature (HB_TAG ('r','t','l','m'));
303affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod      add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
304074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
305074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_TTB:
306074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    case HB_DIRECTION_BTT:
307074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod    default:
308074ea787493a37ae8f68d17be7820f13fff57520Behdad Esfahbod      break;
3092014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
3102014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
31181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  for (i = 0; i < ARRAY_LENGTH (default_features); i++)
3127f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod  {
3130e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    map = allocator.find_feature (default_features[i]);
314affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod    add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
31581c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  }
3166774463883978b00b4d8c719ed75edfc4537c77fBehdad Esfahbod
317967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  for (i = 0; i < c->num_features; i++)
31881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  {
319967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_feature_t *feature = &c->features[i];
3200e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    map = allocator.find_feature (feature->tag);
321affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod    add_feature (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
3220e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod    if (!(feature->start == 0 && feature->end == (unsigned int)-1))
323967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
3247f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod  }
3257f411dbfd9f8d5360c948531ff9f6c3998d1d897Behdad Esfahbod
32681c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
3270e235d0fc9bdeeaffa7215c21abc5d40767a10c7Behdad Esfahbod  /* Sort lookups and merge duplicates */
328e53d77142ac4ecbe38ab3235491fa93cb7ff16abBehdad Esfahbod
3292014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
3302014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3312014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  if (*num_lookups)
3322014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  {
3332014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    for (i = 1, j = 0; i < *num_lookups; i++)
3346b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      if (lookups[i].index != lookups[j].index)
3352014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod	lookups[++j] = lookups[i];
3366b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod      else
3376b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod        lookups[j].mask |= lookups[i].mask;
3386b1b957f6d2955cbe4fa97e2659e033b3eaaf4d2Behdad Esfahbod    j++;
3392014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod    *num_lookups = j;
3402014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  }
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  unsigned int i;
3502014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
351967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_substitution (c->face))
352967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3532014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
354affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GSUB;
355affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
356affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  setup_lookups (c, lookups, &num_lookups);
3572014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3582014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  for (i = 0; i < num_lookups; i++)
359967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
3602014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
361967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_substitute_complex = TRUE;
362967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3632014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
3642014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
365967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
366967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_position_complex (hb_ot_shape_context_t *c)
3672014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod{
368967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  lookup_map lookups[1000]; /* FIXME */
3692014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int num_lookups = ARRAY_LENGTH (lookups);
3702014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  unsigned int i;
3712014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
372967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (!hb_ot_layout_has_positioning (c->face))
373967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    return;
3742014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
375affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  c->table_tag = HB_OT_TAG_GPOS;
376affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod
377affc5abac7bdae51df85856a5478d34d96fda4feBehdad Esfahbod  setup_lookups (c, lookups, &num_lookups);
3782014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
3792014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod  for (i = 0; i < num_lookups; i++)
380967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
3812014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
382967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_layout_position_finish (c->font, c->face, c->buffer);
3832014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod
384967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->applied_position_complex = TRUE;
385967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  return;
3862014b8d110231b13e524008282ece7451f1ae9e7Behdad Esfahbod}
387ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
388ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
389ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Main shaper */
390ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
391ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Prepare */
392ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
393ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic inline hb_bool_t
394ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodis_variation_selector (hb_codepoint_t unicode)
395ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
396ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
397ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
398ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
399ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
400ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
401ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
402ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_form_clusters (hb_buffer_t *buffer)
403ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
404ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
405ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++)
406bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod    if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
407ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod      buffer->info[i].cluster = buffer->info[i - 1].cluster;
408ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
409ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
410967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
411ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_ensure_native_direction (hb_buffer_t *buffer)
412ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
413967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_direction_t direction = buffer->props.direction;
414ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
415ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO vertical */
416967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
417967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      direction != _hb_script_get_horizontal_direction (buffer->props.script))
418ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  {
419ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_buffer_reverse_clusters (buffer);
4204e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
421ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
422ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
423ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
424ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
425ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Substitute */
426ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
427ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
428ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_mirror_chars (hb_buffer_t *buffer)
429ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
430bf36a1074ab23abeab0a7a6c47db26770dc1ab0aBehdad Esfahbod  hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
431ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4324e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
433ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
434ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
4354e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod//  map = allocator.find_feature (HB_TAG ('r','t','l','m'));
4364e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod
437ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len;
438ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
4391094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
4401094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    if (likely (codepoint == buffer->info[i].codepoint))
4414e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod;//      buffer->info[i].mask |= map->mask;
4421094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod    else
4431094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod      buffer->info[i].codepoint = codepoint;
444ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
445ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
446ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
447ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
448ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodhb_map_glyphs (hb_font_t    *font,
449ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_face_t    *face,
450ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod	       hb_buffer_t  *buffer)
451ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
452ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  if (unlikely (!buffer->len))
453ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    return;
454ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
455a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->clear_output ();
456ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  unsigned int count = buffer->len - 1;
457a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  for (buffer->i = 0; buffer->i < count;) {
458a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
459a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
460a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->i++;
461ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    } else {
462a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod      buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
463ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    }
464ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
465f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod  if (likely (buffer->i < buffer->len))
466f2a1b411b1d48c3dfac0df8e78c848d9aa3bb047Behdad Esfahbod    buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
467a224b4d502d026fa642ee4098bf7bc0b4ba7ce27Behdad Esfahbod  buffer->swap ();
468ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
469ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
470ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
471967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_default (hb_ot_shape_context_t *c)
472ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
473967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_map_glyphs (c->font, c->face, c->buffer);
474ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
475ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
476ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
477967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
478ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
479ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Arabic */
480ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
481ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
482ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
483ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Position */
484ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
485ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
486967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_default (hb_ot_shape_context_t *c)
487ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
488967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_buffer_clear_positions (c->buffer);
489ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
490967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
491ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 0; i < count; i++) {
492ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_glyph_metrics_t metrics;
493967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
494967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance = metrics.x_advance;
495967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].y_advance = metrics.y_advance;
496ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
497ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
498ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
499ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
500967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
501ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
502ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Mark pos */
503ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
504ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
505ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
506967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_truetype_kern (hb_ot_shape_context_t *c)
507ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
508ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  /* TODO Check for kern=0 */
509967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  unsigned int count = c->buffer->len;
510ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  for (unsigned int i = 1; i < count; i++) {
511ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    hb_position_t kern, kern1, kern2;
512967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
513ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern1 = kern >> 1;
514ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod    kern2 = kern - kern1;
515967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i - 1].x_advance += kern1;
516967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_advance += kern2;
517967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->pos[i].x_offset += kern2;
518ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod  }
519ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
520ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
521ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbodstatic void
522967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
523ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
524967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_truetype_kern (c);
525ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
526ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
527ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
528ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod/* Do it! */
529ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
530967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodstatic void
531967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape_internal (hb_ot_shape_context_t *c)
532ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod{
533967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_form_clusters (c->buffer);
534ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
5351094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* SUBSTITUTE */
536fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
537967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5381094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
539fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    /* Mirroring needs to see the original direction */
540967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_mirror_chars (c->buffer);
541ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
542967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ensure_native_direction (c->buffer);
543ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
544967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_substitute_default (c);
545bbc7a99d01298f9be1ebaaceacbc9bc961e247e5Behdad Esfahbod
546967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_substitute_complex (c);
547ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
548967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (!c->applied_substitute_complex)
549967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_substitute_complex_fallback (c);
550fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
5511094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
5521094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod  /* POSITION */
553fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  {
554967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    c->buffer->clear_masks ();
5551094a294f6a44c47fc75867983f2b135a6442babBehdad Esfahbod
556967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_position_default (c);
557ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
558967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_ot_position_complex (c);
559ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
560967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    hb_bool_t position_fallback = !c->applied_position_complex;
561fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
562967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback (c);
563ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
564967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
565967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_buffer_reverse (c->buffer);
566ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
567fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod    if (position_fallback)
568967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod      hb_position_complex_fallback_visual (c);
569fdc322a82047c4bda9fa3dab4338a0eac1c1bde7Behdad Esfahbod  }
570ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod
571967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c->buffer->props.direction = c->original_direction;
572967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod}
573967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
574967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodvoid
575967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbodhb_ot_shape (hb_font_t    *font,
576967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_face_t    *face,
577967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_buffer_t  *buffer,
578967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     hb_feature_t *features,
579967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod	     unsigned int  num_features)
580967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod{
581967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
582967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
583967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  /* Setup transient context members */
584967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  c.original_direction = buffer->props.direction;
585967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod
586967240dd8b96802345ef273e75427066e91ea8fbBehdad Esfahbod  hb_ot_shape_internal (&c);
587ca663bb23c16f6a1f04efa6e10dad0e3e7c260a3Behdad Esfahbod}
588acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
589acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
590acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
591