Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9ce195e

Browse filesBrowse files
committed
Fix SVG glyphs for use with Qt (which doesn't look forward for the
glyph definitions). svn path=/branches/v0_91_maint/; revision=4866
1 parent 253a406 commit 9ce195e
Copy full SHA for 9ce195e

File tree

Expand file treeCollapse file tree

1 file changed

+45
-16
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+45
-16
lines changed

‎lib/matplotlib/backends/backend_svg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_svg.py
+45-16Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
284284

285285
fontsize = prop.get_size_in_points()
286286
color = rgb2hex(gc.get_rgb())
287+
write = self._svgwriter.write
287288

288289
if rcParams['svg.embed_char_paths']:
290+
new_chars = []
291+
for c in s:
292+
path = self._add_char_def(prop, c)
293+
if path is not None:
294+
new_chars.append(path)
295+
if len(new_chars):
296+
write('<defs>\n')
297+
for path in new_chars:
298+
write(path)
299+
write('</defs>\n')
300+
289301
svg = ['<g style="fill: %s; opacity: %s" transform="' % (color, gc.get_alpha())]
290302
if angle != 0:
291303
svg.append('translate(%s,%s)rotate(%1.1f)' % (x,y,-angle))
@@ -297,7 +309,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
297309
lastgind = None
298310
currx = 0
299311
for c in s:
300-
charid = self._add_char_def(prop, c)
312+
charnum = self._get_char_def_id(prop, c)
301313
ccode = ord(c)
302314
gind = cmap.get(ccode)
303315
if gind is None:
@@ -312,7 +324,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
312324
lastgind = gind
313325
currx += kern/64.0
314326

315-
svg.append('<use xlink:href="#%s"' % charid)
327+
svg.append('<use xlink:href="#%s"' % charnum)
316328
if currx != 0:
317329
svg.append(' transform="translate(%s)"' %
318330
(currx * (self.FONT_SCALE / fontsize)))
@@ -336,7 +348,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
336348
svg = """\
337349
<text style="%(style)s" x="%(x)s" y="%(y)s" %(transform)s>%(thetext)s</text>
338350
""" % locals()
339-
self._svgwriter.write (svg)
351+
write(svg)
340352

341353
def _add_char_def(self, prop, char):
342354
if isinstance(prop, FontProperties):
@@ -347,9 +359,9 @@ def _add_char_def(self, prop, char):
347359
font.set_size(self.FONT_SCALE, 72)
348360
ps_name = font.get_sfnt()[(1,0,0,6)]
349361
char_id = urllib.quote('%s-%d' % (ps_name, ord(char)))
350-
char_num, path = self._char_defs.get(char_id, (None, None))
362+
char_num = self._char_defs.get(char_id, None)
351363
if char_num is not None:
352-
return char_num
364+
return None
353365

354366
path_data = []
355367
glyph = font.load_char(ord(char), flags=LOAD_NO_HINTING)
@@ -378,8 +390,19 @@ def _add_char_def(self, prop, char):
378390
currx, curry = step[-2], -step[-1]
379391
char_num = 'c_%x' % len(self._char_defs)
380392
path_element = '<path id="%s" d="%s"/>\n' % (char_num, ''.join(path_data))
381-
self._char_defs[char_id] = (char_num, path_element)
382-
return char_num
393+
self._char_defs[char_id] = char_num
394+
return path_element
395+
396+
def _get_char_def_id(self, prop, char):
397+
if isinstance(prop, FontProperties):
398+
newprop = prop.copy()
399+
font = self._get_font(newprop)
400+
else:
401+
font = prop
402+
font.set_size(self.FONT_SCALE, 72)
403+
ps_name = font.get_sfnt()[(1,0,0,6)]
404+
char_id = urllib.quote('%s-%d' % (ps_name, ord(char)))
405+
return self._char_defs[char_id]
383406

384407
def _draw_mathtext(self, gc, x, y, s, prop, angle):
385408
"""
@@ -390,12 +413,22 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
390413
svg_glyphs = svg_elements.svg_glyphs
391414
svg_rects = svg_elements.svg_rects
392415
color = rgb2hex(gc.get_rgb())
393-
394-
self.open_group("mathtext")
416+
write = self._svgwriter.write
395417

396418
style = "fill: %s" % color
397419

398420
if rcParams['svg.embed_char_paths']:
421+
new_chars = []
422+
for font, fontsize, thetext, new_x, new_y_mtc, metrics in svg_glyphs:
423+
path = self._add_char_def(font, thetext)
424+
if path is not None:
425+
new_chars.append(path)
426+
if len(new_chars):
427+
write('<defs>\n')
428+
for path in new_chars:
429+
write(path)
430+
write('</defs>\n')
431+
399432
svg = ['<g style="%s" transform="' % style]
400433
if angle != 0:
401434
svg.append('translate(%s,%s)rotate(%1.1f)'
@@ -405,7 +438,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
405438
svg.append('">\n')
406439

407440
for font, fontsize, thetext, new_x, new_y_mtc, metrics in svg_glyphs:
408-
charid = self._add_char_def(font, thetext)
441+
charid = self._get_char_def_id(font, thetext)
409442

410443
svg.append('<use xlink:href="#%s" transform="translate(%s,%s)scale(%s)"/>\n' %
411444
(charid, new_x, -new_y_mtc, fontsize / self.FONT_SCALE))
@@ -459,16 +492,12 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
459492
svg.append('<rect x="%s" y="%s" width="%s" height="%s" fill="black" stroke="none" />' % (x, -y + height, width, height))
460493
svg.append("</g>")
461494

462-
self._svgwriter.write (''.join(svg))
495+
self.open_group("mathtext")
496+
write (''.join(svg))
463497
self.close_group("mathtext")
464498

465499
def finish(self):
466500
write = self._svgwriter.write
467-
if len(self._char_defs):
468-
write('<defs id="fontpaths">\n')
469-
for char_num, path in self._char_defs.values():
470-
write(path)
471-
write('</defs>\n')
472501
write('</svg>\n')
473502

474503
def flipy(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.