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

Skip to content

Commit 5027f6c

Browse files
committed
FIX: correctly handle generic font families in svg text-as-text mode
This: - expands the generic fonts families to the user configured specific fonts in the svg output - ensures that each font family only appears once per text element in the svg output - ensures that generic families are unquoted and specific families are closes #22528 closes #23492
1 parent c8d438f commit 5027f6c

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,56 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
11511151
weight = fm.weight_dict[prop.get_weight()]
11521152
if weight != 400:
11531153
font_parts.append(f'{weight}')
1154+
1155+
def unique_iter(inp):
1156+
"""Only return unique values."""
1157+
seen = set()
1158+
for i in inp:
1159+
if i not in seen:
1160+
seen.add(i)
1161+
yield i
1162+
1163+
def _format_font_name(fn):
1164+
normalize_names = {
1165+
'sans': 'sans-serif',
1166+
'sans serif': 'sans-serif'
1167+
}
1168+
# A generic font family. We need to do two things:
1169+
# 1. list all of the configured fonts with quoted names
1170+
# 2. append the generic name unquoted
1171+
if fn in fm.font_family_aliases:
1172+
# fix spelling of sans-serif
1173+
# we accept 3 ways CSS only supports 1
1174+
fn = normalize_names.get(fn, fn)
1175+
# get all of the font names and fix spelling of sans-serif
1176+
# if it comes back
1177+
aliases = [
1178+
normalize_names.get(_, _) for _ in
1179+
fm.FontManager._expand_aliases(fn)
1180+
]
1181+
# make sure the generic name appears at least once
1182+
# duplicate is OK, next layer will deduplicate
1183+
aliases.append(fn)
1184+
1185+
for a in aliases:
1186+
# generic font families must not be quoted
1187+
if a in fm.font_family_aliases:
1188+
yield a
1189+
# specific font families must be quoted
1190+
else:
1191+
yield repr(a)
1192+
# specific font families must be quoted
1193+
else:
1194+
yield repr(fn)
1195+
1196+
def _get_all_names(prop):
1197+
for f in prop.get_family():
1198+
yield from _format_font_name(f)
1199+
11541200
font_parts.extend([
11551201
f'{_short_float_fmt(prop.get_size())}px',
1156-
# ensure quoting
1157-
f'{", ".join(repr(f) for f in prop.get_family())}',
1202+
# ensure quoting and expansion of font names
1203+
", ".join(unique_iter(_get_all_names(prop)))
11581204
])
11591205
style['font'] = ' '.join(font_parts)
11601206
if prop.get_stretch() != 'normal':

0 commit comments

Comments
 (0)