Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c7ba52

Browse files
committed
Set clip path for PostScript texts.
The clip path/clip box were simply not set at all before. Also move `_convert_path`/`_get_clip_cmd` (previously `_get_clip_path`) above the block where all `draw_foo`s are defined, rather than being in the middle of them.
1 parent 9cf0d2e commit 5c7ba52

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,37 @@ def get_image_magnification(self):
369369
"""
370370
return self.image_magnification
371371

372+
def _convert_path(self, path, transform, clip=False, simplify=None):
373+
if clip:
374+
clip = (0.0, 0.0, self.width * 72.0, self.height * 72.0)
375+
else:
376+
clip = None
377+
return _path.convert_to_string(
378+
path, transform, clip, simplify, None,
379+
6, [b"m", b"l", b"", b"c", b"cl"], True).decode("ascii")
380+
381+
def _get_clip_cmd(self, gc):
382+
clip = []
383+
rect = gc.get_clip_rectangle()
384+
if rect is not None:
385+
clip.append("%s clipbox\n" % _nums_to_str(*rect.size, *rect.p0))
386+
path, trf = gc.get_clip_path()
387+
if path is not None:
388+
key = (path, id(trf))
389+
custom_clip_cmd = self._clip_paths.get(key)
390+
if custom_clip_cmd is None:
391+
custom_clip_cmd = "c%x" % len(self._clip_paths)
392+
self._pswriter.write(f"""\
393+
/{custom_clip_cmd} {{
394+
{self._convert_path(path, trf, simplify=False)}
395+
clip
396+
newpath
397+
}} bind def
398+
""")
399+
self._clip_paths[key] = custom_clip_cmd
400+
clip.append(f"{custom_clip_cmd}\n")
401+
return "".join(clip)
402+
372403
def draw_image(self, gc, x, y, im, transform=None):
373404
# docstring inherited
374405

@@ -396,20 +427,9 @@ def draw_image(self, gc, x, y, im, transform=None):
396427
xscale = 1.0
397428
yscale = 1.0
398429

399-
bbox = gc.get_clip_rectangle()
400-
clippath, clippath_trans = gc.get_clip_path()
401-
402-
clip = []
403-
if bbox is not None:
404-
clip.append('%s clipbox' % _nums_to_str(*bbox.size, *bbox.p0))
405-
if clippath is not None:
406-
id = self._get_clip_path(clippath, clippath_trans)
407-
clip.append('%s' % id)
408-
clip = '\n'.join(clip)
409-
410430
self._pswriter.write(f"""\
411431
gsave
412-
{clip}
432+
{self._get_clip_cmd(gc)}
413433
{x:f} {y:f} translate
414434
[{matrix}] concat
415435
{xscale:f} {yscale:f} scale
@@ -422,32 +442,6 @@ def draw_image(self, gc, x, y, im, transform=None):
422442
grestore
423443
""")
424444

425-
def _convert_path(self, path, transform, clip=False, simplify=None):
426-
if clip:
427-
clip = (0.0, 0.0, self.width * 72.0, self.height * 72.0)
428-
else:
429-
clip = None
430-
return _path.convert_to_string(
431-
path, transform, clip, simplify, None,
432-
6, [b'm', b'l', b'', b'c', b'cl'], True).decode('ascii')
433-
434-
def _get_clip_path(self, clippath, clippath_transform):
435-
key = (clippath, id(clippath_transform))
436-
pid = self._clip_paths.get(key)
437-
if pid is None:
438-
pid = 'c%x' % len(self._clip_paths)
439-
clippath_bytes = self._convert_path(
440-
clippath, clippath_transform, simplify=False)
441-
self._pswriter.write(f"""\
442-
/{pid} {{
443-
{clippath_bytes}
444-
clip
445-
newpath
446-
}} bind def
447-
""")
448-
self._clip_paths[key] = pid
449-
return pid
450-
451445
def draw_path(self, gc, path, transform, rgbFace=None):
452446
# docstring inherited
453447
clip = rgbFace is None and gc.get_hatch_path() is None
@@ -649,6 +643,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
649643
for x, name in xs_names)
650644
self._pswriter.write(f"""\
651645
gsave
646+
{self._get_clip_cmd(gc)}
652647
{x:f} {y:f} translate
653648
{angle:f} rotate
654649
{thetext}
@@ -763,14 +758,7 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
763758
self.set_color(*gc.get_rgb()[:3])
764759
write('gsave\n')
765760

766-
cliprect = gc.get_clip_rectangle()
767-
if cliprect:
768-
write('%1.4g %1.4g %1.4g %1.4g clipbox\n'
769-
% (*cliprect.size, *cliprect.p0))
770-
clippath, clippath_trans = gc.get_clip_path()
771-
if clippath:
772-
id = self._get_clip_path(clippath, clippath_trans)
773-
write('%s\n' % id)
761+
write(self._get_clip_cmd(gc))
774762

775763
# Jochen, is the strip necessary? - this could be a honking big string
776764
write(ps.strip())

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import matplotlib as mpl
99
import matplotlib.pyplot as plt
1010
from matplotlib import cbook, patheffects
11-
from matplotlib.testing.decorators import image_comparison
11+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1212
from matplotlib.cbook import MatplotlibDeprecationWarning
1313

1414

@@ -165,3 +165,11 @@ def test_useafm():
165165
@image_comparison(["type3.eps"])
166166
def test_type3_font():
167167
plt.figtext(.5, .5, "I/J")
168+
169+
170+
@check_figures_equal(extensions=["eps"])
171+
def test_text_clip(fig_test, fig_ref):
172+
ax = fig_test.add_subplot()
173+
# Fully clipped-out text should not appear.
174+
ax.text(0, 0, "hello", transform=fig_test.transFigure, clip_on=True)
175+
fig_ref.add_subplot()

0 commit comments

Comments
 (0)