Skip to content

Commit cb405cf

Browse files
committed
Improve font weight guessing.
Previously, we would guess that "Times New Roman Bold" is a regular weight font because its name contains the substring "Roman", even though the font correctly sets the BOLD flag (FT_STYLE_FLAG_BOLD). Invert the logic to give priority to the flag, as its presence is clearly more robust than a substring check. `s.find(w) >= 0` is just an obfuscated way to write `w in s`, so change that.
1 parent 1c06de3 commit cb405cf

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/matplotlib/font_manager.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,10 @@ def ttfFontProperty(font):
395395
else:
396396
variant = 'normal'
397397

398-
weight = next((w for w in weight_dict if sfnt4.find(w) >= 0), None)
399-
if not weight:
400-
if font.style_flags & ft2font.BOLD:
401-
weight = 700
402-
else:
403-
weight = 400
398+
if font.style_flags & ft2font.BOLD:
399+
weight = 700
400+
else:
401+
weight = next((w for w in weight_dict if w in sfnt4), 400)
404402

405403
# Stretch can be absolute and relative
406404
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,

0 commit comments

Comments
 (0)