From 5c3df83c888b09d20ab001752a482a1ee37620da Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Aug 2026 14:23:30 -0600 Subject: [PATCH] [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 --- src/hb-limits.hh | 8 ++++++++ src/hb-vector-paint-pdf.cc | 16 +++++++++++----- src/hb-vector-paint-svg.cc | 16 +++++++++++++--- src/hb-vector-paint.cc | 1 + src/hb-vector-paint.hh | 6 ++++++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 2d4eb0246..ec4ca8c33 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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 diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index a9f365777..5b2f0326b 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -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 (); } diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 7cca077bb..336a670cf 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -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 ("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++; diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 5d66c2ac6..92a1de35a 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -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; diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 1bc7d8776..28b0b62b2 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -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;