5
5
import codecs
6
6
import datetime
7
7
from enum import Enum
8
+ import functools
8
9
import glob
9
10
from io import StringIO
10
11
import logging
39
40
_log = logging .getLogger (__name__ )
40
41
41
42
backend_version = 'Level II'
42
-
43
- debugPS = 0
43
+ debugPS = False
44
44
45
45
46
46
class PsBackendHelper :
@@ -214,6 +214,20 @@ def _font_to_ps_type3(font_path, glyph_ids):
214
214
return preamble + "\n " .join (entries ) + postamble
215
215
216
216
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
+
217
231
class RendererPS (_backend_pdf_ps .RendererPDFPSBase ):
218
232
"""
219
233
The renderer handles all the drawing primitives using a graphics
@@ -254,11 +268,9 @@ def __init__(self, width, height, pswriter, imagedpi=72):
254
268
255
269
def set_color (self , r , g , b , store = True ):
256
270
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 " )
262
274
if store :
263
275
self .color = (r , g , b )
264
276
@@ -301,11 +313,9 @@ def set_linedash(self, offset, seq, store=True):
301
313
if np .array_equal (seq , oldseq ) and oldo == offset :
302
314
return
303
315
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 " )
309
319
if store :
310
320
self .linedash = (offset , seq )
311
321
@@ -389,6 +399,7 @@ def _get_clip_cmd(self, gc):
389
399
clip .append (f"{ custom_clip_cmd } \n " )
390
400
return "" .join (clip )
391
401
402
+ @_log_if_debug_on
392
403
def draw_image (self , gc , x , y , im , transform = None ):
393
404
# docstring inherited
394
405
@@ -431,20 +442,19 @@ def draw_image(self, gc, x, y, im, transform=None):
431
442
grestore
432
443
""" )
433
444
445
+ @_log_if_debug_on
434
446
def draw_path (self , gc , path , transform , rgbFace = None ):
435
447
# docstring inherited
436
448
clip = rgbFace is None and gc .get_hatch_path () is None
437
449
simplify = path .should_simplify and clip
438
450
ps = self ._convert_path (path , transform , clip = clip , simplify = simplify )
439
451
self ._draw_ps (ps , gc , rgbFace )
440
452
453
+ @_log_if_debug_on
441
454
def draw_markers (
442
455
self , gc , marker_path , marker_trans , path , trans , rgbFace = None ):
443
456
# docstring inherited
444
457
445
- if debugPS :
446
- self ._pswriter .write ('% draw_markers \n ' )
447
-
448
458
ps_color = (
449
459
None
450
460
if _is_transparent (rgbFace )
@@ -493,6 +503,7 @@ def draw_markers(
493
503
ps = '\n ' .join (ps_cmd )
494
504
self ._draw_ps (ps , gc , rgbFace , fill = False , stroke = False )
495
505
506
+ @_log_if_debug_on
496
507
def draw_path_collection (self , gc , master_transform , paths , all_transforms ,
497
508
offsets , offsetTrans , facecolors , edgecolors ,
498
509
linewidths , linestyles , antialiaseds , urls ,
@@ -537,6 +548,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
537
548
538
549
self ._path_collection_id += 1
539
550
551
+ @_log_if_debug_on
540
552
def draw_tex (self , gc , x , y , s , prop , angle , * , mtext = None ):
541
553
# docstring inherited
542
554
if not hasattr (self , "psfrag" ):
@@ -573,12 +585,10 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
573
585
""" )
574
586
self .textcnt += 1
575
587
588
+ @_log_if_debug_on
576
589
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
577
590
# docstring inherited
578
591
579
- if debugPS :
580
- self ._pswriter .write ("% text\n " )
581
-
582
592
if _is_transparent (gc .get_rgb ()):
583
593
return # Special handling for fully transparent.
584
594
@@ -630,11 +640,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630
640
grestore
631
641
""" )
632
642
643
+ @_log_if_debug_on
633
644
def draw_mathtext (self , gc , x , y , s , prop , angle ):
634
645
"""Draw the math text using matplotlib.mathtext."""
635
- if debugPS :
636
- self ._pswriter .write ("% mathtext\n " )
637
-
638
646
width , height , descent , glyphs , rects = \
639
647
self ._text2path .mathtext_parser .parse (
640
648
s , 72 , prop ,
@@ -661,10 +669,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
661
669
self ._pswriter .write (f"{ ox } { oy } { w } { h } rectfill\n " )
662
670
self ._pswriter .write ("grestore\n " )
663
671
672
+ @_log_if_debug_on
664
673
def draw_gouraud_triangle (self , gc , points , colors , trans ):
665
674
self .draw_gouraud_triangles (gc , points .reshape ((1 , 3 , 2 )),
666
675
colors .reshape ((1 , 3 , 4 )), trans )
667
676
677
+ @_log_if_debug_on
668
678
def draw_gouraud_triangles (self , gc , points , colors , trans ):
669
679
assert len (points ) == len (colors )
670
680
assert points .ndim == 3
@@ -708,20 +718,16 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
708
718
grestore
709
719
""" )
710
720
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 ):
712
722
"""
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.
715
725
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.
720
729
"""
721
- # local variable eliminates all repeated attribute lookups
722
730
write = self ._pswriter .write
723
- if debugPS and command :
724
- write ("% " + command + "\n " )
725
731
mightstroke = (gc .get_linewidth () > 0
726
732
and not _is_transparent (gc .get_rgb ()))
727
733
if not mightstroke :
@@ -740,7 +746,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
740
746
741
747
write (self ._get_clip_cmd (gc ))
742
748
743
- # Jochen, is the strip necessary? - this could be a honking big string
744
749
write (ps .strip ())
745
750
write ("\n " )
746
751
0 commit comments