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,16 @@ 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 (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
+
217
227
class RendererPS (_backend_pdf_ps .RendererPDFPSBase ):
218
228
"""
219
229
The renderer handles all the drawing primitives using a graphics
@@ -254,11 +264,9 @@ def __init__(self, width, height, pswriter, imagedpi=72):
254
264
255
265
def set_color (self , r , g , b , store = True ):
256
266
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 " )
262
270
if store :
263
271
self .color = (r , g , b )
264
272
@@ -301,11 +309,9 @@ def set_linedash(self, offset, seq, store=True):
301
309
if np .array_equal (seq , oldseq ) and oldo == offset :
302
310
return
303
311
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 " )
309
315
if store :
310
316
self .linedash = (offset , seq )
311
317
@@ -389,6 +395,7 @@ def _get_clip_cmd(self, gc):
389
395
clip .append (f"{ custom_clip_cmd } \n " )
390
396
return "" .join (clip )
391
397
398
+ @_log_if_debug_on
392
399
def draw_image (self , gc , x , y , im , transform = None ):
393
400
# docstring inherited
394
401
@@ -431,20 +438,19 @@ def draw_image(self, gc, x, y, im, transform=None):
431
438
grestore
432
439
""" )
433
440
441
+ @_log_if_debug_on
434
442
def draw_path (self , gc , path , transform , rgbFace = None ):
435
443
# docstring inherited
436
444
clip = rgbFace is None and gc .get_hatch_path () is None
437
445
simplify = path .should_simplify and clip
438
446
ps = self ._convert_path (path , transform , clip = clip , simplify = simplify )
439
447
self ._draw_ps (ps , gc , rgbFace )
440
448
449
+ @_log_if_debug_on
441
450
def draw_markers (
442
451
self , gc , marker_path , marker_trans , path , trans , rgbFace = None ):
443
452
# docstring inherited
444
453
445
- if debugPS :
446
- self ._pswriter .write ('% draw_markers \n ' )
447
-
448
454
ps_color = (
449
455
None
450
456
if _is_transparent (rgbFace )
@@ -493,6 +499,7 @@ def draw_markers(
493
499
ps = '\n ' .join (ps_cmd )
494
500
self ._draw_ps (ps , gc , rgbFace , fill = False , stroke = False )
495
501
502
+ @_log_if_debug_on
496
503
def draw_path_collection (self , gc , master_transform , paths , all_transforms ,
497
504
offsets , offsetTrans , facecolors , edgecolors ,
498
505
linewidths , linestyles , antialiaseds , urls ,
@@ -537,6 +544,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
537
544
538
545
self ._path_collection_id += 1
539
546
547
+ @_log_if_debug_on
540
548
def draw_tex (self , gc , x , y , s , prop , angle , * , mtext = None ):
541
549
# docstring inherited
542
550
if not hasattr (self , "psfrag" ):
@@ -573,12 +581,10 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
573
581
""" )
574
582
self .textcnt += 1
575
583
584
+ @_log_if_debug_on
576
585
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
577
586
# docstring inherited
578
587
579
- if debugPS :
580
- self ._pswriter .write ("% text\n " )
581
-
582
588
if _is_transparent (gc .get_rgb ()):
583
589
return # Special handling for fully transparent.
584
590
@@ -630,11 +636,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630
636
grestore
631
637
""" )
632
638
639
+ @_log_if_debug_on
633
640
def draw_mathtext (self , gc , x , y , s , prop , angle ):
634
641
"""Draw the math text using matplotlib.mathtext."""
635
- if debugPS :
636
- self ._pswriter .write ("% mathtext\n " )
637
-
638
642
width , height , descent , glyphs , rects = \
639
643
self ._text2path .mathtext_parser .parse (
640
644
s , 72 , prop ,
@@ -661,10 +665,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
661
665
self ._pswriter .write (f"{ ox } { oy } { w } { h } rectfill\n " )
662
666
self ._pswriter .write ("grestore\n " )
663
667
668
+ @_log_if_debug_on
664
669
def draw_gouraud_triangle (self , gc , points , colors , trans ):
665
670
self .draw_gouraud_triangles (gc , points .reshape ((1 , 3 , 2 )),
666
671
colors .reshape ((1 , 3 , 4 )), trans )
667
672
673
+ @_log_if_debug_on
668
674
def draw_gouraud_triangles (self , gc , points , colors , trans ):
669
675
assert len (points ) == len (colors )
670
676
assert points .ndim == 3
@@ -708,20 +714,16 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
708
714
grestore
709
715
""" )
710
716
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 ):
712
718
"""
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.
715
721
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.
720
725
"""
721
- # local variable eliminates all repeated attribute lookups
722
726
write = self ._pswriter .write
723
- if debugPS and command :
724
- write ("% " + command + "\n " )
725
727
mightstroke = (gc .get_linewidth () > 0
726
728
and not _is_transparent (gc .get_rgb ()))
727
729
if not mightstroke :
@@ -740,7 +742,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
740
742
741
743
write (self ._get_clip_cmd (gc ))
742
744
743
- # Jochen, is the strip necessary? - this could be a honking big string
744
745
write (ps .strip ())
745
746
write ("\n " )
746
747
0 commit comments