diff --git a/src/Makefile.sources b/src/Makefile.sources index e41d7d338..95285643f 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -102,6 +102,8 @@ HB_BASE_sources = \ OT/Layout/GSUB/LigatureSubst.hh \ OT/Layout/GSUB/LigatureSet.hh \ OT/Layout/GSUB/Ligature.hh \ + OT/Layout/GSUB/ReverseChainSingleSubst.hh \ + OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh \ hb-ot-layout-gsubgpos.hh \ hb-ot-layout-jstf-table.hh \ hb-ot-layout.cc \ diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubst.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubst.hh new file mode 100644 index 000000000..435d80fd3 --- /dev/null +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubst.hh @@ -0,0 +1,36 @@ +#ifndef OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBST_HH +#define OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBST_HH + +#include "Common.hh" +#include "ReverseChainSingleSubstFormat1.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct ReverseChainSingleSubst +{ + protected: + union { + HBUINT16 format; /* Format identifier */ + ReverseChainSingleSubstFormat1 format1; + } u; + + public: + template + typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const + { + TRACE_DISPATCH (this, u.format); + if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ()); + switch (u.format) { + case 1: return_trace (c->dispatch (u.format1, std::forward (ds)...)); + default:return_trace (c->default_return_value ()); + } + } +}; + +} +} +} + +#endif /* HB_OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBST_HH */ diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh new file mode 100644 index 000000000..7a79a9df2 --- /dev/null +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -0,0 +1,228 @@ +#ifndef OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH +#define OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH + +#include "Common.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct ReverseChainSingleSubstFormat1 +{ + protected: + HBUINT16 format; /* Format identifier--format = 1 */ + Offset16To + coverage; /* Offset to Coverage table--from + * beginning of table */ + Array16OfOffset16To + backtrack; /* Array of coverage tables + * in backtracking sequence, in glyph + * sequence order */ + Array16OfOffset16To + lookaheadX; /* Array of coverage tables + * in lookahead sequence, in glyph + * sequence order */ + Array16Of + substituteX; /* Array of substitute + * GlyphIDs--ordered by Coverage Index */ + public: + DEFINE_SIZE_MIN (10); + + bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this))) + return_trace (false); + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + if (!lookahead.sanitize (c, this)) + return_trace (false); + const Array16Of &substitute = StructAfter> (lookahead); + return_trace (substitute.sanitize (c)); + } + + bool intersects (const hb_set_t *glyphs) const + { + if (!(this+coverage).intersects (glyphs)) + return false; + + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + + unsigned int count; + + count = backtrack.len; + for (unsigned int i = 0; i < count; i++) + if (!(this+backtrack[i]).intersects (glyphs)) + return false; + + count = lookahead.len; + for (unsigned int i = 0; i < count; i++) + if (!(this+lookahead[i]).intersects (glyphs)) + return false; + + return true; + } + + bool may_have_non_1to1 () const + { return false; } + + void closure (hb_closure_context_t *c) const + { + if (!intersects (c->glyphs)) return; + + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + const Array16Of &substitute = StructAfter> (lookahead); + + + hb_zip (this+coverage, substitute) + | hb_filter (c->parent_active_glyphs (), hb_first) + | hb_map (hb_second) + | hb_sink (c->output) + ; + } + + void closure_lookups (hb_closure_lookups_context_t *c) const {} + + void collect_glyphs (hb_collect_glyphs_context_t *c) const + { + if (unlikely (!(this+coverage).collect_coverage (c->input))) return; + + unsigned int count; + + count = backtrack.len; + for (unsigned int i = 0; i < count; i++) + if (unlikely (!(this+backtrack[i]).collect_coverage (c->before))) return; + + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + count = lookahead.len; + for (unsigned int i = 0; i < count; i++) + if (unlikely (!(this+lookahead[i]).collect_coverage (c->after))) return; + + const Array16Of &substitute = StructAfter> (lookahead); + count = substitute.len; + c->output->add_array (substitute.arrayZ, substitute.len); + } + + const Coverage &get_coverage () const { return this+coverage; } + + bool would_apply (hb_would_apply_context_t *c) const + { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; } + + bool apply (hb_ot_apply_context_t *c) const + { + TRACE_APPLY (this); + if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL)) + return_trace (false); /* No chaining to this type */ + + unsigned int index = (this+coverage).get_coverage (c->buffer->cur ().codepoint); + if (likely (index == NOT_COVERED)) return_trace (false); + + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + const Array16Of &substitute = StructAfter> (lookahead); + + if (unlikely (index >= substitute.len)) return_trace (false); + + unsigned int start_index = 0, end_index = 0; + if (match_backtrack (c, + backtrack.len, (HBUINT16 *) backtrack.arrayZ, + match_coverage, this, + &start_index) && + match_lookahead (c, + lookahead.len, (HBUINT16 *) lookahead.arrayZ, + match_coverage, this, + c->buffer->idx + 1, &end_index)) + { + c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index); + c->replace_glyph_inplace (substitute[index]); + /* Note: We DON'T decrease buffer->idx. The main loop does it + * for us. This is useful for preventing surprises if someone + * calls us through a Context lookup. */ + return_trace (true); + } + else + { + c->buffer->unsafe_to_concat_from_outbuffer (start_index, end_index); + return_trace (false); + } + } + + template + bool serialize_coverage_offset_array (hb_subset_context_t *c, Iterator it) const + { + TRACE_SERIALIZE (this); + auto *out = c->serializer->start_embed> (); + + if (unlikely (!c->serializer->allocate_size (HBUINT16::static_size))) + return_trace (false); + + for (auto& offset : it) { + auto *o = out->serialize_append (c->serializer); + if (unlikely (!o) || !o->serialize_subset (c, offset, this)) + return_trace (false); + } + + return_trace (true); + } + + template + bool serialize (hb_subset_context_t *c, + Iterator coverage_subst_iter, + BacktrackIterator backtrack_iter, + LookaheadIterator lookahead_iter) const + { + TRACE_SERIALIZE (this); + + auto *out = c->serializer->start_embed (this); + if (unlikely (!c->serializer->check_success (out))) return_trace (false); + if (unlikely (!c->serializer->embed (this->format))) return_trace (false); + if (unlikely (!c->serializer->embed (this->coverage))) return_trace (false); + + if (!serialize_coverage_offset_array (c, backtrack_iter)) return_trace (false); + if (!serialize_coverage_offset_array (c, lookahead_iter)) return_trace (false); + + auto *substitute_out = c->serializer->start_embed> (); + auto substitutes = + + coverage_subst_iter + | hb_map (hb_second) + ; + + auto glyphs = + + coverage_subst_iter + | hb_map_retains_sorting (hb_first) + ; + if (unlikely (! c->serializer->check_success (substitute_out->serialize (c->serializer, substitutes)))) + return_trace (false); + + if (unlikely (!out->coverage.serialize_serialize (c->serializer, glyphs))) + return_trace (false); + return_trace (true); + } + + bool subset (hb_subset_context_t *c) const + { + TRACE_SUBSET (this); + const hb_set_t &glyphset = *c->plan->glyphset_gsub (); + const hb_map_t &glyph_map = *c->plan->glyph_map; + + const Array16OfOffset16To &lookahead = StructAfter> (backtrack); + const Array16Of &substitute = StructAfter> (lookahead); + + auto it = + + hb_zip (this+coverage, substitute) + | hb_filter (glyphset, hb_first) + | hb_filter (glyphset, hb_second) + | hb_map_retains_sorting ([&] (hb_pair_t p) -> hb_codepoint_pair_t + { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) + ; + + return_trace (bool (it) && serialize (c, it, backtrack.iter (), lookahead.iter ())); + } +}; + +} +} +} + +#endif /* HB_OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH */ diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index e02ffdf27..3397ce194 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -35,6 +35,7 @@ #include "OT/Layout/GSUB/MultipleSubst.hh" #include "OT/Layout/GSUB/AlternateSubst.hh" #include "OT/Layout/GSUB/LigatureSubst.hh" +#include "OT/Layout/GSUB/ReverseChainSingleSubst.hh" namespace OT { @@ -43,6 +44,7 @@ using Layout::GSUB::SingleSubst; using Layout::GSUB::MultipleSubst; using Layout::GSUB::AlternateSubst; using Layout::GSUB::LigatureSubst; +using Layout::GSUB::ReverseChainSingleSubst; struct ContextSubst : Context {}; @@ -54,243 +56,6 @@ struct ExtensionSubst : Extension bool is_reverse () const; }; - -struct ReverseChainSingleSubstFormat1 -{ - bool intersects (const hb_set_t *glyphs) const - { - if (!(this+coverage).intersects (glyphs)) - return false; - - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - - unsigned int count; - - count = backtrack.len; - for (unsigned int i = 0; i < count; i++) - if (!(this+backtrack[i]).intersects (glyphs)) - return false; - - count = lookahead.len; - for (unsigned int i = 0; i < count; i++) - if (!(this+lookahead[i]).intersects (glyphs)) - return false; - - return true; - } - - bool may_have_non_1to1 () const - { return false; } - - void closure (hb_closure_context_t *c) const - { - if (!intersects (c->glyphs)) return; - - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - const Array16Of &substitute = StructAfter> (lookahead); - - + hb_zip (this+coverage, substitute) - | hb_filter (c->parent_active_glyphs (), hb_first) - | hb_map (hb_second) - | hb_sink (c->output) - ; - } - - void closure_lookups (hb_closure_lookups_context_t *c) const {} - - void collect_glyphs (hb_collect_glyphs_context_t *c) const - { - if (unlikely (!(this+coverage).collect_coverage (c->input))) return; - - unsigned int count; - - count = backtrack.len; - for (unsigned int i = 0; i < count; i++) - if (unlikely (!(this+backtrack[i]).collect_coverage (c->before))) return; - - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - count = lookahead.len; - for (unsigned int i = 0; i < count; i++) - if (unlikely (!(this+lookahead[i]).collect_coverage (c->after))) return; - - const Array16Of &substitute = StructAfter> (lookahead); - count = substitute.len; - c->output->add_array (substitute.arrayZ, substitute.len); - } - - const Coverage &get_coverage () const { return this+coverage; } - - bool would_apply (hb_would_apply_context_t *c) const - { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; } - - bool apply (hb_ot_apply_context_t *c) const - { - TRACE_APPLY (this); - if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL)) - return_trace (false); /* No chaining to this type */ - - unsigned int index = (this+coverage).get_coverage (c->buffer->cur ().codepoint); - if (likely (index == NOT_COVERED)) return_trace (false); - - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - const Array16Of &substitute = StructAfter> (lookahead); - - if (unlikely (index >= substitute.len)) return_trace (false); - - unsigned int start_index = 0, end_index = 0; - if (match_backtrack (c, - backtrack.len, (HBUINT16 *) backtrack.arrayZ, - match_coverage, this, - &start_index) && - match_lookahead (c, - lookahead.len, (HBUINT16 *) lookahead.arrayZ, - match_coverage, this, - c->buffer->idx + 1, &end_index)) - { - c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index); - c->replace_glyph_inplace (substitute[index]); - /* Note: We DON'T decrease buffer->idx. The main loop does it - * for us. This is useful for preventing surprises if someone - * calls us through a Context lookup. */ - return_trace (true); - } - else - { - c->buffer->unsafe_to_concat_from_outbuffer (start_index, end_index); - return_trace (false); - } - } - - template - bool serialize_coverage_offset_array (hb_subset_context_t *c, Iterator it) const - { - TRACE_SERIALIZE (this); - auto *out = c->serializer->start_embed> (); - - if (unlikely (!c->serializer->allocate_size (HBUINT16::static_size))) - return_trace (false); - - for (auto& offset : it) { - auto *o = out->serialize_append (c->serializer); - if (unlikely (!o) || !o->serialize_subset (c, offset, this)) - return_trace (false); - } - - return_trace (true); - } - - template - bool serialize (hb_subset_context_t *c, - Iterator coverage_subst_iter, - BacktrackIterator backtrack_iter, - LookaheadIterator lookahead_iter) const - { - TRACE_SERIALIZE (this); - - auto *out = c->serializer->start_embed (this); - if (unlikely (!c->serializer->check_success (out))) return_trace (false); - if (unlikely (!c->serializer->embed (this->format))) return_trace (false); - if (unlikely (!c->serializer->embed (this->coverage))) return_trace (false); - - if (!serialize_coverage_offset_array (c, backtrack_iter)) return_trace (false); - if (!serialize_coverage_offset_array (c, lookahead_iter)) return_trace (false); - - auto *substitute_out = c->serializer->start_embed> (); - auto substitutes = - + coverage_subst_iter - | hb_map (hb_second) - ; - - auto glyphs = - + coverage_subst_iter - | hb_map_retains_sorting (hb_first) - ; - if (unlikely (! c->serializer->check_success (substitute_out->serialize (c->serializer, substitutes)))) - return_trace (false); - - if (unlikely (!out->coverage.serialize_serialize (c->serializer, glyphs))) - return_trace (false); - return_trace (true); - } - - bool subset (hb_subset_context_t *c) const - { - TRACE_SUBSET (this); - const hb_set_t &glyphset = *c->plan->glyphset_gsub (); - const hb_map_t &glyph_map = *c->plan->glyph_map; - - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - const Array16Of &substitute = StructAfter> (lookahead); - - auto it = - + hb_zip (this+coverage, substitute) - | hb_filter (glyphset, hb_first) - | hb_filter (glyphset, hb_second) - | hb_map_retains_sorting ([&] (hb_pair_t p) -> hb_codepoint_pair_t - { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) - ; - - return_trace (bool (it) && serialize (c, it, backtrack.iter (), lookahead.iter ())); - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this))) - return_trace (false); - const Array16OfOffset16To &lookahead = StructAfter> (backtrack); - if (!lookahead.sanitize (c, this)) - return_trace (false); - const Array16Of &substitute = StructAfter> (lookahead); - return_trace (substitute.sanitize (c)); - } - - protected: - HBUINT16 format; /* Format identifier--format = 1 */ - Offset16To - coverage; /* Offset to Coverage table--from - * beginning of table */ - Array16OfOffset16To - backtrack; /* Array of coverage tables - * in backtracking sequence, in glyph - * sequence order */ - Array16OfOffset16To - lookaheadX; /* Array of coverage tables - * in lookahead sequence, in glyph - * sequence order */ - Array16Of - substituteX; /* Array of substitute - * GlyphIDs--ordered by Coverage Index */ - public: - DEFINE_SIZE_MIN (10); -}; - -struct ReverseChainSingleSubst -{ - template - typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const - { - TRACE_DISPATCH (this, u.format); - if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ()); - switch (u.format) { - case 1: return_trace (c->dispatch (u.format1, std::forward (ds)...)); - default:return_trace (c->default_return_value ()); - } - } - - protected: - union { - HBUINT16 format; /* Format identifier */ - ReverseChainSingleSubstFormat1 format1; - } u; -}; - - - /* * SubstLookup */ diff --git a/src/meson.build b/src/meson.build index 30f35cbaa..abd80f355 100644 --- a/src/meson.build +++ b/src/meson.build @@ -106,6 +106,8 @@ hb_base_sources = files( 'OT/Layout/GSUB/LigatureSubst.hh', 'OT/Layout/GSUB/LigatureSet.hh', 'OT/Layout/GSUB/Ligature.hh', + 'OT/Layout/GSUB/ReverseChainSingleSubst.hh', + 'OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh', 'hb-ot-layout-gsubgpos.hh', 'hb-ot-layout-jstf-table.hh', 'hb-ot-layout.cc',