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