55import codecs
66import datetime
77from enum import Enum
8+ import functools
89import glob
910from io import StringIO
1011import logging
3940_log = logging .getLogger (__name__ )
4041
4142backend_version = 'Level II'
42-
43- debugPS = 0
43+ debugPS = False
4444
4545
4646class PsBackendHelper :
@@ -214,6 +214,20 @@ def _font_to_ps_type3(font_path, glyph_ids):
214214 return preamble + "\n " .join (entries ) + postamble
215215
216216
217+ def _log_if_debug_on (meth ):
218+ """
219+ Wrap `RendererPS` method *meth* to emit a PS comment with the method name,
220+ if the global flag `debugPS` is set.
221+ """
222+ @functools .wraps (meth )
223+ def wrapper (self , * args , ** kwargs ):
224+ if debugPS :
225+ self ._pswriter .write (f"% { meth .__name__ } \n " )
226+ return meth (self , * args , ** kwargs )
227+
228+ return wrapper
229+
230+
217231class RendererPS (_backend_pdf_ps .RendererPDFPSBase ):
218232 """
219233 The renderer handles all the drawing primitives using a graphics
@@ -254,11 +268,9 @@ def __init__(self, width, height, pswriter, imagedpi=72):
254268
255269 def set_color (self , r , g , b , store = True ):
256270 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 ))
271+ self ._pswriter .write (f"{ r :1.3f} setgray\n "
272+ if r == g == b else
273+ f"{ r :1.3f} { g :1.3f} { b :1.3f} setrgbcolor\n " )
262274 if store :
263275 self .color = (r , g , b )
264276
@@ -301,11 +313,9 @@ def set_linedash(self, offset, seq, store=True):
301313 if np .array_equal (seq , oldseq ) and oldo == offset :
302314 return
303315
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 " )
316+ self ._pswriter .write (f"[{ _nums_to_str (* seq )} ] { offset :d} setdash\n "
317+ if seq is not None and len (seq ) else
318+ "[] 0 setdash\n " )
309319 if store :
310320 self .linedash = (offset , seq )
311321
@@ -389,6 +399,7 @@ def _get_clip_cmd(self, gc):
389399 clip .append (f"{ custom_clip_cmd } \n " )
390400 return "" .join (clip )
391401
402+ @_log_if_debug_on
392403 def draw_image (self , gc , x , y , im , transform = None ):
393404 # docstring inherited
394405
@@ -431,20 +442,19 @@ def draw_image(self, gc, x, y, im, transform=None):
431442grestore
432443""" )
433444
445+ @_log_if_debug_on
434446 def draw_path (self , gc , path , transform , rgbFace = None ):
435447 # docstring inherited
436448 clip = rgbFace is None and gc .get_hatch_path () is None
437449 simplify = path .should_simplify and clip
438450 ps = self ._convert_path (path , transform , clip = clip , simplify = simplify )
439451 self ._draw_ps (ps , gc , rgbFace )
440452
453+ @_log_if_debug_on
441454 def draw_markers (
442455 self , gc , marker_path , marker_trans , path , trans , rgbFace = None ):
443456 # docstring inherited
444457
445- if debugPS :
446- self ._pswriter .write ('% draw_markers \n ' )
447-
448458 ps_color = (
449459 None
450460 if _is_transparent (rgbFace )
@@ -493,6 +503,7 @@ def draw_markers(
493503 ps = '\n ' .join (ps_cmd )
494504 self ._draw_ps (ps , gc , rgbFace , fill = False , stroke = False )
495505
506+ @_log_if_debug_on
496507 def draw_path_collection (self , gc , master_transform , paths , all_transforms ,
497508 offsets , offsetTrans , facecolors , edgecolors ,
498509 linewidths , linestyles , antialiaseds , urls ,
@@ -537,6 +548,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
537548
538549 self ._path_collection_id += 1
539550
551+ @_log_if_debug_on
540552 def draw_tex (self , gc , x , y , s , prop , angle , * , mtext = None ):
541553 # docstring inherited
542554 if not hasattr (self , "psfrag" ):
@@ -573,12 +585,10 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
573585""" )
574586 self .textcnt += 1
575587
588+ @_log_if_debug_on
576589 def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
577590 # docstring inherited
578591
579- if debugPS :
580- self ._pswriter .write ("% text\n " )
581-
582592 if _is_transparent (gc .get_rgb ()):
583593 return # Special handling for fully transparent.
584594
@@ -630,11 +640,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630640grestore
631641""" )
632642
643+ @_log_if_debug_on
633644 def draw_mathtext (self , gc , x , y , s , prop , angle ):
634645 """Draw the math text using matplotlib.mathtext."""
635- if debugPS :
636- self ._pswriter .write ("% mathtext\n " )
637-
638646 width , height , descent , glyphs , rects = \
639647 self ._text2path .mathtext_parser .parse (
640648 s , 72 , prop ,
@@ -661,10 +669,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
661669 self ._pswriter .write (f"{ ox } { oy } { w } { h } rectfill\n " )
662670 self ._pswriter .write ("grestore\n " )
663671
672+ @_log_if_debug_on
664673 def draw_gouraud_triangle (self , gc , points , colors , trans ):
665674 self .draw_gouraud_triangles (gc , points .reshape ((1 , 3 , 2 )),
666675 colors .reshape ((1 , 3 , 4 )), trans )
667676
677+ @_log_if_debug_on
668678 def draw_gouraud_triangles (self , gc , points , colors , trans ):
669679 assert len (points ) == len (colors )
670680 assert points .ndim == 3
@@ -708,20 +718,16 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
708718grestore
709719""" )
710720
711- def _draw_ps (self , ps , gc , rgbFace , fill = True , stroke = True , command = None ):
721+ def _draw_ps (self , ps , gc , rgbFace , * , fill = True , stroke = True ):
712722 """
713- Emit the PostScript snippet 'ps' with all the attributes from 'gc'
714- applied. 'ps' must consist of PostScript commands to construct a path.
723+ Emit the PostScript snippet *ps* with all the attributes from *gc*
724+ applied. *ps* must consist of PostScript commands to construct a path.
715725
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.
726+ The *fill* and/or *stroke* kwargs can be set to False if the *ps*
727+ string already includes filling and/or stroking, in which case
728+ `_draw_ps` is just supplying properties and clipping.
720729 """
721- # local variable eliminates all repeated attribute lookups
722730 write = self ._pswriter .write
723- if debugPS and command :
724- write ("% " + command + "\n " )
725731 mightstroke = (gc .get_linewidth () > 0
726732 and not _is_transparent (gc .get_rgb ()))
727733 if not mightstroke :
@@ -740,7 +746,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
740746
741747 write (self ._get_clip_cmd (gc ))
742748
743- # Jochen, is the strip necessary? - this could be a honking big string
744749 write (ps .strip ())
745750 write ("\n " )
746751
0 commit comments