diff --git a/src/Makefile.sources b/src/Makefile.sources index 670e1beb4..d15cf63b6 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -95,6 +95,9 @@ HB_BASE_sources = \ OT/Layout/GSUB/SingleSubst.hh \ OT/Layout/GSUB/MultipleSubstFormat1.hh \ OT/Layout/GSUB/MultipleSubst.hh \ + OT/Layout/GSUB/AlternateSubstFormat1.hh \ + OT/Layout/GSUB/AlternateSubst.hh \ + OT/Layout/GSUB/AlternateSet.hh \ hb-ot-layout-gsubgpos.hh \ hb-ot-layout-jstf-table.hh \ hb-ot-layout.cc \ diff --git a/src/OT/Layout/GSUB/AlternateSet.hh b/src/OT/Layout/GSUB/AlternateSet.hh new file mode 100644 index 000000000..484f34746 --- /dev/null +++ b/src/OT/Layout/GSUB/AlternateSet.hh @@ -0,0 +1,110 @@ +#ifndef OT_LAYOUT_GSUB_ALTERNATESET_HH +#define OT_LAYOUT_GSUB_ALTERNATESET_HH + +#include "Common.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct AlternateSet +{ + protected: + Array16Of + alternates; /* Array of alternate GlyphIDs--in + * arbitrary order */ + public: + DEFINE_SIZE_ARRAY (2, alternates); + + bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (alternates.sanitize (c)); + } + + bool intersects (const hb_set_t *glyphs) const + { return hb_any (alternates, glyphs); } + + void closure (hb_closure_context_t *c) const + { c->output->add_array (alternates.arrayZ, alternates.len); } + + void collect_glyphs (hb_collect_glyphs_context_t *c) const + { c->output->add_array (alternates.arrayZ, alternates.len); } + + bool apply (hb_ot_apply_context_t *c) const + { + TRACE_APPLY (this); + unsigned int count = alternates.len; + + if (unlikely (!count)) return_trace (false); + + hb_mask_t glyph_mask = c->buffer->cur().mask; + hb_mask_t lookup_mask = c->lookup_mask; + + /* Note: This breaks badly if two features enabled this lookup together. */ + unsigned int shift = hb_ctz (lookup_mask); + unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift); + + /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */ + if (alt_index == HB_OT_MAP_MAX_VALUE && c->random) + { + /* Maybe we can do better than unsafe-to-break all; but since we are + * changing random state, it would be hard to track that. Good 'nough. */ + c->buffer->unsafe_to_break (0, c->buffer->len); + alt_index = c->random_number () % count + 1; + } + + if (unlikely (alt_index > count || alt_index == 0)) return_trace (false); + + c->replace_glyph (alternates[alt_index - 1]); + + return_trace (true); + } + + unsigned + get_alternates (unsigned start_offset, + unsigned *alternate_count /* IN/OUT. May be NULL. */, + hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const + { + if (alternates.len && alternate_count) + { + + alternates.sub_array (start_offset, alternate_count) + | hb_sink (hb_array (alternate_glyphs, *alternate_count)) + ; + } + return alternates.len; + } + + template + bool serialize (hb_serialize_context_t *c, + Iterator alts) + { + TRACE_SERIALIZE (this); + return_trace (alternates.serialize (c, alts)); + } + + 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; + + auto it = + + hb_iter (alternates) + | hb_filter (glyphset) + | hb_map (glyph_map) + ; + + auto *out = c->serializer->start_embed (*this); + return_trace (out->serialize (c->serializer, it) && + out->alternates); + } +}; + +} +} +} + + +#endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */ diff --git a/src/OT/Layout/GSUB/AlternateSubst.hh b/src/OT/Layout/GSUB/AlternateSubst.hh new file mode 100644 index 000000000..e5d999261 --- /dev/null +++ b/src/OT/Layout/GSUB/AlternateSubst.hh @@ -0,0 +1,51 @@ +#ifndef OT_LAYOUT_GSUB_ALTERNATESUBST_HH +#define OT_LAYOUT_GSUB_ALTERNATESUBST_HH + +#include "AlternateSubstFormat1.hh" +#include "Common.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct AlternateSubst +{ + protected: + union { + HBUINT16 format; /* Format identifier */ + AlternateSubstFormat1 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 ()); + } + } + + bool serialize (hb_serialize_context_t *c, + hb_sorted_array_t glyphs, + hb_array_t alternate_len_list, + hb_array_t alternate_glyphs_list) + { + TRACE_SERIALIZE (this); + if (unlikely (!c->extend_min (u.format))) return_trace (false); + unsigned int format = 1; + u.format = format; + switch (u.format) { + case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, alternate_glyphs_list)); + default:return_trace (false); + } + } +}; + +} +} +} + +#endif /* OT_LAYOUT_GSUB_ALTERNATESUBST_HH */ diff --git a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh new file mode 100644 index 000000000..af1cd7bed --- /dev/null +++ b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh @@ -0,0 +1,128 @@ +#ifndef OT_LAYOUT_GSUB_ALTERNATESUBSTFORMAT1_HH +#define OT_LAYOUT_GSUB_ALTERNATESUBSTFORMAT1_HH + +#include "AlternateSet.hh" +#include "Common.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct AlternateSubstFormat1 +{ + protected: + HBUINT16 format; /* Format identifier--format = 1 */ + Offset16To + coverage; /* Offset to Coverage table--from + * beginning of Substitution table */ + Array16OfOffset16To + alternateSet; /* Array of AlternateSet tables + * ordered by Coverage Index */ + public: + DEFINE_SIZE_ARRAY (6, alternateSet); + + bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this)); + } + + bool intersects (const hb_set_t *glyphs) const + { return (this+coverage).intersects (glyphs); } + + bool may_have_non_1to1 () const + { return false; } + + void closure (hb_closure_context_t *c) const + { + + hb_zip (this+coverage, alternateSet) + | hb_filter (c->parent_active_glyphs (), hb_first) + | hb_map (hb_second) + | hb_map (hb_add (this)) + | hb_apply ([c] (const AlternateSet &_) { _.closure (c); }) + ; + + } + + 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; + + hb_zip (this+coverage, alternateSet) + | hb_map (hb_second) + | hb_map (hb_add (this)) + | hb_apply ([c] (const AlternateSet &_) { _.collect_glyphs (c); }) + ; + } + + 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; } + + unsigned + get_glyph_alternates (hb_codepoint_t gid, + unsigned start_offset, + unsigned *alternate_count /* IN/OUT. May be NULL. */, + hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const + { return (this+alternateSet[(this+coverage).get_coverage (gid)]) + .get_alternates (start_offset, alternate_count, alternate_glyphs); } + + bool apply (hb_ot_apply_context_t *c) const + { + TRACE_APPLY (this); + + unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); + if (likely (index == NOT_COVERED)) return_trace (false); + + return_trace ((this+alternateSet[index]).apply (c)); + } + + bool serialize (hb_serialize_context_t *c, + hb_sorted_array_t glyphs, + hb_array_t alternate_len_list, + hb_array_t alternate_glyphs_list) + { + TRACE_SERIALIZE (this); + if (unlikely (!c->extend_min (this))) return_trace (false); + if (unlikely (!alternateSet.serialize (c, glyphs.length))) return_trace (false); + for (unsigned int i = 0; i < glyphs.length; i++) + { + unsigned int alternate_len = alternate_len_list[i]; + if (unlikely (!alternateSet[i] + .serialize_serialize (c, alternate_glyphs_list.sub_array (0, alternate_len)))) + return_trace (false); + alternate_glyphs_list += alternate_len; + } + return_trace (coverage.serialize_serialize (c, glyphs)); + } + + 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; + + auto *out = c->serializer->start_embed (*this); + if (unlikely (!c->serializer->extend_min (out))) return_trace (false); + out->format = format; + + hb_sorted_vector_t new_coverage; + + hb_zip (this+coverage, alternateSet) + | hb_filter (glyphset, hb_first) + | hb_filter (subset_offset_array (c, out->alternateSet, this), hb_second) + | hb_map (hb_first) + | hb_map (glyph_map) + | hb_sink (new_coverage) + ; + out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); + return_trace (bool (new_coverage)); + } +}; + +} +} +} + +#endif /* OT_LAYOUT_GSUB_ALTERNATESUBSTFORMAT1_HH */ diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index 5e87ac9fa..1787cbc88 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -33,257 +33,14 @@ #include "OT/Layout/GSUB/Common.hh" #include "OT/Layout/GSUB/SingleSubst.hh" #include "OT/Layout/GSUB/MultipleSubst.hh" - +#include "OT/Layout/GSUB/AlternateSubst.hh" namespace OT { using Layout::GSUB::hb_codepoint_pair_t; using Layout::GSUB::SingleSubst; using Layout::GSUB::MultipleSubst; - -struct AlternateSet -{ - bool intersects (const hb_set_t *glyphs) const - { return hb_any (alternates, glyphs); } - - void closure (hb_closure_context_t *c) const - { c->output->add_array (alternates.arrayZ, alternates.len); } - - void collect_glyphs (hb_collect_glyphs_context_t *c) const - { c->output->add_array (alternates.arrayZ, alternates.len); } - - bool apply (hb_ot_apply_context_t *c) const - { - TRACE_APPLY (this); - unsigned int count = alternates.len; - - if (unlikely (!count)) return_trace (false); - - hb_mask_t glyph_mask = c->buffer->cur().mask; - hb_mask_t lookup_mask = c->lookup_mask; - - /* Note: This breaks badly if two features enabled this lookup together. */ - unsigned int shift = hb_ctz (lookup_mask); - unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift); - - /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */ - if (alt_index == HB_OT_MAP_MAX_VALUE && c->random) - { - /* Maybe we can do better than unsafe-to-break all; but since we are - * changing random state, it would be hard to track that. Good 'nough. */ - c->buffer->unsafe_to_break (0, c->buffer->len); - alt_index = c->random_number () % count + 1; - } - - if (unlikely (alt_index > count || alt_index == 0)) return_trace (false); - - c->replace_glyph (alternates[alt_index - 1]); - - return_trace (true); - } - - unsigned - get_alternates (unsigned start_offset, - unsigned *alternate_count /* IN/OUT. May be NULL. */, - hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const - { - if (alternates.len && alternate_count) - { - + alternates.sub_array (start_offset, alternate_count) - | hb_sink (hb_array (alternate_glyphs, *alternate_count)) - ; - } - return alternates.len; - } - - template - bool serialize (hb_serialize_context_t *c, - Iterator alts) - { - TRACE_SERIALIZE (this); - return_trace (alternates.serialize (c, alts)); - } - - 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; - - auto it = - + hb_iter (alternates) - | hb_filter (glyphset) - | hb_map (glyph_map) - ; - - auto *out = c->serializer->start_embed (*this); - return_trace (out->serialize (c->serializer, it) && - out->alternates); - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - return_trace (alternates.sanitize (c)); - } - - protected: - Array16Of - alternates; /* Array of alternate GlyphIDs--in - * arbitrary order */ - public: - DEFINE_SIZE_ARRAY (2, alternates); -}; - -struct AlternateSubstFormat1 -{ - bool intersects (const hb_set_t *glyphs) const - { return (this+coverage).intersects (glyphs); } - - bool may_have_non_1to1 () const - { return false; } - - void closure (hb_closure_context_t *c) const - { - + hb_zip (this+coverage, alternateSet) - | hb_filter (c->parent_active_glyphs (), hb_first) - | hb_map (hb_second) - | hb_map (hb_add (this)) - | hb_apply ([c] (const AlternateSet &_) { _.closure (c); }) - ; - - } - - 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; - + hb_zip (this+coverage, alternateSet) - | hb_map (hb_second) - | hb_map (hb_add (this)) - | hb_apply ([c] (const AlternateSet &_) { _.collect_glyphs (c); }) - ; - } - - 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; } - - unsigned - get_glyph_alternates (hb_codepoint_t gid, - unsigned start_offset, - unsigned *alternate_count /* IN/OUT. May be NULL. */, - hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const - { return (this+alternateSet[(this+coverage).get_coverage (gid)]) - .get_alternates (start_offset, alternate_count, alternate_glyphs); } - - bool apply (hb_ot_apply_context_t *c) const - { - TRACE_APPLY (this); - - unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); - if (likely (index == NOT_COVERED)) return_trace (false); - - return_trace ((this+alternateSet[index]).apply (c)); - } - - bool serialize (hb_serialize_context_t *c, - hb_sorted_array_t glyphs, - hb_array_t alternate_len_list, - hb_array_t alternate_glyphs_list) - { - TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (this))) return_trace (false); - if (unlikely (!alternateSet.serialize (c, glyphs.length))) return_trace (false); - for (unsigned int i = 0; i < glyphs.length; i++) - { - unsigned int alternate_len = alternate_len_list[i]; - if (unlikely (!alternateSet[i] - .serialize_serialize (c, alternate_glyphs_list.sub_array (0, alternate_len)))) - return_trace (false); - alternate_glyphs_list += alternate_len; - } - return_trace (coverage.serialize_serialize (c, glyphs)); - } - - 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; - - auto *out = c->serializer->start_embed (*this); - if (unlikely (!c->serializer->extend_min (out))) return_trace (false); - out->format = format; - - hb_sorted_vector_t new_coverage; - + hb_zip (this+coverage, alternateSet) - | hb_filter (glyphset, hb_first) - | hb_filter (subset_offset_array (c, out->alternateSet, this), hb_second) - | hb_map (hb_first) - | hb_map (glyph_map) - | hb_sink (new_coverage) - ; - out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); - return_trace (bool (new_coverage)); - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this)); - } - - protected: - HBUINT16 format; /* Format identifier--format = 1 */ - Offset16To - coverage; /* Offset to Coverage table--from - * beginning of Substitution table */ - Array16OfOffset16To - alternateSet; /* Array of AlternateSet tables - * ordered by Coverage Index */ - public: - DEFINE_SIZE_ARRAY (6, alternateSet); -}; - -struct AlternateSubst -{ - bool serialize (hb_serialize_context_t *c, - hb_sorted_array_t glyphs, - hb_array_t alternate_len_list, - hb_array_t alternate_glyphs_list) - { - TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (u.format))) return_trace (false); - unsigned int format = 1; - u.format = format; - switch (u.format) { - case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, alternate_glyphs_list)); - default:return_trace (false); - } - } - - 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 */ - AlternateSubstFormat1 format1; - } u; -}; - +using Layout::GSUB::AlternateSubst; struct Ligature { diff --git a/src/meson.build b/src/meson.build index 6efbe2ee2..fcdfb708f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -99,6 +99,9 @@ hb_base_sources = files( 'OT/Layout/GSUB/SingleSubst.hh', 'OT/Layout/GSUB/MultipleSubstFormat1.hh', 'OT/Layout/GSUB/MultipleSubst.hh', + 'OT/Layout/GSUB/AlternateSubstFormat1.hh', + 'OT/Layout/GSUB/AlternateSubst.hh', + 'OT/Layout/GSUB/AlternateSet.hh', 'hb-ot-layout-gsubgpos.hh', 'hb-ot-layout-jstf-table.hh', 'hb-ot-layout.cc',