diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 85f1f8293..119a5cb9e 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -271,6 +271,48 @@ struct hb_aat_apply_context_t : template struct Lookup; +/* Collects the glyphs of LookupFormat2 / LookupFormat4 segments. + * + * Segments are supposed to be sorted and disjoint, in which case adding + * them one by one touches each glyph and each page of the set at most once. + * The sanitizer does not check that, and a malformed table can repeat a + * segment spanning the whole glyph range thousands of times, each costing a + * full pass. So once a segment is out of order, add the rest as a single + * range; that is a superset of the glyphs, which is fine for a set that is + * only used as a pre-filter. */ +template +static inline void +collect_segments_glyphs_filtered (const segments_t &segments, + set_t &glyphs, + const void *base, + const filter_t &filter) +{ + unsigned count = segments.get_length (); + hb_codepoint_t next = 0; /* One past the last glyph added. */ + unsigned i; + for (i = 0; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first < next) break; /* Not sorted and disjoint. */ + if (!segment.may_match (base, filter)) + continue; + segment.collect_glyphs_filtered (glyphs, base, filter); + next = segment.last + 1; + } + + hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; + for (; i < count; i++) + { + const auto &segment = segments[i]; + if (!segment.may_match (base, filter)) + continue; + first = hb_min (first, (hb_codepoint_t) segment.first); + last = hb_max (last, (hb_codepoint_t) segment.last); + } + if (first <= last) + glyphs.add_range (first, last); +} + template struct LookupFormat0 { @@ -330,11 +372,13 @@ struct LookupSegmentSingle if (first == DELETED_GLYPH) return; glyphs.add_range (first, last); } + template + bool may_match (const void *base HB_UNUSED, const filter_t &filter) const + { return first != DELETED_GLYPH && first <= last && filter (value); } template - void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + void collect_glyphs_filtered (set_t &glyphs, const void *base, const filter_t &filter) const { - if (first == DELETED_GLYPH) return; - if (!filter (value)) return; + if (!may_match (base, filter)) return; glyphs.add_range (first, last); } @@ -375,41 +419,7 @@ struct LookupFormat2 } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const - { - /* Segments are supposed to be sorted and disjoint, in which case adding - * them one by one touches each page of the set at most once. The - * sanitizer does not check that, and a malformed table can repeat a - * segment spanning the whole glyph range thousands of times, each - * costing a full pass over the set. So once a segment is out of order, - * add the rest as a single range; that is a superset of the glyphs, - * which is fine for a set that is only used as a pre-filter. */ - unsigned count = segments.get_length (); - hb_codepoint_t next = 0; /* One past the last glyph added. */ - unsigned i; - for (i = 0; i < count; i++) - { - const auto &segment = segments[i]; - if (segment.first < next) break; /* Not sorted and disjoint. */ - if (segment.first == DELETED_GLYPH || segment.first > segment.last || - !filter (segment.value)) - continue; - glyphs.add_range (segment.first, segment.last); - next = segment.last + 1; - } - - hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; - for (; i < count; i++) - { - const auto &segment = segments[i]; - if (segment.first == DELETED_GLYPH || segment.first > segment.last || - !filter (segment.value)) - continue; - first = hb_min (first, (hb_codepoint_t) segment.first); - last = hb_max (last, (hb_codepoint_t) segment.last); - } - if (first <= last) - glyphs.add_range (first, last); - } + { collect_segments_glyphs_filtered (segments, glyphs, this, filter); } bool sanitize (hb_sanitize_context_t *c) const { @@ -448,6 +458,10 @@ struct LookupSegmentArray if (first == DELETED_GLYPH) return; glyphs.add_range (first, last); } + /* Without looking at the values; for a superset. */ + template + bool may_match (const void *base HB_UNUSED, const filter_t &filter HB_UNUSED) const + { return first != DELETED_GLYPH && first <= last; } template void collect_glyphs_filtered (set_t &glyphs, const void *base, const filter_t &filter) const { @@ -503,17 +517,11 @@ struct LookupFormat4 template void collect_glyphs (set_t &glyphs) const { - unsigned count = segments.get_length (); - for (unsigned i = 0; i < count; i++) - segments[i].collect_glyphs (glyphs); + collect_glyphs_filtered (glyphs, [] (const T &) { return true; }); } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const - { - unsigned count = segments.get_length (); - for (unsigned i = 0; i < count; i++) - segments[i].collect_glyphs_filtered (glyphs, this, filter); - } + { collect_segments_glyphs_filtered (segments, glyphs, this, filter); } bool sanitize (hb_sanitize_context_t *c) const {