[vector] Set alpha through scoped ExtGStates in PDF draw output The PDF draw writer wrote the alpha of a translucent background as "0.502 ca gs". ca is a key of an ExtGState dictionary, not an operator, and gs takes the name of such a dictionary. poppler reports a syntax error and leaves the whole page blank; pdfium, MuPDF and Ghostscript paint the background opaque. The alpha of a translucent foreground did go through an ExtGState, but was never undone, so a path drawn with an opaque foreground after one drawn with a translucent foreground came out translucent as well. Set both through an ExtGState inside q/Q. Testing: hb-vector --foreground=#00000080 --background=#FF000080 on Roboto, written as PDF and as SVG. The PDF differed from the SVG in poppler (Splash and cairo), Ghostscript, pdfium and MuPDF; now it has the pixels of the SVG in all five, antialiased edges aside, and poppler reports no error. Through the API, a 50% black square and then an opaque red one: the red one was at 50% in all five renderers, and is opaque now. The tool's output is added as an expected output. Full test suite passes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 5edaee8..6ff3793 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc
@@ -744,10 +744,13 @@ if (hb_color_get_alpha (draw->background)) { float a = hb_color_get_alpha (draw->background) / 255.f; - if (a < 1.f - 1.f / 512.f) + bool scoped = a < 1.f - 1.f / 512.f; + if (scoped) { - stream.append_num (a, 4); - stream.append_str (" ca gs\n"); + /* Scoped, so the alpha does not leak into the paths. */ + stream.append_str ("q\n/GS"); + stream.append_unsigned (draw->add_pdf_extgstate_alpha (a)); + stream.append_str (" gs\n"); } stream.append_num (hb_color_get_red (draw->background) / 255.f, 4); stream.append_c (' '); @@ -763,6 +766,8 @@ stream.append_c (' '); stream.append_num (eh); stream.append_str (" re f\n"); + if (scoped) + stream.append_str ("Q\n"); } draw->flush_path ();
diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 9035e3e..a882cd4 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh
@@ -108,21 +108,28 @@ path.shrink (0); } + /* Adds an ExtGState that sets the fill alpha; returns its + * resource index. */ + unsigned add_pdf_extgstate_alpha (float alpha) + { + unsigned gs_idx = pdf_extgstate_count++; + pdf_extgstate_dict.append_str ("/GS"); + pdf_extgstate_dict.append_unsigned (gs_idx); + pdf_extgstate_dict.append_str (" << /Type /ExtGState /ca "); + pdf_extgstate_dict.append_num (alpha, 4); + pdf_extgstate_dict.append_str (" >> "); + return gs_idx; + } + void flush_path_pdf () { unsigned a = hb_color_get_alpha (foreground); if (a < 255) { - unsigned gs_idx = pdf_extgstate_count++; - body.append_str ("/GS"); - body.append_unsigned (gs_idx); + /* Scoped, so the alpha does not leak into later paths. */ + body.append_str ("q\n/GS"); + body.append_unsigned (add_pdf_extgstate_alpha (a / 255.f)); body.append_str (" gs\n"); - - pdf_extgstate_dict.append_str ("/GS"); - pdf_extgstate_dict.append_unsigned (gs_idx); - pdf_extgstate_dict.append_str (" << /Type /ExtGState /ca "); - pdf_extgstate_dict.append_num (a / 255.f, 4); - pdf_extgstate_dict.append_str (" >> "); } body.append_num (hb_color_get_red (foreground) / 255.f, 4); body.append_c (' '); @@ -132,6 +139,8 @@ body.append_str (" rg\n"); body.append_len (path.arrayZ, path.length); body.append_str ("f\n"); + if (a < 255) + body.append_str ("Q\n"); } void flush_path_svg ()
diff --git a/test/vector/data/expected/basic/glyf-translucent.pdf b/test/vector/data/expected/basic/glyf-translucent.pdf new file mode 100644 index 0000000..acf304b --- /dev/null +++ b/test/vector/data/expected/basic/glyf-translucent.pdf
@@ -0,0 +1,128 @@ +%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 -516 3351 1916] /Contents 4 0 R /Resources << /ExtGState << /GS0 << /Type /ExtGState /ca 0.502 >> /GS1 << /Type /ExtGState /ca 0.502 >> /GS2 << /Type /ExtGState /ca 0.502 >> /GS3 << /Type /ExtGState /ca 0.502 >> >> >> >> +endobj +4 0 obj +<< /Length 2221 >> +stream +q +/GS3 gs +1 0 0 rg +-16 -516 3367 2432 re f +Q +q +/GS0 gs +0 0 0 rg +808 0 m +797.33 21.33 788.67 59.33 782 114 c +696 24.67 593.33 -20 474 -20 c +367.33 -20 279.83 10.17 211.5 70.5 c +143.17 130.83 109 207.33 109 300 c +109 412.67 151.83 500.17 237.5 562.5 c +323.17 624.83 443.67 656 599 656 c +779 656 l +779 741 l +779 805.67 759.67 857.17 721 895.5 c +682.33 933.83 625.33 953 550 953 c +484 953 428.67 936.33 384 903 c +339.33 869.67 317 829.33 317 782 c +131 782 l +131 836 150.17 888.17 188.5 938.5 c +226.83 988.83 278.83 1028.67 344.5 1058 c +410.17 1087.33 482.33 1102 561 1102 c +685.67 1102 783.33 1070.83 854 1008.5 c +924.67 946.17 961.33 860.33 964 751 c +964 253 l +964 153.67 976.67 74.67 1002 16 c +1002 0 l +808 0 l +h +501 141 m +559 141 614 156 666 186 c +718 216 755.67 255 779 303 c +779 525 l +634 525 l +407.33 525 294 458.67 294 326 c +294 268 313.33 222.67 352 190 c +390.67 157.33 440.33 141 501 141 c +h +f +Q +q +/GS1 gs +0 0 0 rg +2170 529 m +2170 363.67 2132 230.83 2056 130.5 c +1980 30.17 1878 -20 1750 -20 c +1613.33 -20 1507.67 28.33 1433 125 c +1424 0 l +1254 0 l +1254 1536 l +1439 1536 l +1439 963 l +1513.67 1055.67 1616.67 1102 1748 1102 c +1879.33 1102 1982.5 1052.33 2057.5 953 c +2132.5 853.67 2170 717.67 2170 545 c +2170 529 l +h +1985 550 m +1985 676 1960.67 773.33 1912 842 c +1863.33 910.67 1793.33 945 1702 945 c +1580 945 1492.33 888.33 1439 775 c +1439 307 l +1495.67 193.67 1584 137 1704 137 c +1792.67 137 1861.67 171.33 1911 240 c +1960.33 308.67 1985 412 1985 550 c +h +f +Q +q +/GS2 gs +0 0 0 rg +2837 131 m +2903 131 2960.67 151 3010 191 c +3059.33 231 3086.67 281 3092 341 c +3267 341 l +3263.67 279 3242.33 220 3203 164 c +3163.67 108 3111.17 63.33 3045.5 30 c +2979.83 -3.33 2910.33 -20 2837 -20 c +2689.67 -20 2572.5 29.17 2485.5 127.5 c +2398.5 225.83 2355 360.33 2355 531 c +2355 562 l +2355 667.33 2374.33 761 2413 843 c +2451.67 925 2507.17 988.67 2579.5 1034 c +2651.83 1079.33 2737.33 1102 2836 1102 c +2957.33 1102 3058.17 1065.67 3138.5 993 c +3218.83 920.33 3261.67 826 3267 710 c +3092 710 l +3086.67 780 3060.17 837.5 3012.5 882.5 c +2964.83 927.5 2906 950 2836 950 c +2742 950 2669.17 916.17 2617.5 848.5 c +2565.83 780.83 2540 683 2540 555 c +2540 520 l +2540 395.33 2565.67 299.33 2617 232 c +2668.33 164.67 2741.67 131 2837 131 c +h +f +Q +endstream +endobj +xref +0 5 +0000000000 65535 f +0000000015 00000 n +0000000064 00000 n +0000000121 00000 n +0000000401 00000 n +trailer +<< /Size 5 /Root 1 0 R >> +startxref +2673 +%%EOF
diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests index 376910f..db1b2aa 100644 --- a/test/vector/data/tests/basic.tests +++ b/test/vector/data/tests/basic.tests
@@ -14,6 +14,7 @@ ../../../fuzzing/fonts/noto_handwriting-cff_colr_1.otf;;U+270D;../expected/basic/handwriting-cff-colr-1.svg ../../../fuzzing/fonts/noto_handwriting-cff2_colr_1.otf;;U+270D;../expected/basic/handwriting-cff-colr-1.svg ../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=65536;U+0061,U+0062,U+0063;../expected/basic/glyf-quadratics.pdf +../../../api/fonts/Roboto-Regular.abc.ttf;--foreground=#00000080 --background=#FF000080;U+0061,U+0062,U+0063;../expected/basic/glyf-translucent.pdf ../../../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