[subset] Share one CFF charstring budget across a whole subset

Each CFF / CFF2 charstring pass of the subsetter (seac closure, CFF2
extents for instancing, CFF2 usage and instancing passes, subroutine
subsetting with hint dropping, and flattening) created its own budget
with CFF::get_subset_budget(), so one subset could spend that budget
several times over, and the two CFF2 instancing passes reset it
between them.

Keep the budget on hb_subset_plan_t instead: cff_budget() sizes it on
first use from the table, as before, and every pass draws on the same
counter, so the total work of one subset is bounded once.  Retries of
a table with a larger serialize buffer draw on it too.  cff1 depend()
is not part of a subset plan and keeps its own budget per call.

Real fonts use about 1.5 operations per table byte per pass, against
a budget of 32 per byte (at least 2^24).  Tested: full meson test and
the subset suite; hb-subset on SourceSerif4Variable-Roman.otf (CFF2),
Roboto-Regular.otf (CFF), NotoSansCJKhk-VF.otf (30 MB CFF2) with
--gids='*' and --unicodes='*', with and without --no-hinting,
--desubroutinize and partial / full instancing, and on every .otf in
/Library/Fonts with --gids='*', all succeed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 5.5 committed 2026-11-10 19:25:04 -06:00
1 parent 2f641844f2
commit 8d112d5b99
7 files changed
+37 -21

No files matched your search

+6 -5
View File
@@ -259,11 +259,12 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
#define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28)
#endif
/* The flat floor for one pass of the subsetter over the charstrings of
* a CFF or CFF2 table, in operators and operands interpreted. Glyphs
* can share subroutines, so neither HB_CFF_MAX_OPS per glyph nor the
* table size bounds that work. Running out fails the charstring being
* interpreted, as exceeding HB_CFF_MAX_OPS does. */
/* The flat floor for the subsetter's work on the charstrings of a CFF or
* CFF2 table, in operators and operands interpreted, shared by all passes
* of one subset over them. Glyphs can share subroutines, so neither
* HB_CFF_MAX_OPS per glyph nor the table size bounds that work. Running
* out fails the charstring being interpreted, as exceeding HB_CFF_MAX_OPS
* does. */
#ifndef HB_BUDGET_SUBSET_CFF
#define HB_BUDGET_SUBSET_CFF ((int64_t) 1 << 24)
#endif
+1 -1
View File
@@ -56,7 +56,7 @@ static inline const Type& StructAtOffsetOrNull (const void *P, int offset, hb_sa
return obj;
}
/* Work budget of one pass over the charstrings of the table in blob. */
/* Charstring work budget of one subset of the table in blob. */
static inline int64_t
get_subset_budget (const hb_blob_t *blob)
{
+5 -5
View File
@@ -416,7 +416,7 @@ struct subr_flattener_t
unsigned count = plan->num_output_glyphs ();
if (!flat_charstrings.resize_exact (count))
return false;
int64_t budget = get_subset_budget (acc.get_blob ());
int64_t *budget = plan->cff_budget (get_subset_budget (acc.get_blob ()));
for (unsigned int i = 0; i < count; i++)
{
hb_codepoint_t glyph;
@@ -441,7 +441,7 @@ struct subr_flattener_t
plan,
command_capture ? &(*command_capture)[i] : nullptr
};
if (unlikely (!interp.interpret (param, &budget)))
if (unlikely (!interp.interpret (param, budget)))
return false;
}
return true;
@@ -936,7 +936,7 @@ struct subr_subsetter_t
bool coalesce = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING) &&
!plan->inprogress_accelerator;
int64_t budget = get_subset_budget (acc.get_blob ());
int64_t *budget = plan->cff_budget (get_subset_budget (acc.get_blob ()));
/* phase 1 & 2 */
for (auto _ : plan->new_to_old_gid_list)
@@ -973,7 +973,7 @@ struct subr_subsetter_t
plan->flags & HB_SUBSET_FLAGS_NO_HINTING,
coalesce);
if (unlikely (!interp.interpret (param, &budget)))
if (unlikely (!interp.interpret (param, budget)))
return false;
/* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
@@ -989,7 +989,7 @@ struct subr_subsetter_t
&closures.local_closures[fd],
plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
drop_hints_param_t drop (&budget);
drop_hints_param_t drop (budget);
if (drop_hints_in_str (parsed_charstrings[new_glyph], param, drop))
{
parsed_charstrings[new_glyph].set_hint_dropped ();
+3 -4
View File
@@ -1221,7 +1221,7 @@ struct cff2_subset_plan
hb_vector_t<bool> round_charstrings;
if (unlikely (!round_charstrings.resize (num_glyphs))) return false;
hb_set_t used_fds;
int64_t budget = CFF::get_subset_budget (acc.get_blob ());
int64_t *budget = plan->cff_budget (CFF::get_subset_budget (acc.get_blob ()));
for (unsigned int i = 0; i < num_glyphs; i++)
{
hb_codepoint_t glyph;
@@ -1239,7 +1239,7 @@ struct cff2_subset_plan
cff2_usage_param_t param;
param.instancing = partial_instancing ? &instancing : nullptr;
param.drop_hints = drop_hints;
if (unlikely (!interp.interpret (param, &budget)))
if (unlikely (!interp.interpret (param, budget)))
return false;
round_charstrings.arrayZ[i] = param.has_blend && !param.has_flex1;
}
@@ -1271,7 +1271,6 @@ struct cff2_subset_plan
return false;
if (capture_commands && !charstring_commands.resize_exact (num_glyphs))
return false;
budget = CFF::get_subset_budget (acc.get_blob ());
for (unsigned int i = 0; i < num_glyphs; i++)
{
hb_codepoint_t glyph;
@@ -1296,7 +1295,7 @@ struct cff2_subset_plan
cff2_cs_rounding_t rounding;
if (round_charstrings.arrayZ[i]) param.rounding = &rounding;
param.instancer = partial_instancing ? &instancing : nullptr;
if (unlikely (!interp.interpret (param, &budget)))
if (unlikely (!interp.interpret (param, budget)))
return false;
}
}
+3 -3
View File
@@ -732,14 +732,14 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
if (_vmtx.has_data () && _vmtx.var_table.get_length ())
vvar_store_cache = _vmtx.var_table->get_var_store ().create_cache ();
int64_t budget = CFF::get_subset_budget (cff2.get_blob ());
int64_t *budget = plan->cff_budget (CFF::get_subset_budget (cff2.get_blob ()));
for (auto p : *plan->glyph_map)
{
hb_codepoint_t old_gid = p.first;
hb_codepoint_t new_gid = p.second;
if (!cff2.get_extents_at (font, old_gid, &extents,
hb_array (font->coords, font->num_coords),
&budget))
budget))
continue;
bool has_bounds_info = true;
if (extents.x_bearing == 0 && extents.width == 0 &&
@@ -799,7 +799,7 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
plan->bounds_height_vec[new_gid] = hb_saturate_neg (extents.height);
}
}
plan->check_success (budget > 0);
plan->check_success (*budget > 0);
/* No glyph contributed bounds: do not serialize the accumulator's
* inverted sentinels into head (or a downgraded CFF FontBBox). */
if (plan->head_maxp_info.xMin > plan->head_maxp_info.xMax)
+3 -3
View File
@@ -533,11 +533,11 @@ _populate_gids_to_retain (hb_subset_plan_t* plan,
bool has_seac = false;
if (cff->is_valid ())
{
int64_t budget = CFF::get_subset_budget (cff->get_blob ());
int64_t *budget = plan->cff_budget (CFF::get_subset_budget (cff->get_blob ()));
for (hb_codepoint_t gid : cur_glyphset)
if (_add_cff_seac_components (*cff, gid, &plan->_glyphset, &budget))
if (_add_cff_seac_components (*cff, gid, &plan->_glyphset, budget))
has_seac = true;
plan->check_success (budget > 0);
plan->check_success (*budget > 0);
}
plan->has_seac = has_seac;
}
+16
View File
@@ -205,6 +205,10 @@ struct hb_subset_plan_t
// whether we're doing avar2 partial instancing
bool has_avar2;
// see cff_budget ()
mutable int64_t _cff_budget = 0;
mutable bool _cff_budget_set = false;
#define HB_SUBSET_PLAN_MEMBER(Type, Name) Type Name;
#include "hb-subset-plan-member-list.hh"
#undef HB_SUBSET_PLAN_MEMBER
@@ -260,6 +264,18 @@ struct hb_subset_plan_t
return successful;
}
/* Charstring work budget shared by every CFF / CFF2 pass of this
* subset; the first caller sizes it. */
int64_t *cff_budget (int64_t initial) const
{
if (!_cff_budget_set)
{
_cff_budget = initial;
_cff_budget_set = true;
}
return &_cff_budget;
}
/*
* The set of input glyph ids which will be retained in the subset.
* Does NOT include ids kept due to retain_gids. You probably want to use