[subset] Reject unsupported symbolic CFF2 delta products (#6434)

Without coordinates, blend_arg_t stores only a flat delta vector.
A blend used as a later blend's delta loses its own deltas, changing
outlines during partial instancing or desubroutinization.

Reject that case in the usage and flatten passes. Retained subroutine
collection needs only blend identity, and full instancing evaluates
blends numerically, so keep both paths working. Mark CFF2 planning
failures as serialization errors so the subset API returns failure
instead of a face missing its outline table.

Add a generated two-axis font and exercise retained subsetting, full
instancing, rejected partial/desubroutinized products, and supported
nested defaults. The regression fails before the fix. Full Meson suite:
272 passed, one skipped.

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-22 13:04:52 -06:00
1 parent 51c6d7676c
commit 75d8ac5d23
5 files changed
+110 -1

No files matched your search

+19 -1
View File
@@ -204,6 +204,9 @@ struct cff2_cs_interp_env_t : cs_interp_env_t<ELEM, CFF2Subrs>
/* Subsetter instancing can defer rounding until absolute points are known. */
bool round_blends = true;
/* Some analysis passes need only blend presence, not symbolic deltas. */
bool preserve_blends = true;
protected:
const int *coords;
unsigned int num_coords;
@@ -222,6 +225,9 @@ struct cff2_cs_interp_env_t : cs_interp_env_t<ELEM, CFF2Subrs>
template <typename OPSET, typename PARAM, typename ELEM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t<ELEM>, PARAM>>
struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH>
{
/* Retained subroutine collection needs only blend identity. */
static constexpr bool preserve_blends = false;
static void process_op (op_code_t op, cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
{
switch (op) {
@@ -269,8 +275,20 @@ struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PAR
{
double v = arg.to_real () + env.blend_deltas (blends);
arg.set_real (env.round_blends ? round (v) : v);
return;
}
else if (unlikely (arg.blending ()))
/* Without coordinates, the flat delta vector cannot represent a
* product of region scalars. Do not discard a delta's own blend. */
if (OPSET::preserve_blends && env.preserve_blends)
for (const ELEM &delta : blends)
if (unlikely (delta.blending ()))
{
env.set_error ();
return;
}
if (unlikely (arg.blending ()))
{
/* A blend result used as an operand of a later blend: the value is
* default + inner deltas + outer deltas, so accumulate the deltas. */
+6
View File
@@ -329,6 +329,8 @@ struct cff2_usage_param_t
struct cff2_cs_opset_usage_t : cff2_cs_opset_t<cff2_cs_opset_usage_t, cff2_usage_param_t, blend_arg_t>
{
static constexpr bool preserve_blends = true;
static void flush_args_and_op (op_code_t op, cff2_cs_interp_env_t<blend_arg_t> &env, cff2_usage_param_t& param)
{
param.has_flex1 |= op == OpCode_flex1;
@@ -426,6 +428,8 @@ struct cff2_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<>
struct cff2_cs_opset_flatten_t : cff2_cs_opset_t<cff2_cs_opset_flatten_t, flatten_param_t, blend_arg_t>
{
static constexpr bool preserve_blends = true;
static void flush_args_and_op (op_code_t op, cff2_cs_interp_env_t<blend_arg_t> &env, flatten_param_t& param)
{
bool skip_command = false;
@@ -1224,6 +1228,7 @@ struct cff2_subset_plan
used_fds.add (fd);
cff2_cs_interp_env_t<blend_arg_t> env (str, acc, fd);
env.preserve_blends = partial_instancing;
cs_interpreter_t<cff2_cs_interp_env_t<blend_arg_t>, cff2_cs_opset_usage_t, cff2_usage_param_t> interp (env);
cff2_usage_param_t param;
param.instancing = partial_instancing ? &instancing : nullptr;
@@ -2024,6 +2029,7 @@ OT::cff2::accelerator_subset_t::subset (hb_subset_context_t *c) const
if (unlikely (!cff2_plan.create (*this, c->plan)))
{
/* A planning failure is not an intentionally empty outline table. */
c->serializer->err (HB_SERIALIZE_ERROR_OTHER);
return false;
}
Binary file not shown.
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Generate nested-default and nested-delta CFF2 regression charstrings."""
from pathlib import Path
from fontTools.fontBuilder import FontBuilder
from fontTools.misc.psCharStrings import T2CharString
builder = FontBuilder(1000, isTTF=False)
builder.font.recalcBBoxes = builder.font.recalcTimestamp = False
builder.font["head"].created = builder.font["head"].modified = 2082844800
glyphs = [".notdef", "linear", "product"]
builder.setupGlyphOrder(glyphs)
builder.setupCharacterMap({65: "linear", 66: "product"})
builder.setupHorizontalMetrics({glyph: (500, 0) for glyph in glyphs})
builder.setupHorizontalHeader(ascent=800, descent=-200)
builder.setupNameTable({"familyName": "Nested CFF2 delta", "styleName": "Regular"})
builder.setupOS2()
builder.setupPost()
builder.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], [])
move = [0, 0, "rmoveto"]
finish = ["hlineto", 100, "vlineto", -100, "hlineto", -100, "vlineto"]
programs = {
".notdef": [],
# 100 + 20*wght + 30*wdth: a nested default remains linear.
"linear": move + [100, 20, 0, 1, "blend", 0, 30, 1, "blend"] + finish,
# 100 + (10 + 20*wght)*wdth: a nested delta requires a product.
"product": move + [100, 0, 10, 20, 0, 1, "blend", 1, "blend"] + finish,
}
builder.setupCFF2(
{glyph: T2CharString(program=program) for glyph, program in programs.items()},
regions=[{"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}],
)
builder.save(Path(__file__).parent / "fonts" / "cff2-nested-delta.otf")
+52
View File
@@ -308,6 +308,57 @@ test_instance_cff2_absolute_rounding (void)
static void
test_instance_cff2_nested_delta (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/cff2-nested-delta.otf");
for (unsigned mode = 0; mode < 5; mode++)
{
g_test_message ("nested delta mode %u", mode);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, mode == 4 ? 65 : 66);
hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
hb_set_destroy (codepoints);
if (mode == 1 || mode == 2 || mode == 4)
g_assert_true (hb_subset_input_pin_axis_location (input, face,
HB_TAG ('w', 'g', 'h', 't'), 0.25f));
if (mode == 2)
g_assert_true (hb_subset_input_pin_axis_location (input, face,
HB_TAG ('w', 'd', 't', 'h'), 1));
if (mode == 3)
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE);
hb_face_t *subset = hb_subset_or_fail (face, input);
hb_subset_input_destroy (input);
if (mode == 1 || mode == 3)
{
/* The flat symbolic representation cannot retain a variable delta.
* Fail the operation instead of changing the outline or dropping CFF2. */
g_assert_null (subset);
continue;
}
g_assert_nonnull (subset);
hb_blob_t *cff2 = hb_face_reference_table (subset, HB_TAG ('C', 'F', 'F', '2'));
g_assert_cmpuint (hb_blob_get_length (cff2), >, 0);
hb_blob_destroy (cff2);
hb_font_t *font = hb_font_create (subset);
hb_variation_t variations[] = {
{HB_TAG ('w', 'g', 'h', 't'), 0.25f},
{HB_TAG ('w', 'd', 't', 'h'), 1}
};
hb_font_set_variations (font, variations, G_N_ELEMENTS (variations));
hb_codepoint_t glyph;
hb_glyph_extents_t extents;
g_assert_true (hb_font_get_nominal_glyph (font, mode == 4 ? 65 : 66, &glyph));
g_assert_true (hb_font_get_glyph_extents (font, glyph, &extents));
g_assert_cmpint (extents.width, ==, mode == 4 ? 135 : 115);
hb_font_destroy (font);
hb_face_destroy (subset);
}
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
@@ -318,6 +369,7 @@ main (int argc, char **argv)
hb_test_add (test_instance_cff2_empty_bounds);
hb_test_add (test_instance_cff2_large_store);
hb_test_add (test_instance_cff2_absolute_rounding);
hb_test_add (test_instance_cff2_nested_delta);
return hb_test_run();
}