8000 Factor out clip-path attr handling in backend_svg. · matplotlib/matplotlib@f48952c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit f48952c

Browse files
committed
Factor out clip-path attr handling in backend_svg.
Replace _get_clip (which just returns a clip id) by _get_clip_attrs, which also takes care of formatting it and preparing to pass it as as xml attribute.
1 parent 3b2c375 commit f48952c

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def _get_style_dict(self, gc, rgbFace):
581581
def _get_style(self, gc, rgbFace):
582582
return generate_css(self._get_style_dict(gc, rgbFace))
583583

584-
def _get_clip(self, gc):
584+
def _get_clip_attrs(self, gc):
585585
cliprect = gc.get_clip_rectangle()
586586
clippath, clippath_trans = gc.get_clip_path()
587587
if clippath is not None:
@@ -592,8 +592,7 @@ def _get_clip(self, gc):
592592
y = self.height-(y+h)
593593
dictkey = (x, y, w, h)
594594
else:
595-
return None
596-
595+
return {}
597596
clip = self._clipd.get(dictkey)
598597
if clip is None:
599598
oid = self._make_id('p', dictkey)
@@ -603,7 +602,7 @@ def _get_clip(self, gc):
603602
self._clipd[dictkey] = (dictkey, oid)
604603
else:
605604
clip, oid = clip
606-
return oid
605+
return {'clip-path': f'url(#{oid})'}
607606

608607
def _write_clips(self):
609608
if not len(self._clipd):
@@ -663,16 +662,10 @@ def draw_path(self, gc, path, transform, rgbFace=None):
663662
path, trans_and_flip, clip=clip, simplify=simplify,
664663
sketch=gc.get_sketch_params())
665664

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-
673665
if gc.get_url() is not None:
674666
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))
676669
if gc.get_url() is not None:
677670
self.writer.end('a')
678671

@@ -701,12 +694,7 @@ def draw_markers(
701694
writer.end('defs')
702695
self._markers[dictkey] = oid
703696

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))
710698
trans_and_flip = self._make_flip_transform(trans)
711699
attrib = {'xlink:href': '#%s' % oid}
712700
clip = (0, 0, self.width*72, self.height*72)
@@ -758,20 +746,20 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
758746
gc, master_transform, all_transforms, path_codes, offsets,
759747
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
760748
antialiaseds, urls, offset_position):
761-
clipid = self._get_clip(gc0)
762749
url = gc0.get_url()
763750
if url is not None:
764751
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)
767755
attrib = {
768756
'xlink:href': '#%s' % path_id,
769757
'x': short_float_fmt(xo),
770758
'y': short_float_fmt(self.height - yo),
771759
'style': self._get_style(gc0, rgbFace)
772760
}
773761
writer.element('use', attrib=attrib)
774-
if clipid is not None:
762+
if clip_attrs:
775763
writer.end('g')
776764
if url is not None:
777765
writer.end('a')
@@ -912,17 +900,10 @@ def draw_gouraud_triangle(self, gc, points, colors, trans):
912900

913901
def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
914902
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))
922904
transform = transform.frozen()
923905
for tri, col in zip(triangles_array, colors_array):
924906
self.draw_gouraud_triangle(gc, tri, col, transform)
925-
926907
self.writer.end('g')
927908

928909
def option_scale_image(self):
@@ -940,18 +921,18 @@ def draw_image(self, gc, x, y, im, transform=None):
940921
if w == 0 2364 or h == 0:
941922
return
942923

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)
950929

951-
oid = gc.get_gid()
952930
url = gc.get_url()
953931
if url is not None:
954932
self.writer.start('a', attrib={'xlink:href': url})
933+
934+
attrib = {}
935+
oid = gc.get_gid()
955936
if mpl.rcParams['svg.image_inline']:
956937
buf = BytesIO()
957938
Image.fromarray(im).save(buf, format="png")
@@ -969,7 +950,6 @@ def draw_image(self, gc, x, y, im, transform=None):
969950
Image.fromarray(im).save(filename)
970951
oid = oid or 'Im_' + self._make_id('image', filename)
971952
attrib['xlink:href'] = filename
972-
973953
attrib['id'] = oid
974954

975955
if transform is None:
@@ -1009,7 +989,7 @@ def draw_image(self, gc, x, y, im, transform=None):
1009989

1010990
if url is not None:
1011991
self.writer.end('a')
1012-
if clipid is not None:
992+
if clip_attrs:
1013993
self.writer.end('g')
1014994

1015995
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):
12461226
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
12471227
# docstring inherited
12481228

1249-
clipid = self._get_clip(gc)
1250-
if clipid is not None:
1229+
clip_attrs = self._get_clip_attrs(gc)
1230+
if clip_attrs:
12511231
# 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)
12551234

12561235
if gc.get_url() is not None:
12571236
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):
12641243
if gc.get_url() is not None:
12651244
self.writer.end('a')
12661245

1267-
if clipid is not None:
1246+
if clip_attrs:
12681247
self.writer.end('g')
12691248

12701249
def flipy(self):

0 commit comments

Comments
 (0)
0