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

Skip to content

Commit bb14c7f

Browse files
committed
Fixed FontProperties to/from strings
1 parent aa01f9a commit bb14c7f

File tree

2 files changed

+92
-7
lines changed

2 files changed

+92
-7
lines changed

lib/matplotlib/fontconfig_pattern.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,34 @@ def _property(self, s, loc, tokens):
177177
parse_fontconfig_pattern = lru_cache()(FontconfigPatternParser().parse)
178178

179179

180+
def _escape_val(val, escape_func):
181+
if type(val) == list:
182+
val = [escape_func(r'\\\1', str(x)) for x in val
183+
if x is not None]
184+
if val != []:
185+
val = ','.join(val)
186+
else:
187+
val = escape_func(r'\\\1', str(val))
188+
189+
return val
190+
191+
180192
def generate_fontconfig_pattern(d):
181193
"""
182194
Given a dictionary of key/value pairs, generates a fontconfig
183195
pattern string.
184196
"""
185197
props = []
186-
for key in 'family style variant weight stretch file size'.split():
198+
199+
# Family is added first w/o a keyword
200+
family = d.get_family()
201+
if family is not None and family != []:
202+
props.append("%s" % (_escape_val(family, family_escape)))
203+
204+
# The other keys are added as key=value
205+
for key in ['style', 'variant', 'weight', 'stretch', 'file', 'size']:
187206
val = getattr(d, 'get_' + key)()
188207
if val is not None and val != []:
189-
if type(val) == list:
190-
val = [value_escape(r'\\\1', str(x)) for x in val
191-
if x is not None]
192-
if val != []:
193-
val = ','.join(val)
194-
props.append(":%s=%s" % (key, val))
208+
props.append(":%s=%s" % (key, _escape_val(val, value_escape)))
209+
195210
return ''.join(props)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from matplotlib.font_manager import FontProperties
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+
def test_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+
for k in keys:
24+
assert getattr(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+
for k in keys:
33+
assert getattr(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+
for k in keys:
44+
assert getattr(f1, k)() == getattr(f2, k)(), test + k
45+
46+
47+
def test_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+
for k in keys:
60+
assert getattr(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+
for k in keys:
70+
assert getattr(font, k)() == getattr(right, k)(), test + k

0 commit comments

Comments
 (0)