From dfef3f33ae422e67f8bce7760bf3a96e9f450ee7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 18 Apr 2026 04:50:21 -0600 Subject: [PATCH] [vector] Global Y-flip in SVG render, eliminate per-glyph flips MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVG render now wraps all body content in a single . All coordinates inside are in Y-up (font) space. The viewBox maps the flipped range. This eliminates: - Per-path scale(1,-1) wrappers in draw flush_path_svg - Per-glyph Y-flip in paint _glyph_impl's push_transform - The {-xy, -yy, -ty} negation in extents transforms - Format switch in paint _glyph_impl paint _glyph_impl now passes the raw transform to push_transform — no format knowledge needed. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-vector-draw.cc | 33 ++++++++++++------- src/hb-vector-draw.hh | 28 +++------------- src/hb-vector-paint-svg.cc | 26 +++++++++------ src/hb-vector-paint.cc | 14 ++------ test/vector/data/expected/basic/cff1.svg | 12 ++++--- .../data/expected/basic/cff2-var-cntr.svg | 12 ++++--- .../data/expected/basic/cff2-var-wght900.svg | 12 ++++--- 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 | 6 ++-- .../basic/handwriting-glyf-colr-1.svg | 6 ++-- .../data/expected/basic/handwriting-sbix.svg | 6 ++-- .../basic/iran-nastaliq-y-offsets.svg | 20 ++++++----- 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 | 8 +++-- .../expected/varc/conditional-wght840p3.svg | 8 +++-- test/vector/data/expected/varc/default.svg | 10 +++--- test/vector/data/expected/varc/wght840p3.svg | 10 +++--- 21 files changed, 149 insertions(+), 134 deletions(-) diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 8a41b33ce..c2f7512d3 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -495,7 +495,7 @@ hb_vector_draw_glyph_or_fail (hb_vector_draw_t *draw, float yy = draw->transform.yy; float tx = draw->transform.x0; float ty = draw->transform.y0; - hb_transform_t<> extents_transform = {xx, yx, -xy, -yy, tx, -ty}; + hb_transform_t<> extents_transform = {xx, yx, xy, yy, tx, ty}; hb_bool_t has_extents = draw->has_extents; hb_vector_set_glyph_extents_common (extents_transform, @@ -763,18 +763,25 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) (draw->body.length ? draw->body.length : draw->path.length) + 256; out.alloc (estimated); + /* Extents are in Y-up (font) space. SVG is Y-down. + * The global scale(1,-1) wrapper flips content; the + * viewBox maps the flipped range. */ + float vb_x = draw->extents.x; + float vb_y = -(draw->extents.y + draw->extents.height); + float vb_w = draw->extents.width; + float vb_h = draw->extents.height; out.append_str ("extents.x); + out.append_num (vb_x); out.append_c (' '); - out.append_num (draw->extents.y); + out.append_num (vb_y); out.append_c (' '); - out.append_num (draw->extents.width); + out.append_num (vb_w); out.append_c (' '); - out.append_num (draw->extents.height); + out.append_num (vb_h); out.append_str ("\" width=\""); - out.append_num (draw->extents.width); + out.append_num (vb_w); out.append_str ("\" height=\""); - out.append_num (draw->extents.height); + out.append_num (vb_h); out.append_str ("\">\n"); if (draw->defs.length) @@ -788,13 +795,13 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) if (hb_color_get_alpha (draw->background)) { out.append_str ("extents.x); + out.append_num (vb_x); out.append_str ("\" y=\""); - out.append_num (draw->extents.y); + out.append_num (vb_y); out.append_str ("\" width=\""); - out.append_num (draw->extents.width); + out.append_num (vb_w); out.append_str ("\" height=\""); - out.append_num (draw->extents.height); + out.append_num (vb_h); out.append_str ("\" fill=\"rgb("); out.append_unsigned (hb_color_get_red (draw->background)); out.append_c (','); @@ -813,7 +820,11 @@ hb_vector_draw_render_svg (hb_vector_draw_t *draw) draw->flush_path (); if (draw->body.length) + { + out.append_str ("\n"); out.append_len (draw->body.arrayZ, draw->body.length); + out.append_str ("\n"); + } out.append_str ("\n"); diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index d2458cd89..b91ae52de 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -84,31 +84,11 @@ struct hb_vector_draw_t void flush_path_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); - body.append_str ("" - "\n"); + body.append_str ("\" fill=\""); + body.append_svg_color (foreground, true); + body.append_str ("\"/>\n"); } void transform_xy (float x, float y, float *tx, float *ty) diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index e391e86de..6d2a41e9b 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -916,18 +916,22 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) paint->group_stack.arrayZ[0].length + 320; out.alloc (estimated); + float vb_x = paint->extents.x; + float vb_y = -(paint->extents.y + paint->extents.height); + float vb_w = paint->extents.width; + float vb_h = paint->extents.height; out.append_str ("extents.x); + out.append_num (vb_x); out.append_c (' '); - out.append_num (paint->extents.y); + out.append_num (vb_y); out.append_c (' '); - out.append_num (paint->extents.width); + out.append_num (vb_w); out.append_c (' '); - out.append_num (paint->extents.height); + out.append_num (vb_h); out.append_str ("\" width=\""); - out.append_num (paint->extents.width); + out.append_num (vb_w); out.append_str ("\" height=\""); - out.append_num (paint->extents.height); + out.append_num (vb_h); out.append_str ("\">\n"); if (paint->defs.length) @@ -940,13 +944,13 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) if (hb_color_get_alpha (paint->background)) { out.append_str ("extents.x); + out.append_num (vb_x); out.append_str ("\" y=\""); - out.append_num (paint->extents.y); + out.append_num (vb_y); out.append_str ("\" width=\""); - out.append_num (paint->extents.width); + out.append_num (vb_w); out.append_str ("\" height=\""); - out.append_num (paint->extents.height); + out.append_num (vb_h); out.append_str ("\" fill=\"rgb("); out.append_unsigned (hb_color_get_red (paint->background)); out.append_c (','); @@ -963,7 +967,9 @@ hb_vector_paint_render_svg (hb_vector_paint_t *paint) out.append_str ("/>\n"); } + out.append_str ("\n"); out.append_len (paint->group_stack.arrayZ[0].arrayZ, paint->group_stack.arrayZ[0].length); + out.append_str ("\n"); out.append_str ("\n"); diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index a16682490..4a58d22cc 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -548,7 +548,7 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, if (hb_font_get_glyph_extents (font, glyph, &ge)) { hb_bool_t has_extents = paint->has_extents; - hb_transform_t<> extents_transform = {xx, yx, -xy, -yy, tx, -ty}; + hb_transform_t<> extents_transform = {xx, yx, xy, yy, tx, ty}; hb_bool_t ret = hb_vector_set_glyph_extents_common (extents_transform, paint->x_scale_factor, paint->y_scale_factor, @@ -564,17 +564,7 @@ hb_vector_paint_glyph_impl (hb_vector_paint_t *paint, return false; hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint); - switch (paint->format) - { - case HB_VECTOR_FORMAT_SVG: - 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_paint_push_transform (funcs, paint, xx, yx, xy, yy, tx, ty); hb_bool_t ret = true; if (fallible) diff --git a/test/vector/data/expected/basic/cff1.svg b/test/vector/data/expected/basic/cff1.svg index 7f95e4387..0a478e233 100644 --- a/test/vector/data/expected/basic/cff1.svg +++ b/test/vector/data/expected/basic/cff1.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/cff2-var-cntr.svg b/test/vector/data/expected/basic/cff2-var-cntr.svg index 549452ca7..4c71da08e 100644 --- a/test/vector/data/expected/basic/cff2-var-cntr.svg +++ b/test/vector/data/expected/basic/cff2-var-cntr.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/cff2-var-wght900.svg b/test/vector/data/expected/basic/cff2-var-wght900.svg index 0ceb849d6..ce9227cbb 100644 --- a/test/vector/data/expected/basic/cff2-var-wght900.svg +++ b/test/vector/data/expected/basic/cff2-var-wght900.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/default.svg b/test/vector/data/expected/basic/default.svg index 3ae7671a8..049622db9 100644 --- a/test/vector/data/expected/basic/default.svg +++ b/test/vector/data/expected/basic/default.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/glyf-var-wght900.svg b/test/vector/data/expected/basic/glyf-var-wght900.svg index fd1be40f1..ae2eafaaa 100644 --- a/test/vector/data/expected/basic/glyf-var-wght900.svg +++ b/test/vector/data/expected/basic/glyf-var-wght900.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + 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 7637ec48d..2257540b2 100644 --- a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg +++ b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + 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 dcb48cd54..a0f230fdd 100644 --- a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -1,4 +1,4 @@ - + @@ -30,8 +30,8 @@ - - + + 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 e06385caa..4db6d0362 100644 --- a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -1,4 +1,4 @@ - + @@ -30,8 +30,8 @@ - - + + diff --git a/test/vector/data/expected/basic/handwriting-sbix.svg b/test/vector/data/expected/basic/handwriting-sbix.svg index 8ad27b6a2..7c177ad13 100644 --- a/test/vector/data/expected/basic/handwriting-sbix.svg +++ b/test/vector/data/expected/basic/handwriting-sbix.svg @@ -1,6 +1,6 @@ - - - + + + 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 7e652b183..e18d2d363 100644 --- a/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg +++ b/test/vector/data/expected/basic/iran-nastaliq-y-offsets.svg @@ -1,10 +1,12 @@ - - - - - - - - - + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/multiline.svg b/test/vector/data/expected/basic/multiline.svg index 488e74b34..59793ac69 100644 --- a/test/vector/data/expected/basic/multiline.svg +++ b/test/vector/data/expected/basic/multiline.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/ttb.svg b/test/vector/data/expected/basic/ttb.svg index b6c6f99e3..ea4cd0ee8 100644 --- a/test/vector/data/expected/basic/ttb.svg +++ b/test/vector/data/expected/basic/ttb.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/basic/upem-default.svg b/test/vector/data/expected/basic/upem-default.svg index 3ae7671a8..049622db9 100644 --- a/test/vector/data/expected/basic/upem-default.svg +++ b/test/vector/data/expected/basic/upem-default.svg @@ -1,6 +1,8 @@ - - - - - + + + + + + + diff --git a/test/vector/data/expected/varc/conditional-default.svg b/test/vector/data/expected/varc/conditional-default.svg index 9e0cac683..5367268ce 100644 --- a/test/vector/data/expected/varc/conditional-default.svg +++ b/test/vector/data/expected/varc/conditional-default.svg @@ -1,4 +1,6 @@ - - - + + + + + diff --git a/test/vector/data/expected/varc/conditional-wght840p3.svg b/test/vector/data/expected/varc/conditional-wght840p3.svg index 0c1339d9e..56f08e841 100644 --- a/test/vector/data/expected/varc/conditional-wght840p3.svg +++ b/test/vector/data/expected/varc/conditional-wght840p3.svg @@ -1,4 +1,6 @@ - - - + + + + + diff --git a/test/vector/data/expected/varc/default.svg b/test/vector/data/expected/varc/default.svg index 9d4d057e6..361bae980 100644 --- a/test/vector/data/expected/varc/default.svg +++ b/test/vector/data/expected/varc/default.svg @@ -1,5 +1,7 @@ - - - - + + + + + + diff --git a/test/vector/data/expected/varc/wght840p3.svg b/test/vector/data/expected/varc/wght840p3.svg index 201339f79..9c9a940af 100644 --- a/test/vector/data/expected/varc/wght840p3.svg +++ b/test/vector/data/expected/varc/wght840p3.svg @@ -1,5 +1,7 @@ - - - - + + + + + +