From ce4199c303d70c56a6d246df3a92b243ab48eb17 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 15 Apr 2026 15:44:10 -0600 Subject: [PATCH] [vector] Fix linear-gradient anchor reduction hb-vector-paint emitted (2*x1 - x2, 2*y1 - y2) as the SVG end point -- the reflection of P2 about P1, not the projection. For COLR linear gradients the SVG end should be P1 projected onto the line through P0 perpendicular to (P2 - P0); that's the gradient's actual axis (perpendicular to the rotation line). With the old formula, fonts with rotation references far from the color line ended up with massively over-scaled gradient axes -- e.g. BungeeSpice's ABC showed solid red where raster shows a full red->yellow gradient. Add hb_paint_reduce_linear_anchors() to hb-paint.hh as the shared canonical implementation (matches hb-raster-paint and hb-cairo) and use it from hb-vector-paint. Regenerate the two affected basic.tests expected SVGs; the new gradient endpoints are now consistent with the cairo / raster outputs. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-paint.hh | 31 +++++++++++++++++++ src/hb-vector-paint.cc | 14 ++++++--- .../expected/basic/handwriting-cff-colr-1.svg | 2 +- .../basic/handwriting-glyf-colr-1.svg | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/hb-paint.hh b/src/hb-paint.hh index f737ab911..d2ffe8e42 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -451,4 +451,35 @@ done: } +/* Reduce a COLRv1 linear gradient's 3-anchor spec (P0=color + * stop 0, P1=color stop 1, P2=rotation reference) to the + * 2-point axis (P0, P1') used by SVG / cairo / our raster + * backend. P1' is the foot of P1 on the line through P0 + * perpendicular to (P2 - P0); the resulting axis is the + * gradient's actual direction (perpendicular to the + * rotation line). Degenerate (P0 == P2): pass-through. */ +static inline void +hb_paint_reduce_linear_anchors (float x0, float y0, + float x1, float y1, + float x2, float y2, + float *xx0, float *yy0, + float *xx1, float *yy1) +{ + float q2x = x2 - x0, q2y = y2 - y0; + float s = q2x * q2x + q2y * q2y; + if (s < 1e-6f) + { + *xx0 = x0; *yy0 = y0; + *xx1 = x1; *yy1 = y1; + return; + } + float q1x = x1 - x0, q1y = y1 - y0; + float k = (q2x * q1x + q2y * q1y) / s; + *xx0 = x0; + *yy0 = y0; + *xx1 = x1 - k * q2x; + *yy1 = y1 - k * q2y; +} + + #endif /* HB_PAINT_HH */ diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 9945998aa..003bdb23f 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -661,18 +661,24 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, unsigned pfx_len = paint->id_prefix.length; unsigned grad_id = paint->gradient_counter++; + /* Reduce COLR's 3-anchor (P0, P1, P2) to SVG's 2-point + * (start, end) gradient. */ + float lx0, ly0, lx1, ly1; + hb_paint_reduce_linear_anchors (x0, y0, x1, y1, x2, y2, + &lx0, &ly0, &lx1, &ly1); + hb_buf_append_str (&paint->defs, "defs, pfx, pfx_len); hb_buf_append_str (&paint->defs, "gr"); hb_buf_append_unsigned (&paint->defs, grad_id); hb_buf_append_str (&paint->defs, "\" gradientUnits=\"userSpaceOnUse\" x1=\""); - hb_buf_append_num (&paint->defs, x0, paint->precision); + hb_buf_append_num (&paint->defs, lx0, paint->precision); hb_buf_append_str (&paint->defs, "\" y1=\""); - hb_buf_append_num (&paint->defs, y0, paint->precision); + hb_buf_append_num (&paint->defs, ly0, paint->precision); hb_buf_append_str (&paint->defs, "\" x2=\""); - hb_buf_append_num (&paint->defs, x1 + (x1 - x2), paint->precision); + hb_buf_append_num (&paint->defs, lx1, paint->precision); hb_buf_append_str (&paint->defs, "\" y2=\""); - hb_buf_append_num (&paint->defs, y1 + (y1 - y2), paint->precision); + hb_buf_append_num (&paint->defs, ly1, paint->precision); hb_buf_append_str (&paint->defs, "\" spreadMethod=\""); hb_buf_append_str (&paint->defs, hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line))); hb_buf_append_str (&paint->defs, "\">\n"); 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 b2210f447..caceca02e 100644 --- a/test/vector/data/expected/basic/handwriting-cff-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-cff-colr-1.svg @@ -16,7 +16,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 490d431eb..3d3e13522 100644 --- a/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg +++ b/test/vector/data/expected/basic/handwriting-glyf-colr-1.svg @@ -16,7 +16,7 @@ - +