michael@0: /* michael@0: * Copyright © 2007,2008,2009,2010 Red Hat, Inc. michael@0: * Copyright © 2010,2012,2013 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OT_LAYOUT_GSUB_TABLE_HH michael@0: #define HB_OT_LAYOUT_GSUB_TABLE_HH michael@0: michael@0: #include "hb-ot-layout-gsubgpos-private.hh" michael@0: michael@0: michael@0: namespace OT { michael@0: michael@0: michael@0: struct SingleSubstFormat1 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: hb_codepoint_t glyph_id = iter.get_glyph (); michael@0: if (c->glyphs->has (glyph_id)) michael@0: c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFF); michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: hb_codepoint_t glyph_id = iter.get_glyph (); michael@0: c->input->add (glyph_id); michael@0: c->output->add ((glyph_id + deltaGlyphID) & 0xFFFF); michael@0: } michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: hb_codepoint_t glyph_id = c->buffer->cur().codepoint; michael@0: unsigned int index = (this+coverage).get_coverage (glyph_id); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: /* According to the Adobe Annotated OpenType Suite, result is always michael@0: * limited to 16bit. */ michael@0: glyph_id = (glyph_id + deltaGlyphID) & 0xFFFF; michael@0: c->replace_glyph (glyph_id); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: unsigned int num_glyphs, michael@0: int delta) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false); michael@0: deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */ michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 1 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of Substitution table */ michael@0: SHORT deltaGlyphID; /* Add to original GlyphID to get michael@0: * substitute GlyphID */ michael@0: public: michael@0: DEFINE_SIZE_STATIC (6); michael@0: }; michael@0: michael@0: struct SingleSubstFormat2 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: if (c->glyphs->has (iter.get_glyph ())) michael@0: c->glyphs->add (substitute[iter.get_coverage ()]); michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: c->input->add (iter.get_glyph ()); michael@0: c->output->add (substitute[iter.get_coverage ()]); michael@0: } michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: hb_codepoint_t glyph_id = c->buffer->cur().codepoint; michael@0: unsigned int index = (this+coverage).get_coverage (glyph_id); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: if (unlikely (index >= substitute.len)) return TRACE_RETURN (false); michael@0: michael@0: glyph_id = substitute[index]; michael@0: c->replace_glyph (glyph_id); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &substitutes, michael@0: unsigned int num_glyphs) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return TRACE_RETURN (false); michael@0: if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (coverage.sanitize (c, this) && substitute.sanitize (c)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 2 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of Substitution table */ michael@0: ArrayOf michael@0: substitute; /* Array of substitute michael@0: * GlyphIDs--ordered by Coverage Index */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, substitute); michael@0: }; michael@0: michael@0: struct SingleSubst michael@0: { michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &substitutes, michael@0: unsigned int num_glyphs) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false); michael@0: unsigned int format = 2; michael@0: int delta; michael@0: if (num_glyphs) { michael@0: format = 1; michael@0: /* TODO(serialize) check for wrap-around */ michael@0: delta = substitutes[0] - glyphs[0]; michael@0: for (unsigned int i = 1; i < num_glyphs; i++) michael@0: if (delta != substitutes[i] - glyphs[i]) { michael@0: format = 2; michael@0: break; michael@0: } michael@0: } michael@0: u.format.set (format); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs, delta)); michael@0: case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, substitutes, num_glyphs)); michael@0: default:return TRACE_RETURN (false); michael@0: } michael@0: } michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (c->dispatch (u.format1)); michael@0: case 2: return TRACE_RETURN (c->dispatch (u.format2)); michael@0: default:return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.format.sanitize (c)) return TRACE_RETURN (false); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.sanitize (c)); michael@0: case 2: return TRACE_RETURN (u.format2.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: USHORT format; /* Format identifier */ michael@0: SingleSubstFormat1 format1; michael@0: SingleSubstFormat2 format2; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: struct Sequence michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: unsigned int count = substitute.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: c->glyphs->add (substitute[i]); michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: unsigned int count = substitute.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: c->output->add (substitute[i]); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: if (unlikely (!substitute.len)) return TRACE_RETURN (false); michael@0: michael@0: unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ? michael@0: HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0; michael@0: unsigned int count = substitute.len; michael@0: if (count == 1) /* Special-case to make it in-place. */ michael@0: { michael@0: c->replace_glyph (substitute.array[0]); michael@0: } michael@0: else michael@0: { michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i); michael@0: c->output_glyph (substitute.array[i], klass); michael@0: } michael@0: c->buffer->skip_glyph (); michael@0: } michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: unsigned int num_glyphs) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (substitute.sanitize (c)); michael@0: } michael@0: michael@0: protected: michael@0: ArrayOf michael@0: substitute; /* String of GlyphIDs to substitute */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (2, substitute); michael@0: }; michael@0: michael@0: struct MultipleSubstFormat1 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: if (c->glyphs->has (iter.get_glyph ())) michael@0: (this+sequence[iter.get_coverage ()]).closure (c); michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: (this+coverage).add_coverage (c->input); michael@0: unsigned int count = sequence.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: (this+sequence[i]).collect_glyphs (c); michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: michael@0: unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: return TRACE_RETURN ((this+sequence[index]).apply (c)); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &substitute_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &substitute_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!sequence.serialize (c, num_glyphs))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < num_glyphs; i++) michael@0: if (unlikely (!sequence[i].serialize (c, this).serialize (c, michael@0: substitute_glyphs_list, michael@0: substitute_len_list[i]))) return TRACE_RETURN (false); michael@0: substitute_len_list.advance (num_glyphs); michael@0: if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (coverage.sanitize (c, this) && sequence.sanitize (c, this)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 1 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of Substitution table */ michael@0: OffsetArrayOf michael@0: sequence; /* Array of Sequence tables michael@0: * ordered by Coverage Index */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, sequence); michael@0: }; michael@0: michael@0: struct MultipleSubst michael@0: { michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &substitute_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &substitute_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false); michael@0: unsigned int format = 1; michael@0: u.format.set (format); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list)); michael@0: default:return TRACE_RETURN (false); michael@0: } michael@0: } michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (c->dispatch (u.format1)); michael@0: default:return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.format.sanitize (c)) return TRACE_RETURN (false); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: USHORT format; /* Format identifier */ michael@0: MultipleSubstFormat1 format1; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: typedef ArrayOf AlternateSet; /* Array of alternate GlyphIDs--in michael@0: * arbitrary order */ michael@0: michael@0: struct AlternateSubstFormat1 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: if (c->glyphs->has (iter.get_glyph ())) { michael@0: const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()]; michael@0: unsigned int count = alt_set.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: c->glyphs->add (alt_set[i]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: c->input->add (iter.get_glyph ()); michael@0: const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()]; michael@0: unsigned int count = alt_set.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: c->output->add (alt_set[i]); michael@0: } michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: hb_codepoint_t glyph_id = c->buffer->cur().codepoint; michael@0: michael@0: unsigned int index = (this+coverage).get_coverage (glyph_id); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: const AlternateSet &alt_set = this+alternateSet[index]; michael@0: michael@0: if (unlikely (!alt_set.len)) return TRACE_RETURN (false); michael@0: michael@0: hb_mask_t glyph_mask = c->buffer->cur().mask; michael@0: hb_mask_t lookup_mask = c->lookup_mask; michael@0: michael@0: /* Note: This breaks badly if two features enabled this lookup together. */ michael@0: unsigned int shift = _hb_ctz (lookup_mask); michael@0: unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift); michael@0: michael@0: if (unlikely (alt_index > alt_set.len || alt_index == 0)) return TRACE_RETURN (false); michael@0: michael@0: glyph_id = alt_set[alt_index - 1]; michael@0: michael@0: c->replace_glyph (glyph_id); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &alternate_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &alternate_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!alternateSet.serialize (c, num_glyphs))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < num_glyphs; i++) michael@0: if (unlikely (!alternateSet[i].serialize (c, this).serialize (c, michael@0: alternate_glyphs_list, michael@0: alternate_len_list[i]))) return TRACE_RETURN (false); michael@0: alternate_len_list.advance (num_glyphs); michael@0: if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (coverage.sanitize (c, this) && alternateSet.sanitize (c, this)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 1 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of Substitution table */ michael@0: OffsetArrayOf michael@0: alternateSet; /* Array of AlternateSet tables michael@0: * ordered by Coverage Index */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, alternateSet); michael@0: }; michael@0: michael@0: struct AlternateSubst michael@0: { michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &glyphs, michael@0: Supplier &alternate_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &alternate_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false); michael@0: unsigned int format = 1; michael@0: u.format.set (format); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list)); michael@0: default:return TRACE_RETURN (false); michael@0: } michael@0: } michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (c->dispatch (u.format1)); michael@0: default:return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.format.sanitize (c)) return TRACE_RETURN (false); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: USHORT format; /* Format identifier */ michael@0: AlternateSubstFormat1 format1; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: struct Ligature michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: unsigned int count = component.len; michael@0: for (unsigned int i = 1; i < count; i++) michael@0: if (!c->glyphs->has (component[i])) michael@0: return; michael@0: c->glyphs->add (ligGlyph); michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: unsigned int count = component.len; michael@0: for (unsigned int i = 1; i < count; i++) michael@0: c->input->add (component[i]); michael@0: c->output->add (ligGlyph); michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: if (c->len != component.len) michael@0: return TRACE_RETURN (false); michael@0: michael@0: for (unsigned int i = 1; i < c->len; i++) michael@0: if (likely (c->glyphs[i] != component[i])) michael@0: return TRACE_RETURN (false); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: unsigned int count = component.len; michael@0: if (unlikely (count < 1)) return TRACE_RETURN (false); michael@0: michael@0: bool is_mark_ligature = false; michael@0: unsigned int total_component_count = 0; michael@0: michael@0: unsigned int match_length = 0; michael@0: unsigned int match_positions[MAX_CONTEXT_LENGTH]; michael@0: michael@0: if (likely (!match_input (c, count, michael@0: &component[1], michael@0: match_glyph, michael@0: NULL, michael@0: &match_length, michael@0: match_positions, michael@0: &is_mark_ligature, michael@0: &total_component_count))) michael@0: return TRACE_RETURN (false); michael@0: michael@0: ligate_input (c, michael@0: count, michael@0: match_positions, michael@0: match_length, michael@0: ligGlyph, michael@0: is_mark_ligature, michael@0: total_component_count); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: GlyphID ligature, michael@0: Supplier &components, /* Starting from second */ michael@0: unsigned int num_components /* Including first component */) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: ligGlyph = ligature; michael@0: if (unlikely (!component.serialize (c, components, num_components))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: public: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (ligGlyph.sanitize (c) && component.sanitize (c)); michael@0: } michael@0: michael@0: protected: michael@0: GlyphID ligGlyph; /* GlyphID of ligature to substitute */ michael@0: HeadlessArrayOf michael@0: component; /* Array of component GlyphIDs--start michael@0: * with the second component--ordered michael@0: * in writing direction */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (4, component); michael@0: }; michael@0: michael@0: struct LigatureSet michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: unsigned int num_ligs = ligature.len; michael@0: for (unsigned int i = 0; i < num_ligs; i++) michael@0: (this+ligature[i]).closure (c); michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: unsigned int num_ligs = ligature.len; michael@0: for (unsigned int i = 0; i < num_ligs; i++) michael@0: (this+ligature[i]).collect_glyphs (c); michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: unsigned int num_ligs = ligature.len; michael@0: for (unsigned int i = 0; i < num_ligs; i++) michael@0: { michael@0: const Ligature &lig = this+ligature[i]; michael@0: if (lig.would_apply (c)) michael@0: return TRACE_RETURN (true); michael@0: } michael@0: return TRACE_RETURN (false); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: unsigned int num_ligs = ligature.len; michael@0: for (unsigned int i = 0; i < num_ligs; i++) michael@0: { michael@0: const Ligature &lig = this+ligature[i]; michael@0: if (lig.apply (c)) return TRACE_RETURN (true); michael@0: } michael@0: michael@0: return TRACE_RETURN (false); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &ligatures, michael@0: Supplier &component_count_list, michael@0: unsigned int num_ligatures, michael@0: Supplier &component_list /* Starting from second for each ligature */) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!ligature.serialize (c, num_ligatures))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < num_ligatures; i++) michael@0: if (unlikely (!ligature[i].serialize (c, this).serialize (c, michael@0: ligatures[i], michael@0: component_list, michael@0: component_count_list[i]))) return TRACE_RETURN (false); michael@0: ligatures.advance (num_ligatures); michael@0: component_count_list.advance (num_ligatures); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (ligature.sanitize (c, this)); michael@0: } michael@0: michael@0: protected: michael@0: OffsetArrayOf michael@0: ligature; /* Array LigatureSet tables michael@0: * ordered by preference */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (2, ligature); michael@0: }; michael@0: michael@0: struct LigatureSubstFormat1 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: if (c->glyphs->has (iter.get_glyph ())) michael@0: (this+ligatureSet[iter.get_coverage ()]).closure (c); michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: c->input->add (iter.get_glyph ()); michael@0: (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c); michael@0: } michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: unsigned int index = (this+coverage).get_coverage (c->glyphs[0]); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: const LigatureSet &lig_set = this+ligatureSet[index]; michael@0: return TRACE_RETURN (lig_set.would_apply (c)); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: hb_codepoint_t glyph_id = c->buffer->cur().codepoint; michael@0: michael@0: unsigned int index = (this+coverage).get_coverage (glyph_id); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: const LigatureSet &lig_set = this+ligatureSet[index]; michael@0: return TRACE_RETURN (lig_set.apply (c)); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &first_glyphs, michael@0: Supplier &ligature_per_first_glyph_count_list, michael@0: unsigned int num_first_glyphs, michael@0: Supplier &ligatures_list, michael@0: Supplier &component_count_list, michael@0: Supplier &component_list /* Starting from second for each ligature */) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < num_first_glyphs; i++) michael@0: if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c, michael@0: ligatures_list, michael@0: component_count_list, michael@0: ligature_per_first_glyph_count_list[i], michael@0: component_list))) return TRACE_RETURN (false); michael@0: ligature_per_first_glyph_count_list.advance (num_first_glyphs); michael@0: if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 1 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of Substitution table */ michael@0: OffsetArrayOf michael@0: ligatureSet; /* Array LigatureSet tables michael@0: * ordered by Coverage Index */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, ligatureSet); michael@0: }; michael@0: michael@0: struct LigatureSubst michael@0: { michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &first_glyphs, michael@0: Supplier &ligature_per_first_glyph_count_list, michael@0: unsigned int num_first_glyphs, michael@0: Supplier &ligatures_list, michael@0: Supplier &component_count_list, michael@0: Supplier &component_list /* Starting from second for each ligature */) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false); michael@0: unsigned int format = 1; michael@0: u.format.set (format); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs, michael@0: ligatures_list, component_count_list, component_list)); michael@0: default:return TRACE_RETURN (false); michael@0: } michael@0: } michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (c->dispatch (u.format1)); michael@0: default:return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.format.sanitize (c)) return TRACE_RETURN (false); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: USHORT format; /* Format identifier */ michael@0: LigatureSubstFormat1 format1; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: struct ContextSubst : Context {}; michael@0: michael@0: struct ChainContextSubst : ChainContext {}; michael@0: michael@0: struct ExtensionSubst : Extension michael@0: { michael@0: typedef struct SubstLookupSubTable LookupSubTable; michael@0: michael@0: inline bool is_reverse (void) const; michael@0: }; michael@0: michael@0: michael@0: struct ReverseChainSingleSubstFormat1 michael@0: { michael@0: inline void closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: const OffsetArrayOf &lookahead = StructAfter > (backtrack); michael@0: michael@0: unsigned int count; michael@0: michael@0: count = backtrack.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (!(this+backtrack[i]).intersects (c->glyphs)) michael@0: return; michael@0: michael@0: count = lookahead.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (!(this+lookahead[i]).intersects (c->glyphs)) michael@0: return; michael@0: michael@0: const ArrayOf &substitute = StructAfter > (lookahead); michael@0: Coverage::Iter iter; michael@0: for (iter.init (this+coverage); iter.more (); iter.next ()) { michael@0: if (c->glyphs->has (iter.get_glyph ())) michael@0: c->glyphs->add (substitute[iter.get_coverage ()]); michael@0: } michael@0: } michael@0: michael@0: inline void collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: michael@0: const OffsetArrayOf &lookahead = StructAfter > (backtrack); michael@0: michael@0: unsigned int count; michael@0: michael@0: (this+coverage).add_coverage (c->input); michael@0: michael@0: count = backtrack.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: (this+backtrack[i]).add_coverage (c->before); michael@0: michael@0: count = lookahead.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: (this+lookahead[i]).add_coverage (c->after); michael@0: michael@0: const ArrayOf &substitute = StructAfter > (lookahead); michael@0: count = substitute.len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: c->output->add (substitute[i]); michael@0: } michael@0: michael@0: inline const Coverage &get_coverage (void) const michael@0: { michael@0: return this+coverage; michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED); michael@0: } michael@0: michael@0: inline bool apply (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: if (unlikely (c->nesting_level_left != MAX_NESTING_LEVEL)) michael@0: return TRACE_RETURN (false); /* No chaining to this type */ michael@0: michael@0: unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); michael@0: if (likely (index == NOT_COVERED)) return TRACE_RETURN (false); michael@0: michael@0: const OffsetArrayOf &lookahead = StructAfter > (backtrack); michael@0: const ArrayOf &substitute = StructAfter > (lookahead); michael@0: michael@0: if (match_backtrack (c, michael@0: backtrack.len, (USHORT *) backtrack.array, michael@0: match_coverage, this) && michael@0: match_lookahead (c, michael@0: lookahead.len, (USHORT *) lookahead.array, michael@0: match_coverage, this, michael@0: 1)) michael@0: { michael@0: c->replace_glyph_inplace (substitute[index]); michael@0: /* Note: We DON'T decrease buffer->idx. The main loop does it michael@0: * for us. This is useful for preventing surprises if someone michael@0: * calls us through a Context lookup. */ michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: return TRACE_RETURN (false); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this))) michael@0: return TRACE_RETURN (false); michael@0: OffsetArrayOf &lookahead = StructAfter > (backtrack); michael@0: if (!lookahead.sanitize (c, this)) michael@0: return TRACE_RETURN (false); michael@0: ArrayOf &substitute = StructAfter > (lookahead); michael@0: return TRACE_RETURN (substitute.sanitize (c)); michael@0: } michael@0: michael@0: protected: michael@0: USHORT format; /* Format identifier--format = 1 */ michael@0: OffsetTo michael@0: coverage; /* Offset to Coverage table--from michael@0: * beginning of table */ michael@0: OffsetArrayOf michael@0: backtrack; /* Array of coverage tables michael@0: * in backtracking sequence, in glyph michael@0: * sequence order */ michael@0: OffsetArrayOf michael@0: lookaheadX; /* Array of coverage tables michael@0: * in lookahead sequence, in glyph michael@0: * sequence order */ michael@0: ArrayOf michael@0: substituteX; /* Array of substitute michael@0: * GlyphIDs--ordered by Coverage Index */ michael@0: public: michael@0: DEFINE_SIZE_MIN (10); michael@0: }; michael@0: michael@0: struct ReverseChainSingleSubst michael@0: { michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (c->dispatch (u.format1)); michael@0: default:return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.format.sanitize (c)) return TRACE_RETURN (false); michael@0: switch (u.format) { michael@0: case 1: return TRACE_RETURN (u.format1.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: USHORT format; /* Format identifier */ michael@0: ReverseChainSingleSubstFormat1 format1; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: michael@0: /* michael@0: * SubstLookup michael@0: */ michael@0: michael@0: struct SubstLookupSubTable michael@0: { michael@0: friend struct SubstLookup; michael@0: michael@0: enum Type { michael@0: Single = 1, michael@0: Multiple = 2, michael@0: Alternate = 3, michael@0: Ligature = 4, michael@0: Context = 5, michael@0: ChainContext = 6, michael@0: Extension = 7, michael@0: ReverseChainSingle = 8 michael@0: }; michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: switch (lookup_type) { michael@0: case Single: return TRACE_RETURN (u.single.dispatch (c)); michael@0: case Multiple: return TRACE_RETURN (u.multiple.dispatch (c)); michael@0: case Alternate: return TRACE_RETURN (u.alternate.dispatch (c)); michael@0: case Ligature: return TRACE_RETURN (u.ligature.dispatch (c)); michael@0: case Context: return TRACE_RETURN (u.context.dispatch (c)); michael@0: case ChainContext: return TRACE_RETURN (u.chainContext.dispatch (c)); michael@0: case Extension: return TRACE_RETURN (u.extension.dispatch (c)); michael@0: case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.dispatch (c)); michael@0: default: return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) { michael@0: TRACE_SANITIZE (this); michael@0: if (!u.header.sub_format.sanitize (c)) michael@0: return TRACE_RETURN (false); michael@0: switch (lookup_type) { michael@0: case Single: return TRACE_RETURN (u.single.sanitize (c)); michael@0: case Multiple: return TRACE_RETURN (u.multiple.sanitize (c)); michael@0: case Alternate: return TRACE_RETURN (u.alternate.sanitize (c)); michael@0: case Ligature: return TRACE_RETURN (u.ligature.sanitize (c)); michael@0: case Context: return TRACE_RETURN (u.context.sanitize (c)); michael@0: case ChainContext: return TRACE_RETURN (u.chainContext.sanitize (c)); michael@0: case Extension: return TRACE_RETURN (u.extension.sanitize (c)); michael@0: case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.sanitize (c)); michael@0: default: return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: struct { michael@0: USHORT sub_format; michael@0: } header; michael@0: SingleSubst single; michael@0: MultipleSubst multiple; michael@0: AlternateSubst alternate; michael@0: LigatureSubst ligature; michael@0: ContextSubst context; michael@0: ChainContextSubst chainContext; michael@0: ExtensionSubst extension; michael@0: ReverseChainSingleSubst reverseChainContextSingle; michael@0: } u; michael@0: public: michael@0: DEFINE_SIZE_UNION (2, header.sub_format); michael@0: }; michael@0: michael@0: michael@0: struct SubstLookup : Lookup michael@0: { michael@0: inline const SubstLookupSubTable& get_subtable (unsigned int i) const michael@0: { return this+CastR > (subTable)[i]; } michael@0: michael@0: inline static bool lookup_type_is_reverse (unsigned int lookup_type) michael@0: { return lookup_type == SubstLookupSubTable::ReverseChainSingle; } michael@0: michael@0: inline bool is_reverse (void) const michael@0: { michael@0: unsigned int type = get_type (); michael@0: if (unlikely (type == SubstLookupSubTable::Extension)) michael@0: return CastR (get_subtable(0)).is_reverse (); michael@0: return lookup_type_is_reverse (type); michael@0: } michael@0: michael@0: inline hb_closure_context_t::return_t closure (hb_closure_context_t *c) const michael@0: { michael@0: TRACE_CLOSURE (this); michael@0: c->set_recurse_func (dispatch_recurse_func); michael@0: return TRACE_RETURN (dispatch (c)); michael@0: } michael@0: michael@0: inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const michael@0: { michael@0: TRACE_COLLECT_GLYPHS (this); michael@0: c->set_recurse_func (dispatch_recurse_func); michael@0: return TRACE_RETURN (dispatch (c)); michael@0: } michael@0: michael@0: template michael@0: inline void add_coverage (set_t *glyphs) const michael@0: { michael@0: hb_get_coverage_context_t c; michael@0: const Coverage *last = NULL; michael@0: unsigned int count = get_subtable_count (); michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: const Coverage *coverage = &get_subtable (i).dispatch (&c, get_type ()); michael@0: if (coverage != last) { michael@0: coverage->add_coverage (glyphs); michael@0: last = coverage; michael@0: } michael@0: } michael@0: } michael@0: michael@0: inline bool would_apply (hb_would_apply_context_t *c, const hb_set_digest_t *digest) const michael@0: { michael@0: TRACE_WOULD_APPLY (this); michael@0: if (unlikely (!c->len)) return TRACE_RETURN (false); michael@0: if (!digest->may_have (c->glyphs[0])) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (dispatch (c)); michael@0: } michael@0: michael@0: inline bool apply_once (hb_apply_context_t *c) const michael@0: { michael@0: TRACE_APPLY (this); michael@0: if (!c->check_glyph_property (&c->buffer->cur(), c->lookup_props)) michael@0: return TRACE_RETURN (false); michael@0: return TRACE_RETURN (dispatch (c)); michael@0: } michael@0: michael@0: static bool apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index); michael@0: michael@0: inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c, michael@0: unsigned int i) michael@0: { return CastR > (subTable)[i].serialize (c, this); } michael@0: michael@0: inline bool serialize_single (hb_serialize_context_t *c, michael@0: uint32_t lookup_props, michael@0: Supplier &glyphs, michael@0: Supplier &substitutes, michael@0: unsigned int num_glyphs) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs)); michael@0: } michael@0: michael@0: inline bool serialize_multiple (hb_serialize_context_t *c, michael@0: uint32_t lookup_props, michael@0: Supplier &glyphs, michael@0: Supplier &substitute_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &substitute_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (serialize_subtable (c, 0).u.multiple.serialize (c, glyphs, substitute_len_list, num_glyphs, michael@0: substitute_glyphs_list)); michael@0: } michael@0: michael@0: inline bool serialize_alternate (hb_serialize_context_t *c, michael@0: uint32_t lookup_props, michael@0: Supplier &glyphs, michael@0: Supplier &alternate_len_list, michael@0: unsigned int num_glyphs, michael@0: Supplier &alternate_glyphs_list) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (serialize_subtable (c, 0).u.alternate.serialize (c, glyphs, alternate_len_list, num_glyphs, michael@0: alternate_glyphs_list)); michael@0: } michael@0: michael@0: inline bool serialize_ligature (hb_serialize_context_t *c, michael@0: uint32_t lookup_props, michael@0: Supplier &first_glyphs, michael@0: Supplier &ligature_per_first_glyph_count_list, michael@0: unsigned int num_first_glyphs, michael@0: Supplier &ligatures_list, michael@0: Supplier &component_count_list, michael@0: Supplier &component_list /* Starting from second for each ligature */) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (serialize_subtable (c, 0).u.ligature.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs, michael@0: ligatures_list, component_count_list, component_list)); michael@0: } michael@0: michael@0: template michael@0: static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index); michael@0: michael@0: template michael@0: inline typename context_t::return_t dispatch (context_t *c) const michael@0: { michael@0: TRACE_DISPATCH (this); michael@0: unsigned int lookup_type = get_type (); michael@0: unsigned int count = get_subtable_count (); michael@0: for (unsigned int i = 0; i < count; i++) { michael@0: typename context_t::return_t r = get_subtable (i).dispatch (c, lookup_type); michael@0: if (c->stop_sublookup_iteration (r)) michael@0: return TRACE_RETURN (r); michael@0: } michael@0: return TRACE_RETURN (c->default_return_value ()); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) michael@0: { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!Lookup::sanitize (c))) return TRACE_RETURN (false); michael@0: OffsetArrayOf &list = CastR > (subTable); michael@0: if (unlikely (!list.sanitize (c, this, get_type ()))) return TRACE_RETURN (false); michael@0: michael@0: if (unlikely (get_type () == SubstLookupSubTable::Extension)) michael@0: { michael@0: /* The spec says all subtables of an Extension lookup should michael@0: * have the same type. This is specially important if one has michael@0: * a reverse type! */ michael@0: unsigned int type = get_subtable (0).u.extension.get_type (); michael@0: unsigned int count = get_subtable_count (); michael@0: for (unsigned int i = 1; i < count; i++) michael@0: if (get_subtable (i).u.extension.get_type () != type) michael@0: return TRACE_RETURN (false); michael@0: } michael@0: return TRACE_RETURN (true); michael@0: } michael@0: }; michael@0: michael@0: typedef OffsetListOf SubstLookupList; michael@0: michael@0: /* michael@0: * GSUB -- The Glyph Substitution Table michael@0: */ michael@0: michael@0: struct GSUB : GSUBGPOS michael@0: { michael@0: static const hb_tag_t tableTag = HB_OT_TAG_GSUB; michael@0: michael@0: inline const SubstLookup& get_lookup (unsigned int i) const michael@0: { return CastR (GSUBGPOS::get_lookup (i)); } michael@0: michael@0: static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer); michael@0: static inline void substitute_finish (hb_font_t *font, hb_buffer_t *buffer); michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!GSUBGPOS::sanitize (c))) return TRACE_RETURN (false); michael@0: OffsetTo &list = CastR > (lookupList); michael@0: return TRACE_RETURN (list.sanitize (c, this)); michael@0: } michael@0: public: michael@0: DEFINE_SIZE_STATIC (10); michael@0: }; michael@0: michael@0: michael@0: void michael@0: GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer) michael@0: { michael@0: _hb_buffer_allocate_gsubgpos_vars (buffer); michael@0: michael@0: const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef; michael@0: unsigned int count = buffer->len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint)); michael@0: _hb_glyph_info_clear_lig_props (&buffer->info[i]); michael@0: buffer->info[i].syllable() = 0; michael@0: } michael@0: } michael@0: michael@0: void michael@0: GSUB::substitute_finish (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED) michael@0: { michael@0: } michael@0: michael@0: michael@0: /* Out-of-class implementation for methods recursing */ michael@0: michael@0: inline bool ExtensionSubst::is_reverse (void) const michael@0: { michael@0: unsigned int type = get_type (); michael@0: if (unlikely (type == SubstLookupSubTable::Extension)) michael@0: return CastR (get_subtable()).is_reverse (); michael@0: return SubstLookup::lookup_type_is_reverse (type); michael@0: } michael@0: michael@0: template michael@0: inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index) michael@0: { michael@0: const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub); michael@0: const SubstLookup &l = gsub.get_lookup (lookup_index); michael@0: return l.dispatch (c); michael@0: } michael@0: michael@0: inline bool SubstLookup::apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index) michael@0: { michael@0: const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub); michael@0: const SubstLookup &l = gsub.get_lookup (lookup_index); michael@0: unsigned int saved_lookup_props = c->lookup_props; michael@0: c->set_lookup (l); michael@0: bool ret = l.apply_once (c); michael@0: c->lookup_props = saved_lookup_props; michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: } /* namespace OT */ michael@0: michael@0: michael@0: #endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */