diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index f7a52bb5a..0fa0d2803 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1670,6 +1670,15 @@ struct ClassDefFormat1_3 return true; } + template + 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 + 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 + 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) { diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index d9f1a3428..99febc159 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1736,6 +1736,12 @@ static inline void collect_class (hb_set_t *glyphs, unsigned value, const void * const ClassDef &class_def = *reinterpret_cast(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; @@ -3137,7 +3143,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 +3152,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 +3313,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); }) ; } @@ -3847,25 +3853,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 +4690,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 (backtrack); const auto &lookahead = StructAfter (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 +4714,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 +4930,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); }) ; } @@ -5489,32 +5510,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 diff --git a/test/api/test-ot-collect-glyphs.c b/test/api/test-ot-collect-glyphs.c index 41f61807b..8b307906e 100644 --- a/test/api/test-ot-collect-glyphs.c +++ b/test/api/test-ot-collect-glyphs.c @@ -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 (); }