[vector] Write the ends of SVG gradients that repeat more finely

An SVG gradient that repeats or reflects goes on in periods as long
as it is written.  hb-vector wrote its two ends like everything else,
to two decimals by default, and the hb-vector tool writes 64 font
units as 1: the length of a gradient could be two thirds of a font
unit off, and its Nth period N times that.

A radial gradient that reflects from a radius of 62.5 font units to
one of 100 was written as from 0.98 to 1.56, a hundredth short, and
33 periods on its rings were two pixels out of place at 160 pixels
per em, where a period is six pixels.

Write the ends of such a gradient as finely as the scale of a
transform: to seven decimals, or to the precision set if that is
more.  A gradient that pads has no periods; it is written as before.

Testing: a new test checks what is written for linear and radial
gradients of each kind, and fails without the change.  For a font of
eight glyphs whose gradients are 13 to 41 font units long, four of
which repeat or reflect 37 to 88 times across the glyph, how many
pixels of hb-vector's SVG are off by more than 24 levels from
hb-raster in Chrome, at 160 and at 512 pixels per em:

                        before        after
  linear, repeat        17%, 23%      none
  linear, reflect       10%, 47%      none
  radial, repeat         7%,  9%      none
  radial, reflect       27%, 57%      none

librsvg is within half a percent of Chrome in each.  The four that
pad come out the same before and after.

Six fonts of 240 random COLR glyphs each, on top of the other fixes
to hb-vector that are in review: of the 1377 glyphs that paint
anything, 1368 matched hb-raster in Chrome before and 1376 do now.
None got worse.

Of the 1553 mapped glyphs of the COLR test fonts, 106 have such a
gradient and are written differently, in SVG; the rest, and all PDF
output, are byte for byte as before.  Those 106 have few periods:
the 70 of them in test_glyphs-glyf_colr_1.ttf, bad_colrv1.ttf and
colr-negative-radii.ttf matched in Chrome before, and do now.

Found with a font of random COLR glyphs.  The test and the vector
fuzzer are clean under ASan and UBSan, and the test under MSan; three
minutes of fuzzing with coverage and failing allocations (6 jobs, 212
thousand runs) found nothing.  Full test suite passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc
index 61f49e1..da94636 100644
--- a/src/hb-vector-paint-svg.cc
+++ b/src/hb-vector-paint-svg.cc
@@ -44,6 +44,20 @@
   }
 }
 
+/* How many decimals to write the ends of a gradient to.
+ *
+ * A gradient that repeats or reflects goes on in periods as long as
+ * it is, so what its length is off by is multiplied by the number of
+ * the period: written as coarsely as an outline, it is pixels off a
+ * few dozen periods on.  It is written as finely as a scale. */
+static unsigned
+hb_vector_svg_gradient_precision (const hb_vector_buf_t &buf,
+				  hb_paint_extend_t extend)
+{
+  return extend == HB_PAINT_EXTEND_REPEAT || extend == HB_PAINT_EXTEND_REFLECT
+	 ? buf.scale_precision () : buf.precision;
+}
+
 static void
 hb_vector_svg_emit_color_stops (hb_vector_paint_t *paint,
 				hb_vector_buf_t *buf,
@@ -683,6 +697,8 @@
   const char *pfx = paint->id_prefix;
   unsigned pfx_len = paint->id_prefix_length;
   unsigned grad_id = paint->gradient_counter++;
+  unsigned precision = hb_vector_svg_gradient_precision (paint->defs,
+							  hb_color_line_get_extend (color_line));
 
   /* Reduce COLR's 3-anchor (P0, P1, P2) to SVG's 2-point
    * (start, end) gradient. */
@@ -699,13 +715,13 @@
   paint->defs.append_str ("gr");
   paint->defs.append_unsigned (grad_id);
   paint->defs.append_str ("\" gradientUnits=\"userSpaceOnUse\" x1=\"");
-  paint->defs.append_num (paint->sx (gx0));
+  paint->defs.append_num (paint->sx (gx0), precision);
   paint->defs.append_str ("\" y1=\"");
-  paint->defs.append_num (paint->sy (gy0));
+  paint->defs.append_num (paint->sy (gy0), precision);
   paint->defs.append_str ("\" x2=\"");
-  paint->defs.append_num (paint->sx (gx1));
+  paint->defs.append_num (paint->sx (gx1), precision);
   paint->defs.append_str ("\" y2=\"");
-  paint->defs.append_num (paint->sy (gy1));
+  paint->defs.append_num (paint->sy (gy1), precision);
   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");
@@ -751,25 +767,27 @@
   const char *pfx = paint->id_prefix;
   unsigned pfx_len = paint->id_prefix_length;
   unsigned grad_id = paint->gradient_counter++;
+  unsigned precision = hb_vector_svg_gradient_precision (paint->defs,
+							  hb_color_line_get_extend (color_line));
 
   paint->defs.append_str ("<radialGradient id=\"");
   paint->defs.append_len (pfx, pfx_len);
   paint->defs.append_str ("gr");
   paint->defs.append_unsigned (grad_id);
   paint->defs.append_str ("\" gradientUnits=\"userSpaceOnUse\" cx=\"");
-  paint->defs.append_num (paint->sx (gx1));
+  paint->defs.append_num (paint->sx (gx1), precision);
   paint->defs.append_str ("\" cy=\"");
-  paint->defs.append_num (paint->sy (gy1));
+  paint->defs.append_num (paint->sy (gy1), precision);
   paint->defs.append_str ("\" r=\"");
-  paint->defs.append_num (paint->sx (gr1));
+  paint->defs.append_num (paint->sx (gr1), precision);
   paint->defs.append_str ("\" fx=\"");
-  paint->defs.append_num (paint->sx (gx0));
+  paint->defs.append_num (paint->sx (gx0), precision);
   paint->defs.append_str ("\" fy=\"");
-  paint->defs.append_num (paint->sy (gy0));
+  paint->defs.append_num (paint->sy (gy0), precision);
   if (gr0 > 0)
   {
     paint->defs.append_str ("\" fr=\"");
-    paint->defs.append_num (paint->sx (gr0));
+    paint->defs.append_num (paint->sx (gr0), precision);
   }
   paint->defs.append_str ("\" spreadMethod=\"");
   paint->defs.append_str (hb_vector_svg_extend_mode_str (hb_color_line_get_extend (color_line)));
diff --git a/test/vector/meson.build b/test/vector/meson.build
index 8d9a9dd..36818a9 100644
--- a/test/vector/meson.build
+++ b/test/vector/meson.build
@@ -2,6 +2,14 @@
   subdir_done()
 endif
 
+test_vector_svg_repeat = executable('test-vector-svg-repeat', 'test-vector-svg-repeat.c',
+  dependencies: [libharfbuzz_vector_dep],
+)
+
+test('svg-repeat', test_vector_svg_repeat,
+  suite: 'vector',
+)
+
 run_test = find_program('run-tests.py')
 
 test_vector_budget = executable('test-vector-budget', 'test-vector-budget.c',
diff --git a/test/vector/test-vector-svg-repeat.c b/test/vector/test-vector-svg-repeat.c
new file mode 100644
index 0000000..31a56d8
--- /dev/null
+++ b/test/vector/test-vector-svg-repeat.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright © 2026  Behdad Esfahbod
+ *
+ *  This is part of HarfBuzz, a text shaping library.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+/* An SVG gradient that repeats or reflects goes on in periods as long
+ * as it is written, so what its length is rounded by is multiplied by
+ * the number of the period.  Check that its ends are written more
+ * finely than to the two decimals of everything else, and that a
+ * gradient that pads is written as everything else is. */
+
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+#include <assert.h>
+#include <string.h>
+
+#include <hb.h>
+#include <hb-vector.h>
+
+/* ---- a color line from red to blue ---- */
+
+static unsigned
+get_stops (hb_color_line_t *color_line,
+	   void *color_line_data,
+	   unsigned start,
+	   unsigned *count,
+	   hb_color_stop_t *stops,
+	   void *user_data)
+{
+  static const hb_color_t colors[2] = {HB_COLOR (0, 0, 255, 255),
+				       HB_COLOR (255, 0, 0, 255)};
+  (void) color_line;
+  (void) color_line_data;
+  (void) user_data;
+  if (count)
+  {
+    unsigned n = 0;
+    for (unsigned i = start; i < 2 && n < *count; i++, n++)
+    {
+      stops[n].offset = (float) i;
+      stops[n].is_foreground = 0;
+      stops[n].color = colors[i];
+    }
+    *count = n;
+  }
+  return 2;
+}
+
+static hb_paint_extend_t
+get_extend (hb_color_line_t *color_line,
+	    void *color_line_data,
+	    void *user_data)
+{
+  (void) color_line;
+  (void) user_data;
+  return *(hb_paint_extend_t *) color_line_data;
+}
+
+static const char *doc;
+static unsigned doc_len;
+
+static int
+has (const char *needle)
+{
+  unsigned n = (unsigned) strlen (needle);
+  for (unsigned i = 0; i + n <= doc_len; i++)
+    if (!memcmp (doc + i, needle, n))
+      return 1;
+  return 0;
+}
+
+/* Paints a gradient in font units, written at a 64th of them as the
+ * hb-vector tool has it: a linear one from (480, 512) to (511, 519),
+ * or a radial one around (1000, 0) from a radius of 50 to one of 87. */
+static hb_blob_t *
+render (int radial, hb_paint_extend_t extend)
+{
+  hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG);
+  assert (paint);
+  hb_vector_paint_set_scale_factor (paint, 64.f, 64.f);
+  hb_vector_extents_t extents = {0, 0, 16, 16};
+  hb_vector_paint_set_extents (paint, &extents);
+  hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint);
+
+  hb_color_line_t color_line;
+  memset (&color_line, 0, sizeof (color_line));
+  color_line.data = &extend;
+  color_line.get_color_stops = get_stops;
+  color_line.get_extend = get_extend;
+
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 1000, 1000);
+  if (radial)
+    hb_paint_radial_gradient (funcs, paint, &color_line, 1000, 0, 50, 1000, 0, 87);
+  else
+    hb_paint_linear_gradient (funcs, paint, &color_line,
+			      480, 512, 511, 519, 480 - 7, 512 + 31);
+  hb_paint_pop_clip (funcs, paint);
+
+  hb_blob_t *blob = hb_vector_paint_render (paint);
+  assert (blob);
+  hb_vector_paint_destroy (paint);
+  doc = hb_blob_get_data (blob, &doc_len);
+  return blob;
+}
+
+int
+main (void)
+{
+  hb_blob_t *blob;
+
+  /* 31 font units along x are 0.484375 as written, and 7 along y are
+   * 0.109375.  Rounded to 0.48 and 0.11, the gradient would be shorter
+   * by 1 part in 120: a whole period out of place 120 periods on. */
+  blob = render (0, HB_PAINT_EXTEND_REPEAT);
+  assert (has (" x1=\"7.5\" y1=\"8\" x2=\"7.984375\" y2=\"8.109375\" spreadMethod=\"repeat\""));
+  hb_blob_destroy (blob);
+
+  blob = render (0, HB_PAINT_EXTEND_REFLECT);
+  assert (has (" x1=\"7.5\" y1=\"8\" x2=\"7.984375\" y2=\"8.109375\" spreadMethod=\"reflect\""));
+  hb_blob_destroy (blob);
+
+  /* One that pads has no periods. */
+  blob = render (0, HB_PAINT_EXTEND_PAD);
+  assert (has (" x1=\"7.5\" y1=\"8\" x2=\"7.98\" y2=\"8.11\" spreadMethod=\"pad\""));
+  hb_blob_destroy (blob);
+
+  /* Radii of 50 and 87 font units are 0.78125 and 1.359375. */
+  blob = render (1, HB_PAINT_EXTEND_REPEAT);
+  assert (has (" cx=\"15.625\" cy=\"0\" r=\"1.359375\" fx=\"15.625\" fy=\"0\" fr=\"0.78125\" spreadMethod=\"repeat\""));
+  hb_blob_destroy (blob);
+
+  blob = render (1, HB_PAINT_EXTEND_REFLECT);
+  assert (has (" cx=\"15.625\" cy=\"0\" r=\"1.359375\" fx=\"15.625\" fy=\"0\" fr=\"0.78125\" spreadMethod=\"reflect\""));
+  hb_blob_destroy (blob);
+
+  blob = render (1, HB_PAINT_EXTEND_PAD);
+  assert (has (" cx=\"15.62\" cy=\"0\" r=\"1.36\" fx=\"15.62\" fy=\"0\" fr=\"0.78\" spreadMethod=\"pad\""));
+  hb_blob_destroy (blob);
+
+  return 0;
+}