[vector] Paint linear and radial gradients that repeat in PDF

PDF shadings do not repeat.  A linear or radial gradient whose color
line repeats or reflects was written as a shading over the color line
alone, with nothing painted on either side of it.

Paint it a period of the color line at a time, for the periods that
reach what is painted, with the color line backwards in every other
one if it reflects.  What is painted is known from the bounds of the
open clips, which are kept for sizing sweep gradients: the periods are
those whose lines, or circles, touch the bounds.  No more than 256 are
written, those nearest the color line itself.

One shading over a function that repeats the color line would be less
to write, and is what cairo writes.  Viewers do badly with it.
pdfium and MuPDF resolve the function of a shading in 256 steps
whatever it holds, so every period makes all of them coarser.  And
poppler splits a shading for as long as the colors at the two ends of
a piece differ, which they do not when a whole number of periods lie
between them, as when the periods fit the clip: it paints that piece
in one color.  It does so with cairo's own output.

How the periods are written is what it took for the viewers tried to
agree with hb-raster:

- The periods of a linear gradient are one shading, painted at one
  place after another.  Each written with rounded numbers of its own,
  they are not quite parallel, and MuPDF left holes between them.
- Each period goes a little into the next, which is painted over it:
  viewers decide pixel by pixel which side of the line between two
  shadings a pixel is on.
- Under a transform that rotates, the periods of a linear gradient
  are painted in the space of the root.  Ghostscript gets the length
  of an axial shading wrong under such a transform, depending on how
  much of it the clip lets through, and a period is seldom seen whole.
- Circles inside one another make rings, painted from the outside in
  and each with its hole filled: MuPDF leaves holes between rings
  that merely meet.  They are written growing, the color line
  backwards if need be: poppler drops a radial shading that shrinks
  and is extended at one end only.
- The periods of a radial gradient are painted on top of each other,
  so they are painted in a group, and the group once: under a clip
  each would add to its antialiased edge.  So are periods that are
  not opaque, their alphas in a soft mask.

The documentation no longer says that these are not supported.

Testing: of the mapped glyphs of the COLR test fonts, 106 have a
different PDF, in five fonts.  19 of them had the pixels of hb-raster
before, in each of pdfium, MuPDF, poppler's cairo backend and
Ghostscript.  All 106 do now, but for six in pdfium: the two glyphs
of a cone that repeats, in three fonts, where hb-raster paints the
cone's mirror image as well until #6845.

Replaying the same paint calls on hb-raster and hb-vector for 300
random scenes of clipped and transformed linear gradients and rings
that repeat or reflect, the pixels are the same in 300 with MuPDF and
with poppler's cairo backend, 299 with its Splash backend, and 298
with pdfium and with Ghostscript.  With one shading over a repeating
function they were in 291 with pdfium, and poppler painted flat what
a whole number of periods filled.  With cones as well, and #6845 for
hb-raster, they are the same in 300 with Ghostscript, 299 with MuPDF
and 292 with pdfium and with Splash.  poppler's cairo backend is at
165: it paints cones that shrink in one color, repeating or not.

The search for the circles that touch the bounds agrees with brute
force on 30000 random cases.

A new test checks which periods are written and how, and fails
without the change; six glyphs are added as an expected output.  It
and the vector fuzzer are clean under ASan and UBSan, the test under
MSan, and the fuzzer ran three minutes with failing allocations.
Full test suite passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc
index 82085bc..058214e 100644
--- a/src/hb-vector-paint-pdf.cc
+++ b/src/hb-vector-paint-pdf.cc
@@ -45,8 +45,9 @@
  *   - Compositing modes: the blend modes as PDF's own, and the
  *     Porter-Duff modes with soft masks (SRC_ATOP, DEST_ATOP, XOR and
  *     PLUS only approximately)
- *   - Linear gradients (PDF Type 2 axial shading, PAD extension only)
- *   - Radial gradients (PDF Type 3 radial shading, PAD extension only)
+ *   - Linear gradients (PDF Type 2 axial shadings; one for each period
+ *     of a color line that repeats or reflects)
+ *   - Radial gradients (PDF Type 3 radial shadings; likewise)
  *   - Sweep gradients (PDF Type 6 Coons-patch meshes)
  */
 
@@ -1069,6 +1070,587 @@
   return res->add_object (std::move (obj));
 }
 
+/* ---- Gradients that repeat ---- */
+
+/* PDF shadings do not repeat.  A linear or radial gradient whose color
+ * line repeats or reflects is painted a period of the color line at a
+ * time, for the periods that reach what is painted: a shading each,
+ * with the color line backwards in every other one if it reflects.
+ *
+ * One shading over a function that stitches the periods together would
+ * be less to write, but viewers do badly with it.  Many resolve the
+ * function of a shading in a fixed number of steps, whatever it holds.
+ * And poppler splits a shading for as long as the colors at the two
+ * ends of a piece differ, which those of a function that repeats do
+ * not when a whole number of periods lie between them: it then paints
+ * that piece in one color.
+ *
+ * No more periods are written than the open clips let through, and
+ * never more than this many, since a color line can be as short as it
+ * likes next to what it fills.  Those nearest the color line itself are
+ * kept then. */
+#define HB_PDF_GRADIENT_MAX_PERIODS 256
+
+/* Each period's shading goes this much of a period into the next, whose
+ * shading is painted over it.  Viewers decide pixel by pixel which side
+ * of the line between two shadings a pixel is on, and where their
+ * arithmetic for the one and for the other disagreed, a pixel would be
+ * left out of both. */
+#define HB_PDF_GRADIENT_PERIOD_OVERLAP (1. / 64)
+
+/* The stretch of a color line that a gradient is written for, the color
+ * line itself being the stretch from 0 to 1. */
+struct hb_pdf_gradient_span_t
+{
+  /* The periods: [first, last). */
+  int first = 0, last = 1;
+  /* Where the first one starts and the last one ends. */
+  double t0 = 0., t1 = 1.;
+
+  /* Sets the span to the periods that hold the stretch from @tmin to
+   * @tmax.  Returns false if there is no such stretch. */
+  bool set (double tmin, double tmax)
+  {
+    /* Catches NaNs, too. */
+    if (!(tmin <= tmax))
+      return false;
+
+    /* Whole numbers this size are exact as floats, and as ints. */
+    const double limit = 1 << 20;
+    tmin = hb_clamp (tmin, -limit, limit);
+    tmax = hb_clamp (tmax, -limit, limit);
+
+    int nearest = (int) floor (hb_clamp (0.5, tmin, tmax));
+    first = hb_max ((int) floor (tmin), nearest - HB_PDF_GRADIENT_MAX_PERIODS / 2);
+    last = hb_min ((int) ceil (tmax), nearest + HB_PDF_GRADIENT_MAX_PERIODS / 2);
+    if (first == last)
+      last++;
+
+    t0 = first;
+    t1 = last;
+    return true;
+  }
+};
+
+/* Sets @span to the periods that reach what the open clips let through
+ * of a color line that repeats along the line from (@x0, @y0) to
+ * (@x1, @y1).  Returns false if nothing is to be painted. */
+static bool
+hb_pdf_linear_gradient_span (const hb_vector_paint_t *paint,
+			     float x0, float y0,
+			     float x1, float y1,
+			     hb_pdf_gradient_span_t *span)
+{
+  double dx = (double) x1 - (double) x0, dy = (double) y1 - (double) y0;
+  double len2 = dx * dx + dy * dy;
+  /* A line of no length has no colors, as in hb-raster. */
+  if (!(len2 > 0.))
+    return false;
+
+  double tmin = -HUGE_VAL, tmax = HUGE_VAL;
+  float x[4], y[4];
+  switch (paint->clip_corners (x, y))
+  {
+    case hb_bounds_t<>::EMPTY:
+      return false;
+    case hb_bounds_t<>::UNBOUNDED:
+      break;
+    case hb_bounds_t<>::BOUNDED:
+      tmin = HUGE_VAL;
+      tmax = -HUGE_VAL;
+      for (unsigned i = 0; i < 4; i++)
+      {
+	double t = (((double) x[i] - (double) x0) * dx +
+		    ((double) y[i] - (double) y0) * dy) / len2;
+	tmin = hb_min (tmin, t);
+	tmax = hb_max (tmax, t);
+      }
+      break;
+  }
+
+  return span->set (tmin, tmax);
+}
+
+/* Finds the circles that touch the quadrilateral of corners (@qx, @qy),
+ * which are in order around it, among those centered at
+ * (@cx + t @dx, @cy + t @dy) with a radius of @r + t @dr that is not
+ * negative.  Sets @tmin and @tmax to the smallest and largest t of
+ * such circles, and @tmin above @tmax if there are none.
+ *
+ * The circles at the two ends are among those that go through a
+ * corner, those that touch a side from the outside, and the one of no
+ * radius. */
+static void
+hb_pdf_circles_touching (const float qx[4], const float qy[4],
+			 double cx, double cy, double r,
+			 double dx, double dy, double dr,
+			 double *tmin, double *tmax)
+{
+  double lo = HUGE_VAL, hi = -HUGE_VAL;
+  auto add = [&] (double t)
+  {
+    lo = hb_min (lo, t);
+    hi = hb_max (hi, t);
+  };
+  auto exists = [&] (double t) { return r + t * dr >= 0.; };
+
+  /* The corners, from the center of the first circle. */
+  double px[4], py[4];
+  for (unsigned i = 0; i < 4; i++)
+  {
+    px[i] = (double) qx[i] - cx;
+    py[i] = (double) qy[i] - cy;
+  }
+
+  /* A circle goes through (x, y) if a t^2 - 2 b t + c = 0, where
+   *
+   *   a = dx^2 + dy^2 - dr^2
+   *   b = x dx + y dy + r dr
+   *   c = x^2 + y^2 - r^2
+   *
+   * If a is zero the circles all touch one line at one point, where b
+   * is zero, and get bigger without end as they near the rest of it. */
+  double a = dx * dx + dy * dy - dr * dr;
+  bool flat = fabs (a) <= 1e-9 * (dx * dx + dy * dy + dr * dr);
+  bool before = false, after = false;
+  for (unsigned i = 0; i < 4; i++)
+  {
+    double b = px[i] * dx + py[i] * dy + r * dr;
+    double c = px[i] * px[i] + py[i] * py[i] - r * r;
+    if (flat)
+    {
+      if (b <= 0.) before = true;
+      if (b >= 0.) after = true;
+      if (b != 0. && exists (c / (2. * b)))
+	add (c / (2. * b));
+      continue;
+    }
+    double d = b * b - a * c;
+    if (d < 0.)
+      continue;
+    /* The two roots, without subtracting numbers that may be close. */
+    double q = b + (b < 0. ? -sqrt (d) : sqrt (d));
+    if (exists (q / a))
+      add (q / a);
+    if (q != 0. && exists (c / q))
+      add (c / q);
+  }
+  if (flat && before && after)
+    add (dr > 0. ? HUGE_VAL : -HUGE_VAL);
+
+  /* Which way around the corners go. */
+  double area = 0.;
+  for (unsigned i = 0; i < 4; i++)
+  {
+    unsigned j = (i + 1) & 3;
+    area += px[i] * py[j] - px[j] * py[i];
+  }
+  double out = area < 0. ? -1. : 1.;
+
+  /* The circle of no radius is a point. */
+  double tf = dr != 0. ? -r / dr : 0.;
+  bool point_inside = dr != 0.;
+
+  for (unsigned i = 0; i < 4; i++)
+  {
+    unsigned j = (i + 1) & 3;
+    double ex = px[j] - px[i], ey = py[j] - py[i];
+    double len = sqrt (ex * ex + ey * ey);
+    if (!(len > 0.))
+      continue;
+    /* The unit normal of the side that points out. */
+    double nx = out * ey / len, ny = -out * ex / len;
+
+    if ((tf * dx - px[i]) * nx + (tf * dy - py[i]) * ny > 0.)
+      point_inside = false;
+
+    /* A circle touches the line of the side from the outside where its
+     * center is its radius away on that side. */
+    double den = dx * nx + dy * ny - dr;
+    if (den == 0.)
+      continue;
+    double t = (r + px[i] * nx + py[i] * ny) / den;
+    if (!exists (t))
+      continue;
+    /* And the side itself, if that is where it touches the line. */
+    double rt = r + t * dr;
+    double along = (t * dx - rt * nx - px[i]) * ex + (t * dy - rt * ny - py[i]) * ey;
+    if (along >= 0. && along <= len * len)
+      add (t);
+  }
+  if (point_inside)
+    add (tf);
+
+  *tmin = lo;
+  *tmax = hi;
+}
+
+/* Sets @span to the periods that reach what the open clips let through
+ * of a color line that repeats over the circles from the one at
+ * (@x0, @y0) of radius @r0 to the one at (@x1, @y1) of radius @r1.
+ * Returns false if nothing is to be painted. */
+static bool
+hb_pdf_radial_gradient_span (const hb_vector_paint_t *paint,
+			     float x0, float y0, float r0,
+			     float x1, float y1, float r1,
+			     hb_pdf_gradient_span_t *span)
+{
+  double dx = (double) x1 - (double) x0;
+  double dy = (double) y1 - (double) y0;
+  double dr = (double) r1 - (double) r0;
+  /* One circle has no colors, as in hb-raster. */
+  if (dx == 0. && dy == 0. && dr == 0.)
+    return false;
+
+  double tmin = -HUGE_VAL, tmax = HUGE_VAL;
+  float x[4], y[4];
+  switch (paint->clip_corners (x, y))
+  {
+    case hb_bounds_t<>::EMPTY:
+      return false;
+    case hb_bounds_t<>::UNBOUNDED:
+      break;
+    case hb_bounds_t<>::BOUNDED:
+      hb_pdf_circles_touching (x, y,
+			       (double) x0, (double) y0, (double) r0,
+			       dx, dy, dr,
+			       &tmin, &tmax);
+      break;
+  }
+
+  /* There are no circles past the one of no radius. */
+  double tf = dr != 0. ? -(double) r0 / dr : 0.;
+  if (dr > 0.)
+    tmin = hb_max (tmin, tf);
+  else if (dr < 0.)
+    tmax = hb_min (tmax, tf);
+
+  if (!span->set (tmin, tmax))
+    return false;
+
+  /* The periods are whole; the gradient stops at that circle. */
+  if (dr > 0.)
+    span->t0 = hb_max (span->t0, tf);
+  else if (dr < 0.)
+    span->t1 = hb_min (span->t1, tf);
+  return span->t0 < span->t1;
+}
+
+/* Builds a function over two periods of a color line from @func_id,
+ * the function over the color line: the first of them and then the
+ * second, each backwards if so told. */
+static unsigned
+hb_pdf_build_period_function (hb_pdf_resources_t *res,
+			      unsigned func_id,
+			      bool first_backwards,
+			      bool second_backwards)
+{
+  hb_vector_buf_t obj;
+  obj.append_str ("<< /FunctionType 3 /Domain [0 2]\n/Functions [");
+  obj.append_unsigned (func_id);
+  obj.append_str (" 0 R ");
+  obj.append_unsigned (func_id);
+  obj.append_str (" 0 R]\n/Bounds [1]\n/Encode [");
+  obj.append_str (first_backwards ? "1 0 " : "0 1 ");
+  obj.append_str (second_backwards ? "1 0" : "0 1");
+  obj.append_str ("] >>");
+  return res->add_object (std::move (obj));
+}
+
+/* A linear or radial gradient whose color line repeats, as it is
+ * written: the periods of the color line that are painted. */
+struct hb_pdf_gradient_periods_t
+{
+  /* 2 for an axial shading, 3 for a radial one. */
+  unsigned shading_type;
+  /* Where the color line starts and ends: x, y and, if radial, the
+   * radius. */
+  float c0[3], c1[3];
+  hb_pdf_gradient_span_t span;
+  bool reflect;
+  /* Whether the circles are inside one another.  They do not overlap
+   * then, and a period of them is a ring. */
+  bool nested;
+  /* Whether the color line is taken from its end to its start. */
+  bool backwards;
+
+  hb_pdf_gradient_periods_t (unsigned shading_type_,
+			     const float c0_[3], const float c1_[3],
+			     const hb_pdf_gradient_span_t &span_,
+			     bool reflect_)
+    : shading_type (shading_type_), span (span_), reflect (reflect_)
+  {
+    double dx = (double) c1_[0] - (double) c0_[0];
+    double dy = (double) c1_[1] - (double) c0_[1];
+    double dr = (double) c1_[2] - (double) c0_[2];
+    nested = shading_type == 3 && dx * dx + dy * dy < dr * dr;
+
+    /* Rings are written growing: poppler does not paint a radial
+     * shading that shrinks and goes on at one end only, as these do.
+     * Seen from the other end of the color line, t is 1 - t. */
+    backwards = nested && dr < 0.;
+    for (unsigned i = 0; i < 3; i++)
+    {
+      c0[i] = backwards ? c1_[i] : c0_[i];
+      c1[i] = backwards ? c0_[i] : c1_[i];
+    }
+    if (backwards)
+    {
+      span.first = 1 - span_.last;
+      span.last = 1 - span_.first;
+      span.t0 = 1. - span_.t1;
+      span.t1 = 1. - span_.t0;
+    }
+  }
+
+  /* Adds shadings in @color_space for the periods, of the colors that
+   * @func_id is the function of over the color line, and appends the
+   * operators that paint them to @out. */
+  void add_shadings (hb_vector_paint_t *paint,
+		     hb_pdf_resources_t *res,
+		     const char *color_space,
+		     unsigned func_id,
+		     hb_vector_buf_t *out) const
+  {
+    /* The functions of the periods of even and of odd number, each
+     * over its period and the next. */
+    unsigned func_ids[2];
+    func_ids[0] = hb_pdf_build_period_function (res, func_id,
+						backwards, backwards != reflect);
+    func_ids[1] = !reflect ? func_ids[0] :
+		  hb_pdf_build_period_function (res, func_id,
+						!backwards, backwards);
+
+    if (shading_type == 2)
+      add_axial_shadings (paint, res, color_space, func_ids, out);
+    else
+      add_radial_shadings (paint, res, color_space, func_ids, out);
+  }
+
+  /* The periods of an axial gradient are all alike: one shading, or
+   * two if every other one is backwards, painted at one place after
+   * another.  They are then parallel to the last digit.  Each written
+   * with numbers of its own, rounded, they are not, and come apart
+   * away from the color line. */
+  void add_axial_shadings (hb_vector_paint_t *paint,
+			   hb_pdf_resources_t *res,
+			   const char *color_space,
+			   const unsigned func_ids[2],
+			   hb_vector_buf_t *out) const
+  {
+    /* Where the color line starts and how far a period goes. */
+    double x = (double) paint->sx (c0[0]), y = (double) paint->sy (c0[1]);
+    double dx = (double) paint->sx (c1[0]) - x;
+    double dy = (double) paint->sy (c1[1]) - y;
+    const double end = 1. + HB_PDF_GRADIENT_PERIOD_OVERLAP;
+    unsigned precision = paint->get_precision () + 2;
+
+    /* Ghostscript, as of 10.05, gets the length of an axial shading
+     * wrong under a transform that rotates, depending on how much of
+     * the shading the clip lets through.  So under such a transform
+     * the periods are painted in the space of the root: the transform
+     * is undone, and the gradient written as it is there. */
+    hb_transform_t<> ctm = paint->current_ctm ();
+    double det = (double) ctm.xx * (double) ctm.yy - (double) ctm.xy * (double) ctm.yx;
+    bool in_root = (ctm.xy != 0.f || ctm.yx != 0.f) && fabs (det) > 1e-12;
+    if (in_root)
+    {
+      /* How fast t grows there, and so the step that takes it up by
+       * one. */
+      double len2 = dx * dx + dy * dy;
+      double gx = ( (double) ctm.yy * dx - (double) ctm.yx * dy) / (det * len2);
+      double gy = (-(double) ctm.xy * dx + (double) ctm.xx * dy) / (det * len2);
+      double g2 = gx * gx + gy * gy;
+      double rx = (double) ctm.xx * x + (double) ctm.xy * y + (double) ctm.x0;
+      double ry = (double) ctm.yx * x + (double) ctm.yy * y + (double) ctm.y0;
+      x = rx;
+      y = ry;
+      dx = gx / g2;
+      dy = gy / g2;
+
+      unsigned sprec = out->scale_precision ();
+      out->append_str ("q\n");
+      out->append_num ((float) ( (double) ctm.yy / det), sprec);
+      out->append_c (' ');
+      out->append_num ((float) (-(double) ctm.yx / det), sprec);
+      out->append_c (' ');
+      out->append_num ((float) (-(double) ctm.xy / det), sprec);
+      out->append_c (' ');
+      out->append_num ((float) ( (double) ctm.xx / det), sprec);
+      out->append_c (' ');
+      out->append_num ((float) (((double) ctm.xy * (double) ctm.y0 - (double) ctm.yy * (double) ctm.x0) / det), precision);
+      out->append_c (' ');
+      out->append_num ((float) (((double) ctm.yx * (double) ctm.x0 - (double) ctm.xx * (double) ctm.y0) / det), precision);
+      out->append_str (" cm\n");
+    }
+
+    unsigned sh_idx[2];
+    for (unsigned i = 0; i < 2; i++)
+    {
+      if (i && func_ids[1] == func_ids[0])
+      {
+	sh_idx[1] = sh_idx[0];
+	break;
+      }
+      hb_vector_buf_t sh;
+      sh.append_str ("<< /ShadingType 2 /ColorSpace ");
+      sh.append_str (color_space);
+      sh.append_str ("\n/Coords [");
+      sh.append_num ((float) x, precision);
+      sh.append_c (' ');
+      sh.append_num ((float) y, precision);
+      sh.append_c (' ');
+      sh.append_num ((float) (x + end * dx), precision);
+      sh.append_c (' ');
+      sh.append_num ((float) (y + end * dy), precision);
+      sh.append_str ("]\n/Function ");
+      sh.append_unsigned (func_ids[i]);
+      sh.append_str (" 0 R\n/Domain [0 ");
+      sh.append_num ((float) end, 4);
+      sh.append_str ("]\n>>");
+      sh_idx[i] = res->add_shading (std::move (sh));
+    }
+
+    for (int k = span.first; k < span.last; k++)
+    {
+      if (k)
+      {
+	out->append_str ("q\n1 0 0 1 ");
+	out->append_num ((float) (k * dx), precision);
+	out->append_c (' ');
+	out->append_num ((float) (k * dy), precision);
+	out->append_str (" cm\n");
+      }
+      out->append_str ("/SH");
+      out->append_unsigned (sh_idx[(unsigned) k & 1u]);
+      out->append_str (k ? " sh\nQ\n" : " sh\n");
+    }
+
+    if (in_root)
+      out->append_str ("Q\n");
+  }
+
+  /* The periods of a radial gradient are a shading each.
+   *
+   * Later periods are painted over earlier ones, as later circles are
+   * to be, and go half a period into the next: where two periods meet
+   * is a curve within what both paint, and the nearer the one stops to
+   * it, the more of a seam viewers leave along it.
+   *
+   * Rings are painted from the outside in, each with its hole filled
+   * and the next one over that, so that nothing can show between two
+   * of them.  MuPDF, which draws a ring as a polygon, leaves holes
+   * between two that merely meet. */
+  void add_radial_shadings (hb_vector_paint_t *paint,
+			    hb_pdf_resources_t *res,
+			    const char *color_space,
+			    const unsigned func_ids[2],
+			    hb_vector_buf_t *out) const
+  {
+    double dx = (double) c1[0] - (double) c0[0];
+    double dy = (double) c1[1] - (double) c0[1];
+    double dr = (double) c1[2] - (double) c0[2];
+    double overlap = nested ? HB_PDF_GRADIENT_PERIOD_OVERLAP : 0.5;
+
+    for (int n = span.first; n < span.last; n++)
+    {
+      int k = nested ? span.first + span.last - 1 - n : n;
+      double t[2] = {hb_max ((double) k, span.t0),
+		     hb_min ((double) k + 1. + overlap, span.t1)};
+
+      hb_vector_buf_t sh;
+      sh.append_str ("<< /ShadingType 3 /ColorSpace ");
+      sh.append_str (color_space);
+      sh.append_str ("\n/Coords [");
+      for (unsigned i = 0; i < 2; i++)
+      {
+	if (i) sh.append_c (' ');
+	sh.append_num (paint->sx ((float) ((double) c0[0] + t[i] * dx)));
+	sh.append_c (' ');
+	sh.append_num (paint->sy ((float) ((double) c0[1] + t[i] * dy)));
+	sh.append_c (' ');
+	sh.append_num (paint->sx ((float) hb_max ((double) c0[2] + t[i] * dr, 0.)));
+      }
+      sh.append_str ("]\n/Function ");
+      sh.append_unsigned (func_ids[(unsigned) k & 1u]);
+      sh.append_str (" 0 R\n/Domain [");
+      sh.append_num ((float) (t[0] - k), 4);
+      sh.append_c (' ');
+      sh.append_num ((float) (t[1] - k), 4);
+      sh.append_str (nested ? "]\n/Extend [true false]\n>>" : "]\n>>");
+
+      out->append_str ("/SH");
+      out->append_unsigned (res->add_shading (std::move (sh)));
+      out->append_str (" sh\n");
+    }
+  }
+};
+
+/* Paints @periods, of a gradient whose color line is in
+ * paint->color_stops_scratch and whose colors @func_id is the function
+ * of over that color line. */
+static void
+hb_pdf_paint_gradient_periods (hb_vector_paint_t *paint,
+			       hb_pdf_resources_t *res,
+			       const hb_pdf_gradient_periods_t &periods,
+			       unsigned func_id)
+{
+  auto &body = paint->current_body ();
+
+  hb_vector_buf_t colors;
+  periods.add_shadings (paint, res, "/DeviceRGB", func_id, &colors);
+
+  /* The periods of an axial gradient hardly overlap, and are painted
+   * as they are if they are opaque.
+   *
+   * Otherwise the periods are painted in a group, and the group once.
+   * Those of a radial gradient are painted on top of each other, and
+   * under a clip each would add to what shows of the clip's edge,
+   * which is antialiased.  And colors are painted through a soft mask
+   * of their alphas, which would count twice where two periods
+   * overlap. */
+  bool needs_alpha = hb_pdf_gradient_needs_alpha (paint->color_stops_scratch);
+  if (periods.shading_type == 2 && !needs_alpha)
+  {
+    body.append_len (colors.arrayZ, colors.length);
+    return;
+  }
+
+  unsigned form_idx;
+  if (unlikely (!res->add_form_group (colors, &form_idx)))
+    return;
+
+  if (needs_alpha)
+  {
+    hb_vector_buf_t alphas;
+    periods.add_shadings (paint, res, "/DeviceGray",
+			  hb_pdf_build_alpha_gradient_function_from_stops (res, paint),
+			  &alphas);
+
+    unsigned mask_id = res->add_group (alphas, true);
+    if (unlikely (!mask_id))
+      return;
+
+    hb_vector_buf_t gs;
+    gs.append_str ("<< /Type /ExtGState\n");
+    gs.append_str ("/SMask << /Type /Mask /S /Luminosity /G ");
+    gs.append_unsigned (mask_id);
+    gs.append_str (" 0 R >> >>");
+    unsigned gs_idx = res->add_extgstate (std::move (gs));
+
+    body.append_str ("q\n/GS");
+    body.append_unsigned (gs_idx);
+    body.append_str (" gs\n");
+  }
+
+  body.append_str ("/Fm");
+  body.append_unsigned (form_idx);
+  body.append_str (" Do\n");
+
+  if (needs_alpha)
+    body.append_str ("Q\n");
+}
+
 static void
 hb_pdf_paint_linear_gradient (hb_paint_funcs_t *,
 			      void *paint_data,
@@ -1109,6 +1691,22 @@
   const char *extend_str = (extend == HB_PAINT_EXTEND_PAD)
 			    ? "/Extend [true true]\n" : "";
 
+  if (extend == HB_PAINT_EXTEND_REPEAT || extend == HB_PAINT_EXTEND_REFLECT)
+  {
+    hb_pdf_gradient_span_t span;
+    if (hb_pdf_linear_gradient_span (paint,
+				     paint->sx (gx0), paint->sy (gy0),
+				     paint->sx (gx1), paint->sy (gy1),
+				     &span))
+    {
+      const float c0[3] = {gx0, gy0, 0.f}, c1[3] = {gx1, gy1, 0.f};
+      hb_pdf_gradient_periods_t periods (2, c0, c1, span,
+					 extend == HB_PAINT_EXTEND_REFLECT);
+      hb_pdf_paint_gradient_periods (paint, res, periods, func_id);
+    }
+    return;
+  }
+
   /* Build Type 2 (axial) shading — color only. */
   hb_vector_buf_t sh;
   sh.append_str ("<< /ShadingType 2 /ColorSpace /DeviceRGB\n");
@@ -1210,6 +1808,22 @@
   const char *extend_str = (extend == HB_PAINT_EXTEND_PAD)
 			    ? "/Extend [true true]\n" : "";
 
+  if (extend == HB_PAINT_EXTEND_REPEAT || extend == HB_PAINT_EXTEND_REFLECT)
+  {
+    hb_pdf_gradient_span_t span;
+    if (hb_pdf_radial_gradient_span (paint,
+				     paint->sx (gx0), paint->sy (gy0), paint->sx (gr0),
+				     paint->sx (gx1), paint->sy (gy1), paint->sx (gr1),
+				     &span))
+    {
+      const float c0[3] = {gx0, gy0, gr0}, c1[3] = {gx1, gy1, gr1};
+      hb_pdf_gradient_periods_t periods (3, c0, c1, span,
+					 extend == HB_PAINT_EXTEND_REFLECT);
+      hb_pdf_paint_gradient_periods (paint, res, periods, func_id);
+    }
+    return;
+  }
+
   /* Build Type 3 (radial) shading — color only. */
   hb_vector_buf_t sh;
   sh.append_str ("<< /ShadingType 3 /ColorSpace /DeviceRGB\n");
diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh
index 6245df7..00e7230 100644
--- a/src/hb-vector-paint.hh
+++ b/src/hb-vector-paint.hh
@@ -213,6 +213,37 @@
     scope.clip = clip_bounds (e);
   }
 
+  /* The bounds of what the open clips let through, as their corners in
+   * the current space, in order around them.  Returns BOUNDED if it has
+   * set them, EMPTY if the clips let nothing through, and UNBOUNDED if
+   * they do not bound it, or the transform in effect has no inverse to
+   * bring the bounds back through. */
+  hb_bounds_t<>::status_t clip_corners (float x[4], float y[4]) const
+  {
+    hb_bounds_t<> bounds;
+    for (const auto &scope : scopes)
+      bounds.intersect (scope.clip);
+
+    if (bounds.status != hb_bounds_t<>::BOUNDED)
+      return bounds.status;
+
+    hb_transform_t<> t = current_ctm ();
+    float det = t.xx * t.yy - t.xy * t.yx;
+    if (!(fabsf (det) > 1e-12f))
+      return hb_bounds_t<>::UNBOUNDED;
+
+    const hb_extents_t<> &e = bounds.extents;
+    const float xs[4] = {e.xmin, e.xmax, e.xmax, e.xmin};
+    const float ys[4] = {e.ymin, e.ymin, e.ymax, e.ymax};
+    for (unsigned i = 0; i < 4; i++)
+    {
+      float dx = xs[i] - t.x0, dy = ys[i] - t.y0;
+      x[i] = ( t.yy * dx - t.xy * dy) / det;
+      y[i] = (-t.yx * dx + t.xx * dy) / det;
+    }
+    return hb_bounds_t<>::BOUNDED;
+  }
+
   /* The radius that a sweep gradient centered at (@cx, @cy) has to
    * reach to cover everything the open clips let through: zero if that
    * is nothing, and @max_radius if the clips do not bound it.
@@ -225,31 +256,19 @@
    * shapes whose coordinates, in pixels, get near 2^17. */
   float sweep_radius (float cx, float cy, float max_radius) const
   {
-    hb_bounds_t<> bounds;
-    for (const auto &scope : scopes)
-      bounds.intersect (scope.clip);
+    float x[4], y[4];
+    switch (clip_corners (x, y))
+    {
+      case hb_bounds_t<>::EMPTY:     return 0.f;
+      case hb_bounds_t<>::UNBOUNDED: return max_radius;
+      case hb_bounds_t<>::BOUNDED:   break;
+    }
 
-    if (bounds.status == hb_bounds_t<>::EMPTY)
-      return 0.f;
-    if (bounds.status == hb_bounds_t<>::UNBOUNDED)
-      return max_radius;
-
-    /* Bring the corners of the bounds into the gradient's space. */
-    hb_transform_t<> t = current_ctm ();
-    float det = t.xx * t.yy - t.xy * t.yx;
-    if (!(fabsf (det) > 1e-12f))
-      return max_radius;
-
-    const hb_extents_t<> &e = bounds.extents;
-    float xs[2] = {e.xmin, e.xmax};
-    float ys[2] = {e.ymin, e.ymax};
     float r2 = 0.f;
     for (unsigned i = 0; i < 4; i++)
     {
-      float dx = xs[i & 1] - t.x0, dy = ys[i >> 1] - t.y0;
-      float x = ( t.yy * dx - t.xy * dy) / det - cx;
-      float y = (-t.yx * dx + t.xx * dy) / det - cy;
-      r2 = hb_max (r2, x * x + y * y);
+      float dx = x[i] - cx, dy = y[i] - cy;
+      r2 = hb_max (r2, dx * dx + dy * dy);
     }
 
     float r = ceilf (sqrtf (r2)) + 1.f;
diff --git a/src/hb-vector.cc b/src/hb-vector.cc
index fdb0b35..4e5a3ef 100644
--- a/src/hb-vector.cc
+++ b/src/hb-vector.cc
@@ -63,10 +63,6 @@
  * hb_blob_t *svg = hb_vector_paint_render (paint);
  * ]|
  *
- * For linear and radial gradients, PDF output supports only
- * %HB_PAINT_EXTEND_PAD. %HB_PAINT_EXTEND_REPEAT and
- * %HB_PAINT_EXTEND_REFLECT are not supported for these gradients.
- *
  * The blend modes of #hb_paint_composite_mode_t are written as the
  * blend modes that SVG and PDF have, and the Porter-Duff modes with
  * masks. SVG output relies on CSS for both, so its viewer has to
diff --git a/test/vector/data/expected/basic/colrv1-extend-repeat-reflect.pdf b/test/vector/data/expected/basic/colrv1-extend-repeat-reflect.pdf
new file mode 100644
index 0000000..2a13167
--- /dev/null
+++ b/test/vector/data/expected/basic/colrv1-extend-repeat-reflect.pdf
@@ -0,0 +1,641 @@
+%PDF-1.4
+%ÀÁÂÃ
+1 0 obj
+<< /Type /Catalog /Pages 2 0 R >>
+endobj
+2 0 obj
+<< /Type /Pages /Kids [3 0 R] /Count 1 >>
+endobj
+3 0 obj
+<< /Type /Page /Parent 2 0 R /MediaBox [-16 -16 6016 1016]
+/Contents 4 0 R
+/Resources << /Shading << /SH0 9 0 R /SH1 15 0 R /SH2 16 0 R /SH3 21 0 R /SH4 22 0 R /SH5 23 0 R /SH6 24 0 R /SH7 25 0 R /SH8 33 0 R /SH9 34 0 R /SH10 35 0 R /SH11 36 0 R /SH12 37 0 R /SH13 43 0 R /SH14 44 0 R /SH15 45 0 R /SH16 46 0 R /SH17 47 0 R /SH18 54 0 R /SH19 55 0 R /SH20 56 0 R /SH21 57 0 R /SH22 58 0 R >> /XObject << /Fm0 27 0 R /Fm1 38 0 R /Fm2 48 0 R /Fm3 59 0 R >> >> >>
+endobj
+4 0 obj
+<< /Length 1327 >>
+stream
+1 1 1 rg
+-16 -16 6032 1032 re f
+q
+1 0 0 1 0 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/SH0 sh
+q
+1 0 0 1 4.7969 0 cm
+/SH0 sh
+Q
+q
+1 0 0 1 9.5938 0 cm
+/SH0 sh
+Q
+q
+1 0 0 1 14.3906 0 cm
+/SH0 sh
+Q
+Q
+Q
+Q
+Q
+Q
+Q
+q
+1 0 0 1 1000 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/SH1 sh
+q
+1 0 0 1 4.7969 0 cm
+/SH2 sh
+Q
+q
+1 0 0 1 9.5938 0 cm
+/SH1 sh
+Q
+q
+1 0 0 1 14.3906 0 cm
+/SH2 sh
+Q
+Q
+Q
+Q
+Q
+Q
+Q
+q
+1 0 0 1 2000 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/Fm0 Do
+Q
+Q
+Q
+Q
+Q
+Q
+q
+1 0 0 1 3000 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/Fm1 Do
+Q
+Q
+Q
+Q
+Q
+Q
+q
+1 0 0 1 4000 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/Fm2 Do
+Q
+Q
+Q
+Q
+Q
+Q
+q
+1 0 0 1 5000 0 cm
+q
+64 0 0 64 0 0 cm
+q
+0 0 15.62 15.62 re W n
+q
+0.015625 0 0 0.015625 0 0 cm
+q
+0 0 m
+0 1000 l
+1000 1000 l
+1000 0 l
+0 0 l
+h
+W n
+q
+64 0 0 64 0 0 cm
+/Fm3 Do
+Q
+Q
+Q
+Q
+Q
+Q
+endstream
+endobj
+5 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+6 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+7 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [5 0 R 6 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+8 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [7 0 R 7 0 R]
+/Bounds [1]
+/Encode [0 1 0 1] >>
+endobj
+9 0 obj
+<< /ShadingType 2 /ColorSpace /DeviceRGB
+/Coords [0 16 4.8718 16]
+/Function 8 0 R
+/Domain [0 1.0156]
+>>
+endobj
+10 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+11 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+12 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [10 0 R 11 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+13 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [12 0 R 12 0 R]
+/Bounds [1]
+/Encode [0 1 1 0] >>
+endobj
+14 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [12 0 R 12 0 R]
+/Bounds [1]
+/Encode [1 0 0 1] >>
+endobj
+15 0 obj
+<< /ShadingType 2 /ColorSpace /DeviceRGB
+/Coords [0 16 4.8718 16]
+/Function 13 0 R
+/Domain [0 1.0156]
+>>
+endobj
+16 0 obj
+<< /ShadingType 2 /ColorSpace /DeviceRGB
+/Coords [0 16 4.8718 16]
+/Function 14 0 R
+/Domain [0 1.0156]
+>>
+endobj
+17 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+18 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+19 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [17 0 R 18 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+20 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [19 0 R 19 0 R]
+/Bounds [1]
+/Encode [0 1 0 1] >>
+endobj
+21 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 16 2.59 12 20]
+/Function 20 0 R
+/Domain [0 1]
+/Extend [true false]
+>>
+endobj
+22 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 12 2.59 12 16.06]
+/Function 20 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+23 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 8 2.59 12 12.06]
+/Function 20 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+24 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 4 2.59 12 8.06]
+/Function 20 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+25 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 0 2.59 12 4.06]
+/Function 20 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+26 0 obj
+ << /Shading << /SH0 9 0 R /SH1 15 0 R /SH2 16 0 R /SH3 21 0 R /SH4 22 0 R /SH5 23 0 R /SH6 24 0 R /SH7 25 0 R /SH8 33 0 R /SH9 34 0 R /SH10 35 0 R /SH11 36 0 R /SH12 37 0 R /SH13 43 0 R /SH14 44 0 R /SH15 45 0 R /SH16 46 0 R /SH17 47 0 R /SH18 54 0 R /SH19 55 0 R /SH20 56 0 R /SH21 57 0 R /SH22 58 0 R >> /XObject << /Fm0 27 0 R /Fm1 38 0 R /Fm2 48 0 R /Fm3 59 0 R >> >>
+endobj
+27 0 obj
+<< /Type /XObject /Subtype /Form
+/BBox [-32767 -32767 32767 32767]
+/Group << /S /Transparency /CS /DeviceRGB /I true >>
+/Resources 26 0 R
+/Length 40 >>
+stream
+/SH3 sh
+/SH4 sh
+/SH5 sh
+/SH6 sh
+/SH7 sh
+endstream
+endobj
+28 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+29 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+30 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [28 0 R 29 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+31 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [30 0 R 30 0 R]
+/Bounds [1]
+/Encode [0 1 1 0] >>
+endobj
+32 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [30 0 R 30 0 R]
+/Bounds [1]
+/Encode [1 0 0 1] >>
+endobj
+33 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 16 2.59 12 20]
+/Function 31 0 R
+/Domain [0 1]
+/Extend [true false]
+>>
+endobj
+34 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 12 2.59 12 16.06]
+/Function 32 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+35 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 8 2.59 12 12.06]
+/Function 31 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+36 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 4 2.59 12 8.06]
+/Function 32 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+37 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [2.59 12 0 2.59 12 4.06]
+/Function 31 0 R
+/Domain [0 1.0156]
+/Extend [true false]
+>>
+endobj
+38 0 obj
+<< /Type /XObject /Subtype /Form
+/BBox [-32767 -32767 32767 32767]
+/Group << /S /Transparency /CS /DeviceRGB /I true >>
+/Resources 26 0 R
+/Length 43 >>
+stream
+/SH8 sh
+/SH9 sh
+/SH10 sh
+/SH11 sh
+/SH12 sh
+endstream
+endobj
+39 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+40 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+41 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [39 0 R 40 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+42 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [41 0 R 41 0 R]
+/Bounds [1]
+/Encode [0 1 0 1] >>
+endobj
+43 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [1.56 7.81 0 8.59 7.81 2.34]
+/Function 42 0 R
+/Domain [0 1.5]
+>>
+endobj
+44 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [6.25 7.81 1.56 13.28 7.81 3.91]
+/Function 42 0 R
+/Domain [0 1.5]
+>>
+endobj
+45 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [10.94 7.81 3.12 17.97 7.81 5.47]
+/Function 42 0 R
+/Domain [0 1.5]
+>>
+endobj
+46 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [15.62 7.81 4.69 22.66 7.81 7.03]
+/Function 42 0 R
+/Domain [0 1.5]
+>>
+endobj
+47 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [20.31 7.81 6.25 25 7.81 7.81]
+/Function 42 0 R
+/Domain [0 1]
+>>
+endobj
+48 0 obj
+<< /Type /XObject /Subtype /Form
+/BBox [-32767 -32767 32767 32767]
+/Group << /S /Transparency /CS /DeviceRGB /I true >>
+/Resources 26 0 R
+/Length 45 >>
+stream
+/SH13 sh
+/SH14 sh
+/SH15 sh
+/SH16 sh
+/SH17 sh
+endstream
+endobj
+49 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [0 0.502 0]
+/C1 [1 1 1] >>
+endobj
+50 0 obj
+<< /FunctionType 2 /Domain [0 1] /N 1
+/C0 [1 1 1]
+/C1 [1 0 0] >>
+endobj
+51 0 obj
+<< /FunctionType 3 /Domain [0 1]
+/Functions [49 0 R 50 0 R]
+/Bounds [0.5]
+/Encode [0 1 0 1] >>
+endobj
+52 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [51 0 R 51 0 R]
+/Bounds [1]
+/Encode [0 1 1 0] >>
+endobj
+53 0 obj
+<< /FunctionType 3 /Domain [0 2]
+/Functions [51 0 R 51 0 R]
+/Bounds [1]
+/Encode [1 0 0 1] >>
+endobj
+54 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [1.56 7.81 0 8.59 7.81 2.34]
+/Function 53 0 R
+/Domain [0 1.5]
+>>
+endobj
+55 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [6.25 7.81 1.56 13.28 7.81 3.91]
+/Function 52 0 R
+/Domain [0 1.5]
+>>
+endobj
+56 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [10.94 7.81 3.12 17.97 7.81 5.47]
+/Function 53 0 R
+/Domain [0 1.5]
+>>
+endobj
+57 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [15.62 7.81 4.69 22.66 7.81 7.03]
+/Function 52 0 R
+/Domain [0 1.5]
+>>
+endobj
+58 0 obj
+<< /ShadingType 3 /ColorSpace /DeviceRGB
+/Coords [20.31 7.81 6.25 25 7.81 7.81]
+/Function 53 0 R
+/Domain [0 1]
+>>
+endobj
+59 0 obj
+<< /Type /XObject /Subtype /Form
+/BBox [-32767 -32767 32767 32767]
+/Group << /S /Transparency /CS /DeviceRGB /I true >>
+/Resources 26 0 R
+/Length 45 >>
+stream
+/SH18 sh
+/SH19 sh
+/SH20 sh
+/SH21 sh
+/SH22 sh
+endstream
+endobj
+xref
+0 60
+0000000000 65535 f 
+0000000015 00000 n 
+0000000064 00000 n 
+0000000121 00000 n 
+0000000597 00000 n 
+0000001975 00000 n 
+0000002059 00000 n 
+0000002139 00000 n 
+0000002247 00000 n 
+0000002353 00000 n 
+0000002472 00000 n 
+0000002557 00000 n 
+0000002638 00000 n 
+0000002749 00000 n 
+0000002858 00000 n 
+0000002967 00000 n 
+0000003088 00000 n 
+0000003209 00000 n 
+0000003294 00000 n 
+0000003375 00000 n 
+0000003486 00000 n 
+0000003595 00000 n 
+0000003739 00000 n 
+0000003891 00000 n 
+0000004042 00000 n 
+0000004192 00000 n 
+0000004342 00000 n 
+0000004731 00000 n 
+0000004956 00000 n 
+0000005041 00000 n 
+0000005122 00000 n 
+0000005233 00000 n 
+0000005342 00000 n 
+0000005451 00000 n 
+0000005595 00000 n 
+0000005747 00000 n 
+0000005898 00000 n 
+0000006048 00000 n 
+0000006198 00000 n 
+0000006426 00000 n 
+0000006511 00000 n 
+0000006592 00000 n 
+0000006703 00000 n 
+0000006812 00000 n 
+0000006942 00000 n 
+0000007076 00000 n 
+0000007211 00000 n 
+0000007346 00000 n 
+0000007476 00000 n 
+0000007706 00000 n 
+0000007791 00000 n 
+0000007872 00000 n 
+0000007983 00000 n 
+0000008092 00000 n 
+0000008201 00000 n 
+0000008331 00000 n 
+0000008465 00000 n 
+0000008600 00000 n 
+0000008735 00000 n 
+0000008865 00000 n 
+trailer
+<< /Size 60 /Root 1 0 R >>
+startxref
+9095
+%%EOF
diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests
index d1413b1..4d33f13 100644
--- a/test/vector/data/tests/basic.tests
+++ b/test/vector/data/tests/basic.tests
@@ -17,6 +17,7 @@
 ../../../api/fonts/TwemojiMozilla.subset.default.32,3299.ttf;--font-size=65536;U+3299;../expected/basic/colrv0-large-scale.svg
 ../../../api/fonts/TwemojiMozilla.subset.default.32,3299.ttf;--font-size=65536;U+3299;../expected/basic/colrv0-large-scale.pdf
 ../../../api/fonts/Roboto-Regular.abc.ttf;--paint --show-extents;U+0061,U+0062,U+0063;../expected/basic/paint-show-extents.svg
+../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0501,U+F0502,U+F0504,U+F0505,U+F0507,U+F0508;../expected/basic/colrv1-extend-repeat-reflect.pdf
 ../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A17;../expected/basic/colrv1-composite-multiply.svg
 ../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A00,U+F0A01,U+F0A02,U+F0A03,U+F0A04,U+F0A05,U+F0A06,U+F0A07,U+F0A08,U+F0A09,U+F0A0A,U+F0A0B,U+F0A0C;../expected/basic/colrv1-composite-porter-duff.svg
 ../../../api/fonts/test_glyphs-glyf_colr_1.ttf;;U+F0A00,U+F0A01,U+F0A02,U+F0A03,U+F0A04,U+F0A05,U+F0A06,U+F0A07,U+F0A08,U+F0A09,U+F0A0A,U+F0A0B,U+F0A0C;../expected/basic/colrv1-composite-porter-duff.pdf
diff --git a/test/vector/meson.build b/test/vector/meson.build
index 0770eb6..bb9cd80 100644
--- a/test/vector/meson.build
+++ b/test/vector/meson.build
@@ -30,6 +30,14 @@
   suite: 'vector',
 )
 
+test_vector_pdf_repeat = executable('test-vector-pdf-repeat', 'test-vector-pdf-repeat.c',
+  dependencies: [libharfbuzz_vector_dep],
+)
+
+test('pdf-repeat', test_vector_pdf_repeat,
+  suite: 'vector',
+)
+
 test_vector_composite = executable('test-vector-composite', 'test-vector-composite.c',
   dependencies: [libharfbuzz_vector_dep],
 )
diff --git a/test/vector/test-vector-pdf-repeat.c b/test/vector/test-vector-pdf-repeat.c
new file mode 100644
index 0000000..67fa4fb
--- /dev/null
+++ b/test/vector/test-vector-pdf-repeat.c
@@ -0,0 +1,309 @@
+/*
+ * 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.
+ */
+
+/* PDF shadings do not repeat, so a linear or radial gradient whose
+ * color line repeats or reflects is written one period at a time.
+ * Check which periods are written, and how. */
+
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+#include <assert.h>
+#include <string.h>
+
+#include <hb.h>
+#include <hb-vector.h>
+
+/* ---- a color line of two stops ---- */
+
+typedef struct
+{
+  hb_color_t colors[2];
+  hb_paint_extend_t extend;
+} line_t;
+
+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)
+{
+  line_t *line = (line_t *) color_line_data;
+  (void) color_line;
+  (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 = line->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 ((line_t *) color_line_data)->extend;
+}
+
+/* ---- a picture ---- */
+
+static hb_vector_paint_t *paint;
+static hb_paint_funcs_t *funcs;
+static line_t line;
+static hb_color_line_t color_line;
+
+static void
+begin (hb_paint_extend_t extend, unsigned alpha)
+{
+  paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_PDF);
+  assert (paint);
+  hb_vector_extents_t extents = {-100, -100, 200, 200};
+  hb_vector_paint_set_extents (paint, &extents);
+  funcs = hb_vector_paint_get_funcs (paint);
+
+  line.colors[0] = HB_COLOR (0, 0, 255, 255);
+  line.colors[1] = HB_COLOR (255, 0, 0, alpha);
+  line.extend = extend;
+  memset (&color_line, 0, sizeof (color_line));
+  color_line.data = &line;
+  color_line.get_color_stops = get_stops;
+  color_line.get_extend = get_extend;
+}
+
+/* A gradient along the x axis from @x0 to @x1. */
+static void
+linear (float x0, float x1)
+{
+  hb_paint_linear_gradient (funcs, paint, &color_line,
+			    x0, 0, x1, 0, x0, x1 - x0);
+}
+
+/* What was painted: the document, and the content of its page. */
+static hb_blob_t *blob;
+static const char *doc, *page;
+static unsigned doc_len, page_len;
+
+static const char *
+find_in (const char *data, unsigned len, const char *needle)
+{
+  unsigned n = (unsigned) strlen (needle);
+  for (unsigned i = 0; i + n <= len; i++)
+    if (!memcmp (data + i, needle, n))
+      return data + i;
+  return NULL;
+}
+
+static unsigned
+count_in (const char *data, unsigned len, const char *needle)
+{
+  unsigned n = (unsigned) strlen (needle), count = 0;
+  for (unsigned i = 0; i + n <= len; i++)
+    if (!memcmp (data + i, needle, n))
+      count++;
+  return count;
+}
+
+static void
+end (void)
+{
+  blob = hb_vector_paint_render (paint);
+  assert (blob);
+  hb_vector_paint_destroy (paint);
+  doc = hb_blob_get_data (blob, &doc_len);
+
+  page = find_in (doc, doc_len, "\n4 0 obj\n");
+  assert (page);
+  page = find_in (page, doc_len - (unsigned) (page - doc), "stream\n");
+  assert (page);
+  page += strlen ("stream\n");
+  const char *stop = find_in (page, doc_len - (unsigned) (page - doc), "endstream");
+  assert (stop);
+  page_len = (unsigned) (stop - page);
+}
+
+#define IN_PAGE(s) (find_in (page, page_len, s) != NULL)
+#define IN_DOC(s) (find_in (doc, doc_len, s) != NULL)
+#define PAGE_COUNT(s) count_in (page, page_len, s)
+#define DOC_COUNT(s) count_in (doc, doc_len, s)
+
+int
+main (void)
+{
+  /* Ten periods fill the clip: one shading, painted in ten places. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 100, 10);
+  linear (0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 10);
+  assert (DOC_COUNT ("/ShadingType 2") == 1);
+  assert (IN_PAGE ("1 0 0 1 10 0 cm\n"));
+  assert (IN_PAGE ("1 0 0 1 90 0 cm\n"));
+  assert (!IN_PAGE ("1 0 0 1 100 0 cm\n"));
+  assert (!IN_DOC ("/Extend"));
+  hb_blob_destroy (blob);
+
+  /* The color line is backwards in every other one if it reflects,
+   * also before its own start. */
+  begin (HB_PAINT_EXTEND_REFLECT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 100, 10);
+  linear (25, 35);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 11);
+  assert (DOC_COUNT ("/ShadingType 2") == 2);
+  assert (IN_DOC ("/Encode [0 1 1 0]"));
+  assert (IN_DOC ("/Encode [1 0 0 1]"));
+  assert (IN_PAGE ("1 0 0 1 -30 0 cm\n/SH1 sh\n"));
+  assert (IN_PAGE ("1 0 0 1 -20 0 cm\n/SH0 sh\n"));
+  assert (IN_PAGE ("1 0 0 1 70 0 cm\n/SH1 sh\n"));
+  assert (!IN_PAGE ("1 0 0 1 80 0 cm\n"));
+  hb_blob_destroy (blob);
+
+  /* A color line much shorter than what it fills: no more periods
+   * than the limit, around the color line itself. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 100, 10);
+  linear (50, 50.01f);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 256);
+  assert (DOC_COUNT ("/ShadingType 2") == 1);
+  hb_blob_destroy (blob);
+
+  /* The same goes for a gradient that nothing clips. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  linear (0, 10);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 256);
+  assert (IN_PAGE ("1 0 0 1 -1280 0 cm\n"));
+  assert (IN_PAGE ("1 0 0 1 1270 0 cm\n"));
+  hb_blob_destroy (blob);
+
+  /* Nothing is painted under a clip that lets nothing through. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, 10, 0, 10, 10);
+  linear (0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 0);
+  assert (DOC_COUNT ("/ShadingType") == 0);
+  hb_blob_destroy (blob);
+
+  /* Under a transform that rotates, the periods are painted in the
+   * space of the root: the transform is undone, and they step along
+   * the rotated line.  The bounds of the clip are kept in that space,
+   * where they are wider than the clip, by a period at each end. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_transform (funcs, paint, 0.8f, 0.6f, -0.6f, 0.8f, 0, 0);
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 100, 10);
+  linear (0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  hb_paint_pop_transform (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 12);
+  assert (IN_PAGE ("0.8 -0.6 0.6 0.8 0 0 cm\n"));
+  assert (IN_PAGE ("1 0 0 1 -8 -6 cm\n"));
+  assert (IN_PAGE ("1 0 0 1 80 60 cm\n"));
+  hb_blob_destroy (blob);
+
+  /* Through a soft mask of their alphas, the periods are painted as
+   * one group. */
+  begin (HB_PAINT_EXTEND_REPEAT, 128);
+  hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 100, 10);
+  linear (0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 0);
+  assert (PAGE_COUNT (" Do\n") == 1);
+  assert (PAGE_COUNT (" gs\n") == 1);
+  assert (DOC_COUNT (" sh\n") == 20);
+  assert (DOC_COUNT ("/ShadingType 2 /ColorSpace /DeviceRGB") == 1);
+  assert (DOC_COUNT ("/ShadingType 2 /ColorSpace /DeviceGray") == 1);
+  assert (IN_DOC ("/SMask << /Type /Mask /S /Luminosity"));
+  hb_blob_destroy (blob);
+
+  /* Circles around one center are rings: a shading each, with its hole
+   * filled, from the outside in.  They are painted on top of each
+   * other, so as one group. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, -50, -50, 50, 50);
+  hb_paint_radial_gradient (funcs, paint, &color_line, 0, 0, 0, 0, 0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" sh\n") == 0);
+  assert (PAGE_COUNT (" Do\n") == 1);
+  assert (PAGE_COUNT (" gs\n") == 0);
+  assert (DOC_COUNT (" sh\n") == 8);
+  assert (DOC_COUNT ("/ShadingType 3") == 8);
+  assert (DOC_COUNT ("/Extend [true false]") == 8);
+  assert (IN_DOC ("/SH0 sh\n/SH1 sh\n"));
+  assert (find_in (doc, doc_len, "/Coords [0 0 70 0 0 80]") <
+	  find_in (doc, doc_len, "/Coords [0 0 0 0 0 10.16]"));
+  hb_blob_destroy (blob);
+
+  /* Rings that shrink along the color line are written growing, with
+   * the color line backwards. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, -50, -50, 50, 50);
+  hb_paint_radial_gradient (funcs, paint, &color_line, 0, 0, 20, 0, 0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" Do\n") == 1);
+  assert (DOC_COUNT (" sh\n") == 8);
+  assert (DOC_COUNT ("/Extend [true false]") == 8);
+  assert (IN_DOC ("/Encode [1 0 1 0]"));
+  assert (find_in (doc, doc_len, "/Coords [0 0 70 0 0 80]") <
+	  find_in (doc, doc_len, "/Coords [0 0 0 0 0 10.16]"));
+  hb_blob_destroy (blob);
+
+  /* Circles that are not inside one another are painted later over
+   * earlier, as they are, each period half way into the next, and
+   * stop where the radius is zero. */
+  begin (HB_PAINT_EXTEND_REPEAT, 255);
+  hb_paint_push_clip_rectangle (funcs, paint, -50, -50, 50, 50);
+  hb_paint_radial_gradient (funcs, paint, &color_line, 0, 0, 5, 20, 0, 10);
+  hb_paint_pop_clip (funcs, paint);
+  end ();
+  assert (PAGE_COUNT (" Do\n") == 1);
+  assert (!IN_DOC ("/Extend"));
+  assert (IN_DOC ("/Coords [-20 0 0 10 0 7.5]\n"));
+  assert (find_in (doc, doc_len, "/Coords [-20 0 0 10 0 7.5]") <
+	  find_in (doc, doc_len, "/Coords [0 0 5 30 0 12.5]"));
+  hb_blob_destroy (blob);
+
+  return 0;
+}