Blame src/hb-ot-shape.cc

Packit 874993
/*
Packit 874993
 * Copyright © 2009,2010  Red Hat, Inc.
Packit 874993
 * Copyright © 2010,2011,2012  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
#define HB_SHAPER ot
Packit 874993
#define hb_ot_shaper_face_data_t hb_ot_layout_t
Packit 874993
#define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
Packit 874993
#include "hb-shaper-impl-private.hh"
Packit 874993
Packit 874993
#include "hb-ot-shape-private.hh"
Packit 874993
#include "hb-ot-shape-complex-private.hh"
Packit 874993
#include "hb-ot-shape-fallback-private.hh"
Packit 874993
#include "hb-ot-shape-normalize-private.hh"
Packit 874993
Packit 874993
#include "hb-ot-layout-private.hh"
Packit 874993
#include "hb-unicode-private.hh"
Packit 874993
#include "hb-set-private.hh"
Packit 874993
Packit 874993
Packit 874993
static hb_tag_t common_features[] = {
Packit 874993
  HB_TAG('c','c','m','p'),
Packit 874993
  HB_TAG('l','o','c','l'),
Packit 874993
  HB_TAG('m','a','r','k'),
Packit 874993
  HB_TAG('m','k','m','k'),
Packit 874993
  HB_TAG('r','l','i','g'),
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
static hb_tag_t horizontal_features[] = {
Packit 874993
  HB_TAG('c','a','l','t'),
Packit 874993
  HB_TAG('c','l','i','g'),
Packit 874993
  HB_TAG('c','u','r','s'),
Packit 874993
  HB_TAG('k','e','r','n'),
Packit 874993
  HB_TAG('l','i','g','a'),
Packit 874993
  HB_TAG('r','c','l','t'),
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
Packit 874993
static void
Packit 874993
hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
Packit 874993
			      const hb_segment_properties_t  *props,
Packit 874993
			      const hb_feature_t             *user_features,
Packit 874993
			      unsigned int                    num_user_features)
Packit 874993
{
Packit 874993
  hb_ot_map_builder_t *map = &planner->map;
Packit 874993
Packit 874993
  map->add_global_bool_feature (HB_TAG('r','v','r','n'));
Packit 874993
  map->add_gsub_pause (NULL);
Packit 874993
Packit 874993
  switch (props->direction) {
Packit 874993
    case HB_DIRECTION_LTR:
Packit 874993
      map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
Packit 874993
      map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
Packit 874993
      break;
Packit 874993
    case HB_DIRECTION_RTL:
Packit 874993
      map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
Packit 874993
      map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
Packit 874993
      break;
Packit 874993
    case HB_DIRECTION_TTB:
Packit 874993
    case HB_DIRECTION_BTT:
Packit 874993
    case HB_DIRECTION_INVALID:
Packit 874993
    default:
Packit 874993
      break;
Packit 874993
  }
Packit 874993
Packit 874993
  map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
Packit 874993
  map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
Packit 874993
  map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
Packit 874993
Packit 874993
  if (planner->shaper->collect_features)
Packit 874993
    planner->shaper->collect_features (planner);
Packit 874993
Packit 874993
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
Packit 874993
    map->add_global_bool_feature (common_features[i]);
Packit 874993
Packit 874993
  if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
Packit 874993
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
Packit 874993
      map->add_feature (horizontal_features[i], 1, F_GLOBAL |
Packit 874993
			(horizontal_features[i] == HB_TAG('k','e','r','n') ?
Packit 874993
			 F_HAS_FALLBACK : F_NONE));
Packit 874993
  else
Packit 874993
  {
Packit 874993
    /* We really want to find a 'vert' feature if there's any in the font, no
Packit 874993
     * matter which script/langsys it is listed (or not) under.
Packit 874993
     * See various bugs referenced from:
Packit 874993
     * https://github.com/behdad/harfbuzz/issues/63 */
Packit 874993
    map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
Packit 874993
  }
Packit 874993
Packit 874993
  if (planner->shaper->override_features)
Packit 874993
    planner->shaper->override_features (planner);
Packit 874993
Packit 874993
  for (unsigned int i = 0; i < num_user_features; i++) {
Packit 874993
    const hb_feature_t *feature = &user_features[i];
Packit 874993
    map->add_feature (feature->tag, feature->value,
Packit 874993
		      (feature->start == 0 && feature->end == (unsigned int) -1) ?
Packit 874993
		       F_GLOBAL : F_NONE);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper face data
Packit 874993
 */
Packit 874993
Packit 874993
HB_SHAPER_DATA_ENSURE_DEFINE(ot, face)
Packit 874993
Packit 874993
hb_ot_shaper_face_data_t *
Packit 874993
_hb_ot_shaper_face_data_create (hb_face_t *face)
Packit 874993
{
Packit 874993
  return _hb_ot_layout_create (face);
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
Packit 874993
{
Packit 874993
  _hb_ot_layout_destroy (data);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper font data
Packit 874993
 */
Packit 874993
Packit 874993
HB_SHAPER_DATA_ENSURE_DEFINE(ot, font)
Packit 874993
Packit 874993
struct hb_ot_shaper_font_data_t {};
Packit 874993
Packit 874993
hb_ot_shaper_font_data_t *
Packit 874993
_hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
Packit 874993
{
Packit 874993
  return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
Packit 874993
{
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper shape_plan data
Packit 874993
 */
Packit 874993
Packit 874993
hb_ot_shaper_shape_plan_data_t *
Packit 874993
_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
Packit 874993
				      const hb_feature_t *user_features,
Packit 874993
				      unsigned int        num_user_features,
Packit 874993
				      const int          *coords,
Packit 874993
				      unsigned int        num_coords)
Packit 874993
{
Packit 874993
  hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
Packit 874993
  if (unlikely (!plan))
Packit 874993
    return NULL;
Packit 874993
Packit 874993
  hb_ot_shape_planner_t planner (shape_plan);
Packit 874993
Packit 874993
  planner.shaper = hb_ot_shape_complex_categorize (&planner);
Packit 874993
Packit 874993
  hb_ot_shape_collect_features (&planner, &shape_plan->props,
Packit 874993
				user_features, num_user_features);
Packit 874993
Packit 874993
  planner.compile (*plan, coords, num_coords);
Packit 874993
Packit 874993
  if (plan->shaper->data_create) {
Packit 874993
    plan->data = plan->shaper->data_create (plan);
Packit 874993
    if (unlikely (!plan->data))
Packit 874993
      return NULL;
Packit 874993
  }
Packit 874993
Packit 874993
  return plan;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
Packit 874993
{
Packit 874993
  if (plan->shaper->data_destroy)
Packit 874993
    plan->shaper->data_destroy (const_cast<void *> (plan->data));
Packit 874993
Packit 874993
  plan->finish ();
Packit 874993
Packit 874993
  free (plan);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper
Packit 874993
 */
Packit 874993
Packit 874993
struct hb_ot_shape_context_t
Packit 874993
{
Packit 874993
  hb_ot_shape_plan_t *plan;
Packit 874993
  hb_font_t *font;
Packit 874993
  hb_face_t *face;
Packit 874993
  hb_buffer_t  *buffer;
Packit 874993
  const hb_feature_t *user_features;
Packit 874993
  unsigned int        num_user_features;
Packit 874993
Packit 874993
  /* Transient stuff */
Packit 874993
  bool fallback_positioning;
Packit 874993
  bool fallback_glyph_classes;
Packit 874993
  hb_direction_t target_direction;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
Packit 874993
/* Main shaper */
Packit 874993
Packit 874993
Packit 874993
/* Prepare */
Packit 874993
Packit 874993
static void
Packit 874993
hb_set_unicode_props (hb_buffer_t *buffer)
Packit 874993
{
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
Packit 874993
{
Packit 874993
  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
Packit 874993
      buffer->context_len[0] ||
Packit 874993
      _hb_glyph_info_get_general_category (&buffer->info[0]) !=
Packit 874993
      HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Packit 874993
    return;
Packit 874993
Packit 874993
  if (!font->has_glyph (0x25CCu))
Packit 874993
    return;
Packit 874993
Packit 874993
  hb_glyph_info_t dottedcircle = {0};
Packit 874993
  dottedcircle.codepoint = 0x25CCu;
Packit 874993
  _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
Packit 874993
Packit 874993
  buffer->clear_output ();
Packit 874993
Packit 874993
  buffer->idx = 0;
Packit 874993
  hb_glyph_info_t info = dottedcircle;
Packit 874993
  info.cluster = buffer->cur().cluster;
Packit 874993
  info.mask = buffer->cur().mask;
Packit 874993
  buffer->output_info (info);
Packit 874993
  while (buffer->idx < buffer->len && !buffer->in_error)
Packit 874993
    buffer->next_glyph ();
Packit 874993
Packit 874993
  buffer->swap_buffers ();
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
hb_form_clusters (hb_buffer_t *buffer)
Packit 874993
{
Packit 874993
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
Packit 874993
      buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Packit 874993
    return;
Packit 874993
Packit 874993
  /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
Packit 874993
  unsigned int base = 0;
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 1; i < count; i++)
Packit 874993
  {
Packit 874993
    if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
Packit 874993
		!_hb_glyph_info_is_joiner (&info[i])))
Packit 874993
    {
Packit 874993
      buffer->merge_clusters (base, i);
Packit 874993
      base = i;
Packit 874993
    }
Packit 874993
  }
Packit 874993
  buffer->merge_clusters (base, count);
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
hb_ensure_native_direction (hb_buffer_t *buffer)
Packit 874993
{
Packit 874993
  hb_direction_t direction = buffer->props.direction;
Packit 874993
Packit 874993
  /* TODO vertical:
Packit 874993
   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
Packit 874993
   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
Packit 874993
   * first. */
Packit 874993
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
Packit 874993
      (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
Packit 874993
  {
Packit 874993
    /* Same loop as hb_form_clusters().
Packit 874993
     * Since form_clusters() merged clusters already, we don't merge. */
Packit 874993
    unsigned int base = 0;
Packit 874993
    unsigned int count = buffer->len;
Packit 874993
    hb_glyph_info_t *info = buffer->info;
Packit 874993
    for (unsigned int i = 1; i < count; i++)
Packit 874993
    {
Packit 874993
      if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
Packit 874993
      {
Packit 874993
	if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
Packit 874993
	  buffer->merge_clusters (base, i);
Packit 874993
	buffer->reverse_range (base, i);
Packit 874993
Packit 874993
	base = i;
Packit 874993
      }
Packit 874993
    }
Packit 874993
    if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
Packit 874993
      buffer->merge_clusters (base, count);
Packit 874993
    buffer->reverse_range (base, count);
Packit 874993
Packit 874993
    buffer->reverse ();
Packit 874993
Packit 874993
    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Substitute */
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_mirror_chars (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
Packit 874993
    return;
Packit 874993
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
  hb_unicode_funcs_t *unicode = buffer->unicode;
Packit 874993
  hb_mask_t rtlm_mask = c->plan->rtlm_mask;
Packit 874993
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++) {
Packit 874993
    hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
Packit 874993
    if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
Packit 874993
      info[i].mask |= rtlm_mask;
Packit 874993
    else
Packit 874993
      info[i].codepoint = codepoint;
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
Packit 874993
      !c->plan->has_frac)
Packit 874993
    return;
Packit 874993
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  hb_mask_t pre_mask, post_mask;
Packit 874993
  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
Packit 874993
  {
Packit 874993
    pre_mask = c->plan->numr_mask | c->plan->frac_mask;
Packit 874993
    post_mask = c->plan->frac_mask | c->plan->dnom_mask;
Packit 874993
  }
Packit 874993
  else
Packit 874993
  {
Packit 874993
    pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
Packit 874993
    post_mask = c->plan->numr_mask | c->plan->frac_mask;
Packit 874993
  }
Packit 874993
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
  {
Packit 874993
    if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
Packit 874993
    {
Packit 874993
      unsigned int start = i, end = i + 1;
Packit 874993
      while (start &&
Packit 874993
	     _hb_glyph_info_get_general_category (&info[start - 1]) ==
Packit 874993
	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
Packit 874993
        start--;
Packit 874993
      while (end < count &&
Packit 874993
	     _hb_glyph_info_get_general_category (&info[end]) ==
Packit 874993
	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
Packit 874993
        end++;
Packit 874993
Packit 874993
      for (unsigned int j = start; j < i; j++)
Packit 874993
        info[j].mask |= pre_mask;
Packit 874993
      info[i].mask |= c->plan->frac_mask;
Packit 874993
      for (unsigned int j = i + 1; j < end; j++)
Packit 874993
        info[j].mask |= post_mask;
Packit 874993
Packit 874993
      i = end - 1;
Packit 874993
    }
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_ot_map_t *map = &c->plan->map;
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  hb_mask_t global_mask = map->get_global_mask ();
Packit 874993
  buffer->reset_masks (global_mask);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_ot_map_t *map = &c->plan->map;
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  hb_ot_shape_setup_masks_fraction (c);
Packit 874993
Packit 874993
  if (c->plan->shaper->setup_masks)
Packit 874993
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
Packit 874993
Packit 874993
  for (unsigned int i = 0; i < c->num_user_features; i++)
Packit 874993
  {
Packit 874993
    const hb_feature_t *feature = &c->user_features[i];
Packit 874993
    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
Packit 874993
      unsigned int shift;
Packit 874993
      hb_mask_t mask = map->get_mask (feature->tag, &shift);
Packit 874993
      buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
Packit 874993
    }
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
Packit 874993
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
Packit 874993
    return;
Packit 874993
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  hb_glyph_position_t *pos = buffer->pos;
Packit 874993
  unsigned int i = 0;
Packit 874993
  for (i = 0; i < count; i++)
Packit 874993
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
Packit 874993
      pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
Packit 874993
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
Packit 874993
    return;
Packit 874993
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  hb_glyph_position_t *pos = buffer->pos;
Packit 874993
  unsigned int i = 0;
Packit 874993
  for (i = 0; i < count; i++)
Packit 874993
  {
Packit 874993
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
Packit 874993
      break;
Packit 874993
  }
Packit 874993
Packit 874993
  /* No default-ignorables found; return. */
Packit 874993
  if (i == count)
Packit 874993
    return;
Packit 874993
Packit 874993
  hb_codepoint_t space;
Packit 874993
  if (c->font->get_nominal_glyph (' ', &space))
Packit 874993
  {
Packit 874993
    /* Replace default-ignorables with a zero-advance space glyph. */
Packit 874993
    for (/*continue*/; i < count; i++)
Packit 874993
    {
Packit 874993
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
Packit 874993
	info[i].codepoint = space;
Packit 874993
    }
Packit 874993
  }
Packit 874993
  else
Packit 874993
  {
Packit 874993
    /* Merge clusters and delete default-ignorables.
Packit 874993
     * NOTE! We can't use out-buffer as we have positioning data. */
Packit 874993
    unsigned int j = i;
Packit 874993
    for (; i < count; i++)
Packit 874993
    {
Packit 874993
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
Packit 874993
      {
Packit 874993
	/* Merge clusters.
Packit 874993
	 * Same logic as buffer->delete_glyph(), but for in-place removal. */
Packit 874993
Packit 874993
	unsigned int cluster = info[i].cluster;
Packit 874993
	if (i + 1 < count && cluster == info[i + 1].cluster)
Packit 874993
	  continue; /* Cluster survives; do nothing. */
Packit 874993
Packit 874993
	if (j)
Packit 874993
	{
Packit 874993
	  /* Merge cluster backward. */
Packit 874993
	  if (cluster < info[j - 1].cluster)
Packit 874993
	  {
Packit 874993
	    unsigned int old_cluster = info[j - 1].cluster;
Packit 874993
	    for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
Packit 874993
	      info[k - 1].cluster = cluster;
Packit 874993
	  }
Packit 874993
	  continue;
Packit 874993
	}
Packit 874993
Packit 874993
	if (i + 1 < count)
Packit 874993
	  buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
Packit 874993
Packit 874993
	continue;
Packit 874993
      }
Packit 874993
Packit 874993
      if (j != i)
Packit 874993
      {
Packit 874993
	info[j] = info[i];
Packit 874993
	pos[j] = pos[i];
Packit 874993
      }
Packit 874993
      j++;
Packit 874993
    }
Packit 874993
    buffer->len = j;
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
Packit 874993
{
Packit 874993
  /* Normalization process sets up glyph_index(), we just copy it. */
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
    info[i].codepoint = info[i].glyph_index();
Packit 874993
Packit 874993
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  unsigned int count = c->buffer->len;
Packit 874993
  hb_glyph_info_t *info = c->buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
  {
Packit 874993
    hb_ot_layout_glyph_props_flags_t klass;
Packit 874993
Packit 874993
    /* Never mark default-ignorables as marks.
Packit 874993
     * They won't get in the way of lookups anyway,
Packit 874993
     * but having them as mark will cause them to be skipped
Packit 874993
     * over if the lookup-flag says so, but at least for the
Packit 874993
     * Mongolian variation selectors, looks like Uniscribe
Packit 874993
     * marks them as non-mark.  Some Mongolian fonts without
Packit 874993
     * GDEF rely on this.  Another notable character that
Packit 874993
     * this applies to is COMBINING GRAPHEME JOINER. */
Packit 874993
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
Packit 874993
	     HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
Packit 874993
	     _hb_glyph_info_is_default_ignorable (&info[i])) ?
Packit 874993
	    HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
Packit 874993
	    HB_OT_LAYOUT_GLYPH_PROPS_MARK;
Packit 874993
    _hb_glyph_info_set_glyph_props (&info[i], klass);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_substitute_default (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  hb_ot_shape_initialize_masks (c);
Packit 874993
Packit 874993
  hb_ot_mirror_chars (c);
Packit 874993
Packit 874993
  HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
Packit 874993
Packit 874993
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
Packit 874993
Packit 874993
  hb_ot_shape_setup_masks (c);
Packit 874993
Packit 874993
  /* This is unfortunate to go here, but necessary... */
Packit 874993
  if (c->fallback_positioning)
Packit 874993
    _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
Packit 874993
Packit 874993
  hb_ot_map_glyphs_fast (buffer);
Packit 874993
Packit 874993
  HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_substitute_complex (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_buffer_t *buffer = c->buffer;
Packit 874993
Packit 874993
  hb_ot_layout_substitute_start (c->font, buffer);
Packit 874993
Packit 874993
  if (!hb_ot_layout_has_glyph_classes (c->face))
Packit 874993
    hb_synthesize_glyph_classes (c);
Packit 874993
Packit 874993
  c->plan->substitute (c->font, buffer);
Packit 874993
Packit 874993
  return;
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_substitute (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_ot_substitute_default (c);
Packit 874993
Packit 874993
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
Packit 874993
Packit 874993
  hb_ot_substitute_complex (c);
Packit 874993
}
Packit 874993
Packit 874993
/* Position */
Packit 874993
Packit 874993
static inline void
Packit 874993
adjust_mark_offsets (hb_glyph_position_t *pos)
Packit 874993
{
Packit 874993
  pos->x_offset -= pos->x_advance;
Packit 874993
  pos->y_offset -= pos->y_advance;
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
zero_mark_width (hb_glyph_position_t *pos)
Packit 874993
{
Packit 874993
  pos->x_advance = 0;
Packit 874993
  pos->y_advance = 0;
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
Packit 874993
{
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
    if (_hb_glyph_info_is_mark (&info[i]))
Packit 874993
    {
Packit 874993
      if (adjust_offsets)
Packit 874993
        adjust_mark_offsets (&buffer->pos[i]);
Packit 874993
      zero_mark_width (&buffer->pos[i]);
Packit 874993
    }
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_position_default (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_direction_t direction = c->buffer->props.direction;
Packit 874993
  unsigned int count = c->buffer->len;
Packit 874993
  hb_glyph_info_t *info = c->buffer->info;
Packit 874993
  hb_glyph_position_t *pos = c->buffer->pos;
Packit 874993
Packit 874993
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
Packit 874993
  {
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      pos[i].x_advance = c->font->get_glyph_h_advance (info[i].codepoint);
Packit 874993
    /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
Packit 874993
    if (c->font->has_glyph_h_origin_func ())
Packit 874993
      for (unsigned int i = 0; i < count; i++)
Packit 874993
	c->font->subtract_glyph_h_origin (info[i].codepoint,
Packit 874993
					  &pos[i].x_offset,
Packit 874993
					  &pos[i].y_offset);
Packit 874993
  }
Packit 874993
  else
Packit 874993
  {
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
    {
Packit 874993
      pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
Packit 874993
      c->font->subtract_glyph_v_origin (info[i].codepoint,
Packit 874993
					&pos[i].x_offset,
Packit 874993
					&pos[i].y_offset);
Packit 874993
    }
Packit 874993
  }
Packit 874993
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
Packit 874993
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_position_complex (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  hb_ot_layout_position_start (c->font, c->buffer);
Packit 874993
Packit 874993
  unsigned int count = c->buffer->len;
Packit 874993
Packit 874993
  /* If the font has no GPOS, AND, no fallback positioning will
Packit 874993
   * happen, AND, direction is forward, then when zeroing mark
Packit 874993
   * widths, we shift the mark with it, such that the mark
Packit 874993
   * is positioned hanging over the previous glyph.  When
Packit 874993
   * direction is backward we don't shift and it will end up
Packit 874993
   * hanging over the next glyph after the final reordering.
Packit 874993
   * If fallback positinoing happens or GPOS is present, we don't
Packit 874993
   * care.
Packit 874993
   */
Packit 874993
  bool adjust_offsets_when_zeroing = c->fallback_positioning &&
Packit 874993
				     !c->plan->shaper->fallback_position &&
Packit 874993
				     HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
Packit 874993
Packit 874993
  switch (c->plan->shaper->zero_width_marks)
Packit 874993
  {
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
Packit 874993
      zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
Packit 874993
      break;
Packit 874993
Packit 874993
    default:
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
Packit 874993
      break;
Packit 874993
  }
Packit 874993
Packit 874993
  if (likely (!c->fallback_positioning))
Packit 874993
  {
Packit 874993
    hb_glyph_info_t *info = c->buffer->info;
Packit 874993
    hb_glyph_position_t *pos = c->buffer->pos;
Packit 874993
Packit 874993
    /* Change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
Packit 874993
Packit 874993
    /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
Packit 874993
    if (c->font->has_glyph_h_origin_func ())
Packit 874993
      for (unsigned int i = 0; i < count; i++)
Packit 874993
	c->font->add_glyph_h_origin (info[i].codepoint,
Packit 874993
				     &pos[i].x_offset,
Packit 874993
				     &pos[i].y_offset);
Packit 874993
Packit 874993
    c->plan->position (c->font, c->buffer);
Packit 874993
Packit 874993
    /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
Packit 874993
    if (c->font->has_glyph_h_origin_func ())
Packit 874993
      for (unsigned int i = 0; i < count; i++)
Packit 874993
	c->font->subtract_glyph_h_origin (info[i].codepoint,
Packit 874993
					  &pos[i].x_offset,
Packit 874993
					  &pos[i].y_offset);
Packit 874993
Packit 874993
  }
Packit 874993
Packit 874993
  switch (c->plan->shaper->zero_width_marks)
Packit 874993
  {
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
Packit 874993
      zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
Packit 874993
      break;
Packit 874993
Packit 874993
    default:
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
Packit 874993
    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
Packit 874993
      break;
Packit 874993
  }
Packit 874993
Packit 874993
  /* Finishing off GPOS has to follow a certain order. */
Packit 874993
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
Packit 874993
  hb_ot_zero_width_default_ignorables (c);
Packit 874993
  hb_ot_layout_position_finish_offsets (c->font, c->buffer);
Packit 874993
}
Packit 874993
Packit 874993
static inline void
Packit 874993
hb_ot_position (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  c->buffer->clear_positions ();
Packit 874993
Packit 874993
  hb_ot_position_default (c);
Packit 874993
Packit 874993
  hb_ot_position_complex (c);
Packit 874993
Packit 874993
  if (c->fallback_positioning && c->plan->shaper->fallback_position)
Packit 874993
    _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
Packit 874993
Packit 874993
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
Packit 874993
    hb_buffer_reverse (c->buffer);
Packit 874993
Packit 874993
  /* Visual fallback goes here. */
Packit 874993
Packit 874993
  if (c->fallback_positioning)
Packit 874993
    _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
Packit 874993
Packit 874993
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Pull it all together! */
Packit 874993
Packit 874993
static void
Packit 874993
hb_ot_shape_internal (hb_ot_shape_context_t *c)
Packit 874993
{
Packit 874993
  c->buffer->deallocate_var_all ();
Packit 874993
  c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
Packit 874993
  if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_EXPANSION_FACTOR)))
Packit 874993
  {
Packit 874993
    c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_EXPANSION_FACTOR,
Packit 874993
			      (unsigned) HB_BUFFER_MAX_LEN_MIN);
Packit 874993
  }
Packit 874993
Packit 874993
  bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
Packit 874993
  //c->fallback_substitute     = disable_otl || !hb_ot_layout_has_substitution (c->face);
Packit 874993
  c->fallback_positioning    = disable_otl || !hb_ot_layout_has_positioning (c->face);
Packit 874993
  c->fallback_glyph_classes  = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
Packit 874993
Packit 874993
  /* Save the original direction, we use it later. */
Packit 874993
  c->target_direction = c->buffer->props.direction;
Packit 874993
Packit 874993
  _hb_buffer_allocate_unicode_vars (c->buffer);
Packit 874993
Packit 874993
  c->buffer->clear_output ();
Packit 874993
Packit 874993
  hb_set_unicode_props (c->buffer);
Packit 874993
  hb_insert_dotted_circle (c->buffer, c->font);
Packit 874993
  hb_form_clusters (c->buffer);
Packit 874993
Packit 874993
  hb_ensure_native_direction (c->buffer);
Packit 874993
Packit 874993
  if (c->plan->shaper->preprocess_text)
Packit 874993
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
Packit 874993
Packit 874993
  hb_ot_substitute (c);
Packit 874993
  hb_ot_position (c);
Packit 874993
Packit 874993
  hb_ot_hide_default_ignorables (c);
Packit 874993
Packit 874993
  if (c->plan->shaper->postprocess_glyphs)
Packit 874993
    c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
Packit 874993
Packit 874993
  _hb_buffer_deallocate_unicode_vars (c->buffer);
Packit 874993
Packit 874993
  c->buffer->props.direction = c->target_direction;
Packit 874993
Packit 874993
  c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
Packit 874993
  c->buffer->deallocate_var_all ();
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
hb_bool_t
Packit 874993
_hb_ot_shape (hb_shape_plan_t    *shape_plan,
Packit 874993
	      hb_font_t          *font,
Packit 874993
	      hb_buffer_t        *buffer,
Packit 874993
	      const hb_feature_t *features,
Packit 874993
	      unsigned int        num_features)
Packit 874993
{
Packit 874993
  hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
Packit 874993
  hb_ot_shape_internal (&c);
Packit 874993
Packit 874993
  return true;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/**
Packit 874993
 * hb_ot_shape_plan_collect_lookups:
Packit 874993
 *
Packit 874993
 * Since: 0.9.7
Packit 874993
 **/
Packit 874993
void
Packit 874993
hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
Packit 874993
				  hb_tag_t         table_tag,
Packit 874993
				  hb_set_t        *lookup_indexes /* OUT */)
Packit 874993
{
Packit 874993
  /* XXX Does the first part always succeed? */
Packit 874993
  HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
Packit 874993
static void
Packit 874993
add_char (hb_font_t          *font,
Packit 874993
	  hb_unicode_funcs_t *unicode,
Packit 874993
	  hb_bool_t           mirror,
Packit 874993
	  hb_codepoint_t      u,
Packit 874993
	  hb_set_t           *glyphs)
Packit 874993
{
Packit 874993
  hb_codepoint_t glyph;
Packit 874993
  if (font->get_nominal_glyph (u, &glyph))
Packit 874993
    glyphs->add (glyph);
Packit 874993
  if (mirror)
Packit 874993
  {
Packit 874993
    hb_codepoint_t m = unicode->mirroring (u);
Packit 874993
    if (m != u && font->get_nominal_glyph (m, &glyph))
Packit 874993
      glyphs->add (glyph);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/**
Packit 874993
 * hb_ot_shape_glyphs_closure:
Packit 874993
 *
Packit 874993
 * Since: 0.9.2
Packit 874993
 **/
Packit 874993
void
Packit 874993
hb_ot_shape_glyphs_closure (hb_font_t          *font,
Packit 874993
			    hb_buffer_t        *buffer,
Packit 874993
			    const hb_feature_t *features,
Packit 874993
			    unsigned int        num_features,
Packit 874993
			    hb_set_t           *glyphs)
Packit 874993
{
Packit 874993
  hb_ot_shape_plan_t plan;
Packit 874993
Packit 874993
  const char *shapers[] = {"ot", NULL};
Packit 874993
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
Packit 874993
							     features, num_features, shapers);
Packit 874993
Packit 874993
  bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
Packit 874993
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
    add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
Packit 874993
Packit 874993
  hb_set_t lookups;
Packit 874993
  lookups.init ();
Packit 874993
  hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
Packit 874993
Packit 874993
  /* And find transitive closure. */
Packit 874993
  hb_set_t copy;
Packit 874993
  copy.init ();
Packit 874993
  do {
Packit 874993
    copy.set (glyphs);
Packit 874993
    for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
Packit 874993
      hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
Packit 874993
  } while (!copy.is_equal (glyphs));
Packit 874993
Packit 874993
  hb_shape_plan_destroy (shape_plan);
Packit 874993
}