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

Commit6fda79d

Browse files
authored
Merge pull request#15601 from TD22057/fontconfig
Fix FontProperties conversion to/from strings
2 parents60aa0f9 +cf37d54 commit6fda79d

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

‎lib/matplotlib/fontconfig_pattern.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
fromfunctoolsimportlru_cache
1616
importre
17-
17+
importnumpyasnp
1818
frompyparsingimport (Literal,ZeroOrMore,Optional,Regex,StringEnd,
1919
ParseException,Suppress)
2020

@@ -177,19 +177,36 @@ def _property(self, s, loc, tokens):
177177
parse_fontconfig_pattern=lru_cache()(FontconfigPatternParser().parse)
178178

179179

180+
def_escape_val(val,escape_func):
181+
"""
182+
Given a string value or a list of string values, run each value through
183+
the input escape function to make the values into legal font config
184+
strings. The result is returned as a string.
185+
"""
186+
ifnotnp.iterable(val)orisinstance(val,str):
187+
val= [val]
188+
189+
return','.join(escape_func(r'\\\1',str(x))forxinval
190+
ifxisnotNone)
191+
192+
180193
defgenerate_fontconfig_pattern(d):
181194
"""
182195
Given a dictionary of key/value pairs, generates a fontconfig
183196
pattern string.
184197
"""
185198
props= []
186-
forkeyin'family style variant weight stretch file size'.split():
199+
200+
# Family is added first w/o a keyword
201+
family=d.get_family()
202+
iffamilyisnotNoneandfamily!= []:
203+
props.append(_escape_val(family,family_escape))
204+
205+
# The other keys are added as key=value
206+
forkeyin ['style','variant','weight','stretch','file','size']:
187207
val=getattr(d,'get_'+key)()
208+
# Don't use 'if not val' because 0 is a valid input.
188209
ifvalisnotNoneandval!= []:
189-
iftype(val)==list:
190-
val= [value_escape(r'\\\1',str(x))forxinval
191-
ifxisnotNone]
192-
ifval!= []:
193-
val=','.join(val)
194-
props.append(":%s=%s"% (key,val))
210+
props.append(":%s=%s"% (key,_escape_val(val,value_escape)))
211+
195212
return''.join(props)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
frommatplotlib.font_managerimportFontProperties
2+
3+
4+
# Attributes on FontProperties object to check for consistency
5+
keys= [
6+
"get_family",
7+
"get_style",
8+
"get_variant",
9+
"get_weight",
10+
"get_size",
11+
]
12+
13+
14+
deftest_fontconfig_pattern():
15+
"Test converting a FontProperties to string then back."
16+
17+
# Defaults
18+
test="defaults "
19+
f1=FontProperties()
20+
s=str(f1)
21+
22+
f2=FontProperties(s)
23+
forkinkeys:
24+
assertgetattr(f1,k)()==getattr(f2,k)(),test+k
25+
26+
# Basic inputs
27+
test="basic "
28+
f1=FontProperties(family="serif",size=20,style="italic")
29+
s=str(f1)
30+
31+
f2=FontProperties(s)
32+
forkinkeys:
33+
assertgetattr(f1,k)()==getattr(f2,k)(),test+k
34+
35+
# Full set of inputs.
36+
test="full "
37+
f1=FontProperties(family="sans-serif",size=24,weight="bold",
38+
style="oblique",variant="small-caps",
39+
stretch="expanded")
40+
s=str(f1)
41+
42+
f2=FontProperties(s)
43+
forkinkeys:
44+
assertgetattr(f1,k)()==getattr(f2,k)(),test+k
45+
46+
47+
deftest_fontconfig_str():
48+
"Test FontProperties string conversions for correctness"
49+
50+
# Known good strings taken from actual font config specs on a linux box
51+
# and modified for MPL defaults.
52+
53+
# Default values found by inspection.
54+
test="defaults "
55+
s= ("sans\\-serif:style=normal:variant=normal:weight=normal"
56+
":stretch=normal:size=12.0")
57+
font=FontProperties(s)
58+
right=FontProperties()
59+
forkinkeys:
60+
assertgetattr(font,k)()==getattr(right,k)(),test+k
61+
62+
test="full "
63+
s= ("serif:size=24:style=oblique:variant=small-caps:weight=bold"
64+
":stretch=expanded")
65+
font=FontProperties(s)
66+
right=FontProperties(family="serif",size=24,weight="bold",
67+
style="oblique",variant="small-caps",
68+
stretch="expanded")
69+
forkinkeys:
70+
assertgetattr(font,k)()==getattr(right,k)(),test+k

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp