@@ -580,7 +580,7 @@ def _get_style_dict(self, gc, rgbFace):
580
580
def _get_style (self , gc , rgbFace ):
581
581
return generate_css (self ._get_style_dict (gc , rgbFace ))
582
582
583
- def _get_clip (self , gc ):
583
+ def _get_clip_attrs (self , gc ):
584
584
cliprect = gc .get_clip_rectangle ()
585
585
clippath , clippath_trans = gc .get_clip_path ()
586
586
if clippath is not None :
@@ -591,8 +591,7 @@ def _get_clip(self, gc):
591
591
y = self .height - (y + h )
592
592
dictkey = (x , y , w , h )
593
593
else :
594
- return None
595
-
594
+ return {}
596
595
clip = self ._clipd .get (dictkey )
597
596
if clip is None :
598
597
oid = self ._make_id ('p' , dictkey )
@@ -602,7 +601,7 @@ def _get_clip(self, gc):
602
601
self ._clipd [dictkey ] = (dictkey , oid )
603
602
else :
604
603
clip , oid = clip
605
- return oid
604
+ return { 'clip-path' : f'url(# { oid } )' }
606
605
607
606
def _write_clips (self ):
608
607
if not len (self ._clipd ):
@@ -662,16 +661,10 @@ def draw_path(self, gc, path, transform, rgbFace=None):
662
661
path , trans_and_flip , clip = clip , simplify = simplify ,
663
662
sketch = gc .get_sketch_params ())
664
663
665
- attrib = {}
666
- attrib ['style' ] = self ._get_style (gc , rgbFace )
667
-
668
- clipid = self ._get_clip (gc )
669
- if clipid is not None :
670
- attrib ['clip-path' ] = 'url(#%s)' % clipid
671
-
672
664
if gc .get_url () is not None :
673
665
self .writer .start ('a' , {'xlink:href' : gc .get_url ()})
674
- self .writer .element ('path' , d = path_data , attrib = attrib )
666
+ self .writer .element ('path' , d = path_data , ** self ._get_clip_attrs (gc ),
667
+ style = self ._get_style (gc , rgbFace ))
675
668
if gc .get_url () is not None :
676
669
self .writer .end ('a' )
677
670
@@ -700,12 +693,7 @@ def draw_markers(
700
693
writer .end ('defs' )
701
694
self ._markers [dictkey ] = oid
702
695
703
- attrib = {}
704
- clipid = self ._get_clip (gc )
705
- if clipid is not None :
706
- attrib ['clip-path' ] = 'url(#%s)' % clipid
707
- writer .start ('g' , attrib = attrib )
708
-
696
+ writer .start ('g' , ** self ._get_clip_attrs (gc ))
709
697
trans_and_flip = self ._make_flip_transform (trans )
710
698
attrib = {'xlink:href' : '#%s' % oid }
711
699
clip = (0 , 0 , self .width * 72 , self .height * 72 )
@@ -757,20 +745,20 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
757
745
gc , master_transform , all_transforms , path_codes , offsets ,
758
746
offsetTrans , facecolors , edgecolors , linewidths , linestyles ,
759
747
antialiaseds , urls , offset_position ):
760
- clipid = self ._get_clip (gc0 )
761
748
url = gc0 .get_url ()
762
749
if url is not None :
763
750
writer .start ('a' , attrib = {'xlink:href' : url })
764
- if clipid is not None :
765
- writer .start ('g' , attrib = {'clip-path' : 'url(#%s)' % clipid })
751
+ clip_attrs = self ._get_clip_attrs (gc0 )
752
+ if clip_attrs :
753
+ writer .start ('g' , ** clip_attrs )
766
754
attrib = {
767
755
'xlink:href' : '#%s' % path_id ,
768
756
'x' : short_float_fmt (xo ),
769
757
'y' : short_float_fmt (self .height - yo ),
770
758
'style' : self ._get_style (gc0 , rgbFace )
771
759
}
772
760
writer .element ('use' , attrib = attrib )
773
- if clipid is not None :
761
+ if clip_attrs :
774
762
writer .end ('g' )
775
763
if url is not None :
776
764
writer .end ('a' )
@@ -911,17 +899,10 @@ def draw_gouraud_triangle(self, gc, points, colors, trans):
911
899
912
900
def draw_gouraud_triangles (self , gc , triangles_array , colors_array ,
913
901
transform ):
914
- attrib = {}
915
- clipid = self ._get_clip (gc )
916
- if clipid is not None :
917
- attrib ['clip-path' ] = 'url(#%s)' % clipid
918
-
919
- self .writer .start ('g' , attrib = attrib )
920
-
902
+ self .writer .start ('g' , ** self ._get_clip_attrs (gc ))
921
903
transform = transform .frozen ()
922
904
for tri , col in zip (triangles_array , colors_array ):
923
905
self .draw_gouraud_triangle (gc , tri , col , transform )
924
-
925
906
self .writer .end ('g' )
926
907
927
908
def option_scale_image (self ):
@@ -939,18 +920,18 @@ def
A935
draw_image(self, gc, x, y, im, transform=None):
939
920
if w == 0 or h == 0 :
940
921
return
941
922
942
- attrib = {}
943
- clipid = self ._get_clip (gc )
944
- if clipid is not None :
945
- # Can't apply clip-path directly to the image because the
946
- # image has a transformation, which would also be applied
947
- # to the clip-path
948
- self .writer .start ('g' , attrib = {'clip-path' : 'url(#%s)' % clipid })
923
+ clip_attrs = self ._get_clip_attrs (gc )
924
+ if clip_attrs :
925
+ # Can't apply clip-path directly to the image because the image has
926
+ # a transformation, which would also be applied to the clip-path.
927
+ self .writer .start ('g' , ** clip_attrs )
949
928
950
- oid = gc .get_gid ()
951
929
url = gc .get_url ()
952
930
if url is not None :
953
931
self .writer .start ('a' , attrib = {'xlink:href' : url })
932
+
933
+ attrib = {}
934
+ oid = gc .get_gid ()
954
935
if mpl .rcParams ['svg.image_inline' ]:
955
936
buf = BytesIO ()
956
937
Image .fromarray (im ).save (buf , format = "png" )
@@ -968,7 +949,6 @@ def draw_image(self, gc, x, y, im, transform=None):
968
949
Image .fromarray (im ).save (filename )
969
950
oid = oid or 'Im_' + self ._make_id ('image' , filename )
970
951
attrib ['xlink:href' ] = filename
971
-
972
952
attrib ['id' ] = oid
973
953
974
954
if transform is None :
@@ -1008,7 +988,7 @@ def draw_image(self, gc, x, y, im, transform=None):
1008
988
1009
989
if url is not None :
1010
990
self .writer .end ('a' )
1011
- if clipid is not None :
991
+ if clip_attrs :
1012
992
self .writer .end ('g' )
1013
993
1014
994
def _update_glyph_map_defs (self , glyph_map_new ):
@@ -1245,12 +1225,11 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
1245
1225
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
1246
1226
# docstring inherited
1247
1227
1248
- clipid = self ._get_clip (gc )
1249
- if clipid is not None :
1228
+ clip_attrs = self ._get_clip_attrs (gc )
1229
+ if clip_attrs :
1250
1230
# Cannot apply clip-path directly to the text, because
1251
- # is has a transformation
1252
- self .writer .start (
1253
- 'g' , attrib = {'clip-path' : 'url(#%s)' % clipid })
1231
+ # it has a transformation
1232
+ self .writer .start ('g' , ** clip_attrs )
1254
1233
1255
1234
if gc .get_url () is not None :
1256
1235
self .writer .start ('a' , {'xlink:href' : gc .get_url ()})
@@ -1263,7 +1242,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
1263
1242
if gc .get_url () is not None :
1264
1243
self .writer .end ('a' )
1265
1244
1266
- if clipid is not None :
1245
+ if clip_attrs :
1267
1246
self .writer .end ('g' )
1268
1247
1269
1248
def flipy (self ):
0 commit comments