From 78073d3c5bcccf3755aafde3149e37ca7e559bff Mon Sep 17 00:00:00 2001 From: chadawagner Date: Tue, 14 Jul 2015 00:20:47 -0700 Subject: [PATCH 1/3] Update to score_family in font_manager.py This PR fixes the score calculation for the case of matching a generic font name given in the `families` list. The returned score is now constrained by the position `i` of the match in the list. If the font being scored (`family2`) is the first choice for that generic name in rcParams, it will receive the same score as if it were an exact match for that position in the `families` list, namely `i / len(families)`. If the font being scored is in the alias list but not the first choice, the returned score will be greater than `i / len(families)` but less than `(i + 1) / len(families)`, ensuring that the preferred order of fonts listed in both `families` and rcParams font alias list are respected. --- lib/matplotlib/font_manager.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 91fe97e128f4..a94e98ef5940 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -1099,15 +1099,16 @@ def score_family(self, families, family2): Returns a match score between the list of font families in *families* and the font family name *family2*. - An exact match anywhere in the list returns 0.0. + An exact match at the head of the list returns 0.0. - A match by generic font name will return 0.1. + A match further down the list will return between 0 and 1. No match will return 1.0. """ if not isinstance(families, (list, tuple)): families = [families] family2 = family2.lower() + step = 1 / len(families) for i, family1 in enumerate(families): family1 = family1.lower() if family1 in font_family_aliases: @@ -1117,12 +1118,11 @@ def score_family(self, families, family2): options = [x.lower() for x in options] if family2 in options: idx = options.index(family2) - return ((0.1 * (idx / len(options))) * - ((i + 1) / len(families))) + return (i + idx / len(options)) * step elif family1 == family2: # The score should be weighted by where in the # list the font was found. - return i / len(families) + return i * step return 1.0 def score_style(self, style1, style2): From d82c1c69d2f0e22cb58a64e22fb120e414f1adef Mon Sep 17 00:00:00 2001 From: chadawagner Date: Fri, 31 Jul 2015 15:01:37 -0700 Subject: [PATCH 2/3] added parens for clarity --- lib/matplotlib/font_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index a94e98ef5940..f4571df90b6e 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -1118,7 +1118,7 @@ def score_family(self, families, family2): options = [x.lower() for x in options] if family2 in options: idx = options.index(family2) - return (i + idx / len(options)) * step + return (i + (idx / len(options))) * step elif family1 == family2: # The score should be weighted by where in the # list the font was found. From 673feb09d3fa08e46615a46139227f2f4427688e Mon Sep 17 00:00:00 2001 From: chadawagner Date: Thu, 6 Aug 2015 15:24:43 -0700 Subject: [PATCH 3/3] Update to score_family in font_manager.py --- lib/matplotlib/font_manager.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index f4571df90b6e..a0a695bf268e 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -1107,6 +1107,8 @@ def score_family(self, families, family2): """ if not isinstance(families, (list, tuple)): families = [families] + elif len(families) == 0: + return 1.0 family2 = family2.lower() step = 1 / len(families) for i, family1 in enumerate(families):