diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 3e9a164d1458..4d77e1da373c 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -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") 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 else: weight = 400 - weight = weight_as_number(weight) # 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): """ diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index 90785e8f4985..8adf601322fc 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -31,6 +31,17 @@ def test_font_priority(): assert cmap[8729] == 30 +def test_score_weight(): + assert 0 == fontManager.score_weight("regular", "regular") + assert 0 == fontManager.score_weight("bold", "bold") + assert (0 < fontManager.score_weight(400, 400) < + fontManager.score_weight("normal", "bold")) + assert (0 < fontManager.score_weight("normal", "regular") < + fontManager.score_weight("normal", "bold")) + assert (fontManager.score_weight("normal", "regular") == + fontManager.score_weight(400, 400)) + + def test_json_serialization(): # on windows, we can't open a file twice, so save the name and unlink # manually...