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

Skip to content

Commit 6fda79d

Browse files
authored
Merge pull request #15601 from TD22057/fontconfig
Fix FontProperties conversion to/from strings
2 parents 60aa0f9 + cf37d54 commit 6fda79d

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
from functools import lru_cache
1616
import re
17-
17+
import numpy as np
1818
from pyparsing import (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+
if not np.iterable(val) or isinstance(val, str):
187+
val = [val]
188+
189+
return ','.join(escape_func(r'\\\1', str(x)) for x in val
190+
if x is not None)
191+
192+
180193
def generate_fontconfig_pattern(d):
181194
"""
182195
Given a dictionary of key/value pairs, generates a fontconfig
183196
pattern string.
184197
"""
185198
props = []
186-
for key in 'family style variant weight stretch file size'.split():
199+
200+
# Family is added first w/o a keyword
201+
family = d.get_family()
202+
if family is not None and family != []:
203+
props.append(_escape_val(family, family_escape))
204+
205+
# The other keys are added as key=value
206+
for key in ['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
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))
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+
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)