From a3afa61ce8d01784d88de4af3647ebd5b3e71fe6 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 23 Nov 2022 22:24:39 +0000 Subject: [PATCH] [subset] use cached parsed char strings if available. --- src/hb-subset-cff-common.hh | 67 ++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index f77010b27..cd16348e4 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -604,17 +604,38 @@ struct subr_subsetter_t if (unlikely (!closures.valid)) return false; + if (plan->accelerator && plan->accelerator->cff_accelerator) { + // Parsed subroutine char stsrings already exist in the accelerator. Copy + // them in. + // TODO(grieger): use reference instead of copy. + parsed_global_subrs = + plan->accelerator->cff_accelerator->parsed_global_subrs; + parsed_local_subrs = + plan->accelerator->cff_accelerator->parsed_local_subrs; + } + /* phase 1 & 2 */ for (unsigned int i = 0; i < plan->num_output_glyphs (); i++) { hb_codepoint_t glyph; if (!plan->old_gid_for_new_gid (i, &glyph)) continue; + const hb_ubytes_t str = (*acc.charStrings)[glyph]; unsigned int fd = acc.fdSelect->get_fd (glyph); if (unlikely (fd >= acc.fdCount)) return false; + if (plan->accelerator && plan->accelerator->cff_accelerator) + { + // parsed string already exists in accelerator, copy it and move + // on. + // TODO(grieger): use reference instead of copy. + parsed_charstrings[i] = + plan->accelerator->cff_accelerator->parsed_charstrings[glyph]; + continue; + } + ENV env (str, acc, fd); cs_interpreter_t interp (env); @@ -662,22 +683,13 @@ struct subr_subsetter_t /* after dropping hints recreate closures of actually used subrs */ closures.reset (); - for (unsigned int i = 0; i < plan->num_output_glyphs (); i++) - { - hb_codepoint_t glyph; - if (!plan->old_gid_for_new_gid (i, &glyph)) - continue; - unsigned int fd = acc.fdSelect->get_fd (glyph); - if (unlikely (fd >= acc.fdCount)) - return false; - subr_subset_param_t param (&parsed_charstrings[i], - &parsed_global_subrs, - &parsed_local_subrs[fd], - &closures.global_closure, - &closures.local_closures[fd], - plan->flags & HB_SUBSET_FLAGS_NO_HINTING); - collect_subr_refs_in_str (parsed_charstrings[i], param); - } + if (!closure_subroutines()) return false; + } else if (plan->accelerator && plan->accelerator->cff_accelerator) { + // Since parsed strings were loaded from accelerator, we still need + // to compute the subroutine closures which would have normally happened during + // parsing. + closures.reset (); + if (!closure_subroutines()) return false; } remaps.create (closures); @@ -866,6 +878,29 @@ struct subr_subsetter_t return seen_hint; } + bool closure_subroutines () + { + closures.reset (); + for (unsigned int i = 0; i < plan->num_output_glyphs (); i++) + { + hb_codepoint_t glyph; + if (!plan->old_gid_for_new_gid (i, &glyph)) + continue; + unsigned int fd = acc.fdSelect->get_fd (glyph); + if (unlikely (fd >= acc.fdCount)) + return false; + subr_subset_param_t param (&parsed_charstrings[i], + &parsed_global_subrs, + &parsed_local_subrs[fd], + &closures.global_closure, + &closures.local_closures[fd], + plan->flags & HB_SUBSET_FLAGS_NO_HINTING); + collect_subr_refs_in_str (parsed_charstrings[i], param); + } + + return true; + } + void collect_subr_refs_in_subr (parsed_cs_str_t &str, unsigned int pos, unsigned int subr_num, parsed_cs_str_vec_t &subrs, hb_set_t *closure,