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

Skip to content

Commit 627ae26

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 627ae26

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

lib/matplotlib/_fontconfig_pattern.py

Lines changed: 13 additions & 37 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,17 @@
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
25-
23+
_family_escape = partial(re.compile(r'(?=[%s])' % family_punc).sub, r'\\')
2624
value_punc = r'\\=_:,'
2725
_value_unescape = partial(re.compile(r'\\(?=[%s])' % value_punc).sub, '')
28-
value_escape = re.compile(r'([%s])' % value_punc).sub
26+
_value_escape = partial(re.compile(r'(?=[%s])' % value_punc).sub, r'\\')
2927

3028
# Remove after module deprecation elapses (3.8); then remove underscores
31-
# from _family_unescape and _value_unescape.
29+
# from _{family,value}_{un,}escape.
3230
family_unescape = re.compile(r'\\([%s])' % family_punc).sub
3331
value_unescape = re.compile(r'\\([%s])' % value_punc).sub
32+
family_escape = re.compile(r'([%s])' % family_punc).sub
33+
value_escape = re.compile(r'([%s])' % value_punc).sub
3434

3535

3636
class FontconfigPatternParser:
@@ -127,36 +127,12 @@ def parse(self, pattern):
127127
parse_fontconfig_pattern = lru_cache()(FontconfigPatternParser().parse)
128128

129129

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-
143130
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)
131+
"""Convert a `.FontProperties` to a fontconfig pattern string."""
132+
kvs = [(k, getattr(d, f"get_{k}")())
133+
for k in ["style", "variant", "weight", "stretch", "file", "size"]]
134+
# Families is given first without a leading keyword. Other entries (which
135+
# are necessarily scalar) are given as key=value, skipping Nones.
136+
return (",".join(_family_escape(f) for f in d.get_family())
137+
+ "".join(f":{k}={_value_escape(str(v))}"
138+
for k, v in kvs if v is not None))

0 commit comments

Comments
 (0)