[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
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-27 18:05:47 -06:00
1 parent b6bc4e1650
commit 2c0df50ce1
7 files changed
+69 -7

No files matched your search

+1 -1
View File
@@ -90,7 +90,7 @@ struct cff1_cs_opset_t : cs_opset_t<number_t, OPSET, cff1_cs_interp_env_t, PARAM
{
switch (op) {
case OpCode_dotsection:
SUPER::flush_args_and_op (op, env, param);
OPSET::flush_op (op, env, param);
break;
case OpCode_endchar:
+33 -4
View File
@@ -602,8 +602,37 @@ struct cff1_cs_opset_seac_t : cff1_cs_opset_t<cff1_cs_opset_seac_t, get_seac_par
};
static bool
_cff1_charstring_may_end_in_seac (const hb_ubytes_t &str)
_cff1_subrs_may_end_in_seac (const CFF1Subrs &subrs)
{
for (unsigned int i = 0; i < subrs.count; i++)
for (const auto byte : subrs[i])
if (byte == OpCode_endchar)
return true;
return false;
}
OT::cff1::accelerator_subset_t::accelerator_subset_t (hb_face_t *face) : SUPER (face)
{
if (unlikely (!is_valid ())) return;
subrs_may_end_in_seac = _cff1_subrs_may_end_in_seac (*globalSubrs);
for (unsigned int fd = 0; !subrs_may_end_in_seac && fd < fdCount; fd++)
subrs_may_end_in_seac =
_cff1_subrs_may_end_in_seac (*privateDicts[fd].localSubrs);
}
static bool
_cff1_charstring_may_end_in_seac (const hb_ubytes_t &str,
bool subrs_may_end_in_seac)
{
if (subrs_may_end_in_seac)
/* A subroutine can terminate the glyph with endchar, even when the glyph
* has no endchar of its own or has unreachable bytes after the call.
* False positives from operands and masks are safe. */
for (const auto byte : str)
if (byte == OpCode_callsubr || byte == OpCode_callgsubr)
return true;
if (unlikely (!str.length || str[str.length - 1] != OpCode_endchar))
return false;
@@ -625,8 +654,8 @@ _cff1_charstring_may_end_in_seac (const hb_ubytes_t &str)
if (end >= 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<cff1_cs_opset_seac_t, get_seac_param_t> interp (env);
get_seac_param_t param (this);
+2 -1
View File
@@ -1517,7 +1517,7 @@ struct cff1
struct accelerator_subset_t : accelerator_templ_t<cff1_private_dict_opset_subset_t, cff1_private_dict_values_subset_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<cff1_private_dict_opset_subset_t, cff1_private_dict_values_subset_t> SUPER;
};
+7 -1
View File
@@ -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 ())
Binary file not shown.
Binary file not shown.
+26
View File
@@ -268,6 +268,31 @@ test_subset_cff1_seac (void)
hb_face_destroy (face);
}
static void
test_subset_cff1_seac_precheck (void)
{
const char *fonts[] = {
"fonts/cff1_seac_dotsection.otf",
"fonts/cff1_seac_subr_endchar.otf",
};
for (unsigned int i = 0; i < G_N_ELEMENTS (fonts); i++)
{
hb_face_t *face = hb_test_open_font_file (fonts[i]);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 0xC1); /* Aacute */
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
/* .notdef, Aacute and both seac components. */
g_assert_cmpuint (hb_face_get_glyph_count (subset), ==, 4);
hb_face_destroy (subset);
hb_face_destroy (face);
}
}
static void
test_subset_cff1_dotsection (void)
{
@@ -459,6 +484,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_cff1_j_desubr_strip_hints);
hb_test_add (test_subset_cff1_expert);
hb_test_add (test_subset_cff1_seac);
hb_test_add (test_subset_cff1_seac_precheck);
hb_test_add (test_subset_cff1_dotsection);
hb_test_add (test_subset_cff1_retaingids);
hb_test_add (test_subset_cff1_j_retaingids);