From e134bb760e41bd4559cd91a7678cd09a31995286 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 18 Apr 2026 13:07:05 -0600 Subject: [PATCH] [vector] Strip clip-glyph dedup in SVG paint push_clip_glyph kept a hash + linear-scan cache of emitted clip paths to dedup repeated clip-glyph uses within a single COLRv1 paint tree. With paint contexts being one-glyph per use, the cache mostly churned; it also made hb_memcmp trip on sanitizer-null when an empty path matched an equally-empty existing entry (fuzzer-reached null-deref). Drop the cache: always emit a fresh + pair per push_clip_glyph. Removes defined_paths / content_entry_t from the paint struct. Regenerate the two handwriting COLR expected SVGs whose clip-reuse now produces distinct path defs. Assisted-by: Claude Opus 4.7 (1M context) --- src/hb-vector-paint-svg.cc | 58 ++++++------------- src/hb-vector-paint.cc | 1 - src/hb-vector-paint.hh | 8 --- .../expected/basic/handwriting-cff-colr-1.svg | 20 ++++--- .../basic/handwriting-glyf-colr-1.svg | 20 ++++--- 5 files changed, 39 insertions(+), 68 deletions(-) diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 6d2a41e9b..54b2c73aa 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -450,47 +450,23 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); } - uint32_t h = hb_hash (hb_bytes_t (paint->path.arrayZ, paint->path.length)); - unsigned def_id = (unsigned) -1; - for (auto &e : paint->defined_paths) - { - if (e.hash == h && - e.defs_length == paint->path.length && - 0 == hb_memcmp (paint->defs.arrayZ + e.defs_offset, - paint->path.arrayZ, paint->path.length)) - { - def_id = e.def_id; - break; - } - } - if (def_id == (unsigned) -1) - { - def_id = paint->path_def_count++; - paint->defs.append_str ("defs.append_len (pfx, pfx_len); - paint->defs.append_c ('p'); - paint->defs.append_unsigned (def_id); - paint->defs.append_str ("\" d=\""); - unsigned data_offset = paint->defs.length; - paint->defs.append_len (paint->path.arrayZ, paint->path.length); - paint->defs.append_str ("\"/>\n"); - paint->defs.append_str ("defs.append_len (pfx, pfx_len); - paint->defs.append_str ("clip-p"); - paint->defs.append_unsigned (def_id); - paint->defs.append_str ("\">defs.append_len (pfx, pfx_len); - paint->defs.append_c ('p'); - paint->defs.append_unsigned (def_id); - paint->defs.append_str ("\"/>\n"); - - hb_vector_paint_t::content_entry_t entry; - entry.hash = h; - entry.defs_offset = data_offset; - entry.defs_length = paint->path.length; - entry.def_id = def_id; - paint->defined_paths.push (entry); - } + unsigned def_id = paint->path_def_count++; + paint->defs.append_str ("defs.append_len (pfx, pfx_len); + paint->defs.append_c ('p'); + paint->defs.append_unsigned (def_id); + paint->defs.append_str ("\" d=\""); + paint->defs.append_len (paint->path.arrayZ, paint->path.length); + paint->defs.append_str ("\"/>\n"); + paint->defs.append_str ("defs.append_len (pfx, pfx_len); + paint->defs.append_str ("clip-p"); + paint->defs.append_unsigned (def_id); + paint->defs.append_str ("\">defs.append_len (pfx, pfx_len); + paint->defs.append_c ('p'); + paint->defs.append_unsigned (def_id); + paint->defs.append_str ("\"/>\n"); paint->current_body ().append_str ("current_body ().append_len (pfx, pfx_len); diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 076be997d..1756c8bdf 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -746,7 +746,6 @@ hb_vector_paint_clear (hb_vector_paint_t *paint) paint->clip_path_counter = 0; paint->gradient_counter = 0; paint->color_glyph_depth = 0; - paint->defined_paths.clear (); paint->path_def_count = 0; hb_set_clear (paint->active_color_glyphs); paint->color_stops_scratch.clear (); diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 98bf26b52..0fed3ca48 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -41,14 +41,6 @@ struct hb_vector_paint_t hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f}; unsigned gradient_counter = 0; unsigned color_glyph_depth = 0; - struct content_entry_t - { - uint32_t hash; - unsigned defs_offset; - unsigned defs_length; - unsigned def_id; - }; - hb_vector_t defined_paths; unsigned path_def_count = 0; hb_set_t *active_color_glyphs = nullptr; hb_vector_t color_stops_scratch; diff --git a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg index be93f8d28..d930dc0bd 100644 --- a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -15,20 +15,22 @@ + + - - - + + + - - + + @@ -72,21 +74,21 @@ - + - + - + @@ -95,7 +97,7 @@ - + diff --git a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg index f99dba128..2b2ded383 100644 --- a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -15,20 +15,22 @@ + + - - - + + + - - + + @@ -72,21 +74,21 @@ - + - + - + @@ -95,7 +97,7 @@ - +