Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit4345d23

Browse files
committed
Improve font spec for SVG font referencing.
This replaces e.g.`"font-family:DejaVu Sans;font-size:12px;font-style:book;font-weight:book;"`by `"font: 400 12px 'DejaVu Sans'"`.Note that the previous font weight was plain wrong...
1 parentf2c5b37 commit4345d23

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

‎lib/matplotlib/backends/backend_svg.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
frommatplotlib.backends.backend_mixedimportMixedModeRenderer
2222
frommatplotlib.colorsimportrgb2hex
2323
frommatplotlib.datesimportUTC
24-
frommatplotlib.font_managerimportfindfont,get_font
24+
frommatplotlib.font_managerimportfindfont,get_font,ttfFontProperty
2525
frommatplotlib.ft2fontimportLOAD_NO_HINTING
2626
frommatplotlib.mathtextimportMathTextParser
2727
frommatplotlib.pathimportPath
@@ -94,6 +94,12 @@ def escape_attrib(s):
9494
returns
9595

9696

97+
def_quote_escape_attrib(s):
98+
return ('"'+escape_cdata(s)+'"'if'"'notinselse
99+
"'"+escape_cdata(s)+"'"if"'"notinselse
100+
'"'+escape_attrib(s)+'"')
101+
102+
97103
defshort_float_fmt(x):
98104
"""
99105
Create a short string representation of a float, which is %f
@@ -159,8 +165,8 @@ def start(self, tag, attrib={}, **extra):
159165
fork,vinsorted({**attrib,**extra}.items()):
160166
ifv:
161167
k=escape_cdata(k)
162-
v=escape_attrib(v)
163-
self.__write(' %s="%s"'% (k,v))
168+
v=_quote_escape_attrib(v)
169+
self.__write(' %s=%s'% (k,v))
164170
self.__open=1
165171
returnlen(self.__tags)-1
166172

@@ -1197,11 +1203,20 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
11971203
# Sort the characters by font, and output one tspan for each.
11981204
spans=OrderedDict()
11991205
forfont,fontsize,thetext,new_x,new_yinglyphs:
1200-
style=generate_css({
1201-
'font-size':short_float_fmt(fontsize)+'px',
1202-
'font-family':font.family_name,
1203-
'font-style':font.style_name.lower(),
1204-
'font-weight':font.style_name.lower()})
1206+
entry=ttfFontProperty(font)
1207+
font_parts= ['font:']
1208+
ifentry.style!='normal':
1209+
font_parts.append(entry.style)
1210+
ifentry.variant!='normal':
1211+
font_parts.append(entry.variant)
1212+
font_parts.extend([
1213+
f'{entry.weight}',
1214+
f'{short_float_fmt(fontsize)}px',
1215+
f'{entry.name!r}',# ensure quoting
1216+
])
1217+
ifentry.stretch!='normal':
1218+
font_parts.extend(['; font-stretch:',entry.stretch])
1219+
style=' '.join(font_parts)
12051220
ifthetext==32:
12061221
thetext=0xa0# non-breaking space
12071222
spans.setdefault(style, []).append((new_x,-new_y,thetext))

‎lib/matplotlib/tests/test_mathtext.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
importio
2-
importos
2+
frompathlibimportPath
33
importre
4+
importshlex
5+
fromxml.etreeimportElementTreeasET
46

57
importnumpyasnp
68
importpytest
@@ -328,7 +330,7 @@ def test_mathtext_fallback_to_cm_invalid():
328330
("stix", ['DejaVu Sans','mpltest','STIXGeneral'])])
329331
deftest_mathtext_fallback(fallback,fontlist):
330332
mpl.font_manager.fontManager.addfont(
331-
os.path.join((os.path.dirname(os.path.realpath(__file__))),'mpltest.ttf'))
333+
str(Path(__file__).resolve().parent/'mpltest.ttf'))
332334
mpl.rcParams["svg.fonttype"]='none'
333335
mpl.rcParams['mathtext.fontset']='custom'
334336
mpl.rcParams['mathtext.rm']='mpltest'
@@ -342,12 +344,13 @@ def test_mathtext_fallback(fallback, fontlist):
342344
fig,ax=plt.subplots()
343345
fig.text(.5,.5,test_str,fontsize=40,ha='center')
344346
fig.savefig(buff,format="svg")
345-
char_fonts= [
346-
line.split("font-family:")[-1].split(";")[0]
347-
forlineinstr(buff.getvalue()).split(r"\n")if"tspan"inline
348-
]
347+
tspans= (ET.fromstring(buff.getvalue())
348+
.findall(".//{http://www.w3.org/2000/svg}tspan[@style]"))
349+
# Getting the last element of the style attrib is a close enough
350+
# approximation for parsing the font property.
351+
char_fonts= [shlex.split(tspan.attrib["style"])[-1]fortspanintspans]
349352
assertchar_fonts==fontlist
350-
mpl.font_manager.fontManager.ttflist=mpl.font_manager.fontManager.ttflist[:-1]
353+
mpl.font_manager.fontManager.ttflist.pop()
351354

352355

353356
deftest_math_to_image(tmpdir):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp