From 2be60ac235d6d02b5b0a7ab3302712fc2aa0f030 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 18 Apr 2026 04:26:07 -0600 Subject: [PATCH] [vector] Strip paint SVG dedup, simplify paint _glyph_impl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove content-based color glyph dedup from paint — no more / for color glyphs. Each glyph's paint output is emitted inline, wrapped by push_transform/pop_transform from the paint funcs (which emit for SVG and q cm ... Q for PDF). _glyph_impl is now format-agnostic: extend extents, push_transform, paint, pop_transform. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-paint.cc | 144 +++--------------- src/hb-vector-paint.hh | 2 - .../expected/basic/handwriting-cff-colr-1.svg | 7 +- .../basic/handwriting-glyf-colr-1.svg | 7 +- .../data/expected/basic/handwriting-sbix.svg | 7 +- 5 files changed, 29 insertions(+), 138 deletions(-) diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index e67073fa7..a16682490 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -563,133 +563,33 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, if (unlikely (!paint->ensure_initialized ())) return false; + hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint); switch (paint->format) { - case HB_VECTOR_FORMAT_PDF: - { - /* PDF: emit transform + paint directly, no caching. - * Paint callbacks emit in output-space (divided by - * scale_factor), so the per-glyph cm just positions - * with a translation. */ - auto &body = paint->current_body (); - float sx = paint->x_scale_factor; - float sy = paint->y_scale_factor; - unsigned sprec = body.scale_precision (); - body.append_str ("q\n"); - body.append_num (xx, sprec); - body.append_c (' '); - body.append_num (yx, sprec); - body.append_c (' '); - body.append_num (xy, sprec); - body.append_c (' '); - body.append_num (yy, sprec); - body.append_c (' '); - body.append_num (tx / sx); - body.append_c (' '); - body.append_num (ty / sy); - body.append_str (" cm\n"); - - hb_bool_t ret = true; - if (fallible) - ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_pdf_funcs_get (), paint, - (unsigned) paint->palette, - paint->foreground); - else - hb_font_paint_glyph (font, glyph, - hb_vector_paint_pdf_funcs_get (), paint, - (unsigned) paint->palette, - paint->foreground); - body.append_str ("Q\n"); - return ret; - } - case HB_VECTOR_FORMAT_SVG: - { - { - if (unlikely (!paint->group_stack.push_or_fail (hb_vector_buf_t {}))) - return false; - - hb_bool_t ret = true; - if (fallible) - ret = hb_font_paint_glyph_or_fail (font, glyph, - hb_vector_paint_get_funcs (paint), paint, - (unsigned) paint->palette, - paint->foreground); - else - hb_font_paint_glyph (font, glyph, - hb_vector_paint_get_funcs (paint), paint, - (unsigned) paint->palette, - paint->foreground); - if (unlikely (!ret)) - { - paint->group_stack.pop (); - return false; - } - - paint->captured_scratch = paint->group_stack.pop (); - if (unlikely (paint->captured_scratch.in_error () || - !paint->captured_scratch.length)) - return false; - - /* Content-based dedup: hash the captured paint output, - * look for an existing def with identical bytes. */ - uint32_t h = hb_hash (hb_bytes_t (paint->captured_scratch.arrayZ, - paint->captured_scratch.length)); - unsigned def_id = (unsigned) -1; - for (auto &e : paint->defined_color_glyph_entries) - { - if (e.hash == h && - e.defs_length == paint->captured_scratch.length && - 0 == hb_memcmp (paint->defs.arrayZ + e.defs_offset, - paint->captured_scratch.arrayZ, - paint->captured_scratch.length)) - { - def_id = e.def_id; - break; - } - } - if (def_id == (unsigned) -1) - { - def_id = paint->color_glyph_counter++; - - paint->defs.append_str ("defs.append_len (paint->id_prefix.arrayZ, paint->id_prefix.length); - paint->defs.append_str ("cg"); - paint->defs.append_unsigned (def_id); - paint->defs.append_str ("\">\n"); - unsigned data_offset = paint->defs.length; - paint->defs.append_len ( - paint->captured_scratch.arrayZ, - paint->captured_scratch.length); - 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->captured_scratch.length; - entry.def_id = def_id; - paint->defined_color_glyph_entries.push (entry); - } - - auto &body = paint->current_body (); - body.append_str ("id_prefix.arrayZ, paint->id_prefix.length); - body.append_str ("cg"); - body.append_unsigned (def_id); - body.append_str ("\" transform=\""); - hb_vector_svg_append_instance_transform (&body, paint->get_precision (), - paint->x_scale_factor, - paint->y_scale_factor, - xx, yx, xy, yy, tx, ty); - body.append_str ("\"/>\n"); - return !paint->defs.in_error () && !body.in_error (); - } - } - + hb_paint_push_transform (funcs, paint, xx, yx, -xy, -yy, tx, -ty); + break; + case HB_VECTOR_FORMAT_PDF: + hb_paint_push_transform (funcs, paint, xx, yx, xy, yy, tx, ty); + break; case HB_VECTOR_FORMAT_INVALID: default: return false; } + + hb_bool_t ret = true; + if (fallible) + ret = hb_font_paint_glyph_or_fail (font, glyph, + funcs, paint, + (unsigned) paint->palette, + paint->foreground); + else + hb_font_paint_glyph (font, glyph, + funcs, paint, + (unsigned) paint->palette, + paint->foreground); + + hb_paint_pop_transform (funcs, paint); + return ret; } /** @@ -851,12 +751,10 @@ hb_vector_paint_clear (hb_vector_paint_t *paint) paint->clip_rect_counter = 0; paint->clip_path_counter = 0; paint->gradient_counter = 0; - paint->color_glyph_counter = 0; paint->color_glyph_depth = 0; paint->defined_paths.clear (); paint->path_def_count = 0; hb_set_clear (paint->active_color_glyphs); - paint->defined_color_glyph_entries.clear (); paint->color_stops_scratch.clear (); paint->captured_scratch.clear (); } diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index d509a94b5..98bf26b52 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -40,7 +40,6 @@ struct hb_vector_paint_t unsigned clip_path_counter = 0; hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f}; unsigned gradient_counter = 0; - unsigned color_glyph_counter = 0; unsigned color_glyph_depth = 0; struct content_entry_t { @@ -52,7 +51,6 @@ struct hb_vector_paint_t hb_vector_t defined_paths; unsigned path_def_count = 0; hb_set_t *active_color_glyphs = nullptr; - hb_vector_t defined_color_glyph_entries; hb_vector_t color_stops_scratch; hb_vector_buf_t captured_scratch; hb_blob_t *recycled_blob = nullptr; 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 3c26d89f3..dcb48cd54 100644 --- a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -29,7 +29,9 @@ - + + + @@ -102,7 +104,4 @@ - - - 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 281928465..e06385caa 100644 --- a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -29,7 +29,9 @@ - + + + @@ -102,7 +104,4 @@ - - - diff --git a/test/vector/data/expected/basic/handwriting-sbix.svg b/test/vector/data/expected/basic/handwriting-sbix.svg index 8e31dd7cf..8ad27b6a2 100644 --- a/test/vector/data/expected/basic/handwriting-sbix.svg +++ b/test/vector/data/expected/basic/handwriting-sbix.svg @@ -1,11 +1,8 @@ - - + + - - -