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

Skip to content

Commit eeadff3

Browse files
authored
Merge pull request #17669 from anntzer/svgfont
Small changes to svg font embedding details
2 parents 23d3d30 + 7480805 commit eeadff3

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,12 @@ def _update_glyph_map_defs(self, glyph_map_new):
10221022
writer.start('defs')
10231023
for char_id, (vertices, codes) in glyph_map_new.items():
10241024
char_id = self._adjust_char_id(char_id)
1025+
# x64 to go back to FreeType's internal (integral) units.
10251026
path_data = self._convert_path(
1026-
Path(vertices, codes), simplify=False)
1027-
writer.element('path', id=char_id, d=path_data)
1027+
Path(vertices * 64, codes), simplify=False)
1028+
writer.element(
1029+
'path', id=char_id, d=path_data,
1030+
transform=generate_transform([('scale', (1 / 64,))]))
10281031
writer.end('defs')
10291032
self._glyph_map.update(glyph_map_new)
10301033

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22
from io import BytesIO
3-
import re
43
import tempfile
54
import xml.etree.ElementTree
65
import xml.parsers.expat
@@ -211,11 +210,13 @@ def test_unicode_won():
211210

212211
with BytesIO() as fd:
213212
fig.savefig(fd, format='svg')
214-
buf = fd.getvalue().decode('ascii')
213+
buf = fd.getvalue()
215214

216-
won_id = 'Computer_Modern_Sans_Serif-142'
217-
assert re.search(r'<path d=(.|\s)*?id="{0}"/>'.format(won_id), buf)
218-
assert re.search(r'<use[^/>]*? xlink:href="#{0}"/>'.format(won_id), buf)
215+
tree = xml.etree.ElementTree.fromstring(buf)
216+
ns = 'http://www.w3.org/2000/svg'
217+
won_id = 'SFSS3583-8e'
218+
assert len(tree.findall(f'.//{{{ns}}}path[@d][@id="{won_id}"]')) == 1
219+
assert f'#{won_id}' in tree.find(f'.//{{{ns}}}use').attrib.values()
219220

220221

221222
def test_svgnone_with_data_coordinates():

lib/matplotlib/textpath.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,7 @@ def _get_char_id(self, font, ccode):
4141
"""
4242
Return a unique id for the given font and character-code set.
4343
"""
44-
return urllib.parse.quote('{}-{}'.format(font.postscript_name, ccode))
45-
46-
def _get_char_id_ps(self, font, ccode):
47-
"""
48-
Return a unique id for the given font and character-code set (for tex).
49-
"""
50-
ps_name = font.get_ps_font_info()[2]
51-
char_id = urllib.parse.quote('%s-%d' % (ps_name, ccode))
52-
return char_id
44+
return urllib.parse.quote(f"{font.postscript_name}-{ccode:x}")
5345

5446
def get_text_width_height_descent(self, s, prop, ismath):
5547
if ismath == "TeX":
@@ -253,7 +245,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
253245
# characters into strings.
254246
for x1, y1, dvifont, glyph, width in page.text:
255247
font, enc = self._get_ps_font_and_encoding(dvifont.texname)
256-
char_id = self._get_char_id_ps(font, glyph)
248+
char_id = self._get_char_id(font, glyph)
257249

258250
if char_id not in glyph_map:
259251
font.clear()

0 commit comments

Comments
 (0)