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

Skip to content

Commit 689d8d6

Browse files
committed
Simplify generate_fontconfig_pattern.
- Switch _family_escape, _value_escape to be more similar to _family_escape, _value_escape. - generate_fontconfig_pattern doesn't need to go through the generic escape_val, but instead rely on the fact that prop.get_family() always returns a list whereas the other getters always return a scalar value, and directly do the escaping and joining as needed.
1 parent 9b4985e commit 689d8d6

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

lib/matplotlib/_fontconfig_pattern.py

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from functools import lru_cache, partial
1313
import re
1414

15-
import numpy as np
1615
from pyparsing import (
1716
Optional, ParseException, Regex, StringEnd, Suppress, ZeroOrMore)
1817

@@ -21,16 +20,18 @@
2120

2221
family_punc = r'\\\-:,'
2322
_family_unescape = partial(re.compile(r'\\(?=[%s])' % family_punc).sub, '')
24-
family_escape = re.compile(r'([%s])' % family_punc).sub
23+
_family_escape = partial(re.compile(r'(?=[%s])' % family_punc).sub, r'\\')
2524

2625
value_punc = r'\\=_:,'
2726
_value_unescape = partial(re.compile(r'\\(?=[%s])' % value_punc).sub, '')
28-
value_escape = re.compile(r'([%s])' % value_punc).sub
27+
_value_escape = partial(re.compile(r'(?=[%s])' % value_punc).sub, r'\\')
2928

3029
# Remove after module deprecation elapses (3.8); then remove underscores
31-
# from _family_unescape and _value_unescape.
30+
# from _{family,value}_{un,}escape.
3231
family_unescape = re.compile(r'\\([%s])' % family_punc).sub
3332
value_unescape = re.compile(r'\\([%s])' % value_punc).sub
33+
family_escape = re.compile(r'([%s])' % family_punc).sub
34+
value_escape = re.compile(r'([%s])' % value_punc).sub
3435

3536

3637
class FontconfigPatternParser:
@@ -127,36 +128,12 @@ def parse(self, pattern):
127128
parse_fontconfig_pattern = lru_cache()(FontconfigPatternParser().parse)
128129

129130

130-
def _escape_val(val, escape_func):
131-
"""
132-
Given a string value or a list of string values, run each value through
133-
the input escape function to make the values into legal font config
134-
strings. The result is returned as a string.
135-
"""
136-
if not np.iterable(val) or isinstance(val, str):
137-
val = [val]
138-
139-
return ','.join(escape_func(r'\\\1', str(x)) for x in val
140-
if x is not None)
141-
142-
143131
def generate_fontconfig_pattern(d):
144-
"""
145-
Given a dictionary of key/value pairs, generates a fontconfig
146-
pattern string.
147-
"""
148-
props = []
149-
150-
# Family is added first w/o a keyword
151-
family = d.get_family()
152-
if family is not None and family != []:
153-
props.append(_escape_val(family, family_escape))
154-
155-
# The other keys are added as key=value
156-
for key in ['style', 'variant', 'weight', 'stretch', 'file', 'size']:
157-
val = getattr(d, 'get_' + key)()
158-
# Don't use 'if not val' because 0 is a valid input.
159-
if val is not None and val != []:
160-
props.append(":%s=%s" % (key, _escape_val(val, value_escape)))
161-
162-
return ''.join(props)
132+
"""Convert a `.FontProperties` to a fontconfig pattern string."""
133+
kvs = [(k, getattr(d, f"get_{k}")())
134+
for k in ["style", "variant", "weight", "stretch", "file", "size"]]
135+
# Families is given first without a leading keyword. Other entries (which
136+
# are necessarily scalar) are given as key=value, skipping Nones.
137+
return (",".join(_family_escape(f) for f in d.get_family())
138+
+ "".join(f":{k}={_value_escape(str(v))}"
139+
for k, v in kvs if v is not None))

0 commit comments

Comments
 (0)