From bc7e513b2dc57cb2e3ecd15d9829ca31b94df512 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 17 Apr 2026 01:07:45 -0600 Subject: [PATCH] [vector] Emit SVG coordinates in output-space MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scale all SVG paint and draw coordinates by 1/scale_factor so paths, clips, gradients, transforms, and images emit fractional pixel values instead of font-scale integers. The per-glyph transform now applies only a Y-flip (scale(1,-1)) instead of scale(1/sf,-1/sf), since the content is already in output-space. Revert the 1000000 fill rect bandaid back to 32767 — in output-space, 32767 pixels is always large enough. SVG paint callbacks: - push_clip_glyph: path sink divides by scale_factor - push_clip_rectangle: coords divided by sx/sy - push_transform: translation (dx,dy) divided by sx/sy - gradient coords: divided by sx/sy - sweep center: divided by sx/sy - image extents: divided by sx/sy SVG draw: - Glyph outline path sink divides by scale_factor Regenerated all vector test expected outputs. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-draw.cc | 3 +- src/hb-vector-internal.hh | 18 +++---- src/hb-vector-paint-pdf.cc | 6 +-- src/hb-vector-paint-svg.cc | 53 ++++++++++--------- src/hb-vector-paint.hh | 3 ++ test/vector/data/expected/basic/cff1.svg | 12 ++--- .../data/expected/basic/cff2-var-cntr.svg | 12 ++--- .../data/expected/basic/cff2-var-wght900.svg | 8 +-- test/vector/data/expected/basic/default.svg | 12 ++--- .../data/expected/basic/glyf-var-wght900.svg | 12 ++--- .../expected/basic/glyf-vvar-ttb-wght900.svg | 12 ++--- .../expected/basic/handwriting-cff-colr-1.svg | 44 +++++++-------- .../basic/handwriting-glyf-colr-1.svg | 44 +++++++-------- .../data/expected/basic/handwriting-sbix.svg | 4 +- .../basic/iran-nastaliq-y-offsets.svg | 28 +++++----- test/vector/data/expected/basic/multiline.svg | 12 ++--- test/vector/data/expected/basic/ttb.svg | 12 ++--- .../data/expected/basic/upem-default.svg | 12 ++--- .../expected/varc/conditional-default.svg | 4 +- .../expected/varc/conditional-wght840p3.svg | 4 +- test/vector/data/expected/varc/default.svg | 8 +-- test/vector/data/expected/varc/wght840p3.svg | 8 +-- 22 files changed, 165 insertions(+), 166 deletions(-) diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index cd3afc670..46f7657c6 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -557,7 +557,8 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, if (!hb_set_has (draw->defined_glyphs, glyph)) { draw->path.clear (); - hb_vector_path_sink_t sink = {&draw->path, draw->get_precision (), 1.f, 1.f}; + hb_vector_path_sink_t sink = {&draw->path, draw->get_precision (), + draw->x_scale_factor, draw->y_scale_factor}; hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); if (!draw->path.length) return false; diff --git a/src/hb-vector-internal.hh b/src/hb-vector-internal.hh index 2ff6c028f..66af4393f 100644 --- a/src/hb-vector-internal.hh +++ b/src/hb-vector-internal.hh @@ -210,31 +210,25 @@ hb_vector_svg_append_instance_transform (hb_vector_buf_t *out, float xy, float yy, float tx, float ty) { - unsigned sprec = out->scale_precision (); if (xx == 1.f && yx == 0.f && xy == 0.f && yy == 1.f) { - float sx = 1.f / x_scale_factor; - float sy = 1.f / y_scale_factor; out->append_str ("translate("); out->append_num (tx / x_scale_factor, precision); out->append_c (','); out->append_num (-ty / y_scale_factor, precision); - out->append_str (") scale("); - out->append_num (sx, sprec); - out->append_c (','); - out->append_num (-sy, sprec); - out->append_c (')'); + out->append_str (") scale(1,-1)"); } else { + unsigned sprec = out->scale_precision (); out->append_str ("matrix("); - out->append_num (xx / x_scale_factor, sprec); + out->append_num (xx, sprec); out->append_c (','); - out->append_num (yx / y_scale_factor, sprec); + out->append_num (yx, sprec); out->append_c (','); - out->append_num (-xy / x_scale_factor, sprec); + out->append_num (-xy, sprec); out->append_c (','); - out->append_num (-yy / y_scale_factor, sprec); + out->append_num (-yy, sprec); out->append_c (','); out->append_num (tx / x_scale_factor, precision); out->append_c (','); diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index 2b07d36b2..8e3b7a142 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -530,7 +530,7 @@ hb_pdf_paint_solid_color (hb_vector_paint_t *paint, hb_color_t c) body.append_str (" rg\n"); /* Paint a huge rect (will be clipped). */ - body.append_str ("-1000000 -1000000 2000000 2000000 re f\n"); + body.append_str ("-32767 -32767 65534 65534 re f\n"); } static void @@ -1233,7 +1233,7 @@ hb_pdf_add_sweep_patch (hb_vector_buf_t *mesh, float a0, hb_color_t c0_in, float a1, hb_color_t c1_in) { - const float R = 1000000.f; + const float R = 32767.f; const float eps = 0.5f; const float MAX_SECTOR = (float) M_PI / 2.f; @@ -1379,7 +1379,7 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, hb_paint_extend_t extend = hb_color_line_get_extend (color_line); - const float R = 1000000.f; + const float R = 32767.f; float xlo = cx - R - 1, xhi = cx + R + 1; float ylo = cy - R - 1, yhi = cy + R + 1; diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 1dacebc64..c822b4f87 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -431,7 +431,7 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, hb_bool_t opened = !(fabsf (xx - 1.f) < 1e-6f && fabsf (yx) < 1e-6f && fabsf (xy) < 1e-6f && fabsf (yy - 1.f) < 1e-6f && - fabsf (dx) < 1e-6f && fabsf (dy) < 1e-6f); + fabsf (paint->sx (dx)) < 1e-6f && fabsf (paint->sy (dy)) < 1e-6f); paint->transform_group_open_mask = (paint->transform_group_open_mask << 1) | (opened ? 1ull : 0ull); paint->transform_group_depth++; @@ -449,9 +449,9 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, body.append_c (','); body.append_num (yy, sprec); body.append_c (','); - body.append_num (dx); + body.append_num (paint->sx (dx)); body.append_c (','); - body.append_num (dy); + body.append_num (paint->sy (dy)); body.append_str (")\">\n"); } @@ -495,7 +495,8 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, { hb_set_add (paint->defined_outlines, glyph); paint->path.clear (); - hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (), 1.f, 1.f}; + 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->defs.append_str ("defs.append_len (pfx, pfx_len); @@ -546,13 +547,13 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, paint->defs.append_c ('c'); paint->defs.append_unsigned (clip_id); paint->defs.append_str ("\">defs.append_num (xmin); + paint->defs.append_num (paint->sx (xmin)); paint->defs.append_str ("\" y=\""); - paint->defs.append_num (ymin); + paint->defs.append_num (paint->sy (ymin)); paint->defs.append_str ("\" width=\""); - paint->defs.append_num (xmax - xmin); + paint->defs.append_num (paint->sx (xmax - xmin)); paint->defs.append_str ("\" height=\""); - paint->defs.append_num (ymax - ymin); + paint->defs.append_num (paint->sy (ymax - ymin)); paint->defs.append_str ("\"/>\n"); paint->current_body ().append_str ("foreground) * hb_color_get_alpha (color) / 255); auto &body = paint->current_body (); - body.append_str ("\n"); } @@ -679,13 +680,13 @@ hb_vector_paint_image (hb_paint_funcs_t *, return false; body.append_str ("x_bearing); + body.append_num (paint->sx ((float) extents->x_bearing)); body.append_c (','); - body.append_num ((float) extents->y_bearing); + body.append_num (paint->sy ((float) extents->y_bearing)); body.append_str (") scale("); - body.append_num ((float) extents->width / width); + body.append_num (paint->sx ((float) extents->width) / width); body.append_c (','); - body.append_num ((float) extents->height / height); + body.append_num (paint->sy ((float) extents->height) / height); body.append_str (")\">\n"); body.append_str ("defs.append_str ("gr"); paint->defs.append_unsigned (grad_id); paint->defs.append_str ("\" gradientUnits=\"userSpaceOnUse\" x1=\""); - paint->defs.append_num (gx0); + paint->defs.append_num (paint->sx (gx0)); paint->defs.append_str ("\" y1=\""); - paint->defs.append_num (gy0); + paint->defs.append_num (paint->sy (gy0)); paint->defs.append_str ("\" x2=\""); - paint->defs.append_num (gx1); + paint->defs.append_num (paint->sx (gx1)); paint->defs.append_str ("\" y2=\""); - paint->defs.append_num (gy1); + paint->defs.append_num (paint->sy (gy1)); paint->defs.append_str ("\" spreadMethod=\""); paint->defs.append_str (hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line))); paint->defs.append_str ("\">\n"); @@ -759,7 +760,7 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, paint->defs.append_str ("\n"); paint->current_body ().append_str ( - "current_body ().append_len (pfx, pfx_len); paint->current_body ().append_str ("gr"); paint->current_body ().append_unsigned (grad_id); @@ -803,19 +804,19 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, paint->defs.append_str ("gr"); paint->defs.append_unsigned (grad_id); paint->defs.append_str ("\" gradientUnits=\"userSpaceOnUse\" cx=\""); - paint->defs.append_num (gx1); + paint->defs.append_num (paint->sx (gx1)); paint->defs.append_str ("\" cy=\""); - paint->defs.append_num (gy1); + paint->defs.append_num (paint->sy (gy1)); paint->defs.append_str ("\" r=\""); - paint->defs.append_num (gr1); + paint->defs.append_num (paint->sx (gr1)); paint->defs.append_str ("\" fx=\""); - paint->defs.append_num (gx0); + paint->defs.append_num (paint->sx (gx0)); paint->defs.append_str ("\" fy=\""); - paint->defs.append_num (gy0); + paint->defs.append_num (paint->sy (gy0)); if (gr0 > 0) { paint->defs.append_str ("\" fr=\""); - paint->defs.append_num (gr0); + paint->defs.append_num (paint->sx (gr0)); } paint->defs.append_str ("\" spreadMethod=\""); paint->defs.append_str (hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line))); @@ -824,7 +825,7 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, paint->defs.append_str ("\n"); paint->current_body ().append_str ( - "current_body ().append_len (pfx, pfx_len); paint->current_body ().append_str ("gr"); paint->current_body ().append_unsigned (grad_id); @@ -856,7 +857,7 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, float ga1 = start_angle + mx * (end_angle - start_angle); hb_vector_svg_sweep_ctx_t ctx { - &paint->current_body (), paint->get_precision (), cx, cy, 1000000.f + &paint->current_body (), paint->get_precision (), paint->sx (cx), paint->sy (cy), 32767.f }; hb_paint_sweep_gradient_tiles (stops.arrayZ, stops.length, hb_color_line_get_extend (color_line), diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index db719012c..b314a45c0 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -86,6 +86,9 @@ struct hb_vector_paint_t hb_vector_buf_t ¤t_body () { return group_stack.tail (); } + float sx (float v) const { return v / x_scale_factor; } + float sy (float v) const { return v / y_scale_factor; } + void set_precision (unsigned p) { p = hb_min (p, 12u); diff --git a/test/vector/data/expected/basic/cff1.svg b/test/vector/data/expected/basic/cff1.svg index aa8de2cf9..53b463599 100644 --- a/test/vector/data/expected/basic/cff1.svg +++ b/test/vector/data/expected/basic/cff1.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/cff2-var-cntr.svg b/test/vector/data/expected/basic/cff2-var-cntr.svg index 68aa7b03a..a64f73d4b 100644 --- a/test/vector/data/expected/basic/cff2-var-cntr.svg +++ b/test/vector/data/expected/basic/cff2-var-cntr.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/cff2-var-wght900.svg b/test/vector/data/expected/basic/cff2-var-wght900.svg index dd2152a9b..65dd45784 100644 --- a/test/vector/data/expected/basic/cff2-var-wght900.svg +++ b/test/vector/data/expected/basic/cff2-var-wght900.svg @@ -1,9 +1,9 @@ - + - - - + + + diff --git a/test/vector/data/expected/basic/default.svg b/test/vector/data/expected/basic/default.svg index 62fa67f3f..fb3023f53 100644 --- a/test/vector/data/expected/basic/default.svg +++ b/test/vector/data/expected/basic/default.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/glyf-var-wght900.svg b/test/vector/data/expected/basic/glyf-var-wght900.svg index 98865fb44..f6aa8403a 100644 --- a/test/vector/data/expected/basic/glyf-var-wght900.svg +++ b/test/vector/data/expected/basic/glyf-var-wght900.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg index c15f69cf5..e378363c2 100644 --- a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg +++ b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + 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 d9af8f578..eceb49a12 100644 --- a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -1,33 +1,33 @@ - - + + - + - + - + - + - + - + - + - + - + - + @@ -36,7 +36,7 @@ - + @@ -44,42 +44,42 @@ - + - + - + - + - + - + @@ -87,7 +87,7 @@ - + @@ -95,7 +95,7 @@ - + @@ -104,5 +104,5 @@ - + 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 029ec3802..9f84e3ae3 100644 --- a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -1,33 +1,33 @@ - - + + - + - + - + - + - + - + - + - + - + - + @@ -36,7 +36,7 @@ - + @@ -44,42 +44,42 @@ - + - + - + - + - + - + @@ -87,7 +87,7 @@ - + @@ -95,7 +95,7 @@ - + @@ -104,5 +104,5 @@ - + diff --git a/test/vector/data/expected/basic/handwriting-sbix.svg b/test/vector/data/expected/basic/handwriting-sbix.svg index 82fd395a4..8e31dd7cf 100644 --- a/test/vector/data/expected/basic/handwriting-sbix.svg +++ b/test/vector/data/expected/basic/handwriting-sbix.svg @@ -1,11 +1,11 @@ - + - + diff --git a/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg b/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg index b3cb6582d..eb0f5fdf6 100644 --- a/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg +++ b/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg @@ -1,19 +1,19 @@ - - - - - - - + + + + + + + - - - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/multiline.svg b/test/vector/data/expected/basic/multiline.svg index 56d2c46e3..d7f581ce0 100644 --- a/test/vector/data/expected/basic/multiline.svg +++ b/test/vector/data/expected/basic/multiline.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/ttb.svg b/test/vector/data/expected/basic/ttb.svg index 6f80fa929..add4db4ed 100644 --- a/test/vector/data/expected/basic/ttb.svg +++ b/test/vector/data/expected/basic/ttb.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/basic/upem-default.svg b/test/vector/data/expected/basic/upem-default.svg index 62fa67f3f..fb3023f53 100644 --- a/test/vector/data/expected/basic/upem-default.svg +++ b/test/vector/data/expected/basic/upem-default.svg @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/test/vector/data/expected/varc/conditional-default.svg b/test/vector/data/expected/varc/conditional-default.svg index 54b4fac25..97807fb15 100644 --- a/test/vector/data/expected/varc/conditional-default.svg +++ b/test/vector/data/expected/varc/conditional-default.svg @@ -1,7 +1,7 @@ - + - + diff --git a/test/vector/data/expected/varc/conditional-wght840p3.svg b/test/vector/data/expected/varc/conditional-wght840p3.svg index 740faebd9..4142ff169 100644 --- a/test/vector/data/expected/varc/conditional-wght840p3.svg +++ b/test/vector/data/expected/varc/conditional-wght840p3.svg @@ -1,7 +1,7 @@ - + - + diff --git a/test/vector/data/expected/varc/default.svg b/test/vector/data/expected/varc/default.svg index db279cf04..9223f3291 100644 --- a/test/vector/data/expected/varc/default.svg +++ b/test/vector/data/expected/varc/default.svg @@ -1,9 +1,9 @@ - - + + - - + + diff --git a/test/vector/data/expected/varc/wght840p3.svg b/test/vector/data/expected/varc/wght840p3.svg index eb51cb5bf..4a612a91c 100644 --- a/test/vector/data/expected/varc/wght840p3.svg +++ b/test/vector/data/expected/varc/wght840p3.svg @@ -1,9 +1,9 @@ - - + + - - + +