mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 05:51:05 +01:00
[subset] Reject CFF calls without an immediate subroutine number
Retained subroutine remapping assumes the number preceding a call is in the same program. Caller-supplied targets violate that assumption and can silently change glyph outlines when unused subroutines are removed. Enforce the existing support boundary in CFF1 and CFF2. Report plan failures as serialization errors, rather than silently dropping the CFF table. Explicit desubroutinization still handles the regression fonts and preserves both outlines. Validation: full Meson suite, 272 passed and one skipped. Assisted-by: OpenAI Codex
This commit is contained in:
1 parent
4fc369ca36
commit
dde7da182e
8 files changed
+185
-5
No files matched your search
@@ -441,8 +441,7 @@ struct parsed_cs_op_t : op_str_t
|
||||
bool is_hinting () const { return hinting_flag; }
|
||||
void set_hinting () { hinting_flag = true; }
|
||||
|
||||
/* The layout of this struct is designed to fit within the
|
||||
* padding of op_str_t! */
|
||||
/* Metadata for retained charstring tokens. */
|
||||
|
||||
protected:
|
||||
bool hinting_flag = false;
|
||||
@@ -467,6 +466,7 @@ struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
|
||||
void add_op (op_code_t op, const byte_str_ref_t& str_ref)
|
||||
{
|
||||
if (is_parsed ()) return;
|
||||
last_op_ = op;
|
||||
if (coalescing_)
|
||||
{
|
||||
/* Do not record individual tokens; only note their boundaries.
|
||||
@@ -482,10 +482,20 @@ struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
|
||||
SUPER::add_op (op, str_ref);
|
||||
}
|
||||
|
||||
bool has_immediate_subr_number () const
|
||||
{
|
||||
if (is_parsed ()) return true;
|
||||
op_code_t op = coalescing_ ? last_op_ :
|
||||
values.length ? values.tail ().op : OpCode_Invalid;
|
||||
return op == OpCode_shortint || op == OpCode_fixedcs ||
|
||||
(OpCode_OneByteIntFirst <= op && op <= OpCode_TwoByteNegInt3);
|
||||
}
|
||||
|
||||
void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num)
|
||||
{
|
||||
if (is_parsed ()) return;
|
||||
has_calls_ = true;
|
||||
last_op_ = op;
|
||||
|
||||
if (coalescing_)
|
||||
{
|
||||
@@ -601,6 +611,7 @@ struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
|
||||
* hint analysis. */
|
||||
bool coalescing_ : 1;
|
||||
/* End offsets of the last two tokens seen (coalescing mode). */
|
||||
op_code_t last_op_ = OpCode_Invalid;
|
||||
unsigned penultimate_end_ = 0;
|
||||
unsigned last_end_ = 0;
|
||||
op_code_t prefix_op_;
|
||||
@@ -825,8 +836,9 @@ struct subr_subsetter_t
|
||||
* Phase #3 walks charstrings/subroutines forward then backward (hence parsing required),
|
||||
* because we can't tell if a number belongs to a hint op until we see the first moveto.
|
||||
*
|
||||
* Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number
|
||||
* within the same charstring/subroutine, e.g., not split across a charstring and a subroutine.
|
||||
* Retained calls require an immediately preceding subroutine-number token
|
||||
* within the same program. Caller-supplied operands are rejected; use
|
||||
* desubroutinization for those programs.
|
||||
*/
|
||||
bool subset (void)
|
||||
{
|
||||
|
||||
@@ -348,6 +348,11 @@ struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t
|
||||
cff1_cs_interp_env_t &env, subr_subset_param_t& param,
|
||||
cff1_biased_subrs_t& subrs, hb_set_t *closure)
|
||||
{
|
||||
if (unlikely (!param.current_parsed_str->has_immediate_subr_number ()))
|
||||
{
|
||||
env.set_error ();
|
||||
return;
|
||||
}
|
||||
byte_str_ref_t str_ref = env.str_ref;
|
||||
env.call_subr (subrs, type);
|
||||
param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
|
||||
@@ -1009,6 +1014,7 @@ OT::cff1::accelerator_subset_t::subset (hb_subset_context_t *c) const
|
||||
if (unlikely (!cff_plan.create (*this, c->plan)))
|
||||
{
|
||||
DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
|
||||
c->serializer->err (HB_SERIALIZE_ERROR_OTHER);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+10
-1
@@ -815,6 +815,11 @@ struct cff2_cs_opset_subr_subset_t : cff2_cs_opset_t<cff2_cs_opset_subr_subset_t
|
||||
cff2_cs_interp_env_t<blend_arg_t> &env, subr_subset_param_t& param,
|
||||
cff2_biased_subrs_t& subrs, hb_set_t *closure)
|
||||
{
|
||||
if (unlikely (!param.current_parsed_str->has_immediate_subr_number ()))
|
||||
{
|
||||
env.set_error ();
|
||||
return;
|
||||
}
|
||||
byte_str_ref_t str_ref = env.str_ref;
|
||||
env.call_subr (subrs, type);
|
||||
param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
|
||||
@@ -2017,7 +2022,11 @@ OT::cff2::accelerator_subset_t::subset (hb_subset_context_t *c) const
|
||||
{
|
||||
cff2_subset_plan cff2_plan;
|
||||
|
||||
if (unlikely (!cff2_plan.create (*this, c->plan))) return false;
|
||||
if (unlikely (!cff2_plan.create (*this, c->plan)))
|
||||
{
|
||||
c->serializer->err (HB_SERIALIZE_ERROR_OTHER);
|
||||
return false;
|
||||
}
|
||||
|
||||
// If instantiating (pinned) and downgrade flag is set, convert to CFF1
|
||||
if (cff2_plan.pinned && (c->plan->flags & HB_SUBSET_FLAGS_DOWNGRADE_CFF2))
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate small synthetic CFF regression fonts (requires fontTools)."""
|
||||
from pathlib import Path
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.misc.psCharStrings import T2CharString
|
||||
from fontTools.cffLib import SubrsIndex, FDSelect
|
||||
|
||||
OUT = Path(__file__).parent / "fonts"
|
||||
|
||||
|
||||
def builder():
|
||||
f = FontBuilder(1000, isTTF=False)
|
||||
f.font.recalcBBoxes = False
|
||||
f.font.recalcTimestamp = False
|
||||
f.font["head"].created = f.font["head"].modified = 2082844800
|
||||
gs = [".notdef", "A", "B"]
|
||||
f.setupGlyphOrder(gs)
|
||||
f.setupCharacterMap({65: "A", 66: "B"})
|
||||
f.setupHorizontalMetrics({g: (500, 0) for g in gs})
|
||||
f.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
f.setupNameTable({"familyName": "CFF regression", "styleName": "Regular"})
|
||||
f.setupOS2()
|
||||
f.setupPost()
|
||||
return f
|
||||
|
||||
|
||||
for cff2 in [False, True]:
|
||||
for kind in ["caller-dependent"]:
|
||||
f = builder()
|
||||
end = [] if cff2 else ["endchar"]
|
||||
ret = [] if cff2 else ["return"]
|
||||
# An unused first subroutine forces renumbering of caller-supplied IDs.
|
||||
if kind == "caller-dependent":
|
||||
progs = [
|
||||
ret,
|
||||
[10, 0, "rlineto"] + ret,
|
||||
[20, 0, "rlineto"] + ret,
|
||||
["callsubr"] + ret,
|
||||
]
|
||||
glyphs = {
|
||||
".notdef": end,
|
||||
"A": [0, 0, "rmoveto", -106, -104, "callsubr"] + end,
|
||||
"B": [0, 0, "rmoveto", -105, -104, "callsubr"] + end,
|
||||
}
|
||||
elif kind == "shared-hints":
|
||||
progs = [[0, 10] + ret]
|
||||
glyphs = {
|
||||
".notdef": end,
|
||||
"A": [-107, "callsubr", "hstem", 0, 0, "rmoveto", 10, 0, "rlineto"]
|
||||
+ end,
|
||||
"B": [-107, "callsubr", "rmoveto", 20, 0, "rlineto"] + end,
|
||||
}
|
||||
cs = {g: T2CharString(program=p) for g, p in glyphs.items()}
|
||||
if cff2:
|
||||
f.setupCFF2(cs)
|
||||
top = f.font["CFF2"].cff.topDictIndex[0]
|
||||
private = top.FDArray[0].Private
|
||||
else:
|
||||
f.setupCFF("CFFRegression", {}, cs, {})
|
||||
top = f.font["CFF "].cff.topDictIndex[0]
|
||||
private = top.Private
|
||||
private.Subrs = SubrsIndex()
|
||||
for p in progs:
|
||||
private.Subrs.append(
|
||||
T2CharString(program=p, private=private, globalSubrs=top.GlobalSubrs)
|
||||
)
|
||||
f.save(
|
||||
OUT
|
||||
/ (
|
||||
("cff2" if cff2 else "cff1")
|
||||
+ (
|
||||
"_caller_subr.otf"
|
||||
if kind == "caller-dependent"
|
||||
else "_shared_hint_operands.otf"
|
||||
)
|
||||
)
|
||||
)
|
||||
# Multiple blend groups accumulate fourteen live results, not 574 operands.
|
||||
f = builder()
|
||||
f.setupFvar([("wght", 100, 400, 900, "Weight")], [])
|
||||
f.setupCFF2(
|
||||
{
|
||||
g: T2CharString(
|
||||
program=[0, 0, "rmoveto", 10, 0, "rlineto"] if g != ".notdef" else []
|
||||
)
|
||||
for g in [".notdef", "A", "B"]
|
||||
},
|
||||
regions=[{"wght": (0, 1, 1)}] * 40,
|
||||
)
|
||||
private = f.font["CFF2"].cff.topDictIndex[0].FDArray[0].Private
|
||||
private.BlueValues = [[i] + [0] * 40 for i in range(14)]
|
||||
f.save(OUT / "cff2_long_private_blends.otf")
|
||||
@@ -412,11 +412,42 @@ test_subset_cff1_iftb_requirements (void)
|
||||
#endif
|
||||
|
||||
|
||||
/* Caller-dependent subroutine numbers cannot be remapped at a shared offset. */
|
||||
static void
|
||||
test_subset_cff1_caller_supplied_subr (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/cff1_caller_subr.otf");
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B');
|
||||
for (unsigned i = 0; i < 2; i++)
|
||||
{
|
||||
hb_subset_input_set_flags (input, i ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT);
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
g_assert_null (subset);
|
||||
}
|
||||
/* Explicit flattening does not remap caller-supplied operands. */
|
||||
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE);
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
g_assert_nonnull (subset);
|
||||
hb_font_t *font = hb_font_create (subset);
|
||||
hb_glyph_extents_t extents;
|
||||
g_assert_true (hb_font_get_glyph_extents (font, 1, &extents));
|
||||
g_assert_cmpint (extents.width, ==, 10);
|
||||
g_assert_true (hb_font_get_glyph_extents (font, 2, &extents));
|
||||
g_assert_cmpint (extents.width, ==, 20);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (subset);
|
||||
hb_subset_input_destroy (input);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
hb_test_init (&argc, &argv);
|
||||
|
||||
hb_test_add (test_subset_cff1_caller_supplied_subr);
|
||||
|
||||
hb_test_add (test_subset_cff1_noop);
|
||||
hb_test_add (test_subset_cff1);
|
||||
hb_test_add (test_subset_cff1_strip_hints);
|
||||
|
||||
@@ -188,6 +188,35 @@ test_subset_cff2_iftb_requirements (void)
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Caller-dependent subroutine numbers cannot be remapped at a shared offset. */
|
||||
static void
|
||||
test_subset_cff2_caller_supplied_subr (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/cff2_caller_subr.otf");
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B');
|
||||
for (unsigned i = 0; i < 2; i++)
|
||||
{
|
||||
hb_subset_input_set_flags (input, i ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT);
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
g_assert_null (subset);
|
||||
}
|
||||
/* Explicit flattening does not remap caller-supplied operands. */
|
||||
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE);
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
g_assert_nonnull (subset);
|
||||
hb_font_t *font = hb_font_create (subset);
|
||||
hb_glyph_extents_t extents;
|
||||
g_assert_true (hb_font_get_glyph_extents (font, 1, &extents));
|
||||
g_assert_cmpint (extents.width, ==, 10);
|
||||
g_assert_true (hb_font_get_glyph_extents (font, 2, &extents));
|
||||
g_assert_cmpint (extents.width, ==, 20);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (subset);
|
||||
hb_subset_input_destroy (input);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_cff2_long_private_blends (void)
|
||||
{
|
||||
@@ -226,6 +255,7 @@ main (int argc, char **argv)
|
||||
{
|
||||
hb_test_init (&argc, &argv);
|
||||
|
||||
hb_test_add (test_subset_cff2_caller_supplied_subr);
|
||||
hb_test_add (test_subset_cff2_long_private_blends);
|
||||
|
||||
hb_test_add (test_subset_cff2_noop);
|
||||
|
||||
Reference in new issue
Block a user