Merge pull request #6559 from harfbuzz/scanner/layout-opt

[layout] Algorithmic fast-paths for GSUB/GPOS/GDEF closure, subset and shaping
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-11-10 19:23:20 -06:00
commit 0a3f84c495
11 files changed
+537 -89

No files matched your search

+8
View File
@@ -97,6 +97,14 @@ struct CoverageFormat1_3
hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))>
void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const
{
if (glyphArray.len > glyphs.get_population () * hb_bit_storage ((unsigned) glyphArray.len))
{
for (auto g : glyphs)
if (get_coverage (g) != NOT_COVERED)
intersect_glyphs << g;
return;
}
unsigned count = glyphArray.len;
for (unsigned i = 0; i < count; i++)
if (glyphs.has (glyphArray[i]))
+8
View File
@@ -157,6 +157,14 @@ struct CoverageFormat2_4
hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))>
void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const
{
if (rangeRecord.len > glyphs.get_population () * hb_bit_storage ((unsigned) rangeRecord.len))
{
for (auto g : glyphs)
if (get_coverage (g) != NOT_COVERED)
intersect_glyphs << g;
return;
}
/* Break out of loop for overlapping, broken, tables,
* to avoid fuzzer timouts. */
hb_codepoint_t last = 0;
+36 -4
View File
@@ -36,9 +36,25 @@ struct AlternateSubstFormat1_2
void depend (hb_depend_context_t *c) const
{
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
// Filter by parent_active_glyphs like closure does
+ hb_zip (this+coverage, alternateSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
if (alternateSet.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= alternateSet.len)
continue;
(this+alternateSet.arrayZ[i]).depend (c, g);
}
return;
}
+ hb_zip (cov, alternateSet)
| hb_filter (glyph_set, hb_first)
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, const typename Types::template OffsetTo<AlternateSet<Types>>&> &_)
{
const AlternateSet<Types>& as = this+_.second;
@@ -49,8 +65,24 @@ struct AlternateSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
+ hb_zip (this+coverage, alternateSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (alternateSet.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= alternateSet.len)
continue;
(this+alternateSet.arrayZ[i]).closure (c);
}
return;
}
+ hb_zip (cov, alternateSet)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
| hb_map (hb_add (this))
| hb_apply ([c] (const AlternateSet<Types> &_) { _.closure (c); })
+36 -4
View File
@@ -45,8 +45,24 @@ struct LigatureSubstFormat1_2
void depend (hb_depend_context_t *c) const
{
+ hb_zip (this+coverage, ligatureSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (ligatureSet.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= ligatureSet.len)
continue;
(this+ligatureSet.arrayZ[i]).depend (c, g);
}
return;
}
+ hb_zip (cov, ligatureSet)
| hb_filter (glyph_set, hb_first)
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, const typename Types::template OffsetTo<LigatureSet<Types>>&> &_)
{
const LigatureSet<Types>& ls = this+_.second;
@@ -57,8 +73,24 @@ struct LigatureSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
+ hb_zip (this+coverage, ligatureSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (ligatureSet.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= ligatureSet.len)
continue;
(this+ligatureSet.arrayZ[i]).closure (c);
}
return;
}
+ hb_zip (cov, ligatureSet)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
| hb_map (hb_add (this))
| hb_apply ([c] (const LigatureSet<Types> &_) { _.closure (c); })
+36 -4
View File
@@ -36,8 +36,24 @@ struct MultipleSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
+ hb_zip (this+coverage, sequence)
| hb_filter (c->parent_active_glyphs (), hb_first)
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (sequence.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= sequence.len)
continue;
(this+sequence.arrayZ[i]).closure (c);
}
return;
}
+ hb_zip (cov, sequence)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
| hb_map (hb_add (this))
| hb_apply ([c] (const Sequence<Types> &_) { _.closure (c); })
@@ -46,9 +62,25 @@ struct MultipleSubstFormat1_2
void depend (hb_depend_context_t *c) const
{
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
// Filter by parent_active_glyphs like closure does
+ hb_zip (this+coverage, sequence)
| hb_filter (c->parent_active_glyphs (), hb_first)
if (sequence.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= sequence.len)
continue;
(this+sequence.arrayZ[i]).depend (c, g);
}
return;
}
+ hb_zip (cov, sequence)
| hb_filter (glyph_set, hb_first)
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, const typename Types::template OffsetTo<Sequence<Types>>&> &_)
{
const Sequence<Types>& s = this+_.second;
@@ -132,10 +132,26 @@ struct ReverseChainSingleSubstFormat1_2
hb_codepoint_t saved_context = c->depend_data->current_context_set_index;
c->depend_data->current_context_set_index = context_set_idx;
+ hb_zip (this+coverage, substitute)
| hb_filter (c->parent_active_glyphs (), hb_first)
| hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); })
;
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (substitute.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= substitute.len)
continue;
c->depend_data->add_gsub_lookup (g, c->lookup_index, substitute.arrayZ[i]);
}
}
else
{
+ hb_zip (cov, substitute)
| hb_filter (glyph_set, hb_first)
| hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); })
;
}
c->depend_data->current_context_set_index = saved_context;
}
@@ -147,8 +163,24 @@ struct ReverseChainSingleSubstFormat1_2
const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
+ hb_zip (this+coverage, substitute)
| hb_filter (c->parent_active_glyphs (), hb_first)
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
if (substitute.len > glyph_set.get_population () * 4)
{
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
if (i == NOT_COVERED || i >= substitute.len)
continue;
c->output->add (substitute.arrayZ[i]);
}
return;
}
+ hb_zip (cov, substitute)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
| hb_sink (c->output)
;
+36
View File
@@ -1670,6 +1670,15 @@ struct ClassDefFormat1_3
return true;
}
template <typename set_t>
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
{
unsigned int count = classValue.len;
for (unsigned int i = 0; i < count; i++)
if (klasses.has (classValue[i])) glyphs->add (startGlyph + i);
return true;
}
bool intersects (const hb_set_t *glyphs) const
{
hb_codepoint_t start = startGlyph;
@@ -1954,6 +1963,18 @@ struct ClassDefFormat2_4
return true;
}
template <typename set_t>
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
{
for (auto &range : rangeRecord)
{
if (klasses.has (range.value))
if (unlikely (!range.collect_coverage (glyphs)))
return false;
}
return true;
}
bool intersects (const hb_set_t *glyphs) const
{
if (rangeRecord.len > glyphs->get_population () * hb_bit_storage ((unsigned) rangeRecord.len))
@@ -2296,6 +2317,21 @@ struct ClassDef
}
}
/* Like collect_class(), for all of klasses in one walk of the table. */
template <typename set_t>
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
{
switch (u.format.v) {
case 1: hb_barrier (); return u.format1.collect_classes (glyphs, klasses);
case 2: hb_barrier (); return u.format2.collect_classes (glyphs, klasses);
#ifndef HB_NO_BEYOND_64K
case 3: hb_barrier (); return u.format3.collect_classes (glyphs, klasses);
case 4: hb_barrier (); return u.format4.collect_classes (glyphs, klasses);
#endif
default:return false;
}
}
bool intersects (const hb_set_t *glyphs) const
{
switch (u.format.v) {
+152 -71
View File
@@ -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<const ClassDef *>(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<const hb_set_t *> (cache)->has (value);
}
static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, const void *data, void *cache HB_UNUSED)
{
@@ -1736,6 +1728,12 @@ static inline void collect_class (hb_set_t *glyphs, unsigned value, const void *
const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
class_def.collect_class (glyphs, value);
}
static inline void collect_classes (hb_set_t *glyphs, const hb_set_t &classes, const ClassDef &class_def)
{
if (unlikely (classes.in_error ()))
glyphs->err ();
class_def.collect_classes (glyphs, classes);
}
static inline void collect_coverage (hb_set_t *glyphs, unsigned value, const void *data)
{
Offset32To<Coverage> coverage;
@@ -3137,7 +3135,7 @@ struct Rule
}
void collect_second_glyphs (hb_set_digest_t *digest,
const ClassDef *class_def = nullptr) const
hb_set_t *classes = nullptr) const
{
if (inputCount <= 1)
{
@@ -3146,12 +3144,12 @@ struct Rule
}
unsigned second = inputZ.arrayZ[0];
if (!class_def)
if (!classes)
digest->add (second);
else if (!second)
*digest = hb_set_digest_t::full ();
else
class_def->collect_class (digest, second);
classes->add (second);
}
bool would_apply (hb_would_apply_context_t *c,
@@ -3307,11 +3305,11 @@ struct RuleSetOf
}
void collect_second_glyphs (hb_set_digest_t *digest,
const ClassDef *class_def = nullptr) const
hb_set_t *classes = nullptr) const
{
+ hb_iter (rule)
| hb_map (hb_add (this))
| hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, class_def); })
| hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, classes); })
;
}
@@ -3581,6 +3579,22 @@ struct ContextFormat1_4
nullptr
};
if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4)
{
/* Recursion can reallocate the stack; fetch the set afresh each time. */
hb_codepoint_t g = HB_SET_VALUE_INVALID;
while (c->previous_parent_active_glyphs ().next (&g))
{
unsigned i = get_coverage ().get_coverage (g);
if (i == NOT_COVERED || i >= ruleSet.len)
continue;
(this+ruleSet.arrayZ[i]).closure (c, g, lookup_context);
}
c->pop_cur_done_glyphs ();
return;
}
+ hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len))
| hb_filter ([&] (hb_codepoint_t _) {
return c->previous_parent_active_glyphs ().has (_);
@@ -3715,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,
@@ -3735,8 +3750,7 @@ struct ContextFormat2_5
| hb_map (hb_add (this))
| hb_enumerate
| hb_map ([&] (const hb_pair_t<unsigned, const RuleSet &> 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
;
@@ -3756,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},
@@ -3766,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<unsigned, const typename Types::template OffsetTo<RuleSet>&> _)
{
const RuleSet& rule_set = this+_.second;
@@ -3792,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},
@@ -3802,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<unsigned, const typename Types::template OffsetTo<RuleSet>&> _)
{
const RuleSet& rule_set = this+_.second;
@@ -3823,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,
@@ -3834,8 +3853,7 @@ struct ContextFormat2_5
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_enumerate
| hb_filter ([&] (const hb_pair_t<unsigned, const RuleSet &> 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); });
@@ -3847,25 +3865,40 @@ struct ContextFormat2_5
{
(this+coverage).collect_coverage (c->input);
const ClassDef &class_def = this+classDef;
/* Rules repeat classes, and can be shared. Collect the class values
* first, such that the ClassDef is walked once instead of per value. */
hb_set_t input_classes;
hb_set_t *input = c->input;
c->input = &input_classes;
struct ContextCollectGlyphsLookupContext lookup_context = {
{collect_class},
&class_def
{collect_glyph},
nullptr
};
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); })
;
c->input = input;
collect_classes (c->input, input_classes, this+classDef);
}
void collect_second_glyphs (hb_set_digest_t *digest) const
{
const ClassDef &class_def = this+classDef;
/* Ditto. */
hb_set_t classes;
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_apply ([=, &class_def] (const RuleSet &_) { _.collect_second_glyphs (digest, &class_def); })
| hb_apply ([=, &classes] (const RuleSet &_) { _.collect_second_glyphs (digest, &classes); })
;
if (unlikely (classes.in_error ()))
*digest = hb_set_digest_t::full ();
else
(this+classDef).collect_classes (digest, classes);
}
bool would_apply (hb_would_apply_context_t *c) const
@@ -4669,23 +4702,23 @@ struct ChainRule
}
void collect_second_glyphs (hb_set_digest_t *digest,
const ClassDef *input_class_def = nullptr,
const ClassDef *lookahead_class_def = nullptr) const
hb_set_t *input_classes = nullptr,
hb_set_t *lookahead_classes = nullptr) const
{
const auto &input = StructAfter<decltype (inputX)> (backtrack);
const auto &lookahead = StructAfter<decltype (lookaheadX)> (input);
unsigned second;
const ClassDef *class_def;
hb_set_t *classes;
if (input.lenP1 > 1)
{
second = input.arrayZ[0];
class_def = input_class_def;
classes = input_classes;
}
else if (lookahead.len)
{
second = lookahead.arrayZ[0];
class_def = lookahead_class_def;
classes = lookahead_classes;
}
else
{
@@ -4693,12 +4726,12 @@ struct ChainRule
return;
}
if (!class_def)
if (!classes)
digest->add (second);
else if (!second)
*digest = hb_set_digest_t::full ();
else
class_def->collect_class (digest, second);
classes->add (second);
}
bool would_apply (hb_would_apply_context_t *c,
@@ -4909,13 +4942,13 @@ struct ChainRuleSetOf
}
void collect_second_glyphs (hb_set_digest_t *digest,
const ClassDef *input_class_def = nullptr,
const ClassDef *lookahead_class_def = nullptr) const
hb_set_t *input_classes = nullptr,
hb_set_t *lookahead_classes = nullptr) const
{
+ hb_iter (rule)
| hb_map (hb_add (this))
| hb_apply ([=] (const ChainRuleType &_)
{ _.collect_second_glyphs (digest, input_class_def, lookahead_class_def); })
{ _.collect_second_glyphs (digest, input_classes, lookahead_classes); })
;
}
@@ -5209,6 +5242,22 @@ struct ChainContextFormat1_4
{nullptr, nullptr, nullptr}
};
if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4)
{
/* Recursion can reallocate the stack; fetch the set afresh each time. */
hb_codepoint_t g = HB_SET_VALUE_INVALID;
while (c->previous_parent_active_glyphs ().next (&g))
{
unsigned i = get_coverage ().get_coverage (g);
if (i == NOT_COVERED || i >= ruleSet.len)
continue;
(this+ruleSet.arrayZ[i]).closure (c, g, lookup_context);
}
c->pop_cur_done_glyphs ();
return;
}
+ hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len))
| hb_filter ([&] (hb_codepoint_t _) {
return c->previous_parent_active_glyphs ().has (_);
@@ -5343,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,
@@ -5364,8 +5416,7 @@ struct ChainContextFormat2_5
| hb_map (hb_add (this))
| hb_enumerate
| hb_map ([&] (const hb_pair_t<unsigned, const ChainRuleSet &> 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
;
@@ -5387,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},
@@ -5399,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<unsigned, const typename Types::template OffsetTo<ChainRuleSet>&> _)
{
const ChainRuleSet& chainrule_set = this+_.second;
@@ -5427,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},
@@ -5439,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<unsigned, const typename Types::template OffsetTo<ChainRuleSet>&> _)
{
const ChainRuleSet& chainrule_set = this+_.second;
@@ -5462,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,
@@ -5475,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); })
@@ -5489,32 +5550,52 @@ struct ChainContextFormat2_5
{
(this+coverage).collect_coverage (c->input);
const ClassDef &backtrack_class_def = this+backtrackClassDef;
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
/* Rules repeat classes, and can be shared. Collect the class values
* first, such that each ClassDef is walked once instead of per value. */
hb_set_t backtrack_classes, input_classes, lookahead_classes;
hb_set_t *before = c->before;
hb_set_t *input = c->input;
hb_set_t *after = c->after;
c->before = &backtrack_classes;
c->input = &input_classes;
c->after = &lookahead_classes;
struct ChainContextCollectGlyphsLookupContext lookup_context = {
{collect_class},
{&backtrack_class_def,
&input_class_def,
&lookahead_class_def}
{collect_glyph},
{nullptr, nullptr, nullptr}
};
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); })
;
c->before = before;
c->input = input;
c->after = after;
collect_classes (c->before, backtrack_classes, this+backtrackClassDef);
collect_classes (c->input, input_classes, this+inputClassDef);
collect_classes (c->after, lookahead_classes, this+lookaheadClassDef);
}
void collect_second_glyphs (hb_set_digest_t *digest) const
{
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
/* Ditto. */
hb_set_t input_classes, lookahead_classes;
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_apply ([digest, &input_class_def, &lookahead_class_def] (const ChainRuleSet &_)
{ _.collect_second_glyphs (digest, &input_class_def, &lookahead_class_def); })
| hb_apply ([digest, &input_classes, &lookahead_classes] (const ChainRuleSet &_)
{ _.collect_second_glyphs (digest, &input_classes, &lookahead_classes); })
;
if (unlikely (input_classes.in_error () || lookahead_classes.in_error ()))
{
*digest = hb_set_digest_t::full ();
return;
}
(this+inputClassDef).collect_classes (digest, input_classes);
(this+lookaheadClassDef).collect_classes (digest, lookahead_classes);
}
bool would_apply (hb_would_apply_context_t *c) const
+102
View File
@@ -481,6 +481,107 @@ test_ot_layout_lookup_collect_glyphs_qahiri (void)
hb_set_destroy (output);
}
static uint8_t *
put16 (uint8_t *p, unsigned v)
{
*p++ = (uint8_t) (v >> 8);
*p++ = (uint8_t) v;
return p;
}
static void
test_ot_layout_lookup_collect_glyphs_shared_class_rules (void)
{
/* A ContextSubstFormat2 whose RuleSet offsets all point at one RuleSet,
* whose Rule offsets all point at one Rule with 64 class values, over a
* 65535-entry ClassDef. The ClassDef must be walked once per subtable,
* not once per class value of every Rule visited; the latter takes
* about a minute. */
enum { RULESETS = 16, RULES = 1000, CLASSES = 64, GLYPHS = 65535 };
unsigned rule_len = 4 + 2 * CLASSES;
unsigned ruleset_len = 2 + 2 * RULES + rule_len;
unsigned coverage_len = 6;
unsigned classdef_len = 6 + 2 * GLYPHS;
unsigned ruleset_offset = 8 + 2 * RULESETS;
unsigned coverage_offset = ruleset_offset + ruleset_len;
unsigned classdef_offset = coverage_offset + coverage_len;
unsigned gsub_len = 10 + 2 + 2 + 4 + 8 + classdef_offset + classdef_len;
uint8_t *gsub = g_malloc0 (gsub_len);
uint8_t *p = gsub;
/* GSUB header, empty ScriptList and FeatureList, LookupList with one lookup. */
p = put16 (p, 1); p = put16 (p, 0);
p = put16 (p, 10); p = put16 (p, 12); p = put16 (p, 14);
p = put16 (p, 0);
p = put16 (p, 0);
p = put16 (p, 1); p = put16 (p, 4);
/* Lookup: type 5 (Context), one subtable right after. */
p = put16 (p, 5); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8);
/* ContextSubstFormat2 */
p = put16 (p, 2); p = put16 (p, coverage_offset); p = put16 (p, classdef_offset);
p = put16 (p, RULESETS);
for (unsigned i = 0; i < RULESETS; i++)
p = put16 (p, ruleset_offset);
/* RuleSet */
p = put16 (p, RULES);
for (unsigned i = 0; i < RULES; i++)
p = put16 (p, 2 + 2 * RULES);
/* Rule: classes 1 and 2, alternating. */
p = put16 (p, CLASSES + 1); p = put16 (p, 0);
for (unsigned i = 0; i < CLASSES; i++)
p = put16 (p, 1 + (i & 1));
/* Coverage: glyph 1. */
p = put16 (p, 1); p = put16 (p, 1); p = put16 (p, 1);
/* ClassDefFormat1: glyph i has class i % 3. */
p = put16 (p, 1); p = put16 (p, 0); p = put16 (p, GLYPHS);
for (unsigned i = 0; i < GLYPHS; i++)
p = put16 (p, i % 3);
g_assert_cmpuint (p - gsub, ==, gsub_len);
hb_blob_t *blob = hb_blob_create_or_fail ((const char *) gsub, gsub_len,
HB_MEMORY_MODE_WRITABLE, gsub, g_free);
hb_face_t *builder = hb_face_builder_create ();
hb_face_builder_add_table (builder, HB_OT_TAG_GSUB, blob);
hb_blob_destroy (blob);
blob = hb_face_reference_blob (builder);
hb_face_destroy (builder);
hb_face_t *face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
hb_set_t *before = hb_set_create();
hb_set_t *input = hb_set_create();
hb_set_t *after = hb_set_create();
hb_set_t *output = hb_set_create();
hb_codepoint_t g = HB_SET_VALUE_INVALID;
g_assert_cmpuint (1, ==, hb_ot_layout_table_get_lookup_count (face, HB_OT_TAG_GSUB));
BEGIN(HB_OT_TAG_GSUB, 0);
g_assert_cmpuint (0, ==, hb_set_get_population (before));
/* Glyph 1 from the coverage, and every glyph of class 1 or 2. */
g_assert_cmpuint (2 * (GLYPHS / 3), ==, hb_set_get_population (input));
g = HB_SET_VALUE_INVALID;
g_assert_true (hb_set_next (input, &g));
g_assert_cmpuint (1, ==, g);
g_assert_true (hb_set_next (input, &g));
g_assert_cmpuint (2, ==, g);
g_assert_true (hb_set_next (input, &g));
g_assert_cmpuint (4, ==, g);
g_assert_false (hb_set_has (input, 0));
g_assert_true (hb_set_has (input, GLYPHS - 1));
g_assert_cmpuint (0, ==, hb_set_get_population (after));
g_assert_cmpuint (0, ==, hb_set_get_population (output));
END();
hb_face_destroy (face);
hb_set_destroy (before);
hb_set_destroy (input);
hb_set_destroy (after);
hb_set_destroy (output);
}
int
main (int argc, char **argv)
{
@@ -488,5 +589,6 @@ main (int argc, char **argv)
hb_test_add (test_ot_layout_lookup_collect_glyphs_source_sans);
hb_test_add (test_ot_layout_lookup_collect_glyphs_noto_nastaliq);
hb_test_add (test_ot_layout_lookup_collect_glyphs_qahiri);
hb_test_add (test_ot_layout_lookup_collect_glyphs_shared_class_rules);
return hb_test_run ();
}
+85
View File
@@ -380,6 +380,90 @@ test_ot_layout_collect_lookup_variations (void)
}
#endif
static char *
write_be16 (char *p, unsigned v)
{
*p++ = (char) (v >> 8);
*p++ = (char) v;
return p;
}
static void
test_ot_layout_lookups_substitute_closure_large_coverage_one (unsigned lookup_type)
{
/* Lookup 0 is a ContextSubst covering NUM_GLYPHS glyphs; every rule recurses
* into lookup 1, a MultipleSubst or AlternateSubst covering the same glyphs
* and mapping each to OUT1 and OUT2. Closure visits lookup 1 once per glyph
* with a single active glyph, so the cost of a visit must not scale with the
* size of lookup 1's coverage. */
enum { NUM_GLYPHS = 16000, OUT1 = NUM_GLYPHS + 1, OUT2 = NUM_GLYPHS + 2 };
const unsigned context_len = 6 + 2 * NUM_GLYPHS + 4 + 8 + 10;
const unsigned subst_len = 6 + 2 * NUM_GLYPHS + 6 + 10;
const unsigned gsub_len = 10 + 6 + 16 + context_len + subst_len;
char *gsub = g_malloc0 (gsub_len);
char *p = gsub;
unsigned i;
p = write_be16 (p, 1); p = write_be16 (p, 0); /* version 1.0 */
p = write_be16 (p, 0); p = write_be16 (p, 0); /* no ScriptList, no FeatureList */
p = write_be16 (p, 10); /* LookupList */
p = write_be16 (p, 2); /* lookupCount */
p = write_be16 (p, 6); p = write_be16 (p, 14); /* Lookup offsets */
p = write_be16 (p, 5); p = write_be16 (p, 0); /* ContextSubst, no flags */
p = write_be16 (p, 1); p = write_be16 (p, 16); /* one subtable */
p = write_be16 (p, lookup_type); p = write_be16 (p, 0);
p = write_be16 (p, 1); p = write_be16 (p, 8 + context_len);
/* ContextSubstFormat1: all rule sets share one rule applying lookup 1 at 0. */
p = write_be16 (p, 1); p = write_be16 (p, context_len - 10);
p = write_be16 (p, NUM_GLYPHS);
for (i = 0; i < NUM_GLYPHS; i++)
p = write_be16 (p, 6 + 2 * NUM_GLYPHS);
p = write_be16 (p, 1); p = write_be16 (p, 4); /* RuleSet */
p = write_be16 (p, 1); p = write_be16 (p, 1); /* Rule: glyphCount, lookupCount */
p = write_be16 (p, 0); p = write_be16 (p, 1); /* SequenceLookupRecord */
p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */
p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0);
/* MultipleSubstFormat1 / AlternateSubstFormat1: all glyphs share one
* Sequence / AlternateSet. */
p = write_be16 (p, 1); p = write_be16 (p, subst_len - 10);
p = write_be16 (p, NUM_GLYPHS);
for (i = 0; i < NUM_GLYPHS; i++)
p = write_be16 (p, 6 + 2 * NUM_GLYPHS);
p = write_be16 (p, 2); p = write_be16 (p, OUT1); p = write_be16 (p, OUT2);
p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */
p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0);
g_assert_cmpuint (p - gsub, ==, gsub_len);
hb_face_t *face = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create (gsub, gsub_len, HB_MEMORY_MODE_READONLY, NULL, NULL);
g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GSUB, blob));
hb_blob_destroy (blob);
hb_face_set_glyph_count (face, OUT2 + 1);
hb_set_t *glyphs = hb_set_create ();
hb_set_add_range (glyphs, 1, NUM_GLYPHS);
hb_ot_layout_lookups_substitute_closure (face, NULL, glyphs);
g_assert_cmpuint (hb_set_get_population (glyphs), ==, NUM_GLYPHS + 2);
g_assert_true (hb_set_has (glyphs, OUT1));
g_assert_true (hb_set_has (glyphs, OUT2));
hb_set_destroy (glyphs);
hb_face_destroy (face);
g_free (gsub);
}
static void
test_ot_layout_lookups_substitute_closure_large_coverage (void)
{
test_ot_layout_lookups_substitute_closure_large_coverage_one (2); /* MultipleSubst */
test_ot_layout_lookups_substitute_closure_large_coverage_one (3); /* AlternateSubst */
}
int
main (int argc, char **argv)
{
@@ -399,5 +483,6 @@ main (int argc, char **argv)
#ifndef HB_NO_VAR
hb_test_add (test_ot_layout_collect_lookup_variations);
#endif
hb_test_add (test_ot_layout_lookups_substitute_closure_large_coverage);
return hb_test_run ();
}