Skip to content

Commit de67128

Browse files
committed
Make FontManager.score_weight less lenient.
1 parent a43fd85 commit de67128

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
@@ -33,6 +33,7 @@
3333
from functools import lru_cache
3434
import json
3535
import logging
36+
from numbers import Number
3637
import os
3738
from pathlib import Path
3839
import subprocess
@@ -1066,8 +1067,8 @@ def score_style(self, style1, style2):
10661067
"""
10671068
if style1 == style2:
10681069
return 0.0
1069-
elif style1 in ('italic', 'oblique') and \
1070-
style2 in ('italic', 'oblique'):
1070+
elif (style1 in ('italic', 'oblique')
1071+
and style2 in ('italic', 'oblique')):
10711072
return 0.1
10721073
return 1.0
10731074

@@ -1111,21 +1112,12 @@ def score_weight(self, weight1, weight2):
11111112
the CSS numeric values of *weight1* and *weight2*, normalized between
11121113
0.05 and 1.0.
11131114
"""
1114-
11151115
# exact match of the weight names, e.g. weight1 == weight2 == "regular"
1116-
if (isinstance(weight1, str) and
1117-
isinstance(weight2, str) and
1118-
weight1 == weight2):
1116+
if cbook._str_equal(weight1, weight2):
11191117
return 0.0
1120-
try:
1121-
weightval1 = int(weight1)
1122-
except ValueError:
1123-
weightval1 = weight_dict.get(weight1, 500)
1124-
try:
1125-
weightval2 = int(weight2)
1126-
except ValueError:
1127-
weightval2 = weight_dict.get(weight2, 500)
1128-
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
1118+
w1 = weight1 if isinstance(weight1, Number) else weight_dict[weight1]
1119+
w2 = weight2 if isinstance(weight2, Number) else weight_dict[weight2]
1120+
return 0.95 * (abs(w1 - w2) / 1000) + 0.05
11291121

11301122
def score_size(self, size1, size2):
11311123
"""

0 commit comments

Comments
 (0)