Blame src/hb-ot-layout-gsub-table.hh

Packit 874993
/*
Packit 874993
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
Packit 874993
 * Copyright © 2010,2012,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
#ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
Packit 874993
#define HB_OT_LAYOUT_GSUB_TABLE_HH
Packit 874993
Packit 874993
#include "hb-ot-layout-gsubgpos-private.hh"
Packit 874993
Packit 874993
Packit 874993
namespace OT {
Packit 874993
Packit 874993
Packit 874993
struct SingleSubstFormat1
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      /* TODO Switch to range-based API to work around malicious fonts.
Packit 874993
       * https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      hb_codepoint_t glyph_id = iter.get_glyph ();
Packit 874993
      if (c->glyphs->has (glyph_id))
Packit 874993
	c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFFu);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      /* TODO Switch to range-based API to work around malicious fonts.
Packit 874993
       * https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      hb_codepoint_t glyph_id = iter.get_glyph ();
Packit 874993
      c->input->add (glyph_id);
Packit 874993
      c->output->add ((glyph_id + deltaGlyphID) & 0xFFFFu);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
Packit 874993
    unsigned int index = (this+coverage).get_coverage (glyph_id);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    /* According to the Adobe Annotated OpenType Suite, result is always
Packit 874993
     * limited to 16bit. */
Packit 874993
    glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu;
Packit 874993
    c->replace_glyph (glyph_id);
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 unsigned int num_glyphs,
Packit 874993
			 int delta)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
Packit 874993
    deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 1 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of Substitution table */
Packit 874993
  SHORT		deltaGlyphID;		/* Add to original GlyphID to get
Packit 874993
					 * substitute GlyphID */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_STATIC (6);
Packit 874993
};
Packit 874993
Packit 874993
struct SingleSubstFormat2
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = substitute.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      if (c->glyphs->has (iter.get_glyph ()))
Packit 874993
	c->glyphs->add (substitute[iter.get_coverage ()]);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = substitute.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      c->input->add (iter.get_glyph ());
Packit 874993
      c->output->add (substitute[iter.get_coverage ()]);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
Packit 874993
    unsigned int index = (this+coverage).get_coverage (glyph_id);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    if (unlikely (index >= substitute.len)) return_trace (false);
Packit 874993
Packit 874993
    glyph_id = substitute[index];
Packit 874993
    c->replace_glyph (glyph_id);
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<GlyphID> &substitutes,
Packit 874993
			 unsigned int num_glyphs)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return_trace (false);
Packit 874993
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (coverage.sanitize (c, this) && substitute.sanitize (c));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 2 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of Substitution table */
Packit 874993
  ArrayOf<GlyphID>
Packit 874993
		substitute;		/* Array of substitute
Packit 874993
					 * GlyphIDs--ordered by Coverage Index */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (6, substitute);
Packit 874993
};
Packit 874993
Packit 874993
struct SingleSubst
Packit 874993
{
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<GlyphID> &substitutes,
Packit 874993
			 unsigned int num_glyphs)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
Packit 874993
    unsigned int format = 2;
Packit 874993
    int delta = 0;
Packit 874993
    if (num_glyphs) {
Packit 874993
      format = 1;
Packit 874993
      /* TODO(serialize) check for wrap-around */
Packit 874993
      delta = substitutes[0] - glyphs[0];
Packit 874993
      for (unsigned int i = 1; i < num_glyphs; i++)
Packit 874993
	if (delta != substitutes[i] - glyphs[i]) {
Packit 874993
	  format = 2;
Packit 874993
	  break;
Packit 874993
	}
Packit 874993
    }
Packit 874993
    u.format.set (format);
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (u.format1.serialize (c, glyphs, num_glyphs, delta));
Packit 874993
    case 2: return_trace (u.format2.serialize (c, glyphs, substitutes, num_glyphs));
Packit 874993
    default:return_trace (false);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, u.format);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (c->dispatch (u.format1));
Packit 874993
    case 2: return_trace (c->dispatch (u.format2));
Packit 874993
    default:return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT		format;		/* Format identifier */
Packit 874993
  SingleSubstFormat1	format1;
Packit 874993
  SingleSubstFormat2	format2;
Packit 874993
  } u;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
struct Sequence
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    unsigned int count = substitute.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      c->glyphs->add (substitute[i]);
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    unsigned int count = substitute.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      c->output->add (substitute[i]);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    unsigned int count = substitute.len;
Packit 874993
Packit 874993
    /* Special-case to make it in-place and not consider this
Packit 874993
     * as a "multiplied" substitution. */
Packit 874993
    if (unlikely (count == 1))
Packit 874993
    {
Packit 874993
      c->replace_glyph (substitute.array[0]);
Packit 874993
      return_trace (true);
Packit 874993
    }
Packit 874993
    /* Spec disallows this, but Uniscribe allows it.
Packit 874993
     * https://github.com/behdad/harfbuzz/issues/253 */
Packit 874993
    else if (unlikely (count == 0))
Packit 874993
    {
Packit 874993
      c->buffer->delete_glyph ();
Packit 874993
      return_trace (true);
Packit 874993
    }
Packit 874993
Packit 874993
    unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
Packit 874993
			 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
Packit 874993
Packit 874993
    for (unsigned int i = 0; i < count; i++) {
Packit 874993
      _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
Packit 874993
      c->output_glyph_for_component (substitute.array[i], klass);
Packit 874993
    }
Packit 874993
    c->buffer->skip_glyph ();
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 unsigned int num_glyphs)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (substitute.sanitize (c));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  ArrayOf<GlyphID>
Packit 874993
		substitute;		/* String of GlyphIDs to substitute */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (2, substitute);
Packit 874993
};
Packit 874993
Packit 874993
struct MultipleSubstFormat1
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = sequence.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      if (c->glyphs->has (iter.get_glyph ()))
Packit 874993
	(this+sequence[iter.get_coverage ()]).closure (c);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    (this+coverage).add_coverage (c->input);
Packit 874993
    unsigned int count = sequence.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
	(this+sequence[i]).collect_glyphs (c);
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
Packit 874993
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    return_trace ((this+sequence[index]).apply (c));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<unsigned int> &substitute_len_list,
Packit 874993
			 unsigned int num_glyphs,
Packit 874993
			 Supplier<GlyphID> &substitute_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!sequence.serialize (c, num_glyphs))) return_trace (false);
Packit 874993
    for (unsigned int i = 0; i < num_glyphs; i++)
Packit 874993
      if (unlikely (!sequence[i].serialize (c, this).serialize (c,
Packit 874993
								substitute_glyphs_list,
Packit 874993
								substitute_len_list[i]))) return_trace (false);
Packit 874993
    substitute_len_list.advance (num_glyphs);
Packit 874993
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (coverage.sanitize (c, this) && sequence.sanitize (c, this));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 1 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of Substitution table */
Packit 874993
  OffsetArrayOf<Sequence>
Packit 874993
		sequence;		/* Array of Sequence tables
Packit 874993
					 * ordered by Coverage Index */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (6, sequence);
Packit 874993
};
Packit 874993
Packit 874993
struct MultipleSubst
Packit 874993
{
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<unsigned int> &substitute_len_list,
Packit 874993
			 unsigned int num_glyphs,
Packit 874993
			 Supplier<GlyphID> &substitute_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
Packit 874993
    unsigned int format = 1;
Packit 874993
    u.format.set (format);
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list));
Packit 874993
    default:return_trace (false);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, u.format);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (c->dispatch (u.format1));
Packit 874993
    default:return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT		format;		/* Format identifier */
Packit 874993
  MultipleSubstFormat1	format1;
Packit 874993
  } u;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
Packit 874993
					 * arbitrary order */
Packit 874993
Packit 874993
struct AlternateSubstFormat1
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = alternateSet.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      if (c->glyphs->has (iter.get_glyph ())) {
Packit 874993
	const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
Packit 874993
	unsigned int count = alt_set.len;
Packit 874993
	for (unsigned int i = 0; i < count; i++)
Packit 874993
	  c->glyphs->add (alt_set[i]);
Packit 874993
      }
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = alternateSet.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      c->input->add (iter.get_glyph ());
Packit 874993
      const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
Packit 874993
      unsigned int count = alt_set.len;
Packit 874993
      for (unsigned int i = 0; i < count; i++)
Packit 874993
	c->output->add (alt_set[i]);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
Packit 874993
Packit 874993
    unsigned int index = (this+coverage).get_coverage (glyph_id);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    const AlternateSet &alt_set = this+alternateSet[index];
Packit 874993
Packit 874993
    if (unlikely (!alt_set.len)) return_trace (false);
Packit 874993
Packit 874993
    hb_mask_t glyph_mask = c->buffer->cur().mask;
Packit 874993
    hb_mask_t lookup_mask = c->lookup_mask;
Packit 874993
Packit 874993
    /* Note: This breaks badly if two features enabled this lookup together. */
Packit 874993
    unsigned int shift = _hb_ctz (lookup_mask);
Packit 874993
    unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
Packit 874993
Packit 874993
    if (unlikely (alt_index > alt_set.len || alt_index == 0)) return_trace (false);
Packit 874993
Packit 874993
    glyph_id = alt_set[alt_index - 1];
Packit 874993
Packit 874993
    c->replace_glyph (glyph_id);
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<unsigned int> &alternate_len_list,
Packit 874993
			 unsigned int num_glyphs,
Packit 874993
			 Supplier<GlyphID> &alternate_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!alternateSet.serialize (c, num_glyphs))) return_trace (false);
Packit 874993
    for (unsigned int i = 0; i < num_glyphs; i++)
Packit 874993
      if (unlikely (!alternateSet[i].serialize (c, this).serialize (c,
Packit 874993
								    alternate_glyphs_list,
Packit 874993
								    alternate_len_list[i]))) return_trace (false);
Packit 874993
    alternate_len_list.advance (num_glyphs);
Packit 874993
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 1 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of Substitution table */
Packit 874993
  OffsetArrayOf<AlternateSet>
Packit 874993
		alternateSet;		/* Array of AlternateSet tables
Packit 874993
					 * ordered by Coverage Index */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (6, alternateSet);
Packit 874993
};
Packit 874993
Packit 874993
struct AlternateSubst
Packit 874993
{
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &glyphs,
Packit 874993
			 Supplier<unsigned int> &alternate_len_list,
Packit 874993
			 unsigned int num_glyphs,
Packit 874993
			 Supplier<GlyphID> &alternate_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
Packit 874993
    unsigned int format = 1;
Packit 874993
    u.format.set (format);
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list));
Packit 874993
    default:return_trace (false);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, u.format);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (c->dispatch (u.format1));
Packit 874993
    default:return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT		format;		/* Format identifier */
Packit 874993
  AlternateSubstFormat1	format1;
Packit 874993
  } u;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
struct Ligature
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    unsigned int count = component.len;
Packit 874993
    for (unsigned int i = 1; i < count; i++)
Packit 874993
      if (!c->glyphs->has (component[i]))
Packit 874993
        return;
Packit 874993
    c->glyphs->add (ligGlyph);
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    unsigned int count = component.len;
Packit 874993
    for (unsigned int i = 1; i < count; i++)
Packit 874993
      c->input->add (component[i]);
Packit 874993
    c->output->add (ligGlyph);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    if (c->len != component.len)
Packit 874993
      return_trace (false);
Packit 874993
Packit 874993
    for (unsigned int i = 1; i < c->len; i++)
Packit 874993
      if (likely (c->glyphs[i] != component[i]))
Packit 874993
	return_trace (false);
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    unsigned int count = component.len;
Packit 874993
Packit 874993
    if (unlikely (!count)) return_trace (false);
Packit 874993
Packit 874993
    /* Special-case to make it in-place and not consider this
Packit 874993
     * as a "ligated" substitution. */
Packit 874993
    if (unlikely (count == 1))
Packit 874993
    {
Packit 874993
      c->replace_glyph (ligGlyph);
Packit 874993
      return_trace (true);
Packit 874993
    }
Packit 874993
Packit 874993
    bool is_mark_ligature = false;
Packit 874993
    unsigned int total_component_count = 0;
Packit 874993
Packit 874993
    unsigned int match_length = 0;
Packit 874993
    unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
Packit 874993
Packit 874993
    if (likely (!match_input (c, count,
Packit 874993
			      &component[1],
Packit 874993
			      match_glyph,
Packit 874993
			      NULL,
Packit 874993
			      &match_length,
Packit 874993
			      match_positions,
Packit 874993
			      &is_mark_ligature,
Packit 874993
			      &total_component_count)))
Packit 874993
      return_trace (false);
Packit 874993
Packit 874993
    ligate_input (c,
Packit 874993
		  count,
Packit 874993
		  match_positions,
Packit 874993
		  match_length,
Packit 874993
		  ligGlyph,
Packit 874993
		  is_mark_ligature,
Packit 874993
		  total_component_count);
Packit 874993
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 GlyphID ligature,
Packit 874993
			 Supplier<GlyphID> &components, /* Starting from second */
Packit 874993
			 unsigned int num_components /* Including first component */)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    ligGlyph = ligature;
Packit 874993
    if (unlikely (!component.serialize (c, components, num_components))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  public:
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
Packit 874993
  HeadlessArrayOf<GlyphID>
Packit 874993
		component;		/* Array of component GlyphIDs--start
Packit 874993
					 * with the second  component--ordered
Packit 874993
					 * in writing direction */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (4, component);
Packit 874993
};
Packit 874993
Packit 874993
struct LigatureSet
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    unsigned int num_ligs = ligature.len;
Packit 874993
    for (unsigned int i = 0; i < num_ligs; i++)
Packit 874993
      (this+ligature[i]).closure (c);
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    unsigned int num_ligs = ligature.len;
Packit 874993
    for (unsigned int i = 0; i < num_ligs; i++)
Packit 874993
      (this+ligature[i]).collect_glyphs (c);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    unsigned int num_ligs = ligature.len;
Packit 874993
    for (unsigned int i = 0; i < num_ligs; i++)
Packit 874993
    {
Packit 874993
      const Ligature &lig = this+ligature[i];
Packit 874993
      if (lig.would_apply (c))
Packit 874993
        return_trace (true);
Packit 874993
    }
Packit 874993
    return_trace (false);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    unsigned int num_ligs = ligature.len;
Packit 874993
    for (unsigned int i = 0; i < num_ligs; i++)
Packit 874993
    {
Packit 874993
      const Ligature &lig = this+ligature[i];
Packit 874993
      if (lig.apply (c)) return_trace (true);
Packit 874993
    }
Packit 874993
Packit 874993
    return_trace (false);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &ligatures,
Packit 874993
			 Supplier<unsigned int> &component_count_list,
Packit 874993
			 unsigned int num_ligatures,
Packit 874993
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!ligature.serialize (c, num_ligatures))) return_trace (false);
Packit 874993
    for (unsigned int i = 0; i < num_ligatures; i++)
Packit 874993
      if (unlikely (!ligature[i].serialize (c, this).serialize (c,
Packit 874993
								ligatures[i],
Packit 874993
								component_list,
Packit 874993
								component_count_list[i]))) return_trace (false);
Packit 874993
    ligatures.advance (num_ligatures);
Packit 874993
    component_count_list.advance (num_ligatures);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (ligature.sanitize (c, this));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  OffsetArrayOf<Ligature>
Packit 874993
		ligature;		/* Array LigatureSet tables
Packit 874993
					 * ordered by preference */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (2, ligature);
Packit 874993
};
Packit 874993
Packit 874993
struct LigatureSubstFormat1
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = ligatureSet.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      if (c->glyphs->has (iter.get_glyph ()))
Packit 874993
	(this+ligatureSet[iter.get_coverage ()]).closure (c);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    unsigned int count = ligatureSet.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      c->input->add (iter.get_glyph ());
Packit 874993
      (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    const LigatureSet &lig_set = this+ligatureSet[index];
Packit 874993
    return_trace (lig_set.would_apply (c));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
Packit 874993
Packit 874993
    unsigned int index = (this+coverage).get_coverage (glyph_id);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    const LigatureSet &lig_set = this+ligatureSet[index];
Packit 874993
    return_trace (lig_set.apply (c));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &first_glyphs,
Packit 874993
			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
Packit 874993
			 unsigned int num_first_glyphs,
Packit 874993
			 Supplier<GlyphID> &ligatures_list,
Packit 874993
			 Supplier<unsigned int> &component_count_list,
Packit 874993
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (*this))) return_trace (false);
Packit 874993
    if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return_trace (false);
Packit 874993
    for (unsigned int i = 0; i < num_first_glyphs; i++)
Packit 874993
      if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c,
Packit 874993
								   ligatures_list,
Packit 874993
								   component_count_list,
Packit 874993
								   ligature_per_first_glyph_count_list[i],
Packit 874993
								   component_list))) return_trace (false);
Packit 874993
    ligature_per_first_glyph_count_list.advance (num_first_glyphs);
Packit 874993
    if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return_trace (false);
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 1 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of Substitution table */
Packit 874993
  OffsetArrayOf<LigatureSet>
Packit 874993
		ligatureSet;		/* Array LigatureSet tables
Packit 874993
					 * ordered by Coverage Index */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (6, ligatureSet);
Packit 874993
};
Packit 874993
Packit 874993
struct LigatureSubst
Packit 874993
{
Packit 874993
  inline bool serialize (hb_serialize_context_t *c,
Packit 874993
			 Supplier<GlyphID> &first_glyphs,
Packit 874993
			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
Packit 874993
			 unsigned int num_first_glyphs,
Packit 874993
			 Supplier<GlyphID> &ligatures_list,
Packit 874993
			 Supplier<unsigned int> &component_count_list,
Packit 874993
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
Packit 874993
    unsigned int format = 1;
Packit 874993
    u.format.set (format);
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (u.format1.serialize (c,
Packit 874993
					       first_glyphs,
Packit 874993
					       ligature_per_first_glyph_count_list,
Packit 874993
					       num_first_glyphs,
Packit 874993
					       ligatures_list,
Packit 874993
					       component_count_list,
Packit 874993
					       component_list));
Packit 874993
    default:return_trace (false);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, u.format);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (c->dispatch (u.format1));
Packit 874993
    default:return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT		format;		/* Format identifier */
Packit 874993
  LigatureSubstFormat1	format1;
Packit 874993
  } u;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
struct ContextSubst : Context {};
Packit 874993
Packit 874993
struct ChainContextSubst : ChainContext {};
Packit 874993
Packit 874993
struct ExtensionSubst : Extension<ExtensionSubst>
Packit 874993
{
Packit 874993
  typedef struct SubstLookupSubTable LookupSubTable;
Packit 874993
Packit 874993
  inline bool is_reverse (void) const;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
struct ReverseChainSingleSubstFormat1
Packit 874993
{
Packit 874993
  inline void closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Packit 874993
Packit 874993
    unsigned int count;
Packit 874993
Packit 874993
    count = backtrack.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      if (!(this+backtrack[i]).intersects (c->glyphs))
Packit 874993
        return;
Packit 874993
Packit 874993
    count = lookahead.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      if (!(this+lookahead[i]).intersects (c->glyphs))
Packit 874993
        return;
Packit 874993
Packit 874993
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
Packit 874993
    Coverage::Iter iter;
Packit 874993
    count = substitute.len;
Packit 874993
    for (iter.init (this+coverage); iter.more (); iter.next ())
Packit 874993
    {
Packit 874993
      if (unlikely (iter.get_coverage () >= count))
Packit 874993
        break; /* Work around malicious fonts. https://github.com/behdad/harfbuzz/issues/363 */
Packit 874993
      if (c->glyphs->has (iter.get_glyph ()))
Packit 874993
	c->glyphs->add (substitute[iter.get_coverage ()]);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
Packit 874993
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Packit 874993
Packit 874993
    unsigned int count;
Packit 874993
Packit 874993
    (this+coverage).add_coverage (c->input);
Packit 874993
Packit 874993
    count = backtrack.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      (this+backtrack[i]).add_coverage (c->before);
Packit 874993
Packit 874993
    count = lookahead.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      (this+lookahead[i]).add_coverage (c->after);
Packit 874993
Packit 874993
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
Packit 874993
    count = substitute.len;
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      c->output->add (substitute[i]);
Packit 874993
  }
Packit 874993
Packit 874993
  inline const Coverage &get_coverage (void) const
Packit 874993
  {
Packit 874993
    return this+coverage;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL))
Packit 874993
      return_trace (false); /* No chaining to this type */
Packit 874993
Packit 874993
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
Packit 874993
    if (likely (index == NOT_COVERED)) return_trace (false);
Packit 874993
Packit 874993
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Packit 874993
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
Packit 874993
Packit 874993
    if (match_backtrack (c,
Packit 874993
			 backtrack.len, (USHORT *) backtrack.array,
Packit 874993
			 match_coverage, this) &&
Packit 874993
        match_lookahead (c,
Packit 874993
			 lookahead.len, (USHORT *) lookahead.array,
Packit 874993
			 match_coverage, this,
Packit 874993
			 1))
Packit 874993
    {
Packit 874993
      c->replace_glyph_inplace (substitute[index]);
Packit 874993
      /* Note: We DON'T decrease buffer->idx.  The main loop does it
Packit 874993
       * for us.  This is useful for preventing surprises if someone
Packit 874993
       * calls us through a Context lookup. */
Packit 874993
      return_trace (true);
Packit 874993
    }
Packit 874993
Packit 874993
    return_trace (false);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
Packit 874993
      return_trace (false);
Packit 874993
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
Packit 874993
    if (!lookahead.sanitize (c, this))
Packit 874993
      return_trace (false);
Packit 874993
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
Packit 874993
    return_trace (substitute.sanitize (c));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	format;			/* Format identifier--format = 1 */
Packit 874993
  OffsetTo<Coverage>
Packit 874993
		coverage;		/* Offset to Coverage table--from
Packit 874993
					 * beginning of table */
Packit 874993
  OffsetArrayOf<Coverage>
Packit 874993
		backtrack;		/* Array of coverage tables
Packit 874993
					 * in backtracking sequence, in  glyph
Packit 874993
					 * sequence order */
Packit 874993
  OffsetArrayOf<Coverage>
Packit 874993
		lookaheadX;		/* Array of coverage tables
Packit 874993
					 * in lookahead sequence, in glyph
Packit 874993
					 * sequence order */
Packit 874993
  ArrayOf<GlyphID>
Packit 874993
		substituteX;		/* Array of substitute
Packit 874993
					 * GlyphIDs--ordered by Coverage Index */
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_MIN (10);
Packit 874993
};
Packit 874993
Packit 874993
struct ReverseChainSingleSubst
Packit 874993
{
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, u.format);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (u.format) {
Packit 874993
    case 1: return_trace (c->dispatch (u.format1));
Packit 874993
    default:return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT				format;		/* Format identifier */
Packit 874993
  ReverseChainSingleSubstFormat1	format1;
Packit 874993
  } u;
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * SubstLookup
Packit 874993
 */
Packit 874993
Packit 874993
struct SubstLookupSubTable
Packit 874993
{
Packit 874993
  friend struct SubstLookup;
Packit 874993
Packit 874993
  enum Type {
Packit 874993
    Single		= 1,
Packit 874993
    Multiple		= 2,
Packit 874993
    Alternate		= 3,
Packit 874993
    Ligature		= 4,
Packit 874993
    Context		= 5,
Packit 874993
    ChainContext	= 6,
Packit 874993
    Extension		= 7,
Packit 874993
    ReverseChainSingle	= 8
Packit 874993
  };
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
Packit 874993
  {
Packit 874993
    TRACE_DISPATCH (this, lookup_type);
Packit 874993
    if (unlikely (!c->may_dispatch (this, &u.sub_format))) return_trace (c->no_dispatch_return_value ());
Packit 874993
    switch (lookup_type) {
Packit 874993
    case Single:		return_trace (u.single.dispatch (c));
Packit 874993
    case Multiple:		return_trace (u.multiple.dispatch (c));
Packit 874993
    case Alternate:		return_trace (u.alternate.dispatch (c));
Packit 874993
    case Ligature:		return_trace (u.ligature.dispatch (c));
Packit 874993
    case Context:		return_trace (u.context.dispatch (c));
Packit 874993
    case ChainContext:		return_trace (u.chainContext.dispatch (c));
Packit 874993
    case Extension:		return_trace (u.extension.dispatch (c));
Packit 874993
    case ReverseChainSingle:	return_trace (u.reverseChainContextSingle.dispatch (c));
Packit 874993
    default:			return_trace (c->default_return_value ());
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  union {
Packit 874993
  USHORT			sub_format;
Packit 874993
  SingleSubst			single;
Packit 874993
  MultipleSubst			multiple;
Packit 874993
  AlternateSubst		alternate;
Packit 874993
  LigatureSubst			ligature;
Packit 874993
  ContextSubst			context;
Packit 874993
  ChainContextSubst		chainContext;
Packit 874993
  ExtensionSubst		extension;
Packit 874993
  ReverseChainSingleSubst	reverseChainContextSingle;
Packit 874993
  } u;
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_UNION (2, sub_format);
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
struct SubstLookup : Lookup
Packit 874993
{
Packit 874993
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
Packit 874993
  { return Lookup::get_subtable<SubstLookupSubTable> (i); }
Packit 874993
Packit 874993
  inline static bool lookup_type_is_reverse (unsigned int lookup_type)
Packit 874993
  { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
Packit 874993
Packit 874993
  inline bool is_reverse (void) const
Packit 874993
  {
Packit 874993
    unsigned int type = get_type ();
Packit 874993
    if (unlikely (type == SubstLookupSubTable::Extension))
Packit 874993
      return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
Packit 874993
    return lookup_type_is_reverse (type);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool apply (hb_apply_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_APPLY (this);
Packit 874993
    return_trace (dispatch (c));
Packit 874993
  }
Packit 874993
Packit 874993
  inline hb_closure_context_t::return_t closure (hb_closure_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_CLOSURE (this);
Packit 874993
    c->set_recurse_func (dispatch_recurse_func<hb_closure_context_t>);
Packit 874993
    return_trace (dispatch (c));
Packit 874993
  }
Packit 874993
Packit 874993
  inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_COLLECT_GLYPHS (this);
Packit 874993
    c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
Packit 874993
    return_trace (dispatch (c));
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename set_t>
Packit 874993
  inline void add_coverage (set_t *glyphs) const
Packit 874993
  {
Packit 874993
    hb_add_coverage_context_t<set_t> c (glyphs);
Packit 874993
    dispatch (&c);
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool would_apply (hb_would_apply_context_t *c,
Packit 874993
			   const hb_ot_layout_lookup_accelerator_t *accel) const
Packit 874993
  {
Packit 874993
    TRACE_WOULD_APPLY (this);
Packit 874993
    if (unlikely (!c->len))  return_trace (false);
Packit 874993
    if (!accel->may_have (c->glyphs[0]))  return_trace (false);
Packit 874993
      return_trace (dispatch (c));
Packit 874993
  }
Packit 874993
Packit 874993
  static bool apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index);
Packit 874993
Packit 874993
  inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c,
Packit 874993
						  unsigned int i)
Packit 874993
  { return get_subtables<SubstLookupSubTable> ()[i].serialize (c, this); }
Packit 874993
Packit 874993
  inline bool serialize_single (hb_serialize_context_t *c,
Packit 874993
				uint32_t lookup_props,
Packit 874993
			        Supplier<GlyphID> &glyphs,
Packit 874993
			        Supplier<GlyphID> &substitutes,
Packit 874993
			        unsigned int num_glyphs)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return_trace (false);
Packit 874993
    return_trace (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize_multiple (hb_serialize_context_t *c,
Packit 874993
				  uint32_t lookup_props,
Packit 874993
				  Supplier<GlyphID> &glyphs,
Packit 874993
				  Supplier<unsigned int> &substitute_len_list,
Packit 874993
				  unsigned int num_glyphs,
Packit 874993
				  Supplier<GlyphID> &substitute_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return_trace (false);
Packit 874993
    return_trace (serialize_subtable (c, 0).u.multiple.serialize (c,
Packit 874993
								  glyphs,
Packit 874993
								  substitute_len_list,
Packit 874993
								  num_glyphs,
Packit 874993
								  substitute_glyphs_list));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize_alternate (hb_serialize_context_t *c,
Packit 874993
				   uint32_t lookup_props,
Packit 874993
				   Supplier<GlyphID> &glyphs,
Packit 874993
				   Supplier<unsigned int> &alternate_len_list,
Packit 874993
				   unsigned int num_glyphs,
Packit 874993
				   Supplier<GlyphID> &alternate_glyphs_list)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return_trace (false);
Packit 874993
    return_trace (serialize_subtable (c, 0).u.alternate.serialize (c,
Packit 874993
								   glyphs,
Packit 874993
								   alternate_len_list,
Packit 874993
								   num_glyphs,
Packit 874993
								   alternate_glyphs_list));
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool serialize_ligature (hb_serialize_context_t *c,
Packit 874993
				  uint32_t lookup_props,
Packit 874993
				  Supplier<GlyphID> &first_glyphs,
Packit 874993
				  Supplier<unsigned int> &ligature_per_first_glyph_count_list,
Packit 874993
				  unsigned int num_first_glyphs,
Packit 874993
				  Supplier<GlyphID> &ligatures_list,
Packit 874993
				  Supplier<unsigned int> &component_count_list,
Packit 874993
				  Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
Packit 874993
  {
Packit 874993
    TRACE_SERIALIZE (this);
Packit 874993
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return_trace (false);
Packit 874993
    return_trace (serialize_subtable (c, 0).u.ligature.serialize (c,
Packit 874993
								  first_glyphs,
Packit 874993
								  ligature_per_first_glyph_count_list,
Packit 874993
								  num_first_glyphs,
Packit 874993
								  ligatures_list,
Packit 874993
								  component_count_list,
Packit 874993
								  component_list));
Packit 874993
  }
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
Packit 874993
Packit 874993
  template <typename context_t>
Packit 874993
  inline typename context_t::return_t dispatch (context_t *c) const
Packit 874993
  { return Lookup::dispatch<SubstLookupSubTable> (c); }
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    if (unlikely (!Lookup::sanitize (c))) return_trace (false);
Packit 874993
    if (unlikely (!dispatch (c))) return_trace (false);
Packit 874993
Packit 874993
    if (unlikely (get_type () == SubstLookupSubTable::Extension))
Packit 874993
    {
Packit 874993
      /* The spec says all subtables of an Extension lookup should
Packit 874993
       * have the same type.  This is specially important if one has
Packit 874993
       * a reverse type! */
Packit 874993
      unsigned int type = get_subtable (0).u.extension.get_type ();
Packit 874993
      unsigned int count = get_subtable_count ();
Packit 874993
      for (unsigned int i = 1; i < count; i++)
Packit 874993
        if (get_subtable (i).u.extension.get_type () != type)
Packit 874993
	  return_trace (false);
Packit 874993
    }
Packit 874993
    return_trace (true);
Packit 874993
  }
Packit 874993
};
Packit 874993
Packit 874993
typedef OffsetListOf<SubstLookup> SubstLookupList;
Packit 874993
Packit 874993
/*
Packit 874993
 * GSUB -- The Glyph Substitution Table
Packit 874993
 */
Packit 874993
Packit 874993
struct GSUB : GSUBGPOS
Packit 874993
{
Packit 874993
  static const hb_tag_t tableTag	= HB_OT_TAG_GSUB;
Packit 874993
Packit 874993
  inline const SubstLookup& get_lookup (unsigned int i) const
Packit 874993
  { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
Packit 874993
Packit 874993
  static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    if (unlikely (!GSUBGPOS::sanitize (c))) return_trace (false);
Packit 874993
    const OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
Packit 874993
    return_trace (list.sanitize (c, this));
Packit 874993
  }
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
void
Packit 874993
GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
Packit 874993
{
Packit 874993
  _hb_buffer_assert_gsubgpos_vars (buffer);
Packit 874993
Packit 874993
  const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
  {
Packit 874993
    _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
Packit 874993
    _hb_glyph_info_clear_lig_props (&buffer->info[i]);
Packit 874993
    buffer->info[i].syllable() = 0;
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/* Out-of-class implementation for methods recursing */
Packit 874993
Packit 874993
/*static*/ inline bool ExtensionSubst::is_reverse (void) const
Packit 874993
{
Packit 874993
  unsigned int type = get_type ();
Packit 874993
  if (unlikely (type == SubstLookupSubTable::Extension))
Packit 874993
    return CastR<ExtensionSubst> (get_subtable<LookupSubTable>()).is_reverse ();
Packit 874993
  return SubstLookup::lookup_type_is_reverse (type);
Packit 874993
}
Packit 874993
Packit 874993
template <typename context_t>
Packit 874993
/*static*/ inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
Packit 874993
{
Packit 874993
  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
Packit 874993
  const SubstLookup &l = gsub.get_lookup (lookup_index);
Packit 874993
  return l.dispatch (c);
Packit 874993
}
Packit 874993
Packit 874993
/*static*/ inline bool SubstLookup::apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index)
Packit 874993
{
Packit 874993
  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
Packit 874993
  const SubstLookup &l = gsub.get_lookup (lookup_index);
Packit 874993
  unsigned int saved_lookup_props = c->lookup_props;
Packit 874993
  unsigned int saved_lookup_index = c->lookup_index;
Packit 874993
  c->set_lookup_index (lookup_index);
Packit 874993
  c->set_lookup_props (l.get_props ());
Packit 874993
  bool ret = l.dispatch (c);
Packit 874993
  c->set_lookup_index (saved_lookup_index);
Packit 874993
  c->set_lookup_props (saved_lookup_props);
Packit 874993
  return ret;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
} /* namespace OT */
Packit 874993
Packit 874993
Packit 874993
#endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */