8000 Simplify svg font expansion logic. · matplotlib/matplotlib@ec55cba · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ec55cba

Browse files
committed
Simplify svg font expansion logic.
We can delay quoting and deduplication until quite late.
1 parent fd5cf5c commit ec55cba

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,47 +1154,31 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
11541154
if weight != 400:
11551155
font_parts.append(f'{weight}')
11561156

1157-
def _format_font_name(fn):
1158-
normalize_names = {
1159-
'sans': 'sans-serif',
1160-
'sans serif': 'sans-serif'
1161-
}
1162-
# A generic font family. We need to do two things:
1163-
# 1. list all of the configured fonts with quoted names
1164-
# 2. append the generic name unquoted
1157+
def _normalize_sans(name):
1158+
return 'sans-serif' if name in ['sans', 'sans serif'] else name
1159+
1160+
def _expand_family_entry(fn):
1161+
fn = _normalize_sans(fn)
1162+
# prepend generic font families with all configured font names
11651163
if fn in fm.font_family_aliases:
1166-
# fix spelling of sans-serif
1167-
# we accept 3 ways CSS only supports 1
1168-
fn = normalize_names.get(fn, fn)
11691164
# get all of the font names and fix spelling of sans-serif
1170-
# if it comes back
1171-
aliases = [
1172-
normalize_names.get(_, _) for _ in
1173-
fm.FontManager._expand_aliases(fn)
1174-
]
1175-
# make sure the generic name appears at least once
1176-
# duplicate is OK, next layer will deduplicate
1177-
aliases.append(fn)
1178-
1179-
for a in aliases:
1180-
# generic font families must not be quoted
1181-
if a in fm.font_family_aliases:
1182-
yield a
1183-
# specific font families must be quoted
1184-
else:
1185-
yield repr(a)
1186-
# specific font families must be quoted
1187-
else:
1188-
yield repr(fn)
1189-
1190-
def _get_all_names(prop):
1191-
for f in prop.get_family():
1192-
yield from _format_font_name(f)
1165+
# (we accept 3 ways CSS only supports 1)
1166+
for name in fm.FontManager._expand_aliases(fn):
1167+
yield _normalize_sans(name)
1168+
# whether a generic name or a family name, it must appear at
1169+
# least once
1170+
yield fn
1171+
1172+
def _get_all_quoted_names(prop):
1173+
# only quote specific names, not generic names
1174+
return [name if name in fm.font_family_aliases else repr(name)
1175+
for entry in prop.get_family()
1176+
for name in _expand_family_entry(entry)]
11931177

11941178
font_parts.extend([
11951179
f'{_short_float_fmt(prop.get_size())}px',
1196-
# ensure quoting and expansion of font names
1197-
", ".join(dict.fromkeys(_get_all_names(prop)))
1180+
# ensure expansion, quoting, and dedupe of font names
1181+
", ".join(dict.fromkeys(_get_all_quoted_names(prop)))
11981182
])
11991183
style['font'] = ' '.join(font_parts)
12001184
if prop.get_stretch() != 'normal':

0 commit comments

Comments
 (0)
0