From 81e2db7cbcb87668c562329390a49d95a3a604bb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 26 Jun 2023 14:17:44 -0600 Subject: [PATCH] [subset/cff1] Speed up plan_subset_charset --- src/hb-ot-cff-common.hh | 2 +- src/hb-ot-cff1-table.hh | 7 ++++--- src/hb-subset-cff1.cc | 13 ++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index f646425fd..43910a5f7 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -55,7 +55,7 @@ struct code_pair_t using str_buff_t = hb_vector_t; using str_buff_vec_t = hb_vector_t; -using glyph_to_sid_map_t = hb_vector_t; +using glyph_to_sid_map_t = hb_vector_t; /* CFF INDEX */ template diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index 2118c4fb7..c5d7a9212 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -334,7 +334,7 @@ struct Charset0 { mapping->resize (num_glyphs, false); for (hb_codepoint_t gid = 1; gid < num_glyphs; gid++) - mapping->arrayZ[gid] = sids[gid - 1]; + mapping->arrayZ[gid] = {sids[gid - 1], gid + 1}; } hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const @@ -440,8 +440,9 @@ struct Charset1_2 { { hb_codepoint_t sid = ranges[i].first; unsigned count = ranges[i].nLeft + 1; + unsigned last = gid + count; for (unsigned j = 0; j < count; j++) - mapping->arrayZ[gid++] = sid++; + mapping->arrayZ[gid++] = {sid++, last}; if (gid >= num_glyphs) break; @@ -1275,7 +1276,7 @@ struct cff1 auto *mapping = (glyph_to_sid_map_t *) hb_malloc (sizeof (glyph_to_sid_map_t)); if (unlikely (!mapping)) return nullptr; mapping = new (mapping) glyph_to_sid_map_t (); - mapping->push (0); + mapping->push (code_pair_t {0, 1}); charset->collect_glyph_to_sid_map (mapping, num_glyphs); return mapping; } diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index 018d2a105..07e72c9af 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -533,6 +533,7 @@ struct cff1_subset_plan if (it->first == 0) it++; auto _ = *it; bool not_is_cid = !acc.is_CID (); + bool skip = !not_is_cid && glyph_to_sid_map; if (not_is_cid) sidmap.alloc (num_glyphs); for (glyph = 1; glyph < num_glyphs; glyph++) @@ -548,7 +549,11 @@ struct cff1_subset_plan /* Retain the SID for the old missing glyph ID */ old_glyph = glyph; } - unsigned sid = glyph_to_sid_map ? glyph_to_sid_map->arrayZ[old_glyph] : acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); + unsigned sid; + if (glyph_to_sid_map) + sid = glyph_to_sid_map->arrayZ[old_glyph].code; + else + sid = acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); if (not_is_cid) sid = sidmap.add (sid); @@ -557,6 +562,12 @@ struct cff1_subset_plan { code_pair_t pair { sid, glyph }; subset_charset_ranges.push (pair); + + if (skip && glyph == old_glyph) + { + glyph = hb_min (_.first - 1, glyph_to_sid_map->arrayZ[old_glyph].glyph - 1); + sid += glyph - old_glyph; + } } last_sid = sid; }