diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 1bcfc52c3..645b9ea0e 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1656,19 +1656,11 @@ static inline bool intersects_glyph (const hb_set_t *glyphs, unsigned value, con { return glyphs->has (value); } -static inline bool intersects_class (const hb_set_t *glyphs, unsigned value, const void *data, void *cache) +static inline bool intersects_class (const hb_set_t *glyphs HB_UNUSED, unsigned value, const void *data HB_UNUSED, void *cache) { - const ClassDef &class_def = *reinterpret_cast(data); - hb_map_t *map = (hb_map_t *) cache; - - hb_codepoint_t *cached_v; - if (map->has (value, &cached_v)) - return *cached_v; - - bool v = class_def.intersects_class (glyphs, value); - map->set (value, v); - - return v; + /* ClassDef::intersects_class() walks the whole ClassDef, so callers collect + * ClassDef::intersected_classes() of glyphs into the cache upfront, once. */ + return reinterpret_cast (cache)->has (value); } static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, const void *data, void *cache HB_UNUSED) { @@ -3737,7 +3729,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (glyphs, &cache); struct ContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -3757,8 +3750,7 @@ struct ContextFormat2_5 | hb_map (hb_add (this)) | hb_enumerate | hb_map ([&] (const hb_pair_t p) - { return class_def.intersects_class (glyphs, p.first) && - coverage_glyph_classes.has (p.first) && + { return coverage_glyph_classes.has (p.first) && p.second.intersects (glyphs, lookup_context); }) | hb_any ; @@ -3778,7 +3770,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); intersected_class_cache_t intersected_cache; ContextDependLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -3788,10 +3781,11 @@ struct ContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const RuleSet& rule_set = this+_.second; @@ -3814,7 +3808,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); intersected_class_cache_t intersected_cache; struct ContextClosureLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -3824,10 +3819,11 @@ struct ContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const RuleSet& rule_set = this+_.second; @@ -3845,7 +3841,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); struct ContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -3856,8 +3853,7 @@ struct ContextFormat2_5 + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_enumerate - | hb_filter ([&] (const hb_pair_t p) - { return class_def.intersects_class (c->glyphs, p.first); }) + | hb_filter (cache, hb_first) | hb_map (hb_second) | hb_apply ([&] (const RuleSet & _) { _.closure_lookups (c, lookup_context); }); @@ -5396,7 +5392,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (glyphs, &caches[0]); + input_class_def.intersected_classes (glyphs, &caches[1]); + lookahead_class_def.intersected_classes (glyphs, &caches[2]); struct ChainContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -5417,8 +5416,7 @@ struct ChainContextFormat2_5 | hb_map (hb_add (this)) | hb_enumerate | hb_map ([&] (const hb_pair_t p) - { return input_class_def.intersects_class (glyphs, p.first) && - coverage_glyph_classes.has (p.first) && + { return coverage_glyph_classes.has (p.first) && p.second.intersects (glyphs, lookup_context); }) | hb_any ; @@ -5440,7 +5438,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); intersected_class_cache_t intersected_cache; ChainContextDependLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -5452,10 +5453,11 @@ struct ChainContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const ChainRuleSet& chainrule_set = this+_.second; @@ -5480,7 +5482,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); intersected_class_cache_t intersected_cache; struct ChainContextClosureLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -5492,10 +5497,11 @@ struct ChainContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const ChainRuleSet& chainrule_set = this+_.second; @@ -5515,7 +5521,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); struct ChainContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -5528,8 +5537,7 @@ struct ChainContextFormat2_5 + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_enumerate - | hb_filter([&] (unsigned klass) - { return input_class_def.intersects_class (c->glyphs, klass); }, hb_first) + | hb_filter (caches[1], hb_first) | hb_map (hb_second) | hb_apply ([&] (const ChainRuleSet &_) { _.closure_lookups (c, lookup_context); }) diff --git a/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf b/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf new file mode 100644 index 000000000..6e48fd930 Binary files /dev/null and b/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf differ