Skip to content

Commit 6868119

Browse files
committed
Small cleanups to backend_ps.
Make the debugPS functionality more systematic. Remove the command parameter to _draw_ps, which is never used, and make fill and stroke kwonly (as they always are). Small misc. cleanups.
1 parent 91016d8 commit 6868119

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import codecs
66
import datetime
77
from enum import Enum
8+
import functools
89
import glob
910
from io import StringIO
1011
import logging
@@ -39,8 +40,7 @@
3940
_log = logging.getLogger(__name__)
4041

4142
backend_version = 'Level II'
42-
43-
debugPS = 0
43+
debugPS = False
4444

4545

4646
class PsBackendHelper:
@@ -214,6 +214,16 @@ def _font_to_ps_type3(font_path, glyph_ids):
214214
return preamble + "\n".join(entries) + postamble
215215

216216

217+
def _log_if_debug_on(func):
218+
@functools.wraps(func)
219+
def wrapper(self, *args, **kwargs):
220+
if debugPS:
221+
self._pswriter.write(f"% {func.__name__}\n")
222+
return func(self, *args, **kwargs)
223+
224+
return wrapper
225+
226+
217227
class RendererPS(_backend_pdf_ps.RendererPDFPSBase):
218228
"""
219229
The renderer handles all the drawing primitives using a graphics
@@ -254,11 +264,9 @@ def __init__(self, width, height, pswriter, imagedpi=72):
254264

255265
def set_color(self, r, g, b, store=True):
256266
if (r, g, b) != self.color:
257-
if r == g and r == b:
258-
self._pswriter.write("%1.3f setgray\n" % r)
259-
else:
260-
self._pswriter.write(
261-
"%1.3f %1.3f %1.3f setrgbcolor\n" % (r, g, b))
267+
self._pswriter.write(f"{r:1.3f} setgray\n"
268+
if r == g == b else
269+
f"{r:1.3f} {g:1.3f} {b:1.3f} setrgbcolor\n")
262270
if store:
263271
self.color = (r, g, b)
264272

@@ -301,11 +309,9 @@ def set_linedash(self, offset, seq, store=True):
301309
if np.array_equal(seq, oldseq) and oldo == offset:
302310
return
303311

304-
if seq is not None and len(seq):
305-
s = "[%s] %d setdash\n" % (_nums_to_str(*seq), offset)
306-
self._pswriter.write(s)
307-
else:
308-
self._pswriter.write("[] 0 setdash\n")
312+
self._pswriter.write(f"[{_nums_to_str(*seq)}] {offset:d} setdash\n"
313+
if seq is not None and len(seq) else
314+
"[] 0 setdash\n")
309315
if store:
310316
self.linedash = (offset, seq)
311317

@@ -389,6 +395,7 @@ def _get_clip_cmd(self, gc):
389395
clip.append(f"{custom_clip_cmd}\n")
390396
return "".join(clip)
391397

398+
@_log_if_debug_on
392399
def draw_image(self, gc, x, y, im, transform=None):
393400
# docstring inherited
394401

@@ -431,20 +438,19 @@ def draw_image(self, gc, x, y, im, transform=None):
431438
grestore
432439
""")
433440

441+
@_log_if_debug_on
434442
def draw_path(self, gc, path, transform, rgbFace=None):
435443
# docstring inherited
436444
clip = rgbFace is None and gc.get_hatch_path() is None
437445
simplify = path.should_simplify and clip
438446
ps = self._convert_path(path, transform, clip=clip, simplify=simplify)
439447
self._draw_ps(ps, gc, rgbFace)
440448

449+
@_log_if_debug_on
441450
def draw_markers(
442451
self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
443452
# docstring inherited
444453

445-
if debugPS:
446-
self._pswriter.write('% draw_markers \n')
447-
448454
ps_color = (
449455
None
450456
if _is_transparent(rgbFace)
@@ -493,6 +499,7 @@ def draw_markers(
493499
ps = '\n'.join(ps_cmd)
494500
self._draw_ps(ps, gc, rgbFace, fill=False, stroke=False)
495501

502+
@_log_if_debug_on
496503
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
497504
offsets, offsetTrans, facecolors, edgecolors,
498505
linewidths, linestyles, antialiaseds, urls,
@@ -537,6 +544,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
537544

538545
self._path_collection_id += 1
539546

547+
@_log_if_debug_on
540548
def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
541549
# docstring inherited
542550
if not hasattr(self, "psfrag"):
@@ -573,12 +581,10 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
573581
""")
574582
self.textcnt += 1
575583

584+
@_log_if_debug_on
576585
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
577586
# docstring inherited
578587

579-
if debugPS:
580-
self._pswriter.write("% text\n")
581-
582588
if _is_transparent(gc.get_rgb()):
583589
return # Special handling for fully transparent.
584590

@@ -630,11 +636,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630636
grestore
631637
""")
632638

639+
@_log_if_debug_on
633640
def draw_mathtext(self, gc, x, y, s, prop, angle):
634641
"""Draw the math text using matplotlib.mathtext."""
635-
if debugPS:
636-
self._pswriter.write("% mathtext\n")
637-
638642
width, height, descent, glyphs, rects = \
639643
self._text2path.mathtext_parser.parse(
640644
s, 72, prop,
@@ -661,10 +665,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
661665
self._pswriter.write(f"{ox} {oy} {w} {h} rectfill\n")
662666
self._pswriter.write("grestore\n")
663667

668+
@_log_if_debug_on
664669
def draw_gouraud_triangle(self, gc, points, colors, trans):
665670
self.draw_gouraud_triangles(gc, points.reshape((1, 3, 2)),
666671
colors.reshape((1, 3, 4)), trans)
667672

673+
@_log_if_debug_on
668674
def draw_gouraud_triangles(self, gc, points, colors, trans):
669675
assert len(points) == len(colors)
670676
assert points.ndim == 3
@@ -708,20 +714,16 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
708714
grestore
709715
""")
710716

711-
def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
717+
def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
712718
"""
713-
Emit the PostScript snippet 'ps' with all the attributes from 'gc'
714-
applied. 'ps' must consist of PostScript commands to construct a path.
719+
Emit the PostScript snippet *ps* with all the attributes from *gc*
720+
applied. *ps* must consist of PostScript commands to construct a path.
715721
716-
The fill and/or stroke kwargs can be set to False if the
717-
'ps' string already includes filling and/or stroking, in
718-
which case _draw_ps is just supplying properties and
719-
clipping.
722+
The *fill* and/or *stroke* kwargs can be set to False if the *ps*
723+
string already includes filling and/or stroking, in which case
724+
`_draw_ps` is just supplying properties and clipping.
720725
"""
721-
# local variable eliminates all repeated attribute lookups
722726
write = self._pswriter.write
723-
if debugPS and command:
724-
write("% "+command+"\n")
725727
mightstroke = (gc.get_linewidth() > 0
726728
and not _is_transparent(gc.get_rgb()))
727729
if not mightstroke:
@@ -740,7 +742,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
740742

741743
write(self._get_clip_cmd(gc))
742744

743-
# Jochen, is the strip necessary? - this could be a honking big string
744745
write(ps.strip())
745746
write("\n")
746747

0 commit comments

Comments
 (0)