8000 The font with the same weight name as the user specified weight name … · matplotlib/matplotlib@ad11c5e · GitHub
[go: up one dir, main page]

Skip to content

Commit ad11c5e

Browse files
author
kshramt
committed
The font with the same weight name as the user specified weight name should have the highest priority
FiraSans-Hair and FiraSans-Regular has the same numeric weight, 400, and thus they have the same priority for the previous `score_weight` implementation. However, it is clear that if an user set `rcParams["weight"] = "regular"`, the user wants FiraSans-Regular, not FiraSans-Hair.
1 parent 0384b85 commit ad11c5e

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

lib/matplotlib/font_manager.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -346,23 +346,6 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
346346
return [fname for fname in fontfiles if os.path.exists(fname)]
347347

348348

349-
def weight_as_number(weight):
350-
"""
351-
Return the weight property as a numeric value. String values
352-
are converted to their corresponding numeric value.
353-
"""
354-
if isinstance(weight, six.string_types):
355-
try:
356-
weight = weight_dict[weight.lower()]
357-
except KeyError:
358-
weight = 400
359-
elif weight in range(100, 1000, 100):
360-
pass
361-
else:
362-
raise ValueError('weight not a valid integer')
363-
return weight
364-
365-
366349
class FontEntry(object):
367350
"""
368351
A class for storing Font properties. It is used when populating
@@ -435,17 +418,12 @@ def ttfFontProperty(font):
435418
else:
436419
variant = 'normal'
437420

438-
# Weights are: 100, 200, 300, 400 (normal: default), 500 (medium),
439-
# 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black)
440-
# lighter and bolder are also allowed.
441-
442421
weight = next((w for w in weight_dict if sfnt4.find(w) >= 0), None)
443422
if not weight:
444423
if font.style_flags & ft2font.BOLD:
445-
weight = 700
424+
weight = "bold"
446425
else:
447-
weight = 400
448-
weight = weight_as_number(weight)
426+
weight = "normal"
449427

450428
# Stretch can be absolute and relative
451429
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -511,11 +489,7 @@ def afmFontProperty(fontpath, font):
511489
else:
512490
variant = 'normal'
513491

514-
# Weights are: 100, 200, 300, 400 (normal: default), 500 (medium),
515-
# 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black)
516-
# lighter and bolder are also allowed.
517-
518-
weight = weight_as_number(font.get_weight().lower())
492+
weight = font.get_weight().lower()
519493

520494
# Stretch can be absolute and relative
521495
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -855,7 +829,6 @@ def set_weight(self, weight):
855829
except ValueError:
856830
if weight not in weight_dict:
857831
raise ValueError("weight is invalid")
858-
weight = weight_dict[weight]
859832
self._weight = weight
860833

861834
def set_stretch(self, stretch):
@@ -1203,10 +1176,19 @@ def score_weight(self, weight1, weight2):
12031176
"""
12041177
Returns a match score between *weight1* and *weight2*.
12051178
1206-
The result is the absolute value of the difference between the
1179+
The result is 0.0 if both weight1 and weight 2 are given as strings
1180+
and have the same value.
1181+
1182+
Otherwise, the result is the absolute value of the difference between the
12071183
CSS numeric values of *weight1* and *weight2*, normalized
1208-
between 0.0 and 1.0.
1184+
between 0.05 and 1.0.
12091185
"""
1186+
1187+
# exact match of the weight names (e.g. weight1 == weight2 == "regular")
1188+
if (isinstance(weight1, six.string_types) and
1189+
isinstance(weight2, six.string_types) and
1190+
weight1 == weight2):
1191+
return 0.0
12101192
try:
12111193
weightval1 = int(weight1)
12121194
except ValueError:
@@ -1215,7 +1197,7 @@ def score_weight(self, weight1, weight2):
12151197
weightval2 = int(weight2)
12161198
except ValueError:
12171199
weightval2 = weight_dict.get(weight2, 500)
1218-
return abs(weightval1 - weightval2) / 1000.0
1200+
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
12191201

12201202
def score_size(self, size1, size2):
12211203
"""

0 commit comments

Comments
 (0)
0