Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1537da3

Browse files
committed
Fix bug when rendering special XML characters (such as < > &) in SVG
svn path=/trunk/matplotlib/; revision=3675
1 parent d158b7e commit 1537da3

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

lib/matplotlib/backends/backend_svg.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from matplotlib.ft2font import FT2Font, KERNING_UNFITTED, KERNING_DEFAULT, KERNING_UNSCALED
1212
from matplotlib.mathtext import math_parse_s_ft2font_svg
1313

14+
from xml.sax.saxutils import escape as escape_xml_text
15+
1416
backend_version = __version__
1517

1618
def new_figure_manager(num, *args, **kwargs):
@@ -239,7 +241,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
239241

240242
font = self._get_font(prop)
241243

242-
thetext = '%s' % s
244+
thetext = escape_xml_text(s)
243245
fontfamily = font.family_name
244246
fontstyle = font.style_name
245247
fontsize = prop.get_size_in_points()
@@ -369,10 +371,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
369371
curr_x,curr_y = 0.0,0.0
370372

371373
for font, fontsize, thetext, new_x, new_y_mtc, metrics in svg_glyphs:
372-
if rcParams["mathtext.mathtext2"]:
373-
new_y = new_y_mtc - height
374-
else:
375-
new_y = - new_y_mtc
374+
new_y = - new_y_mtc
376375

377376
svg.append('<tspan style="font-size: %f; font-family: %s"' %
378377
(fontsize, font.family_name))
@@ -387,6 +386,8 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
387386
if dy != 0.0:
388387
svg.append(' dy="%f"' % dy)
389388

389+
thetext = escape_xml_text(thetext)
390+
390391
svg.append('>%s</tspan>\n' % thetext)
391392

392393
curr_x = new_x + xadvance

0 commit comments

Comments
 (0)