diff --git a/src/OT/Layout/GSUB/Common.hh b/src/OT/Layout/GSUB/Common.hh new file mode 100644 index 000000000..951c278d0 --- /dev/null +++ b/src/OT/Layout/GSUB/Common.hh @@ -0,0 +1,20 @@ +#ifndef OT_LAYOUT_GSUB_COMMON +#define OT_LAYOUT_GSUB_COMMON + +#include "hb-serialize.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +typedef hb_pair_t hb_codepoint_pair_t; + +template +static void SingleSubst_serialize (hb_serialize_context_t *c, + Iterator it); + +} +} +} + +#endif diff --git a/src/OT/Layout/GSUB/SingleSubst.hh b/src/OT/Layout/GSUB/SingleSubst.hh new file mode 100644 index 000000000..f1e88b3f8 --- /dev/null +++ b/src/OT/Layout/GSUB/SingleSubst.hh @@ -0,0 +1,74 @@ +#ifndef OT_LAYOUT_GSUB_SINGLE_SUBST +#define OT_LAYOUT_GSUB_SINGLE_SUBST + +#include "Common.hh" +#include "SingleSubstFormat1.hh" +#include "SingleSubstFormat2.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct SingleSubst +{ + + template + bool serialize (hb_serialize_context_t *c, + Iterator glyphs) + { + TRACE_SERIALIZE (this); + if (unlikely (!c->extend_min (u.format))) return_trace (false); + unsigned format = 2; + unsigned delta = 0; + if (glyphs) + { + format = 1; + auto get_delta = [=] (hb_codepoint_pair_t _) + { return (unsigned) (_.second - _.first) & 0xFFFF; }; + delta = get_delta (*glyphs); + if (!hb_all (++(+glyphs), delta, get_delta)) format = 2; + } + u.format = format; + switch (u.format) { + case 1: return_trace (u.format1.serialize (c, + + glyphs + | hb_map_retains_sorting (hb_first), + delta)); + case 2: return_trace (u.format2.serialize (c, glyphs)); + 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)...)); + case 2: return_trace (c->dispatch (u.format2, std::forward (ds)...)); + default:return_trace (c->default_return_value ()); + } + } + + protected: + union { + HBUINT16 format; /* Format identifier */ + SingleSubstFormat1 format1; + SingleSubstFormat2 format2; + } u; +}; + +template +static void +SingleSubst_serialize (hb_serialize_context_t *c, + Iterator it) +{ c->start_embed ()->serialize (c, it); } + +} +} +} + +#endif diff --git a/src/OT/Layout/GSUB/SingleSubstFormat1.hh b/src/OT/Layout/GSUB/SingleSubstFormat1.hh new file mode 100644 index 000000000..e7200d848 --- /dev/null +++ b/src/OT/Layout/GSUB/SingleSubstFormat1.hh @@ -0,0 +1,122 @@ +#ifndef OT_LAYOUT_GSUB_SINGLE_SUBST_FORMAT_1 +#define OT_LAYOUT_GSUB_SINGLE_SUBST_FORMAT_1 + +#include "Common.hh" +#include "hb-ot-layout-gsubgpos.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct SingleSubstFormat1 +{ + 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 + { + unsigned d = deltaGlyphID; + + + hb_iter (this+coverage) + | hb_filter (c->parent_active_glyphs ()) + | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; }) + | 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 d = deltaGlyphID; + + hb_iter (this+coverage) + | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; }) + | hb_sink (c->output) + ; + } + + 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); + hb_codepoint_t glyph_id = c->buffer->cur().codepoint; + unsigned int index = (this+coverage).get_coverage (glyph_id); + if (likely (index == NOT_COVERED)) return_trace (false); + + /* According to the Adobe Annotated OpenType Suite, result is always + * limited to 16bit. */ + glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu; + c->replace_glyph (glyph_id); + + return_trace (true); + } + + template + bool serialize (hb_serialize_context_t *c, + Iterator glyphs, + unsigned delta) + { + TRACE_SERIALIZE (this); + if (unlikely (!c->extend_min (this))) return_trace (false); + if (unlikely (!coverage.serialize_serialize (c, glyphs))) return_trace (false); + c->check_assign (deltaGlyphID, delta, HB_SERIALIZE_ERROR_INT_OVERFLOW); + 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; + + hb_codepoint_t delta = deltaGlyphID; + + auto it = + + hb_iter (this+coverage) + | hb_filter (glyphset) + | hb_map_retains_sorting ([&] (hb_codepoint_t g) { + return hb_codepoint_pair_t (g, + (g + delta) & 0xFFFF); }) + | hb_filter (glyphset, hb_second) + | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t + { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) + ; + + bool ret = bool (it); + SingleSubst_serialize (c->serializer, it); + return_trace (ret); + } + + bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c)); + } + + protected: + HBUINT16 format; /* Format identifier--format = 1 */ + Offset16To + coverage; /* Offset to Coverage table--from + * beginning of Substitution table */ + HBUINT16 deltaGlyphID; /* Add to original GlyphID to get + * substitute GlyphID, modulo 0x10000 */ + public: + DEFINE_SIZE_STATIC (6); +}; + +} +} +} + + +#endif diff --git a/src/OT/Layout/GSUB/SingleSubstFormat2.hh b/src/OT/Layout/GSUB/SingleSubstFormat2.hh new file mode 100644 index 000000000..8948c052f --- /dev/null +++ b/src/OT/Layout/GSUB/SingleSubstFormat2.hh @@ -0,0 +1,120 @@ +#ifndef OT_LAYOUT_GSUB_SINGLE_SUBST_FORMAT2 +#define OT_LAYOUT_GSUB_SINGLE_SUBST_FORMAT2 + +#include "Common.hh" +#include "hb-ot-layout-gsubgpos.hh" + +namespace OT { +namespace Layout { +namespace GSUB { + +struct SingleSubstFormat2 +{ + 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, 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; + + hb_zip (this+coverage, substitute) + | hb_map (hb_second) + | hb_sink (c->output) + ; + } + + 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); + unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); + if (likely (index == NOT_COVERED)) return_trace (false); + + if (unlikely (index >= substitute.len)) return_trace (false); + + c->replace_glyph (substitute[index]); + + return_trace (true); + } + + template + bool serialize (hb_serialize_context_t *c, + Iterator it) + { + TRACE_SERIALIZE (this); + auto substitutes = + + it + | hb_map (hb_second) + ; + auto glyphs = + + it + | hb_map_retains_sorting (hb_first) + ; + if (unlikely (!c->extend_min (this))) return_trace (false); + if (unlikely (!substitute.serialize (c, substitutes))) return_trace (false); + if (unlikely (!coverage.serialize_serialize (c, 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; + + 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]); }) + ; + + bool ret = bool (it); + SingleSubst_serialize (c->serializer, it); + return_trace (ret); + } + + bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (coverage.sanitize (c, this) && substitute.sanitize (c)); + } + + protected: + HBUINT16 format; /* Format identifier--format = 2 */ + Offset16To + coverage; /* Offset to Coverage table--from + * beginning of Substitution table */ + Array16Of + substitute; /* Array of substitute + * GlyphIDs--ordered by Coverage Index */ + public: + DEFINE_SIZE_ARRAY (6, substitute); +}; + +} +} +} + +#endif diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index 0b0bc547b..d314a0d43 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -30,285 +30,15 @@ #define HB_OT_LAYOUT_GSUB_TABLE_HH #include "hb-ot-layout-gsubgpos.hh" +#include "OT/Layout/GSUB/Common.hh" +#include "OT/Layout/GSUB/SingleSubst.hh" + namespace OT { -typedef hb_pair_t hb_codepoint_pair_t; - -template -static void SingleSubst_serialize (hb_serialize_context_t *c, - Iterator it); - - -struct SingleSubstFormat1 -{ - 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 - { - unsigned d = deltaGlyphID; - - + hb_iter (this+coverage) - | hb_filter (c->parent_active_glyphs ()) - | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; }) - | 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 d = deltaGlyphID; - + hb_iter (this+coverage) - | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; }) - | hb_sink (c->output) - ; - } - - 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); - hb_codepoint_t glyph_id = c->buffer->cur().codepoint; - unsigned int index = (this+coverage).get_coverage (glyph_id); - if (likely (index == NOT_COVERED)) return_trace (false); - - /* According to the Adobe Annotated OpenType Suite, result is always - * limited to 16bit. */ - glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu; - c->replace_glyph (glyph_id); - - return_trace (true); - } - - template - bool serialize (hb_serialize_context_t *c, - Iterator glyphs, - unsigned delta) - { - TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (this))) return_trace (false); - if (unlikely (!coverage.serialize_serialize (c, glyphs))) return_trace (false); - c->check_assign (deltaGlyphID, delta, HB_SERIALIZE_ERROR_INT_OVERFLOW); - 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; - - hb_codepoint_t delta = deltaGlyphID; - - auto it = - + hb_iter (this+coverage) - | hb_filter (glyphset) - | hb_map_retains_sorting ([&] (hb_codepoint_t g) { - return hb_codepoint_pair_t (g, - (g + delta) & 0xFFFF); }) - | hb_filter (glyphset, hb_second) - | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t - { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) - ; - - bool ret = bool (it); - SingleSubst_serialize (c->serializer, it); - return_trace (ret); - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c)); - } - - protected: - HBUINT16 format; /* Format identifier--format = 1 */ - Offset16To - coverage; /* Offset to Coverage table--from - * beginning of Substitution table */ - HBUINT16 deltaGlyphID; /* Add to original GlyphID to get - * substitute GlyphID, modulo 0x10000 */ - public: - DEFINE_SIZE_STATIC (6); -}; - -struct SingleSubstFormat2 -{ - 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, 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; - + hb_zip (this+coverage, substitute) - | hb_map (hb_second) - | hb_sink (c->output) - ; - } - - 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); - unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); - if (likely (index == NOT_COVERED)) return_trace (false); - - if (unlikely (index >= substitute.len)) return_trace (false); - - c->replace_glyph (substitute[index]); - - return_trace (true); - } - - template - bool serialize (hb_serialize_context_t *c, - Iterator it) - { - TRACE_SERIALIZE (this); - auto substitutes = - + it - | hb_map (hb_second) - ; - auto glyphs = - + it - | hb_map_retains_sorting (hb_first) - ; - if (unlikely (!c->extend_min (this))) return_trace (false); - if (unlikely (!substitute.serialize (c, substitutes))) return_trace (false); - if (unlikely (!coverage.serialize_serialize (c, 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; - - 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]); }) - ; - - bool ret = bool (it); - SingleSubst_serialize (c->serializer, it); - return_trace (ret); - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - return_trace (coverage.sanitize (c, this) && substitute.sanitize (c)); - } - - protected: - HBUINT16 format; /* Format identifier--format = 2 */ - Offset16To - coverage; /* Offset to Coverage table--from - * beginning of Substitution table */ - Array16Of - substitute; /* Array of substitute - * GlyphIDs--ordered by Coverage Index */ - public: - DEFINE_SIZE_ARRAY (6, substitute); -}; - -struct SingleSubst -{ - - template - bool serialize (hb_serialize_context_t *c, - Iterator glyphs) - { - TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (u.format))) return_trace (false); - unsigned format = 2; - unsigned delta = 0; - if (glyphs) - { - format = 1; - auto get_delta = [=] (hb_codepoint_pair_t _) - { return (unsigned) (_.second - _.first) & 0xFFFF; }; - delta = get_delta (*glyphs); - if (!hb_all (++(+glyphs), delta, get_delta)) format = 2; - } - u.format = format; - switch (u.format) { - case 1: return_trace (u.format1.serialize (c, - + glyphs - | hb_map_retains_sorting (hb_first), - delta)); - case 2: return_trace (u.format2.serialize (c, glyphs)); - 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)...)); - case 2: return_trace (c->dispatch (u.format2, std::forward (ds)...)); - default:return_trace (c->default_return_value ()); - } - } - - protected: - union { - HBUINT16 format; /* Format identifier */ - SingleSubstFormat1 format1; - SingleSubstFormat2 format2; - } u; -}; - -template -static void -SingleSubst_serialize (hb_serialize_context_t *c, - Iterator it) -{ c->start_embed ()->serialize (c, it); } +using Layout::GSUB::hb_codepoint_pair_t; +using Layout::GSUB::SingleSubst; struct Sequence { diff --git a/src/meson.build b/src/meson.build index e6a5f221a..a93314763 100644 --- a/src/meson.build +++ b/src/meson.build @@ -92,6 +92,10 @@ hb_base_sources = files( 'hb-ot-layout-gdef-table.hh', 'hb-ot-layout-gpos-table.hh', 'hb-ot-layout-gsub-table.hh', + 'OT/Layout/GSUB/Common.hh', + 'OT/Layout/GSUB/SingleSubstFormat1.hh', + 'OT/Layout/GSUB/SingleSubstFormat2.hh', + 'OT/Layout/GSUB/SingleSubst.hh', 'hb-ot-layout-gsubgpos.hh', 'hb-ot-layout-jstf-table.hh', 'hb-ot-layout.cc',