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

Skip to content

Simplify generate_fontconfig_pattern. #24303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 13 additions & 37 deletions lib/matplotlib/_fontconfig_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from functools import lru_cache, partial
import re

import numpy as np
from pyparsing import (
Optional, ParseException, Regex, StringEnd, Suppress, ZeroOrMore)

Expand All @@ -21,16 +20,17 @@

family_punc = r'\\\-:,'
_family_unescape = partial(re.compile(r'\\(?=[%s])' % family_punc).sub, '')
family_escape = re.compile(r'([%s])' % family_punc).sub

_family_escape = partial(re.compile(r'(?=[%s])' % family_punc).sub, r'\\')
value_punc = r'\\=_:,'
_value_unescape = partial(re.compile(r'\\(?=[%s])' % value_punc).sub, '')
value_escape = re.compile(r'([%s])' % value_punc).sub
_value_escape = partial(re.compile(r'(?=[%s])' % value_punc).sub, r'\\')

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


class FontconfigPatternParser:
Expand Down Expand Up @@ -127,36 +127,12 @@ def parse(self, pattern):
parse_fontconfig_pattern = lru_cache()(FontconfigPatternParser().parse)


def _escape_val(val, escape_func):
"""
Given a string value or a list of string values, run each value through
the input escape function to make the values into legal font config
strings. The result is returned as a string.
"""
if not np.iterable(val) or isinstance(val, str):
val = [val]

return ','.join(escape_func(r'\\\1', str(x)) for x in val
if x is not None)


def generate_fontconfig_pattern(d):
"""
Given a dictionary of key/value pairs, generates a fontconfig
pattern string.
"""
props = []

# Family is added first w/o a keyword
family = d.get_family()
if family is not None and family != []:
props.append(_escape_val(family, family_escape))

# The other keys are added as key=value
for key in ['style', 'variant', 'weight', 'stretch', 'file', 'size']:
val = getattr(d, 'get_' + key)()
# Don't use 'if not val' because 0 is a valid input.
if val is not None and val != []:
props.append(":%s=%s" % (key, _escape_val(val, value_escape)))

return ''.join(props)
"""Convert a `.FontProperties` to a fontconfig pattern string."""
kvs = [(k, getattr(d, f"get_{k}")())
for k in ["style", "variant", "weight", "stretch", "file", "size"]]
# Families is given first without a leading keyword. Other entries (which
# are necessarily scalar) are given as key=value, skipping Nones.
return (",".join(_family_escape(f) for f in d.get_family())
+ "".join(f":{k}={_value_escape(str(v))}"
for k, v in kvs if v is not None))