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

Skip to content

Commit 818443f

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 818443f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

lib/matplotlib/font_manager.py

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

348348

349+
@cbook.deprecated("2.1")
349350
def weight_as_number(weight):
350351
"""
351352
Return the weight property as a numeric value. String values
@@ -435,17 +436,12 @@ def ttfFontProperty(font):
435436
else:
436437
variant = 'normal'
437438

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-
442439
weight = next((w for w in weight_dict if sfnt4.find(w) >= 0), None)
443440
if not weight:
444441
if font.style_flags & ft2font.BOLD:
445-
weight = 700
442+
weight = "bold"
446443
else:
447-
weight = 400
448-
weight = weight_as_number(weight)
444+
weight = "normal"
449445

450446
# Stretch can be absolute and relative
451447
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -511,11 +507,7 @@ def afmFontProperty(fontpath, font):
511507
else:
512508
variant = 'normal'
513509

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())
510+
weight = font.get_weight().lower()
519511

520512
# Stretch can be absolute and relative
521513
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -855,7 +847,6 @@ def set_weight(self, weight):
855847
except ValueError:
856848
if weight not in weight_dict:
857849
raise ValueError("weight is invalid")
858-
weight = weight_dict[weight]
859850
self._weight = weight
860851

861852
def set_stretch(self, stretch):
@@ -1203,10 +1194,19 @@ def score_weight(self, weight1, weight2):
12031194
"""
12041195
Returns a match score between *weight1* and *weight2*.
12051196
1206-
The result is the absolute value of the difference between the
1197+
The result is 0.0 if both weight1 and weight 2 are given as strings
1198+
and have the same value.
1199+
1200+
Otherwise, the result is the absolute value of the difference between the
12071201
CSS numeric values of *weight1* and *weight2*, normalized
1208-
between 0.0 and 1.0.
1202+
between 0.05 and 1.0.
12091203
"""
1204+
1205+
# exact match of the weight names (e.g. weight1 == weight2 == "regular")
1206+
if (isinstance(weight1, six.string_types) and
1207+
isinstance(weight2, six.string_types) and
1208+
weight1 == weight2):
1209+
return 0.0
12101210
try:
12111211
weightval1 = int(weight1)
12121212
except ValueError:
@@ -1215,7 +1215,7 @@ def score_weight(self, weight1, weight2):
12151215
weightval2 = int(weight2)
12161216
except ValueError:
12171217
weightval2 = weight_dict.get(weight2, 500)
1218-
return abs(weightval1 - weightval2) / 1000.0
1218+
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
12191219

12201220
def score_size(self, size1, size2):
12211221
"""

0 commit comments

Comments
 (0)
0