Blame src/hb-ot-map.cc

Packit 874993
/*
Packit 874993
 * Copyright © 2009,2010  Red Hat, Inc.
Packit 874993
 * Copyright © 2010,2011,2013  Google, Inc.
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 *
Packit 874993
 * Red Hat Author(s): Behdad Esfahbod
Packit 874993
 * Google Author(s): Behdad Esfahbod
Packit 874993
 */
Packit 874993
Packit 874993
#include "hb-ot-map-private.hh"
Packit 874993
Packit 874993
#include "hb-ot-layout-private.hh"
Packit 874993
Packit 874993
Packit 874993
void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
Packit 874993
{
Packit 874993
  for (unsigned int i = 0; i < lookups[table_index].len; i++)
Packit 874993
    hb_set_add (lookups_out, lookups[table_index][i].index);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
Packit 874993
					  const hb_segment_properties_t *props_)
Packit 874993
{
Packit 874993
  memset (this, 0, sizeof (*this));
Packit 874993
Packit 874993
  face = face_;
Packit 874993
  props = *props_;
Packit 874993
Packit 874993
Packit 874993
  /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
Packit 874993
   * features not available in either table and not waste precious bits for them. */
Packit 874993
Packit 874993
  hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE};
Packit 874993
  hb_tag_t language_tag;
Packit 874993
Packit 874993
  hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]);
Packit 874993
  language_tag = hb_ot_tag_from_language (props.language);
Packit 874993
Packit 874993
  for (unsigned int table_index = 0; table_index < 2; table_index++) {
Packit 874993
    hb_tag_t table_tag = table_tags[table_index];
Packit 874993
    found_script[table_index] = (bool) hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &chosen_script[table_index]);
Packit 874993
    hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value,
Packit 874993
				       hb_ot_map_feature_flags_t flags)
Packit 874993
{
Packit 874993
  feature_info_t *info = feature_infos.push();
Packit 874993
  if (unlikely (!info)) return;
Packit 874993
  if (unlikely (!tag)) return;
Packit 874993
  info->tag = tag;
Packit 874993
  info->seq = feature_infos.len;
Packit 874993
  info->max_value = value;
Packit 874993
  info->flags = flags;
Packit 874993
  info->default_value = (flags & F_GLOBAL) ? value : 0;
Packit 874993
  info->stage[0] = current_stage[0];
Packit 874993
  info->stage[1] = current_stage[1];
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
hb_ot_map_builder_t::add_lookups (hb_ot_map_t  &m,
Packit 874993
				  hb_face_t    *face,
Packit 874993
				  unsigned int  table_index,
Packit 874993
				  unsigned int  feature_index,
Packit 874993
				  unsigned int  variations_index,
Packit 874993
				  hb_mask_t     mask,
Packit 874993
				  bool          auto_zwnj,
Packit 874993
				  bool          auto_zwj)
Packit 874993
{
Packit 874993
  unsigned int lookup_indices[32];
Packit 874993
  unsigned int offset, len;
Packit 874993
  unsigned int table_lookup_count;
Packit 874993
Packit 874993
  table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
Packit 874993
Packit 874993
  offset = 0;
Packit 874993
  do {
Packit 874993
    len = ARRAY_LENGTH (lookup_indices);
Packit 874993
    hb_ot_layout_feature_with_variations_get_lookups (face,
Packit 874993
						      table_tags[table_index],
Packit 874993
						      feature_index,
Packit 874993
						      variations_index,
Packit 874993
						      offset, &len,
Packit 874993
						      lookup_indices);
Packit 874993
Packit 874993
    for (unsigned int i = 0; i < len; i++)
Packit 874993
    {
Packit 874993
      if (lookup_indices[i] >= table_lookup_count)
Packit 874993
	continue;
Packit 874993
      hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
Packit 874993
      if (unlikely (!lookup))
Packit 874993
        return;
Packit 874993
      lookup->mask = mask;
Packit 874993
      lookup->index = lookup_indices[i];
Packit 874993
      lookup->auto_zwnj = auto_zwnj;
Packit 874993
      lookup->auto_zwj = auto_zwj;
Packit 874993
    }
Packit 874993
Packit 874993
    offset += len;
Packit 874993
  } while (len == ARRAY_LENGTH (lookup_indices));
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
Packit 874993
{
Packit 874993
  stage_info_t *s = stages[table_index].push ();
Packit 874993
  if (likely (s)) {
Packit 874993
    s->index = current_stage[table_index];
Packit 874993
    s->pause_func = pause_func;
Packit 874993
  }
Packit 874993
Packit 874993
  current_stage[table_index]++;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
hb_ot_map_builder_t::compile (hb_ot_map_t  &m,
Packit 874993
			      const int    *coords,
Packit 874993
			      unsigned int  num_coords)
Packit 874993
{
Packit 874993
  m.global_mask = 1;
Packit 874993
Packit 874993
  unsigned int required_feature_index[2];
Packit 874993
  hb_tag_t required_feature_tag[2];
Packit 874993
  /* We default to applying required feature in stage 0.  If the required
Packit 874993
   * feature has a tag that is known to the shaper, we apply required feature
Packit 874993
   * in the stage for that tag.
Packit 874993
   */
Packit 874993
  unsigned int required_feature_stage[2] = {0, 0};
Packit 874993
Packit 874993
  for (unsigned int table_index = 0; table_index < 2; table_index++)
Packit 874993
  {
Packit 874993
    m.chosen_script[table_index] = chosen_script[table_index];
Packit 874993
    m.found_script[table_index] = found_script[table_index];
Packit 874993
Packit 874993
    hb_ot_layout_language_get_required_feature (face,
Packit 874993
						table_tags[table_index],
Packit 874993
						script_index[table_index],
Packit 874993
						language_index[table_index],
Packit 874993
						&required_feature_index[table_index],
Packit 874993
						&required_feature_tag[table_index]);
Packit 874993
  }
Packit 874993
Packit 874993
  if (!feature_infos.len)
Packit 874993
    return;
Packit 874993
Packit 874993
  /* Sort features and merge duplicates */
Packit 874993
  {
Packit 874993
    feature_infos.qsort ();
Packit 874993
    unsigned int j = 0;
Packit 874993
    for (unsigned int i = 1; i < feature_infos.len; i++)
Packit 874993
      if (feature_infos[i].tag != feature_infos[j].tag)
Packit 874993
	feature_infos[++j] = feature_infos[i];
Packit 874993
      else {
Packit 874993
	if (feature_infos[i].flags & F_GLOBAL) {
Packit 874993
	  feature_infos[j].flags |= F_GLOBAL;
Packit 874993
	  feature_infos[j].max_value = feature_infos[i].max_value;
Packit 874993
	  feature_infos[j].default_value = feature_infos[i].default_value;
Packit 874993
	} else {
Packit 874993
	  feature_infos[j].flags &= ~F_GLOBAL;
Packit 874993
	  feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
Packit 874993
	  /* Inherit default_value from j */
Packit 874993
	}
Packit 874993
	feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
Packit 874993
	feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
Packit 874993
	feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
Packit 874993
      }
Packit 874993
    feature_infos.shrink (j + 1);
Packit 874993
  }
Packit 874993
Packit 874993
Packit 874993
  /* Allocate bits now */
Packit 874993
  unsigned int next_bit = 1;
Packit 874993
  for (unsigned int i = 0; i < feature_infos.len; i++)
Packit 874993
  {
Packit 874993
    const feature_info_t *info = &feature_infos[i];
Packit 874993
Packit 874993
    unsigned int bits_needed;
Packit 874993
Packit 874993
    if ((info->flags & F_GLOBAL) && info->max_value == 1)
Packit 874993
      /* Uses the global bit */
Packit 874993
      bits_needed = 0;
Packit 874993
    else
Packit 874993
      /* Limit to 8 bits per feature. */
Packit 874993
      bits_needed = MIN(8u, _hb_bit_storage (info->max_value));
Packit 874993
Packit 874993
    if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
Packit 874993
      continue; /* Feature disabled, or not enough bits. */
Packit 874993
Packit 874993
Packit 874993
    hb_bool_t found = false;
Packit 874993
    unsigned int feature_index[2];
Packit 874993
    for (unsigned int table_index = 0; table_index < 2; table_index++)
Packit 874993
    {
Packit 874993
      if (required_feature_tag[table_index] == info->tag)
Packit 874993
	required_feature_stage[table_index] = info->stage[table_index];
Packit 874993
Packit 874993
      found |= hb_ot_layout_language_find_feature (face,
Packit 874993
						   table_tags[table_index],
Packit 874993
						   script_index[table_index],
Packit 874993
						   language_index[table_index],
Packit 874993
						   info->tag,
Packit 874993
						   &feature_index[table_index]);
Packit 874993
    }
Packit 874993
    if (!found && (info->flags & F_GLOBAL_SEARCH))
Packit 874993
    {
Packit 874993
      for (unsigned int table_index = 0; table_index < 2; table_index++)
Packit 874993
      {
Packit 874993
	found |= hb_ot_layout_table_find_feature (face,
Packit 874993
						  table_tags[table_index],
Packit 874993
						  info->tag,
Packit 874993
						  &feature_index[table_index]);
Packit 874993
      }
Packit 874993
    }
Packit 874993
    if (!found && !(info->flags & F_HAS_FALLBACK))
Packit 874993
      continue;
Packit 874993
Packit 874993
Packit 874993
    hb_ot_map_t::feature_map_t *map = m.features.push ();
Packit 874993
    if (unlikely (!map))
Packit 874993
      break;
Packit 874993
Packit 874993
    map->tag = info->tag;
Packit 874993
    map->index[0] = feature_index[0];
Packit 874993
    map->index[1] = feature_index[1];
Packit 874993
    map->stage[0] = info->stage[0];
Packit 874993
    map->stage[1] = info->stage[1];
Packit 874993
    map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
Packit 874993
    map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
Packit 874993
    if ((info->flags & F_GLOBAL) && info->max_value == 1) {
Packit 874993
      /* Uses the global bit */
Packit 874993
      map->shift = 0;
Packit 874993
      map->mask = 1;
Packit 874993
    } else {
Packit 874993
      map->shift = next_bit;
Packit 874993
      map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
Packit 874993
      next_bit += bits_needed;
Packit 874993
      m.global_mask |= (info->default_value << map->shift) & map->mask;
Packit 874993
    }
Packit 874993
    map->_1_mask = (1u << map->shift) & map->mask;
Packit 874993
    map->needs_fallback = !found;
Packit 874993
Packit 874993
  }
Packit 874993
  feature_infos.shrink (0); /* Done with these */
Packit 874993
Packit 874993
Packit 874993
  add_gsub_pause (NULL);
Packit 874993
  add_gpos_pause (NULL);
Packit 874993
Packit 874993
  for (unsigned int table_index = 0; table_index < 2; table_index++)
Packit 874993
  {
Packit 874993
    /* Collect lookup indices for features */
Packit 874993
Packit 874993
    unsigned int variations_index;
Packit 874993
    hb_ot_layout_table_find_feature_variations (face,
Packit 874993
						table_tags[table_index],
Packit 874993
						coords,
Packit 874993
						num_coords,
Packit 874993
						&variations_index);
Packit 874993
Packit 874993
    unsigned int stage_index = 0;
Packit 874993
    unsigned int last_num_lookups = 0;
Packit 874993
    for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
Packit 874993
    {
Packit 874993
      if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
Packit 874993
	  required_feature_stage[table_index] == stage)
Packit 874993
	add_lookups (m, face, table_index,
Packit 874993
		     required_feature_index[table_index],
Packit 874993
		     variations_index,
Packit 874993
		     1 /* mask */);
Packit 874993
Packit 874993
      for (unsigned i = 0; i < m.features.len; i++)
Packit 874993
        if (m.features[i].stage[table_index] == stage)
Packit 874993
	  add_lookups (m, face, table_index,
Packit 874993
		       m.features[i].index[table_index],
Packit 874993
		       variations_index,
Packit 874993
		       m.features[i].mask,
Packit 874993
		       m.features[i].auto_zwnj,
Packit 874993
		       m.features[i].auto_zwj);
Packit 874993
Packit 874993
      /* Sort lookups and merge duplicates */
Packit 874993
      if (last_num_lookups < m.lookups[table_index].len)
Packit 874993
      {
Packit 874993
	m.lookups[table_index].qsort (last_num_lookups, m.lookups[table_index].len);
Packit 874993
Packit 874993
	unsigned int j = last_num_lookups;
Packit 874993
	for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
Packit 874993
	  if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
Packit 874993
	    m.lookups[table_index][++j] = m.lookups[table_index][i];
Packit 874993
	  else
Packit 874993
	  {
Packit 874993
	    m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
Packit 874993
	    m.lookups[table_index][j].auto_zwnj &= m.lookups[table_index][i].auto_zwnj;
Packit 874993
	    m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
Packit 874993
	  }
Packit 874993
	m.lookups[table_index].shrink (j + 1);
Packit 874993
      }
Packit 874993
Packit 874993
      last_num_lookups = m.lookups[table_index].len;
Packit 874993
Packit 874993
      if (stage_index < stages[table_index].len && stages[table_index][stage_index].index == stage) {
Packit 874993
	hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
Packit 874993
	if (likely (stage_map)) {
Packit 874993
	  stage_map->last_lookup = last_num_lookups;
Packit 874993
	  stage_map->pause_func = stages[table_index][stage_index].pause_func;
Packit 874993
	}
Packit 874993
Packit 874993
	stage_index++;
Packit 874993
      }
Packit 874993
    }
Packit 874993
  }
Packit 874993
}