[vector] Add cumulative work budget to paint sessions

The SVG and PDF paint backends extract a fresh glyph outline via
hb_font_draw_glyph() for every fill-glyph and clip-glyph paint node,
each with a fresh per-glyph budget (glyf points / CFF interpreter
ops), and append the formatted path data to the output document.
The COLR paint-graph traversal limits multiply with those per-glyph
limits, and the generated path data grows unboundedly.

Add a work_left counter to hb_vector_paint_t, reset per session by
hb_vector_paint_clear() to the new HB_VECTOR_MAX_PAINT_WORK limit
and charged with the bytes of path data generated by each outline
extraction.  Once exhausted, further extractions are skipped: an
empty path (or empty clip path) keeps the document structure intact.
This mirrors the session budgets in the raster and GPU paint
backends.

Full test suite passes; normal SVG output is unaffected.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-10 14:23:30 -06:00
1 parent 1b56d42437
commit 5c3df83c88
5 files changed
+39 -8

No files matched your search

+8
View File
@@ -139,6 +139,14 @@
#define HB_SVG_MAX_DOCUMENT_SIZE ((size_t) 16 << 20)
#endif
/* Cumulative work budget for one vector (SVG/PDF) paint session, in
* bytes of generated outline path data. Bounds the product of the
* paint-graph traversal limits (COLR) and per-glyph outline work.
* Once exhausted, further glyph-outline extractions are skipped. */
#ifndef HB_VECTOR_MAX_PAINT_WORK
#define HB_VECTOR_MAX_PAINT_WORK ((int64_t) 16 << 20)
#endif
#ifndef HB_RASTER_MAX_BUFFER_SIZE
#define HB_RASTER_MAX_BUFFER_SIZE ((size_t) 1 << 30)
#endif
+11 -5
View File
@@ -339,12 +339,18 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint,
hb_codepoint_t glyph,
hb_vector_buf_t *buf)
{
hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (),
paint->x_scale_factor, paint->y_scale_factor};
paint->path.clear ();
hb_font_draw_glyph (font, glyph,
hb_vector_pdf_path_draw_funcs_get (),
&sink);
/* Skip the outline extraction when the session work budget is
* spent; an empty path keeps the document structure intact. */
if (likely (paint->work_left > 0))
{
hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (),
paint->x_scale_factor, paint->y_scale_factor};
hb_font_draw_glyph (font, glyph,
hb_vector_pdf_path_draw_funcs_get (),
&sink);
paint->work_left -= paint->path.length;
}
buf->append_len (paint->path.arrayZ, paint->path.length);
paint->path.clear ();
}
+13 -3
View File
@@ -429,9 +429,15 @@ hb_vector_paint_fill_glyph (hb_paint_funcs_t *,
return;
paint->path.clear ();
hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (),
paint->x_scale_factor, paint->y_scale_factor};
hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink);
/* Skip the outline extraction when the session work budget is
* spent; an empty path keeps the document structure intact. */
if (likely (paint->work_left > 0))
{
hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (),
paint->x_scale_factor, paint->y_scale_factor};
hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink);
paint->work_left -= paint->path.length;
}
auto &body = paint->current_body ();
body.append_str ("<path d=\"");
@@ -456,10 +462,14 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *,
unsigned pfx_len = paint->id_prefix_length;
paint->path.clear ();
/* Skip the outline extraction when the session work budget is
* spent; an empty clip path clips everything out. */
if (likely (paint->work_left > 0))
{
hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (),
paint->x_scale_factor, paint->y_scale_factor};
hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink);
paint->work_left -= paint->path.length;
}
unsigned def_id = paint->path_def_count++;
+1
View File
@@ -744,6 +744,7 @@ hb_vector_paint_clear (hb_vector_paint_t *paint)
paint->transform_group_overflow_depth = 0;
paint->clip_rect_counter = 0;
paint->clip_path_counter = 0;
paint->work_left = HB_VECTOR_MAX_PAINT_WORK;
paint->gradient_counter = 0;
paint->color_glyph_depth = 0;
paint->path_def_count = 0;
+6
View File
@@ -65,6 +65,12 @@ struct hb_vector_paint_t
unsigned clip_rect_counter = 0;
unsigned clip_path_counter = 0;
/* Cumulative work budget for the current paint session; reset by
* hb_vector_paint_clear(). Charged with the bytes of path data
* generated by each glyph-outline extraction, so per-glyph outline
* limits cannot multiply with the paint-graph traversal limits of
* the font tables driving us (e.g. COLR). */
int64_t work_left = HB_VECTOR_MAX_PAINT_WORK;
hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f};
unsigned gradient_counter = 0;
unsigned color_glyph_depth = 0;