8000 Merge pull request #7931 from kshramt/font_weight · matplotlib/matplotlib@249f7bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 249f7bb

Browse files
authored
10000 Merge pull request #7931 from kshramt/font_weight
FIX: consider 'weight-as-string' more highly than 'weight-as-number' - Internally store the text weight as a string - if compare weight as strings before comparing as number, apply bias against numeric but not string match Slight concern that our font selection method is getting farther away from 'standard', but it fixes the handling of fonts that do not correctly report their weights or use non-standard weights and does not obviously break anything els.
2 parents 1144a64 + 42b1659 commit 249f7bb

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

lib/matplotlib/font_manager.py

Lines changed: 14 additions & 14 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:
445442
weight = 700
446443
else:
447444
weight = 400
448-
weight = weight_as_number(weight)
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):
@@ -1204,10 +1195,19 @@ def score_weight(self, weight1, weight2):
12041195
"""
12051196
Returns a match score between *weight1* and *weight2*.
12061197
1207-
The result is the absolute value of the difference between the
1198+
The result is 0.0 if both weight1 and weight 2 are given as strings
1199+
and have the same value.
1200+
1201+
Otherwise, the result is the absolute value of the difference between the
12081202
CSS numeric values of *weight1* and *weight2*, normalized
1209-
between 0.0 and 1.0.
1203+
between 0.05 and 1.0.
12101204
"""
1205+
1206+
# exact match of the weight names (e.g. weight1 == weight2 == "regular")
1207+
if (isinstance(weight1, six.string_types) and
1208+
isinstance(weight2, six.string_types) and
1209+
weight1 == weight2):
1210+
return 0.0
12111211
try:
12121212
weightval1 = int(weight1)
12131213
except ValueError:
@@ -1216,7 +1216,7 @@ def score_weight(self, weight1, weight2):
12161216
weightval2 = int(weight2)
12171217
except ValueError:
12181218
weightval2 = weight_dict.get(weight2, 500)
1219-
return abs(weightval1 - weightval2) / 1000.0
1219+
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05
12201220

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

lib/matplotlib/tests/test_font_manager.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def test_font_priority():
3838
assert cmap[8729] == 30
3939

4040

41+
def test_score_weight():
42+
assert 0 == fontManager.score_weight("regular", "regular")
43+
assert 0 == fontManager.score_weight("bold", "bold")
44+
assert (0 < fontManager.score_weight(400, 400) <
45+
fontManager.score_weight("normal", "bold"))
46+
assert (0 < fontManager.score_weight("normal", "regular") <
47+
fontManager.score_weight("normal", "bold"))
48+
assert (fontManager.score_weight("normal", "regular") ==
49+
fontManager.score_weight(400, 400))
50+
51+
4152
def test_json_serialization():
4253
# on windows, we can't open a file twice, so save the name and unlink
4354
# manually...

0 commit comments

Comments
 (0)
0