Skip to content

Commit 10da5a8

Browse files
committed
Simplify binary data handling in ps backend.
- bytes.hex() can now perform linewrapping itself, so use that in draw_image. - draw_gouraud_triangles can rely on PostScript's hexstring format (`<...>` instead of `(...)`) and thus on bytes.hex() too. This makes _quote_ps_string fully unused, thus tweak the deprecation of quote_ps_string. Also add a test for PostScript Gouraud shading, which was previously not covered at all.
1 parent dab648a commit 10da5a8

File tree

3 files changed

+343
-22
lines changed

3 files changed

+343
-22
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ def _nums_to_str(*args):
9191

9292
@_api.deprecated("3.6", alternative="Vendor the code")
9393
def quote_ps_string(s):
94-
return _quote_ps_string(s)
95-
96-
97-
def _quote_ps_string(s):
9894
"""
9995
Quote dangerous characters of S for use in a PostScript string constant.
10096
"""
@@ -447,17 +443,7 @@ def draw_image(self, gc, x, y, im, transform=None):
447443
h, w = im.shape[:2]
448444
imagecmd = "false 3 colorimage"
449445
data = im[::-1, :, :3] # Vertically flipped rgb values.
450-
# data.tobytes().hex() has no spaces, so can be linewrapped by simply
451-
# splitting data every nchars. It's equivalent to textwrap.fill only
452-
# much faster.
453-
nchars = 128
454-
data = data.tobytes().hex()
455-
hexlines = "\n".join(
456-
[
457-
data[n * nchars:(n + 1) * nchars]
458-
for n in range(math.ceil(len(data) / nchars))
459-
]
460-
)
446+
hexdata = data.tobytes().hex("\n", -64) # Linewrap to 128 chars.
461447

462448
if transform is None:
463449
matrix = "1 0 0 1 0 0"
@@ -479,7 +465,7 @@ def draw_image(self, gc, x, y, im, transform=None):
479465
{{
480466
currentfile DataString readhexstring pop
481467
}} bind {imagecmd}
482-
{hexlines}
468+
{hexdata}
483469
grestore
484470
""")
485471

@@ -737,13 +723,13 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
737723
xmin, ymin = points_min
738724
xmax, ymax = points_max
739725

740-
streamarr = np.empty(
726+
data = np.empty(
741727
shape[0] * shape[1],
742728
dtype=[('flags', 'u1'), ('points', '2>u4'), ('colors', '3u1')])
743-
streamarr['flags'] = 0
744-
streamarr['points'] = (flat_points - points_min) * factor
745-
streamarr['colors'] = flat_colors[:, :3] * 255.0
746-
stream = _quote_ps_string(streamarr.tobytes())
729+
data['flags'] = 0
730+
data['points'] = (flat_points - points_min) * factor
731+
data['colors'] = flat_colors[:, :3] * 255.0
732+
hexdata = data.tobytes().hex("\n", -64) # Linewrap to 128 chars.
747733

748734
self._pswriter.write(f"""\
749735
gsave
@@ -754,7 +740,9 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
754740
/BitsPerFlag 8
755741
/AntiAlias true
756742
/Decode [ {xmin:g} {xmax:g} {ymin:g} {ymax:g} 0 1 0 1 0 1 ]
757-
/DataSource ({stream})
743+
/DataSource <
744+
{hexdata}
745+
>
758746
>>
759747
shfill
760748
grestore

lib/matplotlib/tests/baseline_images/test_axes/pcolormesh_small.eps

Lines changed: 312 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)