From 2c0df50ce1cefa1659a10e44ba02ef3692a3b36e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 27 Oct 2026 18:05:47 -0600 Subject: [PATCH] [subset] Make CFF1 seac precheck conservative (#6458) A subroutine can terminate a glyph with endchar, and dotsection preserves live operands. Account for both cases when deciding whether a CFF1 charstring can form a seac composite. Cache whether any subroutine may contain endchar so ordinary fonts keep the fast trailing-byte check. Skip component closure when all glyphs are already retained, except when computing reusable accelerator metadata. Add regressions for endchar in a local subroutine and operands preserved across dotsection. Assisted-by: OpenAI Codex --- src/hb-cff1-interp-cs.hh | 2 +- src/hb-ot-cff1-table.cc | 37 +++++++++++++++++++--- src/hb-ot-cff1-table.hh | 3 +- src/hb-subset-plan.cc | 8 ++++- test/api/fonts/cff1_seac_dotsection.otf | Bin 0 -> 728 bytes test/api/fonts/cff1_seac_subr_endchar.otf | Bin 0 -> 736 bytes test/api/test-subset-cff1.c | 26 +++++++++++++++ 7 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 test/api/fonts/cff1_seac_dotsection.otf create mode 100644 test/api/fonts/cff1_seac_subr_endchar.otf diff --git a/src/hb-cff1-interp-cs.hh b/src/hb-cff1-interp-cs.hh index d8868efa5..d67b50b87 100644 --- a/src/hb-cff1-interp-cs.hh +++ b/src/hb-cff1-interp-cs.hh @@ -90,7 +90,7 @@ struct cff1_cs_opset_t : cs_opset_t= 5 && str[end - 5] == OpCode_fixedcs) return true; - /* A subroutine can leave the seac operands on the shared stack. */ - return last == OpCode_callsubr || last == OpCode_callgsubr; + /* Escaped operators include stack-preserving dotsection. */ + return end >= 2 && str[end - 2] == OpCode_escape; } bool OT::cff1::accelerator_subset_t::get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const @@ -635,7 +664,7 @@ bool OT::cff1::accelerator_subset_t::get_seac_components (hb_codepoint_t glyph, unsigned int fd = fdSelect->get_fd (glyph); const hb_ubytes_t str = (*charStrings)[glyph]; - if (unlikely (!_cff1_charstring_may_end_in_seac (str))) return false; + if (unlikely (!_cff1_charstring_may_end_in_seac (str, subrs_may_end_in_seac))) return false; cff1_cs_interp_env_t env (str, *this, fd); cff1_cs_interpreter_t interp (env); get_seac_param_t param (this); diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index b20d5e588..6b77d2f7f 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -1517,7 +1517,7 @@ struct cff1 struct accelerator_subset_t : accelerator_templ_t { - accelerator_subset_t (hb_face_t *face) : SUPER (face) {} + HB_INTERNAL accelerator_subset_t (hb_face_t *face); ~accelerator_subset_t () { if (cff_accelerator) @@ -1530,6 +1530,7 @@ struct cff1 HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const; mutable CFF::cff_subset_accelerator_t* cff_accelerator = nullptr; + bool subrs_may_end_in_seac = false; typedef accelerator_templ_t SUPER; }; diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index e6b425b10..23b29e624 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -501,7 +501,13 @@ _populate_gids_to_retain (hb_subset_plan_t* plan, else plan->_glyphset.union_ (cur_glyphset); #ifndef HB_NO_SUBSET_CFF - if (!plan->accelerator || plan->accelerator->has_seac) + /* Component closure cannot add anything when all valid glyphs are already + * retained. A new accelerator still needs an accurate has_seac value for + * future partial subsets. */ + bool all_glyphs_retained = + cur_glyphset.get_population () == plan->source->get_num_glyphs (); + if ((!all_glyphs_retained || plan->attach_accelerator_data) && + (!plan->accelerator || plan->accelerator->has_seac)) { bool has_seac = false; if (cff->is_valid ()) diff --git a/test/api/fonts/cff1_seac_dotsection.otf b/test/api/fonts/cff1_seac_dotsection.otf new file mode 100644 index 0000000000000000000000000000000000000000..970d7cfd197e129dac51973e03d42af815811d25 GIT binary patch literal 728 zcmZ`$O-mb56g}_F#2P7yhNK26Ww1h5(jrAl5K63M*%Tw))*%jwkS}Ho1VnHp!a(&0 z_y3Po?EVjLm_wKpp-uv!(Gv%$VGLkrif!TMZl6BPo zz7NPX04L?j%avlG_zMsWv0ZK29bAb9;~T~sb;sUWKF$<~&$w6XB#a~3C%)!hsyFv~ zVnhTv;4#y*dmWe`6fxnRYS~R^@$z|<_iZ!ob=uw@bJ} z%Kn%dQsceH4j0%d6-#4}VTEr9I9;4Fs~>Qej4PDsW(< z3JWe#O`je-?4pepHZ|8pgEcPasIQ?EXVuNxn|MV(&82choZ})4?@{^hy`bg_; z<^N*>rhpJ)Udg4#NbPbgm#N(3shlhrz8?gM+w|wLl^KgUNFnks9=y?tx*pJK^db)M gm9vCQ&gN6Tp9}nia(UVS>loe@{3)CfE0QX=|ah= zr~U`M_NplK&_j=ZP|zklZNHi9qNsht%=_N=-kbN$ZnjX!B98YkF#2X~%{u&b z_6Lxw06t~&&lfT)nZJNwh+?U3xA0xm7@snJTXF2|`GeF7@n`O(3JGIh9ufcKo~_h( z+TxN3QpF>6yWN7hp@=^BP{Xb}>2J?UysyZ(-D-L})IpQeJS;tyqhEgfFte3hy@Fv} zsOQ