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);