8000 The font with the same weight name as the user specified weight name … by kshramt · Pull Request #7931 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 7 commits into from
Apr 16, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we deprecating this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because weight_as_number is not used by matplotlib after this patch.

def weight_as_number(weight):
"""
Return the weight property as a numeric value. String values
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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):
"""
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you break this up into a bunch of asserts

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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...
Expand Down
0