From b6bc4e16502d15c92d698d8b569fdb0aac81717d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 27 Oct 2026 15:50:48 -0600 Subject: [PATCH] [subset] Reuse identity CFF1 program bytes (#6457) After computing subroutine closure, reuse source charstrings and subroutines when every retained subroutine keeps its original number. This avoids re-encoding full identity CFF1 subsets while preserving the existing pruning behavior when any subroutine is unused. The full Meson test suite passes (295 passed, 1 skipped). Assisted-by: OpenAI Codex --- src/hb-subset-cff-common.hh | 11 +++++ src/hb-subset-cff1.cc | 93 +++++++++++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 02294f904..869875990 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -1040,6 +1040,17 @@ struct subr_subsetter_t return encode_subrs ((*parsed_local_subrs)[fd], remaps.local_remaps[fd], fd, buffArray); } + bool has_identity_global_remap () const + { + return remaps.global_remap.get_population () == acc.globalSubrs->count; + } + + bool has_identity_local_remap (unsigned int fd) const + { + return remaps.local_remaps[fd].get_population () == + acc.privateDicts[fd].localSubrs->count; + } + protected: struct drop_hints_param_t { diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index d8865f01a..0f969cc8d 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -627,6 +627,51 @@ struct cff1_subset_plan return true; } + bool can_copy_programs (const OT::cff1::accelerator_subset_t &acc, + const hb_subset_plan_t *plan) const + { + if (drop_hints || desubroutinize || plan->inprogress_accelerator || + !(plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE) || + num_glyphs != acc.num_glyphs || + plan->new_to_old_gid_list.length != num_glyphs) + return false; + + for (const auto &_ : plan->new_to_old_gid_list) + if (_.first != _.second) + return false; + return true; + } + + template + static bool copy_index (const INDEX &index, str_buff_vec_t &output) + { + if (unlikely (!output.resize_exact (index.count))) + return false; + for (unsigned int i = 0; i < index.count; i++) + { + hb_ubytes_t str = index[i]; + output[i].set_storage (const_cast (str.arrayZ), str.length); + } + return true; + } + + bool copy_programs (const OT::cff1::accelerator_subset_t &acc) + { + if (!copy_index (*acc.charStrings, subset_charstrings) || + !copy_index (*acc.globalSubrs, subset_globalsubrs) || + unlikely (!subset_localsubrs.resize (orig_fdcount))) + return false; + + for (unsigned int fd = 0; fd < orig_fdcount; fd++) + { + subset_localsubrs[fd].init (); + if (fdmap.has (fd) && + !copy_index (*acc.privateDicts[fd].localSubrs, subset_localsubrs[fd])) + return false; + } + return true; + } + bool create (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan) { @@ -722,23 +767,43 @@ struct cff1_subset_plan if (!subr_subsetter.subset ()) return false; - /* encode charstrings, global subrs, local subrs with new subroutine numbers */ - if (!subr_subsetter.encode_charstrings (subset_charstrings)) - return false; + bool copy = can_copy_programs (acc, plan) && + subr_subsetter.has_identity_global_remap (); + if (copy) + for (unsigned int fd = 0; fd < orig_fdcount; fd++) + if (fdmap.has (fd) && !subr_subsetter.has_identity_local_remap (fd)) + { + copy = false; + break; + } - if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs)) - return false; - - /* local subrs */ - if (!subset_localsubrs.resize (orig_fdcount)) - return false; - for (unsigned int fd = 0; fd < orig_fdcount; fd++) + if (copy) { - subset_localsubrs[fd].init (); - if (fdmap.has (fd)) + /* Calls and their biases remain valid. Reuse source bytes instead of + * re-encoding every charstring and retained subroutine. */ + if (!copy_programs (acc)) + return false; + } + else + { + /* encode charstrings, global subrs, local subrs with new subroutine numbers */ + if (!subr_subsetter.encode_charstrings (subset_charstrings)) + return false; + + if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs)) + return false; + + /* local subrs */ + if (!subset_localsubrs.resize (orig_fdcount)) + return false; + for (unsigned int fd = 0; fd < orig_fdcount; fd++) { - if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd])) - return false; + subset_localsubrs[fd].init (); + if (fdmap.has (fd)) + { + if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd])) + return false; + } } } }