-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
The font with the same weight name as the user specified weight name … #7931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
d94a6a5
30031b5
97e7dac
8a10efc
91075a3
a7026e0
42b1659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'): | |
return [fname for fname in fontfiles if os.path.exists(fname)] | ||
|
||
|
||
@cbook.deprecated("2.1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we deprecating this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
def weight_as_number(weight): | ||
""" | ||
Return the weight property as a numeric value. String values | ||
|
@@ -435,17 +436,12 @@ def ttfFontProperty(font): | |
else: | ||
variant = 'normal' | ||
|
||
# Weights are: 100, 200, 300, 400 (normal: default), 500 (medium), | ||
# 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black) | ||
# lighter and bolder are also allowed. | ||
|
||
weight = next((w for w in weight_dict if sfnt4.find(w) >= 0), None) | ||
if not weight: | ||
if font.style_flags & ft2font.BOLD: | ||
weight = 700 | ||
weight = "bold" | ||
else: | ||
weight = 400 | ||
weight = weight_as_number(weight) | ||
weight = "normal" | ||
|
||
# Stretch can be absolute and relative | ||
# Absolute stretches are: ultra-condensed, extra-condensed, condensed, | ||
|
@@ -511,11 +507,7 @@ def afmFontProperty(fontpath, font): | |
else: | ||
variant = 'normal' | ||
|
||
# Weights are: 100, 200, 300, 400 (normal: default), 500 (medium), | ||
# 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black) | ||
# lighter and bolder are also allowed. | ||
|
||
weight = weight_as_number(font.get_weight().lower()) | ||
weight = font.get_weight().lower() | ||
|
||
# Stretch can be absolute and relative | ||
# Absolute stretches are: ultra-condensed, extra-condensed, condensed, | ||
|
@@ -855,7 +847,6 @@ def set_weight(self, weight): | |
except ValueError: | ||
if weight not in weight_dict: | ||
raise ValueError("weight is invalid") | ||
weight = weight_dict[weight] | ||
self._weight = weight | ||
|
||
def set_stretch(self, stretch): | ||
|
@@ -1203,10 +1194,19 @@ def score_weight(self, weight1, weight2): | |
""" | ||
Returns a match score between *weight1* and *weight2*. | ||
|
||
The result is the absolute value of the difference between the | ||
The result is 0.0 if both weight1 and weight 2 are given as strings | ||
and have the same value. | ||
|
||
Otherwise, the result is the absolute value of the difference between the | ||
CSS numeric values of *weight1* and *weight2*, normalized | ||
between 0.0 and 1.0. | ||
between 0.05 and 1.0. | ||
""" | ||
|
||
# exact match of the weight names (e.g. weight1 == weight2 == "regular") | ||
if (isinstance(weight1, six.string_types) and | ||
isinstance(weight2, six.string_types) and | ||
weight1 == weight2): | ||
return 0.0 | ||
try: | ||
weightval1 = int(weight1) | ||
except ValueError: | ||
|
@@ -1215,7 +1215,7 @@ def score_weight(self, weight1, weight2): | |
weightval2 = int(weight2) | ||
except ValueError: | ||
weightval2 = weight_dict.get(weight2, 500) | ||
return abs(weightval1 - weightval2) / 1000.0 | ||
return 0.95*(abs(weightval1 - weightval2) / 1000.0) + 0.05 | ||
|
||
def score_size(self, size1, size2): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,21 @@ def test_font_priority(): | |
assert cmap[8729] == 30 | ||
|
||
|
||
def test_ttflist_weight(): | ||
assert all(isinstance(f.weight, six.string_types) | ||
for f in fontManager.ttflist) | ||
|
||
|
||
def test_score_weight(): | ||
assert (0 == | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you break this up into a bunch of asserts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
fontManager.score_weight("regular", "regular") == | ||
fontManager.score_weight("bold", "bold") < | ||
fontManager.score_weight(400, 400) == | ||
# "normal" and "regular" have the same numerical weight | ||
fontManager.score_weight("normal", "regular") < | ||
fontManager.score_weight("normal", "bold")) | ||
|
||
|
||
def test_json_serialization(): | ||
# on windows, we can't open a file twice, so save the name and unlink | ||
# manually... | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as on the other PR, please do not remove functions even if unused. Because Matplotlib is so widely used, we need to go through a deprecation cycle.