@@ -369,6 +369,37 @@ def get_image_magnification(self):
369
369
"""
370
370
return self .image_magnification
371
371
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
+
372
403
def draw_image (self , gc , x , y , im , transform = None ):
373
404
# docstring inherited
374
405
@@ -396,20 +427,9 @@ def draw_image(self, gc, x, y, im, transform=None):
396
427
xscale = 1.0
397
428
yscale = 1.0
398
429
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
-
410
430
self ._pswriter .write (f"""\
411
431
gsave
412
- { clip }
432
+ { self . _get_clip_cmd ( gc ) }
413
433
{ x :f} { y :f} translate
414
434
[{ matrix } ] concat
415
435
{ xscale :f} { yscale :f} scale
@@ -422,32 +442,6 @@ def draw_image(self, gc, x, y, im, transform=None):
422
442
grestore
423
443
""" )
424
444
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
-
451
445
def draw_path (self , gc , path , transform , rgbFace = None ):
452
446
# docstring inherited
453
447
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):
649
643
for x , name in xs_names )
650
644
self ._pswriter .write (f"""\
651
645
gsave
646
+ { self ._get_clip_cmd (gc )}
652
647
{ x :f} { y :f} translate
653
648
{ angle :f} rotate
654
649
{ thetext }
@@ -763,14 +758,7 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
763
758
self .set_color (* gc .get_rgb ()[:3 ])
764
759
write ('gsave\n ' )
765
760
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 ))
774
762
775
763
# Jochen, is the strip necessary? - this could be a honking big string
776
764
write (ps .strip ())
0 commit comments