gfx/harfbuzz/src/hb-ot-layout-gsub-table.hh

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/hb-ot-layout-gsub-table.hh	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1372 @@
     1.4 +/*
     1.5 + * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
     1.6 + * Copyright © 2010,2012,2013  Google, Inc.
     1.7 + *
     1.8 + *  This is part of HarfBuzz, a text shaping library.
     1.9 + *
    1.10 + * Permission is hereby granted, without written agreement and without
    1.11 + * license or royalty fees, to use, copy, modify, and distribute this
    1.12 + * software and its documentation for any purpose, provided that the
    1.13 + * above copyright notice and the following two paragraphs appear in
    1.14 + * all copies of this software.
    1.15 + *
    1.16 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
    1.17 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    1.18 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
    1.19 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    1.20 + * DAMAGE.
    1.21 + *
    1.22 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
    1.23 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.24 + * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.25 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
    1.26 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.27 + *
    1.28 + * Red Hat Author(s): Behdad Esfahbod
    1.29 + * Google Author(s): Behdad Esfahbod
    1.30 + */
    1.31 +
    1.32 +#ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
    1.33 +#define HB_OT_LAYOUT_GSUB_TABLE_HH
    1.34 +
    1.35 +#include "hb-ot-layout-gsubgpos-private.hh"
    1.36 +
    1.37 +
    1.38 +namespace OT {
    1.39 +
    1.40 +
    1.41 +struct SingleSubstFormat1
    1.42 +{
    1.43 +  inline void closure (hb_closure_context_t *c) const
    1.44 +  {
    1.45 +    TRACE_CLOSURE (this);
    1.46 +    Coverage::Iter iter;
    1.47 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
    1.48 +      hb_codepoint_t glyph_id = iter.get_glyph ();
    1.49 +      if (c->glyphs->has (glyph_id))
    1.50 +	c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFF);
    1.51 +    }
    1.52 +  }
    1.53 +
    1.54 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
    1.55 +  {
    1.56 +    TRACE_COLLECT_GLYPHS (this);
    1.57 +    Coverage::Iter iter;
    1.58 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
    1.59 +      hb_codepoint_t glyph_id = iter.get_glyph ();
    1.60 +      c->input->add (glyph_id);
    1.61 +      c->output->add ((glyph_id + deltaGlyphID) & 0xFFFF);
    1.62 +    }
    1.63 +  }
    1.64 +
    1.65 +  inline const Coverage &get_coverage (void) const
    1.66 +  {
    1.67 +    return this+coverage;
    1.68 +  }
    1.69 +
    1.70 +  inline bool would_apply (hb_would_apply_context_t *c) const
    1.71 +  {
    1.72 +    TRACE_WOULD_APPLY (this);
    1.73 +    return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
    1.74 +  }
    1.75 +
    1.76 +  inline bool apply (hb_apply_context_t *c) const
    1.77 +  {
    1.78 +    TRACE_APPLY (this);
    1.79 +    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
    1.80 +    unsigned int index = (this+coverage).get_coverage (glyph_id);
    1.81 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
    1.82 +
    1.83 +    /* According to the Adobe Annotated OpenType Suite, result is always
    1.84 +     * limited to 16bit. */
    1.85 +    glyph_id = (glyph_id + deltaGlyphID) & 0xFFFF;
    1.86 +    c->replace_glyph (glyph_id);
    1.87 +
    1.88 +    return TRACE_RETURN (true);
    1.89 +  }
    1.90 +
    1.91 +  inline bool serialize (hb_serialize_context_t *c,
    1.92 +			 Supplier<GlyphID> &glyphs,
    1.93 +			 unsigned int num_glyphs,
    1.94 +			 int delta)
    1.95 +  {
    1.96 +    TRACE_SERIALIZE (this);
    1.97 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
    1.98 +    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
    1.99 +    deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */
   1.100 +    return TRACE_RETURN (true);
   1.101 +  }
   1.102 +
   1.103 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.104 +    TRACE_SANITIZE (this);
   1.105 +    return TRACE_RETURN (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
   1.106 +  }
   1.107 +
   1.108 +  protected:
   1.109 +  USHORT	format;			/* Format identifier--format = 1 */
   1.110 +  OffsetTo<Coverage>
   1.111 +		coverage;		/* Offset to Coverage table--from
   1.112 +					 * beginning of Substitution table */
   1.113 +  SHORT		deltaGlyphID;		/* Add to original GlyphID to get
   1.114 +					 * substitute GlyphID */
   1.115 +  public:
   1.116 +  DEFINE_SIZE_STATIC (6);
   1.117 +};
   1.118 +
   1.119 +struct SingleSubstFormat2
   1.120 +{
   1.121 +  inline void closure (hb_closure_context_t *c) const
   1.122 +  {
   1.123 +    TRACE_CLOSURE (this);
   1.124 +    Coverage::Iter iter;
   1.125 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.126 +      if (c->glyphs->has (iter.get_glyph ()))
   1.127 +	c->glyphs->add (substitute[iter.get_coverage ()]);
   1.128 +    }
   1.129 +  }
   1.130 +
   1.131 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.132 +  {
   1.133 +    TRACE_COLLECT_GLYPHS (this);
   1.134 +    Coverage::Iter iter;
   1.135 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.136 +      c->input->add (iter.get_glyph ());
   1.137 +      c->output->add (substitute[iter.get_coverage ()]);
   1.138 +    }
   1.139 +  }
   1.140 +
   1.141 +  inline const Coverage &get_coverage (void) const
   1.142 +  {
   1.143 +    return this+coverage;
   1.144 +  }
   1.145 +
   1.146 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.147 +  {
   1.148 +    TRACE_WOULD_APPLY (this);
   1.149 +    return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
   1.150 +  }
   1.151 +
   1.152 +  inline bool apply (hb_apply_context_t *c) const
   1.153 +  {
   1.154 +    TRACE_APPLY (this);
   1.155 +    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
   1.156 +    unsigned int index = (this+coverage).get_coverage (glyph_id);
   1.157 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.158 +
   1.159 +    if (unlikely (index >= substitute.len)) return TRACE_RETURN (false);
   1.160 +
   1.161 +    glyph_id = substitute[index];
   1.162 +    c->replace_glyph (glyph_id);
   1.163 +
   1.164 +    return TRACE_RETURN (true);
   1.165 +  }
   1.166 +
   1.167 +  inline bool serialize (hb_serialize_context_t *c,
   1.168 +			 Supplier<GlyphID> &glyphs,
   1.169 +			 Supplier<GlyphID> &substitutes,
   1.170 +			 unsigned int num_glyphs)
   1.171 +  {
   1.172 +    TRACE_SERIALIZE (this);
   1.173 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.174 +    if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return TRACE_RETURN (false);
   1.175 +    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
   1.176 +    return TRACE_RETURN (true);
   1.177 +  }
   1.178 +
   1.179 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.180 +    TRACE_SANITIZE (this);
   1.181 +    return TRACE_RETURN (coverage.sanitize (c, this) && substitute.sanitize (c));
   1.182 +  }
   1.183 +
   1.184 +  protected:
   1.185 +  USHORT	format;			/* Format identifier--format = 2 */
   1.186 +  OffsetTo<Coverage>
   1.187 +		coverage;		/* Offset to Coverage table--from
   1.188 +					 * beginning of Substitution table */
   1.189 +  ArrayOf<GlyphID>
   1.190 +		substitute;		/* Array of substitute
   1.191 +					 * GlyphIDs--ordered by Coverage Index */
   1.192 +  public:
   1.193 +  DEFINE_SIZE_ARRAY (6, substitute);
   1.194 +};
   1.195 +
   1.196 +struct SingleSubst
   1.197 +{
   1.198 +  inline bool serialize (hb_serialize_context_t *c,
   1.199 +			 Supplier<GlyphID> &glyphs,
   1.200 +			 Supplier<GlyphID> &substitutes,
   1.201 +			 unsigned int num_glyphs)
   1.202 +  {
   1.203 +    TRACE_SERIALIZE (this);
   1.204 +    if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
   1.205 +    unsigned int format = 2;
   1.206 +    int delta;
   1.207 +    if (num_glyphs) {
   1.208 +      format = 1;
   1.209 +      /* TODO(serialize) check for wrap-around */
   1.210 +      delta = substitutes[0] - glyphs[0];
   1.211 +      for (unsigned int i = 1; i < num_glyphs; i++)
   1.212 +	if (delta != substitutes[i] - glyphs[i]) {
   1.213 +	  format = 2;
   1.214 +	  break;
   1.215 +	}
   1.216 +    }
   1.217 +    u.format.set (format);
   1.218 +    switch (u.format) {
   1.219 +    case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs, delta));
   1.220 +    case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, substitutes, num_glyphs));
   1.221 +    default:return TRACE_RETURN (false);
   1.222 +    }
   1.223 +  }
   1.224 +
   1.225 +  template <typename context_t>
   1.226 +  inline typename context_t::return_t dispatch (context_t *c) const
   1.227 +  {
   1.228 +    TRACE_DISPATCH (this);
   1.229 +    switch (u.format) {
   1.230 +    case 1: return TRACE_RETURN (c->dispatch (u.format1));
   1.231 +    case 2: return TRACE_RETURN (c->dispatch (u.format2));
   1.232 +    default:return TRACE_RETURN (c->default_return_value ());
   1.233 +    }
   1.234 +  }
   1.235 +
   1.236 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.237 +    TRACE_SANITIZE (this);
   1.238 +    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
   1.239 +    switch (u.format) {
   1.240 +    case 1: return TRACE_RETURN (u.format1.sanitize (c));
   1.241 +    case 2: return TRACE_RETURN (u.format2.sanitize (c));
   1.242 +    default:return TRACE_RETURN (true);
   1.243 +    }
   1.244 +  }
   1.245 +
   1.246 +  protected:
   1.247 +  union {
   1.248 +  USHORT		format;		/* Format identifier */
   1.249 +  SingleSubstFormat1	format1;
   1.250 +  SingleSubstFormat2	format2;
   1.251 +  } u;
   1.252 +};
   1.253 +
   1.254 +
   1.255 +struct Sequence
   1.256 +{
   1.257 +  inline void closure (hb_closure_context_t *c) const
   1.258 +  {
   1.259 +    TRACE_CLOSURE (this);
   1.260 +    unsigned int count = substitute.len;
   1.261 +    for (unsigned int i = 0; i < count; i++)
   1.262 +      c->glyphs->add (substitute[i]);
   1.263 +  }
   1.264 +
   1.265 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.266 +  {
   1.267 +    TRACE_COLLECT_GLYPHS (this);
   1.268 +    unsigned int count = substitute.len;
   1.269 +    for (unsigned int i = 0; i < count; i++)
   1.270 +      c->output->add (substitute[i]);
   1.271 +  }
   1.272 +
   1.273 +  inline bool apply (hb_apply_context_t *c) const
   1.274 +  {
   1.275 +    TRACE_APPLY (this);
   1.276 +    if (unlikely (!substitute.len)) return TRACE_RETURN (false);
   1.277 +
   1.278 +    unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
   1.279 +			 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
   1.280 +    unsigned int count = substitute.len;
   1.281 +    if (count == 1) /* Special-case to make it in-place. */
   1.282 +    {
   1.283 +      c->replace_glyph (substitute.array[0]);
   1.284 +    }
   1.285 +    else
   1.286 +    {
   1.287 +      for (unsigned int i = 0; i < count; i++) {
   1.288 +	_hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
   1.289 +	c->output_glyph (substitute.array[i], klass);
   1.290 +      }
   1.291 +      c->buffer->skip_glyph ();
   1.292 +    }
   1.293 +
   1.294 +    return TRACE_RETURN (true);
   1.295 +  }
   1.296 +
   1.297 +  inline bool serialize (hb_serialize_context_t *c,
   1.298 +			 Supplier<GlyphID> &glyphs,
   1.299 +			 unsigned int num_glyphs)
   1.300 +  {
   1.301 +    TRACE_SERIALIZE (this);
   1.302 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.303 +    if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
   1.304 +    return TRACE_RETURN (true);
   1.305 +  }
   1.306 +
   1.307 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.308 +    TRACE_SANITIZE (this);
   1.309 +    return TRACE_RETURN (substitute.sanitize (c));
   1.310 +  }
   1.311 +
   1.312 +  protected:
   1.313 +  ArrayOf<GlyphID>
   1.314 +		substitute;		/* String of GlyphIDs to substitute */
   1.315 +  public:
   1.316 +  DEFINE_SIZE_ARRAY (2, substitute);
   1.317 +};
   1.318 +
   1.319 +struct MultipleSubstFormat1
   1.320 +{
   1.321 +  inline void closure (hb_closure_context_t *c) const
   1.322 +  {
   1.323 +    TRACE_CLOSURE (this);
   1.324 +    Coverage::Iter iter;
   1.325 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.326 +      if (c->glyphs->has (iter.get_glyph ()))
   1.327 +	(this+sequence[iter.get_coverage ()]).closure (c);
   1.328 +    }
   1.329 +  }
   1.330 +
   1.331 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.332 +  {
   1.333 +    TRACE_COLLECT_GLYPHS (this);
   1.334 +    (this+coverage).add_coverage (c->input);
   1.335 +    unsigned int count = sequence.len;
   1.336 +    for (unsigned int i = 0; i < count; i++)
   1.337 +	(this+sequence[i]).collect_glyphs (c);
   1.338 +  }
   1.339 +
   1.340 +  inline const Coverage &get_coverage (void) const
   1.341 +  {
   1.342 +    return this+coverage;
   1.343 +  }
   1.344 +
   1.345 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.346 +  {
   1.347 +    TRACE_WOULD_APPLY (this);
   1.348 +    return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
   1.349 +  }
   1.350 +
   1.351 +  inline bool apply (hb_apply_context_t *c) const
   1.352 +  {
   1.353 +    TRACE_APPLY (this);
   1.354 +
   1.355 +    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
   1.356 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.357 +
   1.358 +    return TRACE_RETURN ((this+sequence[index]).apply (c));
   1.359 +  }
   1.360 +
   1.361 +  inline bool serialize (hb_serialize_context_t *c,
   1.362 +			 Supplier<GlyphID> &glyphs,
   1.363 +			 Supplier<unsigned int> &substitute_len_list,
   1.364 +			 unsigned int num_glyphs,
   1.365 +			 Supplier<GlyphID> &substitute_glyphs_list)
   1.366 +  {
   1.367 +    TRACE_SERIALIZE (this);
   1.368 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.369 +    if (unlikely (!sequence.serialize (c, num_glyphs))) return TRACE_RETURN (false);
   1.370 +    for (unsigned int i = 0; i < num_glyphs; i++)
   1.371 +      if (unlikely (!sequence[i].serialize (c, this).serialize (c,
   1.372 +								substitute_glyphs_list,
   1.373 +								substitute_len_list[i]))) return TRACE_RETURN (false);
   1.374 +    substitute_len_list.advance (num_glyphs);
   1.375 +    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
   1.376 +    return TRACE_RETURN (true);
   1.377 +  }
   1.378 +
   1.379 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.380 +    TRACE_SANITIZE (this);
   1.381 +    return TRACE_RETURN (coverage.sanitize (c, this) && sequence.sanitize (c, this));
   1.382 +  }
   1.383 +
   1.384 +  protected:
   1.385 +  USHORT	format;			/* Format identifier--format = 1 */
   1.386 +  OffsetTo<Coverage>
   1.387 +		coverage;		/* Offset to Coverage table--from
   1.388 +					 * beginning of Substitution table */
   1.389 +  OffsetArrayOf<Sequence>
   1.390 +		sequence;		/* Array of Sequence tables
   1.391 +					 * ordered by Coverage Index */
   1.392 +  public:
   1.393 +  DEFINE_SIZE_ARRAY (6, sequence);
   1.394 +};
   1.395 +
   1.396 +struct MultipleSubst
   1.397 +{
   1.398 +  inline bool serialize (hb_serialize_context_t *c,
   1.399 +			 Supplier<GlyphID> &glyphs,
   1.400 +			 Supplier<unsigned int> &substitute_len_list,
   1.401 +			 unsigned int num_glyphs,
   1.402 +			 Supplier<GlyphID> &substitute_glyphs_list)
   1.403 +  {
   1.404 +    TRACE_SERIALIZE (this);
   1.405 +    if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
   1.406 +    unsigned int format = 1;
   1.407 +    u.format.set (format);
   1.408 +    switch (u.format) {
   1.409 +    case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list));
   1.410 +    default:return TRACE_RETURN (false);
   1.411 +    }
   1.412 +  }
   1.413 +
   1.414 +  template <typename context_t>
   1.415 +  inline typename context_t::return_t dispatch (context_t *c) const
   1.416 +  {
   1.417 +    TRACE_DISPATCH (this);
   1.418 +    switch (u.format) {
   1.419 +    case 1: return TRACE_RETURN (c->dispatch (u.format1));
   1.420 +    default:return TRACE_RETURN (c->default_return_value ());
   1.421 +    }
   1.422 +  }
   1.423 +
   1.424 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.425 +    TRACE_SANITIZE (this);
   1.426 +    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
   1.427 +    switch (u.format) {
   1.428 +    case 1: return TRACE_RETURN (u.format1.sanitize (c));
   1.429 +    default:return TRACE_RETURN (true);
   1.430 +    }
   1.431 +  }
   1.432 +
   1.433 +  protected:
   1.434 +  union {
   1.435 +  USHORT		format;		/* Format identifier */
   1.436 +  MultipleSubstFormat1	format1;
   1.437 +  } u;
   1.438 +};
   1.439 +
   1.440 +
   1.441 +typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
   1.442 +					 * arbitrary order */
   1.443 +
   1.444 +struct AlternateSubstFormat1
   1.445 +{
   1.446 +  inline void closure (hb_closure_context_t *c) const
   1.447 +  {
   1.448 +    TRACE_CLOSURE (this);
   1.449 +    Coverage::Iter iter;
   1.450 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.451 +      if (c->glyphs->has (iter.get_glyph ())) {
   1.452 +	const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
   1.453 +	unsigned int count = alt_set.len;
   1.454 +	for (unsigned int i = 0; i < count; i++)
   1.455 +	  c->glyphs->add (alt_set[i]);
   1.456 +      }
   1.457 +    }
   1.458 +  }
   1.459 +
   1.460 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.461 +  {
   1.462 +    TRACE_COLLECT_GLYPHS (this);
   1.463 +    Coverage::Iter iter;
   1.464 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.465 +      c->input->add (iter.get_glyph ());
   1.466 +      const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
   1.467 +      unsigned int count = alt_set.len;
   1.468 +      for (unsigned int i = 0; i < count; i++)
   1.469 +	c->output->add (alt_set[i]);
   1.470 +    }
   1.471 +  }
   1.472 +
   1.473 +  inline const Coverage &get_coverage (void) const
   1.474 +  {
   1.475 +    return this+coverage;
   1.476 +  }
   1.477 +
   1.478 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.479 +  {
   1.480 +    TRACE_WOULD_APPLY (this);
   1.481 +    return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
   1.482 +  }
   1.483 +
   1.484 +  inline bool apply (hb_apply_context_t *c) const
   1.485 +  {
   1.486 +    TRACE_APPLY (this);
   1.487 +    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
   1.488 +
   1.489 +    unsigned int index = (this+coverage).get_coverage (glyph_id);
   1.490 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.491 +
   1.492 +    const AlternateSet &alt_set = this+alternateSet[index];
   1.493 +
   1.494 +    if (unlikely (!alt_set.len)) return TRACE_RETURN (false);
   1.495 +
   1.496 +    hb_mask_t glyph_mask = c->buffer->cur().mask;
   1.497 +    hb_mask_t lookup_mask = c->lookup_mask;
   1.498 +
   1.499 +    /* Note: This breaks badly if two features enabled this lookup together. */
   1.500 +    unsigned int shift = _hb_ctz (lookup_mask);
   1.501 +    unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
   1.502 +
   1.503 +    if (unlikely (alt_index > alt_set.len || alt_index == 0)) return TRACE_RETURN (false);
   1.504 +
   1.505 +    glyph_id = alt_set[alt_index - 1];
   1.506 +
   1.507 +    c->replace_glyph (glyph_id);
   1.508 +
   1.509 +    return TRACE_RETURN (true);
   1.510 +  }
   1.511 +
   1.512 +  inline bool serialize (hb_serialize_context_t *c,
   1.513 +			 Supplier<GlyphID> &glyphs,
   1.514 +			 Supplier<unsigned int> &alternate_len_list,
   1.515 +			 unsigned int num_glyphs,
   1.516 +			 Supplier<GlyphID> &alternate_glyphs_list)
   1.517 +  {
   1.518 +    TRACE_SERIALIZE (this);
   1.519 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.520 +    if (unlikely (!alternateSet.serialize (c, num_glyphs))) return TRACE_RETURN (false);
   1.521 +    for (unsigned int i = 0; i < num_glyphs; i++)
   1.522 +      if (unlikely (!alternateSet[i].serialize (c, this).serialize (c,
   1.523 +								    alternate_glyphs_list,
   1.524 +								    alternate_len_list[i]))) return TRACE_RETURN (false);
   1.525 +    alternate_len_list.advance (num_glyphs);
   1.526 +    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
   1.527 +    return TRACE_RETURN (true);
   1.528 +  }
   1.529 +
   1.530 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.531 +    TRACE_SANITIZE (this);
   1.532 +    return TRACE_RETURN (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
   1.533 +  }
   1.534 +
   1.535 +  protected:
   1.536 +  USHORT	format;			/* Format identifier--format = 1 */
   1.537 +  OffsetTo<Coverage>
   1.538 +		coverage;		/* Offset to Coverage table--from
   1.539 +					 * beginning of Substitution table */
   1.540 +  OffsetArrayOf<AlternateSet>
   1.541 +		alternateSet;		/* Array of AlternateSet tables
   1.542 +					 * ordered by Coverage Index */
   1.543 +  public:
   1.544 +  DEFINE_SIZE_ARRAY (6, alternateSet);
   1.545 +};
   1.546 +
   1.547 +struct AlternateSubst
   1.548 +{
   1.549 +  inline bool serialize (hb_serialize_context_t *c,
   1.550 +			 Supplier<GlyphID> &glyphs,
   1.551 +			 Supplier<unsigned int> &alternate_len_list,
   1.552 +			 unsigned int num_glyphs,
   1.553 +			 Supplier<GlyphID> &alternate_glyphs_list)
   1.554 +  {
   1.555 +    TRACE_SERIALIZE (this);
   1.556 +    if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
   1.557 +    unsigned int format = 1;
   1.558 +    u.format.set (format);
   1.559 +    switch (u.format) {
   1.560 +    case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list));
   1.561 +    default:return TRACE_RETURN (false);
   1.562 +    }
   1.563 +  }
   1.564 +
   1.565 +  template <typename context_t>
   1.566 +  inline typename context_t::return_t dispatch (context_t *c) const
   1.567 +  {
   1.568 +    TRACE_DISPATCH (this);
   1.569 +    switch (u.format) {
   1.570 +    case 1: return TRACE_RETURN (c->dispatch (u.format1));
   1.571 +    default:return TRACE_RETURN (c->default_return_value ());
   1.572 +    }
   1.573 +  }
   1.574 +
   1.575 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.576 +    TRACE_SANITIZE (this);
   1.577 +    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
   1.578 +    switch (u.format) {
   1.579 +    case 1: return TRACE_RETURN (u.format1.sanitize (c));
   1.580 +    default:return TRACE_RETURN (true);
   1.581 +    }
   1.582 +  }
   1.583 +
   1.584 +  protected:
   1.585 +  union {
   1.586 +  USHORT		format;		/* Format identifier */
   1.587 +  AlternateSubstFormat1	format1;
   1.588 +  } u;
   1.589 +};
   1.590 +
   1.591 +
   1.592 +struct Ligature
   1.593 +{
   1.594 +  inline void closure (hb_closure_context_t *c) const
   1.595 +  {
   1.596 +    TRACE_CLOSURE (this);
   1.597 +    unsigned int count = component.len;
   1.598 +    for (unsigned int i = 1; i < count; i++)
   1.599 +      if (!c->glyphs->has (component[i]))
   1.600 +        return;
   1.601 +    c->glyphs->add (ligGlyph);
   1.602 +  }
   1.603 +
   1.604 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.605 +  {
   1.606 +    TRACE_COLLECT_GLYPHS (this);
   1.607 +    unsigned int count = component.len;
   1.608 +    for (unsigned int i = 1; i < count; i++)
   1.609 +      c->input->add (component[i]);
   1.610 +    c->output->add (ligGlyph);
   1.611 +  }
   1.612 +
   1.613 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.614 +  {
   1.615 +    TRACE_WOULD_APPLY (this);
   1.616 +    if (c->len != component.len)
   1.617 +      return TRACE_RETURN (false);
   1.618 +
   1.619 +    for (unsigned int i = 1; i < c->len; i++)
   1.620 +      if (likely (c->glyphs[i] != component[i]))
   1.621 +	return TRACE_RETURN (false);
   1.622 +
   1.623 +    return TRACE_RETURN (true);
   1.624 +  }
   1.625 +
   1.626 +  inline bool apply (hb_apply_context_t *c) const
   1.627 +  {
   1.628 +    TRACE_APPLY (this);
   1.629 +    unsigned int count = component.len;
   1.630 +    if (unlikely (count < 1)) return TRACE_RETURN (false);
   1.631 +
   1.632 +    bool is_mark_ligature = false;
   1.633 +    unsigned int total_component_count = 0;
   1.634 +
   1.635 +    unsigned int match_length = 0;
   1.636 +    unsigned int match_positions[MAX_CONTEXT_LENGTH];
   1.637 +
   1.638 +    if (likely (!match_input (c, count,
   1.639 +			      &component[1],
   1.640 +			      match_glyph,
   1.641 +			      NULL,
   1.642 +			      &match_length,
   1.643 +			      match_positions,
   1.644 +			      &is_mark_ligature,
   1.645 +			      &total_component_count)))
   1.646 +      return TRACE_RETURN (false);
   1.647 +
   1.648 +    ligate_input (c,
   1.649 +		  count,
   1.650 +		  match_positions,
   1.651 +		  match_length,
   1.652 +		  ligGlyph,
   1.653 +		  is_mark_ligature,
   1.654 +		  total_component_count);
   1.655 +
   1.656 +    return TRACE_RETURN (true);
   1.657 +  }
   1.658 +
   1.659 +  inline bool serialize (hb_serialize_context_t *c,
   1.660 +			 GlyphID ligature,
   1.661 +			 Supplier<GlyphID> &components, /* Starting from second */
   1.662 +			 unsigned int num_components /* Including first component */)
   1.663 +  {
   1.664 +    TRACE_SERIALIZE (this);
   1.665 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.666 +    ligGlyph = ligature;
   1.667 +    if (unlikely (!component.serialize (c, components, num_components))) return TRACE_RETURN (false);
   1.668 +    return TRACE_RETURN (true);
   1.669 +  }
   1.670 +
   1.671 +  public:
   1.672 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.673 +    TRACE_SANITIZE (this);
   1.674 +    return TRACE_RETURN (ligGlyph.sanitize (c) && component.sanitize (c));
   1.675 +  }
   1.676 +
   1.677 +  protected:
   1.678 +  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
   1.679 +  HeadlessArrayOf<GlyphID>
   1.680 +		component;		/* Array of component GlyphIDs--start
   1.681 +					 * with the second  component--ordered
   1.682 +					 * in writing direction */
   1.683 +  public:
   1.684 +  DEFINE_SIZE_ARRAY (4, component);
   1.685 +};
   1.686 +
   1.687 +struct LigatureSet
   1.688 +{
   1.689 +  inline void closure (hb_closure_context_t *c) const
   1.690 +  {
   1.691 +    TRACE_CLOSURE (this);
   1.692 +    unsigned int num_ligs = ligature.len;
   1.693 +    for (unsigned int i = 0; i < num_ligs; i++)
   1.694 +      (this+ligature[i]).closure (c);
   1.695 +  }
   1.696 +
   1.697 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.698 +  {
   1.699 +    TRACE_COLLECT_GLYPHS (this);
   1.700 +    unsigned int num_ligs = ligature.len;
   1.701 +    for (unsigned int i = 0; i < num_ligs; i++)
   1.702 +      (this+ligature[i]).collect_glyphs (c);
   1.703 +  }
   1.704 +
   1.705 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.706 +  {
   1.707 +    TRACE_WOULD_APPLY (this);
   1.708 +    unsigned int num_ligs = ligature.len;
   1.709 +    for (unsigned int i = 0; i < num_ligs; i++)
   1.710 +    {
   1.711 +      const Ligature &lig = this+ligature[i];
   1.712 +      if (lig.would_apply (c))
   1.713 +        return TRACE_RETURN (true);
   1.714 +    }
   1.715 +    return TRACE_RETURN (false);
   1.716 +  }
   1.717 +
   1.718 +  inline bool apply (hb_apply_context_t *c) const
   1.719 +  {
   1.720 +    TRACE_APPLY (this);
   1.721 +    unsigned int num_ligs = ligature.len;
   1.722 +    for (unsigned int i = 0; i < num_ligs; i++)
   1.723 +    {
   1.724 +      const Ligature &lig = this+ligature[i];
   1.725 +      if (lig.apply (c)) return TRACE_RETURN (true);
   1.726 +    }
   1.727 +
   1.728 +    return TRACE_RETURN (false);
   1.729 +  }
   1.730 +
   1.731 +  inline bool serialize (hb_serialize_context_t *c,
   1.732 +			 Supplier<GlyphID> &ligatures,
   1.733 +			 Supplier<unsigned int> &component_count_list,
   1.734 +			 unsigned int num_ligatures,
   1.735 +			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
   1.736 +  {
   1.737 +    TRACE_SERIALIZE (this);
   1.738 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.739 +    if (unlikely (!ligature.serialize (c, num_ligatures))) return TRACE_RETURN (false);
   1.740 +    for (unsigned int i = 0; i < num_ligatures; i++)
   1.741 +      if (unlikely (!ligature[i].serialize (c, this).serialize (c,
   1.742 +								ligatures[i],
   1.743 +								component_list,
   1.744 +								component_count_list[i]))) return TRACE_RETURN (false);
   1.745 +    ligatures.advance (num_ligatures);
   1.746 +    component_count_list.advance (num_ligatures);
   1.747 +    return TRACE_RETURN (true);
   1.748 +  }
   1.749 +
   1.750 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.751 +    TRACE_SANITIZE (this);
   1.752 +    return TRACE_RETURN (ligature.sanitize (c, this));
   1.753 +  }
   1.754 +
   1.755 +  protected:
   1.756 +  OffsetArrayOf<Ligature>
   1.757 +		ligature;		/* Array LigatureSet tables
   1.758 +					 * ordered by preference */
   1.759 +  public:
   1.760 +  DEFINE_SIZE_ARRAY (2, ligature);
   1.761 +};
   1.762 +
   1.763 +struct LigatureSubstFormat1
   1.764 +{
   1.765 +  inline void closure (hb_closure_context_t *c) const
   1.766 +  {
   1.767 +    TRACE_CLOSURE (this);
   1.768 +    Coverage::Iter iter;
   1.769 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.770 +      if (c->glyphs->has (iter.get_glyph ()))
   1.771 +	(this+ligatureSet[iter.get_coverage ()]).closure (c);
   1.772 +    }
   1.773 +  }
   1.774 +
   1.775 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.776 +  {
   1.777 +    TRACE_COLLECT_GLYPHS (this);
   1.778 +    Coverage::Iter iter;
   1.779 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.780 +      c->input->add (iter.get_glyph ());
   1.781 +      (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c);
   1.782 +    }
   1.783 +  }
   1.784 +
   1.785 +  inline const Coverage &get_coverage (void) const
   1.786 +  {
   1.787 +    return this+coverage;
   1.788 +  }
   1.789 +
   1.790 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.791 +  {
   1.792 +    TRACE_WOULD_APPLY (this);
   1.793 +    unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
   1.794 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.795 +
   1.796 +    const LigatureSet &lig_set = this+ligatureSet[index];
   1.797 +    return TRACE_RETURN (lig_set.would_apply (c));
   1.798 +  }
   1.799 +
   1.800 +  inline bool apply (hb_apply_context_t *c) const
   1.801 +  {
   1.802 +    TRACE_APPLY (this);
   1.803 +    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
   1.804 +
   1.805 +    unsigned int index = (this+coverage).get_coverage (glyph_id);
   1.806 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.807 +
   1.808 +    const LigatureSet &lig_set = this+ligatureSet[index];
   1.809 +    return TRACE_RETURN (lig_set.apply (c));
   1.810 +  }
   1.811 +
   1.812 +  inline bool serialize (hb_serialize_context_t *c,
   1.813 +			 Supplier<GlyphID> &first_glyphs,
   1.814 +			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
   1.815 +			 unsigned int num_first_glyphs,
   1.816 +			 Supplier<GlyphID> &ligatures_list,
   1.817 +			 Supplier<unsigned int> &component_count_list,
   1.818 +			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
   1.819 +  {
   1.820 +    TRACE_SERIALIZE (this);
   1.821 +    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
   1.822 +    if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return TRACE_RETURN (false);
   1.823 +    for (unsigned int i = 0; i < num_first_glyphs; i++)
   1.824 +      if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c,
   1.825 +								   ligatures_list,
   1.826 +								   component_count_list,
   1.827 +								   ligature_per_first_glyph_count_list[i],
   1.828 +								   component_list))) return TRACE_RETURN (false);
   1.829 +    ligature_per_first_glyph_count_list.advance (num_first_glyphs);
   1.830 +    if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return TRACE_RETURN (false);
   1.831 +    return TRACE_RETURN (true);
   1.832 +  }
   1.833 +
   1.834 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.835 +    TRACE_SANITIZE (this);
   1.836 +    return TRACE_RETURN (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
   1.837 +  }
   1.838 +
   1.839 +  protected:
   1.840 +  USHORT	format;			/* Format identifier--format = 1 */
   1.841 +  OffsetTo<Coverage>
   1.842 +		coverage;		/* Offset to Coverage table--from
   1.843 +					 * beginning of Substitution table */
   1.844 +  OffsetArrayOf<LigatureSet>
   1.845 +		ligatureSet;		/* Array LigatureSet tables
   1.846 +					 * ordered by Coverage Index */
   1.847 +  public:
   1.848 +  DEFINE_SIZE_ARRAY (6, ligatureSet);
   1.849 +};
   1.850 +
   1.851 +struct LigatureSubst
   1.852 +{
   1.853 +  inline bool serialize (hb_serialize_context_t *c,
   1.854 +			 Supplier<GlyphID> &first_glyphs,
   1.855 +			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
   1.856 +			 unsigned int num_first_glyphs,
   1.857 +			 Supplier<GlyphID> &ligatures_list,
   1.858 +			 Supplier<unsigned int> &component_count_list,
   1.859 +			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
   1.860 +  {
   1.861 +    TRACE_SERIALIZE (this);
   1.862 +    if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
   1.863 +    unsigned int format = 1;
   1.864 +    u.format.set (format);
   1.865 +    switch (u.format) {
   1.866 +    case 1: return TRACE_RETURN (u.format1.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs,
   1.867 +						      ligatures_list, component_count_list, component_list));
   1.868 +    default:return TRACE_RETURN (false);
   1.869 +    }
   1.870 +  }
   1.871 +
   1.872 +  template <typename context_t>
   1.873 +  inline typename context_t::return_t dispatch (context_t *c) const
   1.874 +  {
   1.875 +    TRACE_DISPATCH (this);
   1.876 +    switch (u.format) {
   1.877 +    case 1: return TRACE_RETURN (c->dispatch (u.format1));
   1.878 +    default:return TRACE_RETURN (c->default_return_value ());
   1.879 +    }
   1.880 +  }
   1.881 +
   1.882 +  inline bool sanitize (hb_sanitize_context_t *c) {
   1.883 +    TRACE_SANITIZE (this);
   1.884 +    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
   1.885 +    switch (u.format) {
   1.886 +    case 1: return TRACE_RETURN (u.format1.sanitize (c));
   1.887 +    default:return TRACE_RETURN (true);
   1.888 +    }
   1.889 +  }
   1.890 +
   1.891 +  protected:
   1.892 +  union {
   1.893 +  USHORT		format;		/* Format identifier */
   1.894 +  LigatureSubstFormat1	format1;
   1.895 +  } u;
   1.896 +};
   1.897 +
   1.898 +
   1.899 +struct ContextSubst : Context {};
   1.900 +
   1.901 +struct ChainContextSubst : ChainContext {};
   1.902 +
   1.903 +struct ExtensionSubst : Extension<ExtensionSubst>
   1.904 +{
   1.905 +  typedef struct SubstLookupSubTable LookupSubTable;
   1.906 +
   1.907 +  inline bool is_reverse (void) const;
   1.908 +};
   1.909 +
   1.910 +
   1.911 +struct ReverseChainSingleSubstFormat1
   1.912 +{
   1.913 +  inline void closure (hb_closure_context_t *c) const
   1.914 +  {
   1.915 +    TRACE_CLOSURE (this);
   1.916 +    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
   1.917 +
   1.918 +    unsigned int count;
   1.919 +
   1.920 +    count = backtrack.len;
   1.921 +    for (unsigned int i = 0; i < count; i++)
   1.922 +      if (!(this+backtrack[i]).intersects (c->glyphs))
   1.923 +        return;
   1.924 +
   1.925 +    count = lookahead.len;
   1.926 +    for (unsigned int i = 0; i < count; i++)
   1.927 +      if (!(this+lookahead[i]).intersects (c->glyphs))
   1.928 +        return;
   1.929 +
   1.930 +    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
   1.931 +    Coverage::Iter iter;
   1.932 +    for (iter.init (this+coverage); iter.more (); iter.next ()) {
   1.933 +      if (c->glyphs->has (iter.get_glyph ()))
   1.934 +	c->glyphs->add (substitute[iter.get_coverage ()]);
   1.935 +    }
   1.936 +  }
   1.937 +
   1.938 +  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
   1.939 +  {
   1.940 +    TRACE_COLLECT_GLYPHS (this);
   1.941 +
   1.942 +    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
   1.943 +
   1.944 +    unsigned int count;
   1.945 +
   1.946 +    (this+coverage).add_coverage (c->input);
   1.947 +
   1.948 +    count = backtrack.len;
   1.949 +    for (unsigned int i = 0; i < count; i++)
   1.950 +      (this+backtrack[i]).add_coverage (c->before);
   1.951 +
   1.952 +    count = lookahead.len;
   1.953 +    for (unsigned int i = 0; i < count; i++)
   1.954 +      (this+lookahead[i]).add_coverage (c->after);
   1.955 +
   1.956 +    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
   1.957 +    count = substitute.len;
   1.958 +    for (unsigned int i = 0; i < count; i++)
   1.959 +      c->output->add (substitute[i]);
   1.960 +  }
   1.961 +
   1.962 +  inline const Coverage &get_coverage (void) const
   1.963 +  {
   1.964 +    return this+coverage;
   1.965 +  }
   1.966 +
   1.967 +  inline bool would_apply (hb_would_apply_context_t *c) const
   1.968 +  {
   1.969 +    TRACE_WOULD_APPLY (this);
   1.970 +    return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
   1.971 +  }
   1.972 +
   1.973 +  inline bool apply (hb_apply_context_t *c) const
   1.974 +  {
   1.975 +    TRACE_APPLY (this);
   1.976 +    if (unlikely (c->nesting_level_left != MAX_NESTING_LEVEL))
   1.977 +      return TRACE_RETURN (false); /* No chaining to this type */
   1.978 +
   1.979 +    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
   1.980 +    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
   1.981 +
   1.982 +    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
   1.983 +    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
   1.984 +
   1.985 +    if (match_backtrack (c,
   1.986 +			 backtrack.len, (USHORT *) backtrack.array,
   1.987 +			 match_coverage, this) &&
   1.988 +        match_lookahead (c,
   1.989 +			 lookahead.len, (USHORT *) lookahead.array,
   1.990 +			 match_coverage, this,
   1.991 +			 1))
   1.992 +    {
   1.993 +      c->replace_glyph_inplace (substitute[index]);
   1.994 +      /* Note: We DON'T decrease buffer->idx.  The main loop does it
   1.995 +       * for us.  This is useful for preventing surprises if someone
   1.996 +       * calls us through a Context lookup. */
   1.997 +      return TRACE_RETURN (true);
   1.998 +    }
   1.999 +
  1.1000 +    return TRACE_RETURN (false);
  1.1001 +  }
  1.1002 +
  1.1003 +  inline bool sanitize (hb_sanitize_context_t *c) {
  1.1004 +    TRACE_SANITIZE (this);
  1.1005 +    if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
  1.1006 +      return TRACE_RETURN (false);
  1.1007 +    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
  1.1008 +    if (!lookahead.sanitize (c, this))
  1.1009 +      return TRACE_RETURN (false);
  1.1010 +    ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
  1.1011 +    return TRACE_RETURN (substitute.sanitize (c));
  1.1012 +  }
  1.1013 +
  1.1014 +  protected:
  1.1015 +  USHORT	format;			/* Format identifier--format = 1 */
  1.1016 +  OffsetTo<Coverage>
  1.1017 +		coverage;		/* Offset to Coverage table--from
  1.1018 +					 * beginning of table */
  1.1019 +  OffsetArrayOf<Coverage>
  1.1020 +		backtrack;		/* Array of coverage tables
  1.1021 +					 * in backtracking sequence, in  glyph
  1.1022 +					 * sequence order */
  1.1023 +  OffsetArrayOf<Coverage>
  1.1024 +		lookaheadX;		/* Array of coverage tables
  1.1025 +					 * in lookahead sequence, in glyph
  1.1026 +					 * sequence order */
  1.1027 +  ArrayOf<GlyphID>
  1.1028 +		substituteX;		/* Array of substitute
  1.1029 +					 * GlyphIDs--ordered by Coverage Index */
  1.1030 +  public:
  1.1031 +  DEFINE_SIZE_MIN (10);
  1.1032 +};
  1.1033 +
  1.1034 +struct ReverseChainSingleSubst
  1.1035 +{
  1.1036 +  template <typename context_t>
  1.1037 +  inline typename context_t::return_t dispatch (context_t *c) const
  1.1038 +  {
  1.1039 +    TRACE_DISPATCH (this);
  1.1040 +    switch (u.format) {
  1.1041 +    case 1: return TRACE_RETURN (c->dispatch (u.format1));
  1.1042 +    default:return TRACE_RETURN (c->default_return_value ());
  1.1043 +    }
  1.1044 +  }
  1.1045 +
  1.1046 +  inline bool sanitize (hb_sanitize_context_t *c) {
  1.1047 +    TRACE_SANITIZE (this);
  1.1048 +    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  1.1049 +    switch (u.format) {
  1.1050 +    case 1: return TRACE_RETURN (u.format1.sanitize (c));
  1.1051 +    default:return TRACE_RETURN (true);
  1.1052 +    }
  1.1053 +  }
  1.1054 +
  1.1055 +  protected:
  1.1056 +  union {
  1.1057 +  USHORT				format;		/* Format identifier */
  1.1058 +  ReverseChainSingleSubstFormat1	format1;
  1.1059 +  } u;
  1.1060 +};
  1.1061 +
  1.1062 +
  1.1063 +
  1.1064 +/*
  1.1065 + * SubstLookup
  1.1066 + */
  1.1067 +
  1.1068 +struct SubstLookupSubTable
  1.1069 +{
  1.1070 +  friend struct SubstLookup;
  1.1071 +
  1.1072 +  enum Type {
  1.1073 +    Single		= 1,
  1.1074 +    Multiple		= 2,
  1.1075 +    Alternate		= 3,
  1.1076 +    Ligature		= 4,
  1.1077 +    Context		= 5,
  1.1078 +    ChainContext	= 6,
  1.1079 +    Extension		= 7,
  1.1080 +    ReverseChainSingle	= 8
  1.1081 +  };
  1.1082 +
  1.1083 +  template <typename context_t>
  1.1084 +  inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
  1.1085 +  {
  1.1086 +    TRACE_DISPATCH (this);
  1.1087 +    switch (lookup_type) {
  1.1088 +    case Single:		return TRACE_RETURN (u.single.dispatch (c));
  1.1089 +    case Multiple:		return TRACE_RETURN (u.multiple.dispatch (c));
  1.1090 +    case Alternate:		return TRACE_RETURN (u.alternate.dispatch (c));
  1.1091 +    case Ligature:		return TRACE_RETURN (u.ligature.dispatch (c));
  1.1092 +    case Context:		return TRACE_RETURN (u.context.dispatch (c));
  1.1093 +    case ChainContext:		return TRACE_RETURN (u.chainContext.dispatch (c));
  1.1094 +    case Extension:		return TRACE_RETURN (u.extension.dispatch (c));
  1.1095 +    case ReverseChainSingle:	return TRACE_RETURN (u.reverseChainContextSingle.dispatch (c));
  1.1096 +    default:			return TRACE_RETURN (c->default_return_value ());
  1.1097 +    }
  1.1098 +  }
  1.1099 +
  1.1100 +  inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
  1.1101 +    TRACE_SANITIZE (this);
  1.1102 +    if (!u.header.sub_format.sanitize (c))
  1.1103 +      return TRACE_RETURN (false);
  1.1104 +    switch (lookup_type) {
  1.1105 +    case Single:		return TRACE_RETURN (u.single.sanitize (c));
  1.1106 +    case Multiple:		return TRACE_RETURN (u.multiple.sanitize (c));
  1.1107 +    case Alternate:		return TRACE_RETURN (u.alternate.sanitize (c));
  1.1108 +    case Ligature:		return TRACE_RETURN (u.ligature.sanitize (c));
  1.1109 +    case Context:		return TRACE_RETURN (u.context.sanitize (c));
  1.1110 +    case ChainContext:		return TRACE_RETURN (u.chainContext.sanitize (c));
  1.1111 +    case Extension:		return TRACE_RETURN (u.extension.sanitize (c));
  1.1112 +    case ReverseChainSingle:	return TRACE_RETURN (u.reverseChainContextSingle.sanitize (c));
  1.1113 +    default:			return TRACE_RETURN (true);
  1.1114 +    }
  1.1115 +  }
  1.1116 +
  1.1117 +  protected:
  1.1118 +  union {
  1.1119 +  struct {
  1.1120 +    USHORT			sub_format;
  1.1121 +  } header;
  1.1122 +  SingleSubst			single;
  1.1123 +  MultipleSubst			multiple;
  1.1124 +  AlternateSubst		alternate;
  1.1125 +  LigatureSubst			ligature;
  1.1126 +  ContextSubst			context;
  1.1127 +  ChainContextSubst		chainContext;
  1.1128 +  ExtensionSubst		extension;
  1.1129 +  ReverseChainSingleSubst	reverseChainContextSingle;
  1.1130 +  } u;
  1.1131 +  public:
  1.1132 +  DEFINE_SIZE_UNION (2, header.sub_format);
  1.1133 +};
  1.1134 +
  1.1135 +
  1.1136 +struct SubstLookup : Lookup
  1.1137 +{
  1.1138 +  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
  1.1139 +  { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
  1.1140 +
  1.1141 +  inline static bool lookup_type_is_reverse (unsigned int lookup_type)
  1.1142 +  { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
  1.1143 +
  1.1144 +  inline bool is_reverse (void) const
  1.1145 +  {
  1.1146 +    unsigned int type = get_type ();
  1.1147 +    if (unlikely (type == SubstLookupSubTable::Extension))
  1.1148 +      return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
  1.1149 +    return lookup_type_is_reverse (type);
  1.1150 +  }
  1.1151 +
  1.1152 +  inline hb_closure_context_t::return_t closure (hb_closure_context_t *c) const
  1.1153 +  {
  1.1154 +    TRACE_CLOSURE (this);
  1.1155 +    c->set_recurse_func (dispatch_recurse_func<hb_closure_context_t>);
  1.1156 +    return TRACE_RETURN (dispatch (c));
  1.1157 +  }
  1.1158 +
  1.1159 +  inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
  1.1160 +  {
  1.1161 +    TRACE_COLLECT_GLYPHS (this);
  1.1162 +    c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
  1.1163 +    return TRACE_RETURN (dispatch (c));
  1.1164 +  }
  1.1165 +
  1.1166 +  template <typename set_t>
  1.1167 +  inline void add_coverage (set_t *glyphs) const
  1.1168 +  {
  1.1169 +    hb_get_coverage_context_t c;
  1.1170 +    const Coverage *last = NULL;
  1.1171 +    unsigned int count = get_subtable_count ();
  1.1172 +    for (unsigned int i = 0; i < count; i++) {
  1.1173 +      const Coverage *coverage = &get_subtable (i).dispatch (&c, get_type ());
  1.1174 +      if (coverage != last) {
  1.1175 +        coverage->add_coverage (glyphs);
  1.1176 +        last = coverage;
  1.1177 +      }
  1.1178 +    }
  1.1179 +  }
  1.1180 +
  1.1181 +  inline bool would_apply (hb_would_apply_context_t *c, const hb_set_digest_t *digest) const
  1.1182 +  {
  1.1183 +    TRACE_WOULD_APPLY (this);
  1.1184 +    if (unlikely (!c->len))  return TRACE_RETURN (false);
  1.1185 +    if (!digest->may_have (c->glyphs[0]))  return TRACE_RETURN (false);
  1.1186 +      return TRACE_RETURN (dispatch (c));
  1.1187 +  }
  1.1188 +
  1.1189 +  inline bool apply_once (hb_apply_context_t *c) const
  1.1190 +  {
  1.1191 +    TRACE_APPLY (this);
  1.1192 +    if (!c->check_glyph_property (&c->buffer->cur(), c->lookup_props))
  1.1193 +      return TRACE_RETURN (false);
  1.1194 +    return TRACE_RETURN (dispatch (c));
  1.1195 +  }
  1.1196 +
  1.1197 +  static bool apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index);
  1.1198 +
  1.1199 +  inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c,
  1.1200 +						  unsigned int i)
  1.1201 +  { return CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i].serialize (c, this); }
  1.1202 +
  1.1203 +  inline bool serialize_single (hb_serialize_context_t *c,
  1.1204 +				uint32_t lookup_props,
  1.1205 +			        Supplier<GlyphID> &glyphs,
  1.1206 +			        Supplier<GlyphID> &substitutes,
  1.1207 +			        unsigned int num_glyphs)
  1.1208 +  {
  1.1209 +    TRACE_SERIALIZE (this);
  1.1210 +    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return TRACE_RETURN (false);
  1.1211 +    return TRACE_RETURN (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs));
  1.1212 +  }
  1.1213 +
  1.1214 +  inline bool serialize_multiple (hb_serialize_context_t *c,
  1.1215 +				  uint32_t lookup_props,
  1.1216 +				  Supplier<GlyphID> &glyphs,
  1.1217 +				  Supplier<unsigned int> &substitute_len_list,
  1.1218 +				  unsigned int num_glyphs,
  1.1219 +				  Supplier<GlyphID> &substitute_glyphs_list)
  1.1220 +  {
  1.1221 +    TRACE_SERIALIZE (this);
  1.1222 +    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return TRACE_RETURN (false);
  1.1223 +    return TRACE_RETURN (serialize_subtable (c, 0).u.multiple.serialize (c, glyphs, substitute_len_list, num_glyphs,
  1.1224 +									 substitute_glyphs_list));
  1.1225 +  }
  1.1226 +
  1.1227 +  inline bool serialize_alternate (hb_serialize_context_t *c,
  1.1228 +				   uint32_t lookup_props,
  1.1229 +				   Supplier<GlyphID> &glyphs,
  1.1230 +				   Supplier<unsigned int> &alternate_len_list,
  1.1231 +				   unsigned int num_glyphs,
  1.1232 +				   Supplier<GlyphID> &alternate_glyphs_list)
  1.1233 +  {
  1.1234 +    TRACE_SERIALIZE (this);
  1.1235 +    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return TRACE_RETURN (false);
  1.1236 +    return TRACE_RETURN (serialize_subtable (c, 0).u.alternate.serialize (c, glyphs, alternate_len_list, num_glyphs,
  1.1237 +									  alternate_glyphs_list));
  1.1238 +  }
  1.1239 +
  1.1240 +  inline bool serialize_ligature (hb_serialize_context_t *c,
  1.1241 +				  uint32_t lookup_props,
  1.1242 +				  Supplier<GlyphID> &first_glyphs,
  1.1243 +				  Supplier<unsigned int> &ligature_per_first_glyph_count_list,
  1.1244 +				  unsigned int num_first_glyphs,
  1.1245 +				  Supplier<GlyphID> &ligatures_list,
  1.1246 +				  Supplier<unsigned int> &component_count_list,
  1.1247 +				  Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  1.1248 +  {
  1.1249 +    TRACE_SERIALIZE (this);
  1.1250 +    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return TRACE_RETURN (false);
  1.1251 +    return TRACE_RETURN (serialize_subtable (c, 0).u.ligature.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs,
  1.1252 +									 ligatures_list, component_count_list, component_list));
  1.1253 +  }
  1.1254 +
  1.1255 +  template <typename context_t>
  1.1256 +  static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
  1.1257 +
  1.1258 +  template <typename context_t>
  1.1259 +  inline typename context_t::return_t dispatch (context_t *c) const
  1.1260 +  {
  1.1261 +    TRACE_DISPATCH (this);
  1.1262 +    unsigned int lookup_type = get_type ();
  1.1263 +    unsigned int count = get_subtable_count ();
  1.1264 +    for (unsigned int i = 0; i < count; i++) {
  1.1265 +      typename context_t::return_t r = get_subtable (i).dispatch (c, lookup_type);
  1.1266 +      if (c->stop_sublookup_iteration (r))
  1.1267 +        return TRACE_RETURN (r);
  1.1268 +    }
  1.1269 +    return TRACE_RETURN (c->default_return_value ());
  1.1270 +  }
  1.1271 +
  1.1272 +  inline bool sanitize (hb_sanitize_context_t *c)
  1.1273 +  {
  1.1274 +    TRACE_SANITIZE (this);
  1.1275 +    if (unlikely (!Lookup::sanitize (c))) return TRACE_RETURN (false);
  1.1276 +    OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
  1.1277 +    if (unlikely (!list.sanitize (c, this, get_type ()))) return TRACE_RETURN (false);
  1.1278 +
  1.1279 +    if (unlikely (get_type () == SubstLookupSubTable::Extension))
  1.1280 +    {
  1.1281 +      /* The spec says all subtables of an Extension lookup should
  1.1282 +       * have the same type.  This is specially important if one has
  1.1283 +       * a reverse type! */
  1.1284 +      unsigned int type = get_subtable (0).u.extension.get_type ();
  1.1285 +      unsigned int count = get_subtable_count ();
  1.1286 +      for (unsigned int i = 1; i < count; i++)
  1.1287 +        if (get_subtable (i).u.extension.get_type () != type)
  1.1288 +	  return TRACE_RETURN (false);
  1.1289 +    }
  1.1290 +    return TRACE_RETURN (true);
  1.1291 +  }
  1.1292 +};
  1.1293 +
  1.1294 +typedef OffsetListOf<SubstLookup> SubstLookupList;
  1.1295 +
  1.1296 +/*
  1.1297 + * GSUB -- The Glyph Substitution Table
  1.1298 + */
  1.1299 +
  1.1300 +struct GSUB : GSUBGPOS
  1.1301 +{
  1.1302 +  static const hb_tag_t tableTag	= HB_OT_TAG_GSUB;
  1.1303 +
  1.1304 +  inline const SubstLookup& get_lookup (unsigned int i) const
  1.1305 +  { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
  1.1306 +
  1.1307 +  static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
  1.1308 +  static inline void substitute_finish (hb_font_t *font, hb_buffer_t *buffer);
  1.1309 +
  1.1310 +  inline bool sanitize (hb_sanitize_context_t *c) {
  1.1311 +    TRACE_SANITIZE (this);
  1.1312 +    if (unlikely (!GSUBGPOS::sanitize (c))) return TRACE_RETURN (false);
  1.1313 +    OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
  1.1314 +    return TRACE_RETURN (list.sanitize (c, this));
  1.1315 +  }
  1.1316 +  public:
  1.1317 +  DEFINE_SIZE_STATIC (10);
  1.1318 +};
  1.1319 +
  1.1320 +
  1.1321 +void
  1.1322 +GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
  1.1323 +{
  1.1324 +  _hb_buffer_allocate_gsubgpos_vars (buffer);
  1.1325 +
  1.1326 +  const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
  1.1327 +  unsigned int count = buffer->len;
  1.1328 +  for (unsigned int i = 0; i < count; i++)
  1.1329 +  {
  1.1330 +    _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
  1.1331 +    _hb_glyph_info_clear_lig_props (&buffer->info[i]);
  1.1332 +    buffer->info[i].syllable() = 0;
  1.1333 +  }
  1.1334 +}
  1.1335 +
  1.1336 +void
  1.1337 +GSUB::substitute_finish (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
  1.1338 +{
  1.1339 +}
  1.1340 +
  1.1341 +
  1.1342 +/* Out-of-class implementation for methods recursing */
  1.1343 +
  1.1344 +inline bool ExtensionSubst::is_reverse (void) const
  1.1345 +{
  1.1346 +  unsigned int type = get_type ();
  1.1347 +  if (unlikely (type == SubstLookupSubTable::Extension))
  1.1348 +    return CastR<ExtensionSubst> (get_subtable<SubstLookupSubTable>()).is_reverse ();
  1.1349 +  return SubstLookup::lookup_type_is_reverse (type);
  1.1350 +}
  1.1351 +
  1.1352 +template <typename context_t>
  1.1353 +inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
  1.1354 +{
  1.1355 +  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
  1.1356 +  const SubstLookup &l = gsub.get_lookup (lookup_index);
  1.1357 +  return l.dispatch (c);
  1.1358 +}
  1.1359 +
  1.1360 +inline bool SubstLookup::apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index)
  1.1361 +{
  1.1362 +  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
  1.1363 +  const SubstLookup &l = gsub.get_lookup (lookup_index);
  1.1364 +  unsigned int saved_lookup_props = c->lookup_props;
  1.1365 +  c->set_lookup (l);
  1.1366 +  bool ret = l.apply_once (c);
  1.1367 +  c->lookup_props = saved_lookup_props;
  1.1368 +  return ret;
  1.1369 +}
  1.1370 +
  1.1371 +
  1.1372 +} /* namespace OT */
  1.1373 +
  1.1374 +
  1.1375 +#endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */

mercurial