[vector] Pad a PDF gradient in the color of the outer stop at its end
A color line that pads goes on before its first stop in that stop's
color, and after its last stop in that one's. Two stops can share
the first offset, or the last: red before the gradient, say, and blue
right from where it starts.
hb-vector wrote that as a shading whose function has a piece of no
length at its end, and a shading that extends goes on in what its
function gives at 0 and at 1. In pdfium, MuPDF and poppler that is
the color of the inner one of the two stops, so the padding on that
side was the wrong color; Ghostscript had the start right and the end
wrong. The SVG output has both right.
Lengthen such a color line by a stretch in the outer stop's color,
and move that end of the shading out with it. The stretch is a
fifteenth of the length, which puts the two stops a sixteenth in from
the end: pdfium samples the function at every 256th, and has a sample
there. A radial gradient is lengthened no further than halfway to a
radius of zero; one that starts or ends there paints nothing beyond,
and is written without the outer stop.
Testing: a new test checks what is written for linear and radial
gradients with such stops, and fails without the change. Replaying
the same paint calls on hb-raster and hb-vector for 300 random scenes
of them, the pixels are the same in:
before after
pdfium 145 300
MuPDF 145 300
poppler cairo 144 300
poppler Splash 144 300
Ghostscript 207 296
and in Chrome and librsvg in all 300 before and after. Of the four
that Ghostscript still differs on, the two that were reduced are a
linear gradient under a rotation, which it gets wrong whatever the
color line. Scenes of other gradients, 1500 of them, are written
byte for byte as before, and so are all 1553 mapped glyphs of the
COLR test fonts.
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, 241
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-pdf.cc b/src/hb-vector-paint-pdf.cc
index e8d0987..05ebdfd 100644
--- a/src/hb-vector-paint-pdf.cc
+++ b/src/hb-vector-paint-pdf.cc
@@ -926,6 +926,91 @@
return false;
}
+/* A color line that pads goes on before its first stop in that stop's
+ * color, and after its last stop in that one's. A shading that extends
+ * goes on in what its function gives at 0 and at 1. Where two stops
+ * share the first offset, or the last, that is the color of the inner
+ * one of the two in most viewers: the outer one has no stretch of the
+ * function to itself.
+ *
+ * Gives it one. Lengthens the color line in paint->color_stops_scratch,
+ * which is sorted and goes from 0 to 1, by a stretch in the outer stop's
+ * color at such an end, and moves that end of the gradient out with it.
+ * The two stops are then a sixteenth in from the end, where a viewer
+ * that samples the function at every 256th, as pdfium does, has a
+ * sample.
+ *
+ * A radial gradient, which has radii, is lengthened no further than
+ * halfway to where its radius is zero. One that starts or ends there
+ * paints nothing beyond, and does without the outer stop. */
+static void
+hb_pdf_pad_color_line (hb_vector_paint_t *paint,
+ float *x0, float *y0, float *r0,
+ float *x1, float *y1, float *r1)
+{
+ hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
+ unsigned count = stops.length;
+
+ /* How many stops share the first offset, and the last. */
+ unsigned first = 1, last = 1;
+ while (first < count && stops.arrayZ[first].offset == stops.arrayZ[0].offset)
+ first++;
+ while (last < count && stops.arrayZ[count - 1 - last].offset == stops.arrayZ[count - 1].offset)
+ last++;
+ if ((first == 1 && last == 1) || first + last > count)
+ return;
+
+ float length = first > 1 && last > 1 ? 1.f / 14 : 1.f / 15;
+ float before = first > 1 ? length : 0.f, after = last > 1 ? length : 0.f;
+ if (r0)
+ {
+ float dr = *r1 - *r0;
+ if (dr > 0.f) before = hb_min (before, *r0 / (2 * dr));
+ if (dr < 0.f) after = hb_min (after, *r1 / (-2 * dr));
+ }
+
+ hb_vector_t<hb_color_stop_t> out;
+ if (unlikely (!out.alloc (count + 2)))
+ return;
+ unsigned i = 0, end = count;
+ if (first > 1)
+ {
+ if (before > 0.f)
+ {
+ hb_color_stop_t stop = stops.arrayZ[0];
+ stop.offset = -before;
+ out.push (stop);
+ }
+ else
+ i = first - 1;
+ }
+ if (last > 1 && !(after > 0.f))
+ end = count - (last - 1);
+ for (; i < end; i++)
+ out.push (stops.arrayZ[i]);
+ if (last > 1 && after > 0.f)
+ {
+ hb_color_stop_t stop = stops.arrayZ[count - 1];
+ stop.offset = 1.f + after;
+ out.push (stop);
+ }
+ for (auto &stop : out)
+ stop.offset = (stop.offset + before) / (1.f + before + after);
+ hb_swap (stops, out);
+
+ float dx = *x1 - *x0, dy = *y1 - *y0;
+ *x0 -= before * dx;
+ *y0 -= before * dy;
+ *x1 += after * dx;
+ *y1 += after * dy;
+ if (r0)
+ {
+ float dr = *r1 - *r0;
+ *r0 -= before * dr;
+ *r1 += after * dr;
+ }
+}
+
/* Build a PDF Type 2 (exponential interpolation) function for
* a single alpha stop pair (DeviceGray, scalar output). */
static void
@@ -1141,6 +1226,9 @@
if (gx0 == gx1 && gy0 == gy1)
return;
+ if (hb_color_line_get_extend (color_line) == HB_PAINT_EXTEND_PAD)
+ hb_pdf_pad_color_line (paint, &gx0, &gy0, nullptr, &gx1, &gy1, nullptr);
+
unsigned func_id = hb_pdf_build_gradient_function_from_stops (res, paint);
hb_paint_extend_t extend = hb_color_line_get_extend (color_line);
@@ -1283,6 +1371,9 @@
&gx0, &gy0, &gr0, &gx1, &gy1, &gr1))
return;
+ if (hb_color_line_get_extend (color_line) == HB_PAINT_EXTEND_PAD)
+ hb_pdf_pad_color_line (paint, &gx0, &gy0, &gr0, &gx1, &gy1, &gr1);
+
unsigned func_id = hb_pdf_build_gradient_function_from_stops (res, paint);
hb_paint_extend_t extend = hb_color_line_get_extend (color_line);
diff --git a/test/vector/meson.build b/test/vector/meson.build
index b1975c5..6c648fe 100644
--- a/test/vector/meson.build
+++ b/test/vector/meson.build
@@ -38,6 +38,14 @@
suite: 'vector',
)
+test_vector_pdf_pad = executable('test-vector-pdf-pad', 'test-vector-pdf-pad.c',
+ dependencies: [libharfbuzz_vector_dep],
+)
+
+test('pdf-pad', test_vector_pdf_pad,
+ suite: 'vector',
+)
+
test_vector_sweep = executable('test-vector-sweep', 'test-vector-sweep.c',
dependencies: [libharfbuzz_vector_dep],
)
diff --git a/test/vector/test-vector-pdf-pad.c b/test/vector/test-vector-pdf-pad.c
new file mode 100644
index 0000000..85895ea
--- /dev/null
+++ b/test/vector/test-vector-pdf-pad.c
@@ -0,0 +1,221 @@
+/*
+ * 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.
+ */
+
+/* A color line that pads goes on before its first stop in that stop's
+ * color, and after its last stop in that one's. Where two stops share
+ * the first offset or the last, a PDF shading would go on in the color
+ * of the inner one. Check that the color line is lengthened so that
+ * the outer one has a stretch of the shading to itself. */
+
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+#include <assert.h>
+#include <string.h>
+
+#include <hb.h>
+#include <hb-vector.h>
+
+/* ---- a color line of up to four stops: red, blue, green, red ---- */
+
+typedef struct
+{
+ unsigned count;
+ float offsets[4];
+ 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)
+{
+ static const hb_color_t colors[4] = {HB_COLOR (0, 0, 255, 255),
+ HB_COLOR (255, 0, 0, 255),
+ HB_COLOR (0, 255, 0, 255),
+ HB_COLOR (0, 0, 255, 255)};
+ line_t *line = (line_t *) color_line_data;
+ (void) color_line;
+ (void) user_data;
+ if (count)
+ {
+ unsigned n = 0;
+ for (unsigned i = start; i < line->count && n < *count; i++, n++)
+ {
+ stops[n].offset = line->offsets[i];
+ stops[n].is_foreground = 0;
+ stops[n].color = colors[i];
+ }
+ *count = n;
+ }
+ return line->count;
+}
+
+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;
+}
+
+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 whose color line has @count stops at @offsets: along
+ * the x axis from 0 to @length if @r1 is negative, and otherwise in
+ * circles around the origin of radius @r0 at 0 and @r1 at 1. */
+static hb_blob_t *
+render (hb_vector_format_t format,
+ float length, float r0, float r1,
+ unsigned count, const float *offsets,
+ hb_paint_extend_t extend)
+{
+ hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (format);
+ assert (paint);
+ hb_vector_extents_t extents = {-200, -200, 400, 400};
+ hb_vector_paint_set_extents (paint, &extents);
+ hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint);
+
+ line_t line;
+ line.count = count;
+ for (unsigned i = 0; i < count; i++)
+ line.offsets[i] = offsets[i];
+ line.extend = extend;
+ hb_color_line_t color_line;
+ memset (&color_line, 0, sizeof (color_line));
+ color_line.data = &line;
+ color_line.get_color_stops = get_stops;
+ color_line.get_extend = get_extend;
+
+ hb_paint_push_clip_rectangle (funcs, paint, -200, -200, 200, 200);
+ if (r1 < 0)
+ hb_paint_linear_gradient (funcs, paint, &color_line, 0, 0, length, 0, 0, length);
+ else
+ hb_paint_radial_gradient (funcs, paint, &color_line, 0, 0, r0, 0, 0, r1);
+ 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)
+{
+ static const float at_start[3] = {0, 0, 1};
+ static const float at_end[3] = {0, 1, 1};
+ static const float at_both[4] = {0, 0, 1, 1};
+ hb_blob_t *blob;
+
+ /* Red and blue at 0, green at 1, along 150: red before x = 0. The
+ * shading starts a fifteenth earlier, at -10, and is red up to a
+ * sixteenth of the way. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 150, 0, -1, 3, at_start, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [-10 0 150 0]"));
+ assert (has ("/Bounds [0.0625 0.0625]"));
+ assert (has ("/C0 [1 0 0]\n/C1 [1 0 0]"));
+ assert (has ("/Extend [true true]"));
+ hb_blob_destroy (blob);
+
+ /* Red at 0, blue and green at 1: green after x = 150. The shading
+ * ends a fifteenth later, and is green for its last sixteenth. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 150, 0, -1, 3, at_end, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [0 0 160 0]"));
+ assert (has ("/Bounds [0.9375 0.9375]"));
+ assert (has ("/C0 [0 1 0]\n/C1 [0 1 0]"));
+ hb_blob_destroy (blob);
+
+ /* Both, along 140: a fourteenth more at either end. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 140, 0, -1, 4, at_both, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [-10 0 150 0]"));
+ assert (has ("/Bounds [0.0625 0.0625 0.9375 0.9375]"));
+ hb_blob_destroy (blob);
+
+ /* A color line that repeats has nothing before it, and gets no
+ * stretch of red. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 150, 0, -1, 3, at_start, HB_PAINT_EXTEND_REPEAT);
+ assert (has ("/C0 [1 0 0]\n/C1 [0 0 1]"));
+ assert (!has ("/C0 [1 0 0]\n/C1 [1 0 0]"));
+ hb_blob_destroy (blob);
+
+ /* SVG has the color of the first stop before the color line, and
+ * that of the last after it: the stops are written as they are. */
+ blob = render (HB_VECTOR_FORMAT_SVG, 150, 0, -1, 3, at_start, HB_PAINT_EXTEND_PAD);
+ assert (has (" x1=\"0\" y1=\"0\" x2=\"150\" y2=\"0\""));
+ assert (has ("<stop offset=\"0\" stop-color=\"rgb(255,0,0)\"/>\n"
+ "<stop offset=\"0\" stop-color=\"rgb(0,0,255)\"/>\n"
+ "<stop offset=\"1\" stop-color=\"rgb(0,255,0)\"/>"));
+ hb_blob_destroy (blob);
+
+ /* Circles from a radius of 30 to 180: red within 30. The shading
+ * starts a fifteenth earlier, at a radius of 20. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 0, 30, 180, 3, at_start, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [0 0 20 0 0 180]"));
+ assert (has ("/Bounds [0.0625 0.0625]"));
+ hb_blob_destroy (blob);
+
+ /* From a radius of 6 to 156, a fifteenth earlier would be at a radius
+ * of -4. The shading starts halfway to zero, at 3. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 0, 6, 156, 3, at_start, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [0 0 3 0 0 156]"));
+ assert (has ("/Bounds [0.0196 0.0196]"));
+ hb_blob_destroy (blob);
+
+ /* From a radius of 0 nothing is painted before the color line, and
+ * the red stop is not written. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 0, 0, 150, 3, at_start, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [0 0 0 0 0 150]"));
+ assert (!has ("/Bounds"));
+ assert (has ("/C0 [0 0 1]\n/C1 [0 1 0]"));
+ assert (!has ("/C0 [1 0 0]"));
+ hb_blob_destroy (blob);
+
+ /* Circles that shrink to nothing, with blue and green where they
+ * end: nothing is painted after the color line either. */
+ blob = render (HB_VECTOR_FORMAT_PDF, 0, 150, 0, 3, at_end, HB_PAINT_EXTEND_PAD);
+ assert (has ("/Coords [0 0 150 0 0 0]"));
+ assert (!has ("/Bounds"));
+ assert (has ("/C0 [1 0 0]\n/C1 [0 0 1]"));
+ hb_blob_destroy (blob);
+
+ return 0;
+}