[vector] Clip everything out in PDF when the clip has no outline A clip to a glyph with no outline, or to a path with no points, lets nothing through. The PDF writer wrote the points it had and then `W n`, and with no points that is a `W` with no path, which leaves the clip as it was. What was painted under it then covered the page: a COLR glyph one of whose layers has an empty glyph came out with that layer's gradient all over it. The SVG writer and hb-raster clip everything out. Write a rectangle of no area as the path when there are no points. The same holds for a glyph whose outline was not extracted because the work budget was spent. Testing: U+0306 in the COLRv1 font ba51123c...ttf under test/shape/data/in-house has three layers, and the glyph of the first has no contours. Its PDF differed from hb-raster on 94% of the pixels in pdfium, poppler (Splash and cairo) and Ghostscript; MuPDF drew it as hb-raster does. Now all five do. Of the mapped glyphs of the COLR test fonts, it is the only one whose output changes. It is added as an expected output, and a new test checks the path written for a clip path with no points and for one with three. The test and the vector fuzzer are clean under ASan and UBSan, and the test under MSan. 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 22675f8..7334ae7 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc
@@ -491,6 +491,16 @@ } +/* A path that encloses nothing, for clipping to an outline that has + * no points: the clip then lets nothing through, as it should, where + * `W` with no path at all would leave the clip as it was and let + * everything through. */ +static void +hb_pdf_empty_path (hb_vector_buf_t &body) +{ + body.append_str ("0 0 0 0 re\n"); +} + /* ---- paint callbacks ---- */ static void @@ -610,6 +620,8 @@ body.append_str ("q\n"); hb_extents_t<> extents; hb_pdf_emit_glyph_path (paint, font, glyph, &body, &extents); + if (extents.is_void ()) + hb_pdf_empty_path (body); body.append_str ("W n\n"); if (auto *res = hb_pdf_get_resources (paint)) @@ -689,6 +701,8 @@ auto *paint = (hb_vector_paint_t *) paint_data; if (unlikely (!paint->ensure_initialized ())) return; + if (paint->clip_path_sink.extents && paint->clip_path_sink.extents->is_void ()) + hb_pdf_empty_path (paint->current_body ()); paint->current_body ().append_str ("W n\n"); auto *res = hb_pdf_get_resources (paint);
diff --git a/test/vector/data/expected/basic/colrv1-empty-layer.pdf b/test/vector/data/expected/basic/colrv1-empty-layer.pdf new file mode 100644 index 0000000..5cf4ae0 --- /dev/null +++ b/test/vector/data/expected/basic/colrv1-empty-layer.pdf
@@ -0,0 +1,205 @@ +%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 -656 876 1816] +/Contents 4 0 R +/Resources << /Shading << /SH0 6 0 R /SH1 13 0 R /SH2 15 0 R >> >> >> +endobj +4 0 obj +<< /Length 1995 >> +stream +1 1 1 rg +-16 -656 892 2472 re f +q +1 0 0 1 0 0 cm +q +64 0 0 64 0 0 cm +q +0.015625 0 0 0.015625 0 0 cm +q +0 0 0 0 re +W n +q +64 0 0 64 0 0 cm +/SH0 sh +Q +Q +Q +q +0.015625 0 0 0.015625 0 0 cm +q +334 922 m +170 1036 l +170 1114 l +310 1146 l +504 1146 l +692 1146 l +818 1114 l +818 1036 l +652 922 l +334 922 l +h +W n +q +64 0 0 64 0 0 cm +/SH1 sh +Q +Q +Q +q +0.015625 0 0 0.015625 0 0 cm +q +638 966 m +684 967.33 719 980.5 743 1005.5 c +767 1030.5 779.33 1058 780 1088 c +780 1088 780 1088 780 1088 c +780 1088 780 1088 780 1088 c +780 1088 780 1088 780 1088 c +780 1088 780 1088 780 1088 c +767.33 1088 747.83 1088 721.5 1088 c +695.17 1088 667 1088 637 1088 c +607 1088 579 1088 553 1088 c +527 1088 507.33 1088 494 1088 c +470 1088 448.17 1088 428.5 1088 c +408.83 1088 382.83 1088 350.5 1088 c +318.17 1088 270.67 1088 208 1088 c +208 1088 208 1088 208 1088 c +208 1088 208 1088 208 1088 c +208 1088 208 1088 208 1088 c +208 1088 208 1088 208 1088 c +209.33 1058 221.83 1030.5 245.5 1005.5 c +269.17 980.5 304 967.33 350 966 c +350 894 l +290.67 899.33 245 913 213 935 c +181 957 158.83 983.67 146.5 1015 c +134.17 1046.33 128 1078.67 128 1112 c +128 1117.33 128 1123.33 128 1130 c +128 1136.67 128 1142.83 128 1148.5 c +128 1154.17 128 1158.83 128 1162.5 c +128 1166.17 128 1168 128 1168 c +128 1168 128 1168 128 1168 c +128 1168 128 1168 128 1168 c +128 1168 128 1168 128 1168 c +128 1168 128 1168 128 1168 c +200 1168 257.5 1168 300.5 1168 c +343.5 1168 377 1168 401 1168 c +425 1168 443.5 1168 456.5 1168 c +469.5 1168 482 1168 494 1168 c +504 1168 519.5 1168 540.5 1168 c +561.5 1168 596.5 1168 645.5 1168 c +694.5 1168 766 1168 860 1168 c +860 1168 860 1168 860 1168 c +860 1168 860 1168 860 1168 c +860 1168 860 1168 860 1168 c +860 1168 860 1168 860 1168 c +860 1168 860 1166.17 860 1162.5 c +860 1158.83 860 1154.17 860 1148.5 c +860 1142.83 860 1136.67 860 1130 c +860 1123.33 860 1117.33 860 1112 c +860 1078.67 853.83 1046.33 841.5 1015 c +829.17 983.67 807 957 775 935 c +743 913 697.33 899.33 638 894 c +638 966 l +h +W n +q +64 0 0 64 0 0 cm +/SH2 sh +Q +Q +Q +Q +Q +endstream +endobj +5 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [0 0 0] +/C1 [0 0 0] >> +endobj +6 0 obj +<< /ShadingType 2 /ColorSpace /DeviceRGB +/Coords [0 0 0 15.62] +/Function 5 0 R +/Extend [true true] +>> +endobj +7 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [1 0.2745 0.6863] +/C1 [1 0.2353 0.6863] >> +endobj +8 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [1 0.2353 0.6863] +/C1 [1 0.4588 0.3725] >> +endobj +9 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [1 0.4588 0.3725] +/C1 [1 0.7804 0.3255] >> +endobj +10 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [1 0.7804 0.3255] +/C1 [1 1 0.4706] >> +endobj +11 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [1 1 0.4706] +/C1 [1 1 0.698] >> +endobj +12 0 obj +<< /FunctionType 3 /Domain [0 1] +/Functions [7 0 R 8 0 R 9 0 R 10 0 R 11 0 R] +/Bounds [0.2 0.4 0.6 0.8] +/Encode [0 1 0 1 0 1 0 1 0 1] >> +endobj +13 0 obj +<< /ShadingType 2 /ColorSpace /DeviceRGB +/Coords [0 0 0 15.62] +/Function 12 0 R +/Extend [true true] +>> +endobj +14 0 obj +<< /FunctionType 2 /Domain [0 1] /N 1 +/C0 [0 0 0] +/C1 [0 0 0] >> +endobj +15 0 obj +<< /ShadingType 2 /ColorSpace /DeviceRGB +/Coords [0 0 0 15.62] +/Function 14 0 R +/Extend [true true] +>> +endobj +xref +0 16 +0000000000 65535 f +0000000015 00000 n +0000000064 00000 n +0000000121 00000 n +0000000281 00000 n +0000002327 00000 n +0000002407 00000 n +0000002524 00000 n +0000002624 00000 n +0000002724 00000 n +0000002824 00000 n +0000002920 00000 n +0000003010 00000 n +0000003163 00000 n +0000003282 00000 n +0000003363 00000 n +trailer +<< /Size 16 /Root 1 0 R >> +startxref +3482 +%%EOF
diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index 376910f..e20c33c 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests
@@ -1,5 +1,6 @@ # font-file;options;unicodes;expected-file ../../../api/fonts/Roboto-Regular.abc.ttf;;U+0061,U+0062,U+0063;../expected/basic/default.svg +../../../shape/data/in-house/fonts/ba51123c80fa9526c9c3032cd41c22700a4bc3e2.ttf;;U+0306;../expected/basic/colrv1-empty-layer.pdf ../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=upem;U+0061,U+0062,U+0063;../expected/basic/upem-default.svg ../../../api/fonts/Roboto-Regular.abc.ttf;--direction=ttb;U+0061,U+0062,U+0063;../expected/basic/ttb.svg ../../../api/fonts/Roboto-Regular.abc.ttf;;U+0061,U+000A,U+0062,U+0063;../expected/basic/multiline.svg
diff --git a/test/vector/meson.build b/test/vector/meson.build index 8d9a9dd..e546248 100644 --- a/test/vector/meson.build +++ b/test/vector/meson.build
@@ -55,3 +55,11 @@ suite: 'vector', ) endforeach + +test_vector_pdf_clip = executable('test-vector-pdf-clip', 'test-vector-pdf-clip.c', + dependencies: [libharfbuzz_vector_dep], +) + +test('pdf-clip', test_vector_pdf_clip, + suite: 'vector', +)
diff --git a/test/vector/test-vector-pdf-clip.c b/test/vector/test-vector-pdf-clip.c new file mode 100644 index 0000000..6e5ba86 --- /dev/null +++ b/test/vector/test-vector-pdf-clip.c
@@ -0,0 +1,104 @@ +/* + * 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 clip to an outline that has no points lets nothing through. In + * PDF, `W` with no path before it would leave the clip as it was, and + * what is painted under it would cover the page. */ + +#ifdef NDEBUG +#undef NDEBUG +#endif +#include <assert.h> +#include <string.h> + +#include <hb.h> +#include <hb-vector.h> + +static const char * +find (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; +} + +/* Paints a color under a clip path of @points points, and something + * else so that the document is not empty. */ +static hb_blob_t * +render (unsigned points) +{ + hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_PDF); + assert (paint); + hb_vector_extents_t extents = {0, 0, 100, 100}; + hb_vector_paint_set_extents (paint, &extents); + hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint); + + void *draw_data; + hb_draw_funcs_t *draw = hb_paint_push_clip_path_start (funcs, paint, &draw_data); + assert (draw); + if (points) + { + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_draw_move_to (draw, draw_data, &st, 10, 10); + hb_draw_line_to (draw, draw_data, &st, 90, 10); + hb_draw_line_to (draw, draw_data, &st, 50, 90); + hb_draw_close_path (draw, draw_data, &st); + } + hb_paint_push_clip_path_end (funcs, paint); + hb_paint_color (funcs, paint, 0, HB_COLOR (0, 0, 255, 255)); + hb_paint_pop_clip (funcs, paint); + + hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 10, 10); + hb_paint_color (funcs, paint, 0, HB_COLOR (255, 0, 0, 255)); + hb_paint_pop_clip (funcs, paint); + + hb_blob_t *blob = hb_vector_paint_render (paint); + assert (blob); + hb_vector_paint_destroy (paint); + return blob; +} + +int +main (void) +{ + unsigned len; + const char *data; + + /* No points: a path that encloses nothing takes their place. */ + hb_blob_t *blob = render (0); + data = hb_blob_get_data (blob, &len); + assert (find (data, len, "q\n0 0 0 0 re\nW n\n")); + hb_blob_destroy (blob); + + /* A triangle is its own path. */ + blob = render (3); + data = hb_blob_get_data (blob, &len); + assert (!find (data, len, "0 0 0 0 re")); + assert (find (data, len, "h\nW n\n")); + hb_blob_destroy (blob); + + return 0; +}