8000 Merge pull request #19728 from anntzer/svgclip · matplotlib/matplotlib@3cd72b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cd72b1

Browse files
authored
Merge pull request #19728 from anntzer/svgclip
Factor out clip-path attr handling in backend_svg.
2 parents 645b146 + f48952c commit 3cd72b1

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
@@ -580,7 +580,7 @@ def _get_style_dict(self, gc, rgbFace):
580580
def _get_style(self, gc, rgbFace):
581581
return generate_css(self._get_style_dict(gc, rgbFace))
582582

583-
def _get_clip(self, gc):
583+
def _get_clip_attrs(self, gc):
584584
cliprect = gc.get_clip_rectangle()
585585
clippath, clippath_trans = gc.get_clip_path()
586586
if clippath is not None:
@@ -591,8 +591,7 @@ def _get_clip(self, gc):
591591
y = self.height-(y+h)
592592
dictkey = (x, y, w, h)
593593
else:
594-
return None
595-
594+
return {}
596595
clip = self._clipd.get(dictkey)
597596
if clip is None:
598597
oid = self._make_id('p', dictkey)
@@ -602,7 +601,7 @@ def _get_clip(self, gc):
602601
self._clipd[dictkey] = (dictkey, oid)
603602
else:
604603
clip, oid = clip
605-
return oid
604+
return {'clip-path': f'url(#{oid})'}
606605

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

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

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

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

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

927908
def option_scale_image(self):
@@ -939,18 +920,18 @@ def A935 draw_image(self, gc, x, y, im, transform=None):
939920
if w == 0 or h == 0:
940921
return
941922

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

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

974954
if transform is None:
@@ -1008,7 +988,7 @@ def draw_image(self, gc, x, y, im, transform=None):
1008988

1009989
if url is not None:
1010990
self.writer.end('a')
1011-
if clipid is not None:
991+
if clip_attrs:
1012992
self.writer.end('g')
1013993

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

1248-
clipid = self._get_clip(gc)
1249-
if clipid is not None:
1228+
clip_attrs = self._get_clip_attrs(gc)
1229+
if clip_attrs:
12501230
# 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)
12541233

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

1266-
if clipid is not None:
1245+
if clip_attrs:
12671246
self.writer.end('g')
12681247

12691248
def flipy(self):

0 commit comments

Comments
 (0)
0