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

Skip to content

Commit ea34677

Browse files
tacaswellQuLogic
authored andcommitted
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 9868db6 commit ea34677

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,10 +1153,48 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
11531153
weight = fm.weight_dict[prop.get_weight()]
11541154
if weight != 400:
11551155
font_parts.append(f'{weight}')
1156+
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
1165+
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)
1169+
# 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)
1193+
11561194
font_parts.extend([
11571195
f'{_short_float_fmt(prop.get_size())}px',
1158-
# ensure quoting
1159-
f'{", ".join(repr(f) for f in prop.get_family())}',
1196+
# ensure quoting and expansion of font names
1197+
", ".join(dict.fromkeys(_get_all_names(prop)))
11601198
])
11611199
style['font'] = ' '.join(font_parts)
11621200
if prop.get_stretch() != 'normal':

0 commit comments

Comments
 (0)