From acdd1707a35fea95cf11089508a8848aecf4ff4c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:40:29 -0600 Subject: [PATCH 1/3] [cmap] Bound format 4 collect_unicodes/collect_mapping over overlapping segments CmapSubtableFormat4::accelerator_t::collect_unicodes() and collect_mapping() trusted the format's invariant that segments are sorted and disjoint, so a subtable whose 32766 segments all cover U+0000..U+FFFE was walked once per segment: ~2.1 billion insertions (about 30 s of CPU) for a single hb_subset() or hb_face_collect_nominal_glyph_mapping() call on a 262 KB font. Both walkers now keep a cursor `next` past the last collected code point and clamp each segment's start to it, skipping segments that end before the cursor. Total work per subtable is therefore at most 65536 code points plus one step per segment, mirroring the ordering guard CmapSubtableLongSegmented::collect_mapping() already has for formats 12/13. The glyphIdArray index is still computed from the raw startCount[i], so range-offset segments map exactly as before, and get_glyph() is untouched. Behaviour changes only for malformed subtables: where sorted segments overlap the earlier segment now wins the shared code points, and a segment lying wholly inside an earlier one is ignored. Adds test_collect_unicodes_format4_overlapping_segments to test/api/test-collect-unicodes.c. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GV7L48CF. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-cmap-table.hh | 16 +++++++- test/api/test-collect-unicodes.c | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index 8bad1da72..cf7d78474 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -615,10 +615,16 @@ struct CmapSubtableFormat4 unsigned int count = this->segCount; if (count && this->startCount[count - 1] == 0xFFFFu) count--; /* Skip sentinel segment. */ + hb_codepoint_t next = 0; for (unsigned int i = 0; i < count; i++) { - hb_codepoint_t start = this->startCount[i]; + /* Segments must be sorted and disjoint; never walk a codepoint twice, + * so that the total work is bounded regardless of segment count. */ + hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next); hb_codepoint_t end = this->endCount[i]; + if (unlikely (start > end)) + continue; + next = end + 1; unsigned int rangeOffset = this->idRangeOffset[i]; out->add_range(start, end); if (rangeOffset == 0) @@ -656,10 +662,16 @@ struct CmapSubtableFormat4 unsigned count = this->segCount; if (count && this->startCount[count - 1] == 0xFFFFu) count--; /* Skip sentinel segment. */ + hb_codepoint_t next = 0; for (unsigned i = 0; i < count; i++) { - hb_codepoint_t start = this->startCount[i]; + /* Segments must be sorted and disjoint; never walk a codepoint twice, + * so that the total work is bounded regardless of segment count. */ + hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next); hb_codepoint_t end = this->endCount[i]; + if (unlikely (start > end)) + continue; + next = end + 1; unsigned rangeOffset = this->idRangeOffset[i]; if (rangeOffset == 0) { diff --git a/test/api/test-collect-unicodes.c b/test/api/test-collect-unicodes.c index 735ff7c1d..fe0c77104 100644 --- a/test/api/test-collect-unicodes.c +++ b/test/api/test-collect-unicodes.c @@ -107,6 +107,71 @@ test_collect_unicodes_format4_with_range_offsets_and_deltas (void) hb_face_destroy (face); } +static void +test_collect_unicodes_format4_overlapping_segments (void) +{ + /* Segments overlap and one lies entirely inside an earlier one; each + * codepoint must be collected once, from the first segment covering it. */ + const char cmap_data[60] = { + 0, 0, 0, 1, /* cmap header */ + 0, 0, 0, 3, 0, 0, 0, 12, /* encoding records */ + /* cmap 4 */ + 0, 4, /* format */ + 0, 48, /* length */ + 0, 0, /* lang */ + 0, 8, /* segcountx2 */ + 0, 0, /* search range */ + 0, 0, /* entry selector */ + 0, 0, /* range shift */ + 0, 0x45, /* end code [0] */ + 0, 0x47, /* end code [1] */ + 0, 0x47, /* end code [2] */ + -1, -1, /* end code [3] */ + 0, 0, /* pad */ + 0, 0x41, /* start code [0] */ + 0, 0x43, /* start code [1] */ + 0, 0x40, /* start code [2] */ + -1, -1, /* start code [3] */ + 0, 0, /* id delta [0] */ + 1, 0, /* id delta [1] */ + 2, 0, /* id delta [2] */ + 0, 1, /* id delta [3] */ + 0, 0, /* id range offset [0] */ + 0, 0, /* id range offset [1] */ + 0, 0, /* id range offset [2] */ + 0, 0, /* id range offset [3] */ + }; + + hb_blob_t* cmap = hb_blob_create(cmap_data, 60, HB_MEMORY_MODE_READONLY, 0, 0); + hb_face_t* face = hb_face_builder_create(); + hb_face_builder_add_table(face, HB_TAG('c', 'm', 'a', 'p'), cmap); + hb_blob_destroy(cmap); + + hb_set_t *codepoints = hb_set_create(); + hb_map_t *mapping = hb_map_create(); + hb_codepoint_t cp; + + hb_face_collect_unicodes (face, codepoints); + + g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7); + g_assert_cmpuint (hb_set_get_min (codepoints), ==, 0x41); + g_assert_cmpuint (hb_set_get_max (codepoints), ==, 0x47); + + hb_set_clear(codepoints); + hb_face_collect_nominal_glyph_mapping(face, mapping, codepoints); + + g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7); + g_assert_cmpuint (hb_map_get_population (mapping), ==, 7); + for (cp = 0x41; cp <= 0x45; cp++) + g_assert_cmpuint (hb_map_get(mapping, cp), ==, cp); + g_assert_cmpuint (hb_map_get(mapping, 0x46), ==, 0x146); + g_assert_cmpuint (hb_map_get(mapping, 0x47), ==, 0x147); + + hb_map_destroy(mapping); + hb_set_destroy (codepoints); + hb_face_destroy (face); +} + static void test_collect_unicodes_format12_notdef (void) { @@ -275,6 +340,7 @@ main (int argc, char **argv) hb_test_add (test_collect_unicodes_format12_clamped); hb_test_add (test_collect_unicodes_format13); hb_test_add (test_collect_unicodes_format4_with_range_offsets_and_deltas); + hb_test_add (test_collect_unicodes_format4_overlapping_segments); return hb_test_run(); } From 60bd82dcb4dd9db49e40a20fb66e550399edbca6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:44:26 -0600 Subject: [PATCH 2/3] [subset] Bound cmap per-record unicode set collection When rewriting cmap, the subsetter built one hb_set_t of covered code points per encoding record. SubtableUnicodesCache keyed those sets by the record's own offset and collected each with num_glyphs = UINT_MAX, so a font with many records sharing one full-range format 12 subtable built one ~1.1M-code-point set per record: ~41 s of CPU, or ~2.1 GB of memory for a 96 KB input, with nothing but the record count (bounded only by the sanitizer) limiting the work. Three changes bound it, all matching how nearby cmap code already works: 1. Key cached_unicodes by record->subtable, so records sharing a subtable share one set (both set_for overloads). 2. Give the cache a num_glyphs member and collect with it instead of UINT_MAX, as the cmap accelerator already does for hb_face_collect_unicodes; cmap::serialize fills it from plan->source->get_num_glyphs() and create_filled_cache() takes it as a parameter, passed as face->get_num_glyphs() for hb_subset_preprocess. 3. Process only the first encoding record of each (platformID, encodingID) pair in both cmap::serialize and create_filled_cache (variation-selector records exempt). Lookup already sees one record per pair and EncodingRecord::copy links later records of a format to the first serialized subtable, so this bounds both the number of sets built and the _can_drop walk regardless of how many records a malformed font repeats, independent of whether they share a subtable. No public API or ABI change. Output is unchanged for well-formed fonts: the plan's unicodes are already restricted to gids below num_glyphs, and valid fonts never repeat a (platformID, encodingID) pair. The attached reproducer's hostile fonts drop from ~41 s / 2.1 GB to ~0 s / 12 MiB with byte-identical output. Adds test_subset_cmap_shared_subtable_many_records and test_subset_cmap_repeated_encoding_records to test/api/test-subset-cmap.c. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FSXRDPBT. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-cmap-table.hh | 62 +++++++++++++++----- src/hb-subset.cc | 2 +- test/api/test-subset-cmap.c | 111 ++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 15 deletions(-) diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index cf7d78474..a5aa7e163 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -1813,11 +1813,14 @@ struct SubtableUnicodesCache { hb_blob_ptr_t base_blob; const char* base; unsigned int table_length; + unsigned int num_glyphs; + /* Keyed by subtable offset: several encoding records may share one subtable. */ hb_hashmap_t> cached_unicodes; public: - static SubtableUnicodesCache* create (hb_blob_ptr_t source_table) + static SubtableUnicodesCache* create (hb_blob_ptr_t source_table, + unsigned int num_glyphs) { SubtableUnicodesCache* cache = (SubtableUnicodesCache*) hb_malloc (sizeof(SubtableUnicodesCache)); @@ -1825,7 +1828,7 @@ struct SubtableUnicodesCache { if (unlikely (!cache)) return nullptr; - new (cache) SubtableUnicodesCache (source_table); + new (cache) SubtableUnicodesCache (source_table, num_glyphs); return cache; } @@ -1838,17 +1841,21 @@ struct SubtableUnicodesCache { } SubtableUnicodesCache(const void* cmap_base, - unsigned int table_length_ = 0) + unsigned int table_length_ = 0, + unsigned int num_glyphs_ = UINT_MAX) : base_blob(), base ((const char*) cmap_base), table_length (table_length_), + num_glyphs (num_glyphs_), cached_unicodes () {} - SubtableUnicodesCache(hb_blob_ptr_t base_blob_) + SubtableUnicodesCache(hb_blob_ptr_t base_blob_, + unsigned int num_glyphs_ = UINT_MAX) : base_blob(base_blob_), base ((const char *) base_blob.get()), table_length (base_blob.get_length ()), + num_glyphs (num_glyphs_), cached_unicodes () {} @@ -1865,15 +1872,15 @@ struct SubtableUnicodesCache { const hb_set_t* set_for (const EncodingRecord* record, SubtableUnicodesCache& mutable_cache) const { - if (cached_unicodes.has ((unsigned) ((const char *) record - base))) - return cached_unicodes.get ((unsigned) ((const char *) record - base)); + if (cached_unicodes.has ((unsigned) record->subtable)) + return cached_unicodes.get ((unsigned) record->subtable); return mutable_cache.set_for (record); } const hb_set_t* set_for (const EncodingRecord* record) { - if (!cached_unicodes.has ((unsigned) ((const char *) record - base))) + if (!cached_unicodes.has ((unsigned) record->subtable)) { hb_set_t *s = hb_set_create (); if (unlikely (s->in_error ())) @@ -1882,14 +1889,14 @@ struct SubtableUnicodesCache { unsigned int subtable_data_size = record->subtable < table_length ? table_length - (unsigned int) record->subtable : 0; - (base+record->subtable).collect_unicodes (s, UINT_MAX, subtable_data_size); + (base+record->subtable).collect_unicodes (s, num_glyphs, subtable_data_size); - if (unlikely (!cached_unicodes.set ((unsigned) ((const char *) record - base), hb::unique_ptr {s}))) + if (unlikely (!cached_unicodes.set ((unsigned) record->subtable, hb::unique_ptr {s}))) return hb_set_get_empty (); return s; } - return cached_unicodes.get ((unsigned) ((const char *) record - base)); + return cached_unicodes.get ((unsigned) record->subtable); } }; @@ -1914,7 +1921,8 @@ struct cmap static constexpr hb_tag_t tableTag = HB_OT_TAG_cmap; - static SubtableUnicodesCache* create_filled_cache(hb_blob_ptr_t source_table) { + static SubtableUnicodesCache* create_filled_cache(hb_blob_ptr_t source_table, + unsigned int num_glyphs) { const cmap* cmap = source_table.get(); auto it = + hb_iter (cmap->encodingRecord) @@ -1923,13 +1931,15 @@ struct cmap }) ; - SubtableUnicodesCache* cache = SubtableUnicodesCache::create(source_table); + SubtableUnicodesCache* cache = SubtableUnicodesCache::create(source_table, num_glyphs); if (unlikely (!cache)) return nullptr; + hb_set_t seen_encodings; for (const EncodingRecord& _ : it) - cache->set_for(&_); // populate the cache for this encoding record. + if (cmap::is_first_record_for_encoding (cmap, _, seen_encodings)) + cache->set_for(&_); // populate the cache for this encoding record. return cache; } @@ -1953,7 +1963,8 @@ struct cmap #endif auto snap = c->snapshot (); - SubtableUnicodesCache local_unicodes_cache (base, source_table_length); + SubtableUnicodesCache local_unicodes_cache (base, source_table_length, + plan->source->get_num_glyphs ()); const SubtableUnicodesCache* unicodes_cache = &local_unicodes_cache; if (plan->accelerator && @@ -1961,6 +1972,7 @@ struct cmap plan->accelerator->cmap_cache->same_base (base)) unicodes_cache = plan->accelerator->cmap_cache; + hb_set_t seen_encodings; for (const EncodingRecord& _ : encodingrec_iter) { if (c->in_error ()) @@ -1973,6 +1985,8 @@ struct cmap #endif ) continue; + if (!is_first_record_for_encoding (base, _, seen_encodings)) continue; + const hb_set_t* unicodes_set = unicodes_cache->set_for (&_, local_unicodes_cache); if (!drop_format_4 && format == 4) @@ -2526,6 +2540,26 @@ struct cmap (cmap + _.subtable).is_variation_selector (); } + /* Only the first encoding record of each (platformID, encodingID) pair is + * used: lookup binary-searches the records and sees one per pair, and + * EncodingRecord::copy already links later records of a format to the first + * serialized subtable. Collapsing them here bounds the number of per-record + * unicode sets the subsetter builds (and the _can_drop walk) regardless of + * how many records a malformed font repeats. Variation-selector records are + * exempt, so a font with both a format 14 and a format 15 subtable keeps + * both. */ + static bool is_first_record_for_encoding (const void *base, + const EncodingRecord& _, + hb_set_t& seen_encodings) + { + if ((base+_.subtable).is_variation_selector ()) return true; + + hb_codepoint_t encoding = ((unsigned) _.platformID << 16) | _.encodingID; + if (seen_encodings.has (encoding)) return false; + seen_encodings.add (encoding); + return true; + } + protected: HBUINT16 version; /* Table version number (0). */ SortedArray16Of diff --git a/src/hb-subset.cc b/src/hb-subset.cc index e9e1bebd3..f3d7c8859 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc @@ -380,7 +380,7 @@ static void _attach_accelerator_data (hb_subset_plan_t* plan, // Populate caches that need access to the final tables. hb_blob_ptr_t cmap_ptr (hb_sanitize_context_t ().reference_table (face)); - accel->cmap_cache = OT::cmap::create_filled_cache (cmap_ptr); + accel->cmap_cache = OT::cmap::create_filled_cache (cmap_ptr, face->get_num_glyphs ()); accel->destroy_cmap_cache = OT::SubtableUnicodesCache::destroy; if (!hb_face_set_user_data(face, diff --git a/test/api/test-subset-cmap.c b/test/api/test-subset-cmap.c index 3c9bc0743..d226715e5 100644 --- a/test/api/test-subset-cmap.c +++ b/test/api/test-subset-cmap.c @@ -352,6 +352,115 @@ test_subset_DMAP (void) hb_face_destroy (face); } +/* Many encoding records sharing one format 12 subtable whose group spans + * all of Unicode: the subsetter must collect the subtable once, and only + * up to the font's glyph count, instead of once per record. */ +static void +test_subset_cmap_shared_subtable_many_records (void) +{ + const unsigned num_records = 4000; + const char maxp_data[] = { + 0, 0, 0x50, 0, 0, 3, + }; + unsigned cmap_len = 4 + 8 * num_records + 16 + 12; + char *cmap_data = calloc (1, cmap_len); + g_assert_nonnull (cmap_data); + + char *p = cmap_data; + *p++ = 0; *p++ = 0; /* version */ + *p++ = num_records >> 8; *p++ = num_records & 0xFF; /* numTables */ + unsigned subtable_offset = 4 + 8 * num_records; + for (unsigned i = 0; i < num_records; i++) + { + *p++ = 0; *p++ = 3; *p++ = 0; *p++ = 10; /* platform 3, encoding 10 */ + *p++ = subtable_offset >> 24; *p++ = subtable_offset >> 16; + *p++ = subtable_offset >> 8; *p++ = subtable_offset; + } + const char subtable[] = { + 0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */ + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1, /* U+0041..U+10FFFF -> 1... */ + }; + memcpy (p, subtable, sizeof (subtable)); + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", maxp_data); + hb_blob_t *cmap_blob = hb_blob_create_or_fail (cmap_data, cmap_len, + HB_MEMORY_MODE_WRITABLE, + cmap_data, free); + hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), cmap_blob); + hb_blob_destroy (cmap_blob); + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 'A'); + hb_face_t *subset = hb_subset_test_create_subset (face, + hb_subset_test_create_input (codepoints)); + hb_set_destroy (codepoints); + + hb_font_t *font = hb_font_create (subset); + hb_codepoint_t glyph; + g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph)); + g_assert_cmpuint (glyph, ==, 1); + g_assert_false (hb_font_get_nominal_glyph (font, 'B', &glyph)); + hb_font_destroy (font); + + hb_face_destroy (subset); + hb_face_destroy (face); +} + +/* Several encoding records that repeat one (platformID, encodingID) pair, + * two sharing a subtable and one with its own copy: the subset must keep + * only the first record of the pair, so the output cmap has a single + * encoding record and still maps the retained code point. */ +static void +test_subset_cmap_repeated_encoding_records (void) +{ + const char maxp_data[] = { + 0, 0, 0x50, 0, 0, 3, + }; + const char cmap_data[] = { + 0, 0, 0, 3, /* version, numTables = 3 */ + 0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */ + 0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */ + 0, 3, 0, 10, 0, 0, 0, 56, /* (3,10) -> subtable B */ + /* subtable A at 28 */ + 0, 12, 0, 0, 0, 0, 0, 28, + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1, + /* subtable B at 56 */ + 0, 12, 0, 0, 0, 0, 0, 28, + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1, + }; + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face, "cmap", cmap_data); + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 'A'); + hb_face_t *subset = hb_subset_test_create_subset (face, + hb_subset_test_create_input (codepoints)); + hb_set_destroy (codepoints); + + hb_blob_t *out = hb_face_reference_table (subset, HB_TAG ('c','m','a','p')); + unsigned out_len = 0; + const char *out_data = hb_blob_get_data (out, &out_len); + g_assert_cmpuint (out_len, >=, 4); + unsigned num_tables = ((unsigned char) out_data[2] << 8) | (unsigned char) out_data[3]; + g_assert_cmpuint (num_tables, ==, 1); + hb_blob_destroy (out); + + hb_font_t *font = hb_font_create (subset); + hb_codepoint_t glyph; + g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph)); + g_assert_cmpuint (glyph, ==, 1); + hb_font_destroy (font); + + hb_face_destroy (subset); + hb_face_destroy (face); +} + #ifndef HB_NO_BEYOND_64K static void test_subset_cmap_format15 (void) @@ -424,6 +533,8 @@ main (int argc, char **argv) hb_test_add (test_subset_cmap_noto_color_emoji_noop); hb_test_add (test_subset_cmap_noto_color_emoji_non_consecutive_glyphs); hb_test_add (test_subset_DMAP); + hb_test_add (test_subset_cmap_shared_subtable_many_records); + hb_test_add (test_subset_cmap_repeated_encoding_records); #ifndef HB_NO_BEYOND_64K hb_test_add (test_subset_cmap_format15); hb_test_add (test_subset_cmap15_default_uvs_range_limit); From c3fb3045499a044c427c62be2293a62d450066a6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:48:53 -0600 Subject: [PATCH 3/3] [subset] Only visit non-default UVS mappings in the DMAP cmap closure cmap_accelerator_t::closure_glyphs() in the DMAP path expanded, for every retained variation selector, all of the selector's Default UVS ranges and resolved each code point with get_variation_glyph(). Any number of 11-byte selector records may share one Default UVS table and a 4-byte range covers 256 code points, so the work was bounded by selectors x covered code points rather than by the table size: a 223 KB font with 20000 selectors on one 168-range table drove ~860M lookups and ~49 s of CPU in hb_subset_plan_create_or_fail(). A default mapping can only ever resolve to the nominal glyph, which the subset plan already retains with the unicode itself, so the closure only needs the code points that carry a Non-Default UVS mapping in cmap or DMAP; get_variation_glyph() still decides which table wins for each. This is exactly what the non-DMAP path (NonDefaultUVS::closure_glyphs) already does. A collect_variation_non_default_unicodes() accessor is added alongside the existing collect_variation_unicodes() chain (VariationSelectorRecord, CmapSubtableFormat14_15, CmapSubtable, cmap::accelerator_t) and used from the DMAP closure, so the work is one lookup per 5-byte mapping visited. No public API or ABI change; hb_face_collect_variation_unicodes() keeps expanding Default UVS ranges as before, and fonts without DMAP take the unchanged first branch. The reproducer's plan drops from ~49 s to 0.01 s with the same 257-glyph result. The separate, older per-record DefaultUVS::copy() serialization cost (~5.2 s for this font, independent of DMAP) is left in place. Adds test_subset_DMAP_shared_default_uvs to test/api/test-subset-cmap.c. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-8RP452C0. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-cmap-table.hh | 29 +++++++++++++- test/api/test-subset-cmap.c | 80 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index a5aa7e163..370d59a19 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -1330,6 +1330,9 @@ struct VariationSelectorRecord (base+nonDefaultUVS).collect_unicodes (out); } + void collect_non_default_unicodes (hb_set_t *out, const void *base) const + { (base+nonDefaultUVS).collect_unicodes (out); } + void collect_mapping (const void *base, hb_set_t *unicodes, /* OUT */ hb_map_t *mapping /* OUT */) const @@ -1417,6 +1420,9 @@ struct CmapSubtableFormat14_15 void collect_variation_unicodes (hb_codepoint_t variation_selector, hb_set_t *out) const { record.bsearch (variation_selector).collect_unicodes (out, this); } + void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector, + hb_set_t *out) const + { record.bsearch (variation_selector).collect_non_default_unicodes (out, this); } void serialize (hb_serialize_context_t *c, const hb_set_t *unicodes, @@ -1598,6 +1604,18 @@ struct CmapSubtable } } + void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector, + hb_set_t *out) const + { + switch (u.format.v) { + case 14: hb_barrier (); u.format14.collect_variation_non_default_unicodes (variation_selector, out); return; +#ifndef HB_NO_BEYOND_64K + case 15: hb_barrier (); u.format15.collect_variation_non_default_unicodes (variation_selector, out); return; +#endif + default: return; + } + } + void closure_glyphs (const hb_set_t *unicodes, hb_set_t *glyphset) const { switch (u.format.v) { @@ -2396,6 +2414,9 @@ struct cmap void collect_variation_unicodes (hb_codepoint_t variation_selector, hb_set_t *out) const { subtable_uvs->collect_variation_unicodes (variation_selector, out); } + void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector, + hb_set_t *out) const + { subtable_uvs->collect_variation_non_default_unicodes (variation_selector, out); } protected: typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj, @@ -2674,8 +2695,14 @@ struct cmap_accelerator_t if (!unicodes->has (variation_selector)) continue; + /* As in CmapSubtableFormat14_15::closure_glyphs(), only non-default + * mappings can add glyphs: a default mapping resolves to the nominal + * glyph, which the caller retains with the unicode itself. Expanding + * the Default UVS ranges instead would cost work proportional to the + * codepoints they cover, for every selector that shares them. */ hb_set_t variation_unicodes; - collect_variation_unicodes (variation_selector, &variation_unicodes); + base.collect_variation_non_default_unicodes (variation_selector, &variation_unicodes); + dmap.collect_variation_non_default_unicodes (variation_selector, &variation_unicodes); for (hb_codepoint_t unicode : variation_unicodes) { if (!unicodes->has (unicode)) diff --git a/test/api/test-subset-cmap.c b/test/api/test-subset-cmap.c index d226715e5..124c3a6e9 100644 --- a/test/api/test-subset-cmap.c +++ b/test/api/test-subset-cmap.c @@ -461,6 +461,85 @@ test_subset_cmap_repeated_encoding_records (void) hb_face_destroy (face); } +static void +write_be (unsigned char *p, unsigned int value, unsigned int size) +{ + while (size--) + { + p[size] = value & 0xFF; + value >>= 8; + } +} + +static void +test_subset_DMAP_shared_default_uvs (void) +{ + /* 20000 variation selectors sharing one Default UVS table whose ranges + * cover U+0000..U+A7FF, plus an empty DMAP so the subsetter takes the + * DMAP closure path. Only the first selector has a non-default mapping. + * The glyph closure must not resolve every covered codepoint for every + * selector; that used to take tens of seconds. */ + static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3}; + static const char dmap_data[] = {0, 0, 0, 0}; + static const unsigned char cmap_start[] = { + 0, 0, 0, 2, /* cmap header */ + 0, 0, 0, 5, 0, 0, 0, 48, /* UVS encoding record */ + 0, 3, 0, 10, 0, 0, 0, 20, /* nominal encoding record */ + 0, 13, 0, 0, 0, 0, 0, 28, /* format 13 header */ + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0xA8, 0, 0, 0, 0, 1, /* U+0000..U+A800 -> glyph 1 */ + }; + const unsigned int num_selectors = 20000; + const unsigned int num_ranges = 0xA8; + const unsigned int default_uvs = 10 + 11 * num_selectors; + const unsigned int non_default_uvs = default_uvs + 4 + 4 * num_ranges; + const unsigned int format14_length = non_default_uvs + 4 + 5; + const unsigned int cmap_length = sizeof (cmap_start) + format14_length; + unsigned char *cmap_data = g_malloc0 (cmap_length); + unsigned char *format14 = cmap_data + sizeof (cmap_start); + memcpy (cmap_data, cmap_start, sizeof (cmap_start)); + + write_be (format14 + 0, 14, 2); /* format 14 header */ + write_be (format14 + 2, format14_length, 4); + write_be (format14 + 6, num_selectors, 4); + for (unsigned int i = 0; i < num_selectors; i++) + { + unsigned char *record = format14 + 10 + 11 * i; /* variation selector */ + write_be (record + 0, 0xE0100 + i, 3); + write_be (record + 3, default_uvs, 4); + write_be (record + 7, i ? 0 : non_default_uvs, 4); + } + write_be (format14 + default_uvs, num_ranges, 4); /* default UVS */ + for (unsigned int i = 0; i < num_ranges; i++) + { + write_be (format14 + default_uvs + 4 + 4 * i, i << 8, 3); + write_be (format14 + default_uvs + 4 + 4 * i + 3, 0xFF, 1); + } + write_be (format14 + non_default_uvs, 1, 4); /* non-default UVS */ + write_be (format14 + non_default_uvs + 4, 0xA800, 3); + write_be (format14 + non_default_uvs + 7, 2, 2); + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face, "DMAP", dmap_data); + hb_blob_t *blob = hb_blob_create ((const char *) cmap_data, cmap_length, + HB_MEMORY_MODE_WRITABLE, cmap_data, g_free); + g_assert_true (hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), blob)); + hb_blob_destroy (blob); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_invert (hb_subset_input_unicode_set (input)); + hb_subset_plan_t *plan = hb_subset_plan_create_or_fail (face, input); + g_assert_nonnull (plan); + /* notdef, the nominal glyph and the one non-default variant. */ + g_assert_cmpuint (hb_map_get_population (hb_subset_plan_old_to_new_glyph_mapping (plan)), ==, 3); + g_assert_true (hb_map_has (hb_subset_plan_old_to_new_glyph_mapping (plan), 2)); + hb_subset_plan_destroy (plan); + hb_subset_input_destroy (input); + + hb_face_destroy (face); +} + #ifndef HB_NO_BEYOND_64K static void test_subset_cmap_format15 (void) @@ -535,6 +614,7 @@ main (int argc, char **argv) hb_test_add (test_subset_DMAP); hb_test_add (test_subset_cmap_shared_subtable_many_records); hb_test_add (test_subset_cmap_repeated_encoding_records); + hb_test_add (test_subset_DMAP_shared_default_uvs); #ifndef HB_NO_BEYOND_64K hb_test_add (test_subset_cmap_format15); hb_test_add (test_subset_cmap15_default_uvs_range_limit);