From 2f78333f1bf075f0e6662eaa74b8d92f7be64cac Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 16 Apr 2026 19:24:21 -0600 Subject: [PATCH] [vector-draw] Extract flush_path helper; apply foreground to all paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the path→body flush logic into draw->flush_path() and use it everywhere: per-glyph emission, pre-glyph flush, and render-time final flush. The helper applies the current foreground color to every flushed path — both glyph outlines and free-form draw ops (like show-extents overlays) inherit the foreground that was set at flush time. SVG: fill="rgb(...)" on the inside the Y-flip . PDF: rg operator before each fill. Regenerated vector test expected outputs. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-draw.cc | 54 ++++--------------------------------------- src/hb-vector-draw.hh | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index c80ba54f2..8af275661 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -607,23 +607,7 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, * glyph clobbers the scratch buffer. Wrap the SVG case in * the same scale(1,-1) Y-flip per-glyph applies, so * the free-form coords share the glyph's coord system. */ - if (draw->path.length) - { - switch (draw->format) - { - case HB_VECTOR_FORMAT_PDF: - hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "f\n"); - break; - case HB_VECTOR_FORMAT_SVG: - hb_buf_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "\"/>\n"); - break; - case HB_VECTOR_FORMAT_INVALID: default: break; - } - draw->path.clear (); - } + draw->flush_path (); switch (draw->format) { @@ -639,29 +623,13 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, if (!draw->path.length) return false; - /* Per-glyph fill color. */ - hb_buf_append_num (&draw->body, hb_color_get_red (draw->foreground) / 255.f, 4); - hb_buf_append_c (&draw->body, ' '); - hb_buf_append_num (&draw->body, hb_color_get_green (draw->foreground) / 255.f, 4); - hb_buf_append_c (&draw->body, ' '); - hb_buf_append_num (&draw->body, hb_color_get_blue (draw->foreground) / 255.f, 4); - hb_buf_append_str (&draw->body, " rg\n"); - - hb_buf_append_len (&draw->body, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "f\n"); - draw->path.clear (); + draw->flush_path (); return true; } case HB_VECTOR_FORMAT_SVG: { - if (draw->path.length) - { - hb_buf_append_str (&draw->body, "body, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&draw->body, "\"/>\n"); - draw->path.shrink (0); - } + draw->flush_path (); if (!hb_set_has (draw->defined_glyphs, glyph)) { @@ -944,16 +912,9 @@ hb_vector_draw_render_pdf (hb_vector_draw_t *draw) hb_buf_append_str (&stream, " re f\n"); } + draw->flush_path (); if (draw->body.length) hb_buf_append_len (&stream, draw->body.arrayZ, draw->body.length); - if (draw->path.length) - { - /* Free-form draw ops accumulated after the last glyph - * (e.g. extents-overlay rectangles) need their own fill - * appended to the stream. */ - hb_buf_append_len (&stream, draw->path.arrayZ, draw->path.length); - hb_buf_append_str (&stream, "f\n"); - } /* Build PDF objects, tracking byte offsets for xref. */ hb_vector_t out; @@ -1076,14 +1037,9 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) hb_buf_append_str (&out, "/>\n"); } + draw->flush_path (); if (draw->body.length) hb_buf_append_len (&out, draw->body.arrayZ, draw->body.length); - if (draw->path.length) - { - hb_buf_append_str (&out, "path.arrayZ, draw->path.length); - hb_buf_append_str (&out, "\"/>\n"); - } hb_buf_append_str (&out, "\n"); diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index d4c7e8b53..94ddebd76 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -54,6 +54,56 @@ struct hb_vector_draw_t } } + void flush_path () + { + if (!path.length) return; + switch (format) + { + case HB_VECTOR_FORMAT_PDF: + /* Per-glyph fill color. */ + hb_buf_append_num (&body, hb_color_get_red (foreground) / 255.f, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, hb_color_get_green (foreground) / 255.f, 4); + hb_buf_append_c (&body, ' '); + hb_buf_append_num (&body, hb_color_get_blue (foreground) / 255.f, 4); + hb_buf_append_str (&body, " rg\n"); + hb_buf_append_len (&body, path.arrayZ, path.length); + hb_buf_append_str (&body, "f\n"); + break; + case HB_VECTOR_FORMAT_SVG: + { + unsigned r = hb_color_get_red (foreground); + unsigned g = hb_color_get_green (foreground); + unsigned b = hb_color_get_blue (foreground); + unsigned a = hb_color_get_alpha (foreground); + hb_buf_append_str (&body, "" + "\n"); + break; + } + case HB_VECTOR_FORMAT_INVALID: default: break; + } + path.shrink (0); + } + void append_xy (float x, float y) { float tx, ty;