Skip to content

Commit 9b01a03

Browse files
authored
Merge pull request #12565 from anntzer/score_weight
Make FontManager.score_weight less lenient.
2 parents 1ffaf41 + de67128 commit 9b01a03

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
`FontManager.score_weight` is more strict with its inputs
2+
`````````````````````````````````````````````````````````
3+
4+
Previously, when a weight string was passed to `FontManager.score_weight`,
5+
6+
- if the weight was the string representation of an integer, it would be
7+
converted to that integer,
8+
- otherwise, if the weight was not a standard weight name, it would be silently
9+
replaced by a value of 500 ("normal" weight).
10+
11+
`FontManager.score_weight` now raises an exception on such inputs.

lib/matplotlib/font_manager.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from functools import lru_cache
2727
import json
2828
import logging
29+
from numbers import Number
2930
import os
3031
from pathlib import Path
3132
import subprocess
@@ -1049,8 +1050,8 @@ def score_style(self, style1, style2):
10491050
"""
10501051
if style1 == style2:
10511052
return 0.0
1052-
elif style1 in ('italic', 'oblique') and \
1053-
style2 in ('italic', 'oblique'):
1053+
elif (style1 in ('italic', 'oblique')
1054+
and style2 in ('italic', 'oblique')):
10541055
return 0.1
10551056
return 1.0
10561057

@@ -1094,21 +1095,12 @@ def score_weight(self, weight1, weight2):
10941095
the CSS numeric values of *weight1* and *weight2*, normalized between
10951096
0.05 and 1.0.
10961097
"""
1097-
10981098
# exact match of the weight names, e.g. weight1 == weight2 == "regular"
1099-
if (isinstance(weight1, str) and
1100-
isinstance(weight2, str) and
1101-
weight1 == weight2):
1099+
if cbook._str_equal(weight1, weight2):
11021100
return 0.0
1103-
try:
1104-
weightval1 = int(weight1)
1105-
except ValueError:
1106-
weightval1 = weight_dict.get(weight1, 500)
1107-
try:
1108-
weightval2 = int(weight2)
1109-
except ValueError:
1110-
weightval2 = weight_dict.get(weight2, 500)
1111-
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
1101+
w1 = weight1 if isinstance(weight1, Number) else weight_dict[weight1]
1102+
w2 = weight2 if isinstance(weight2, Number) else weight_dict[weight2]
1103+
return 0.95 * (abs(w1 - w2) / 1000) + 0.05
11121104

11131105
def score_size(self, size1, size2):
11141106
"""

0 commit comments

Comments
 (0)