From 314a0cfdf2ebfb2faffdec5e4d702e5e6ed1f2ac Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 27 Jan 2014 12:39:34 -0500 Subject: [PATCH 1/4] Fixes a bug in font family lookup. Any font in the font alias list (e.g. font.sans-serif) would be considered a perfect match whenever the selected font alias is the first in the font.family list. This broke the priority ordering of font names. --- 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 5b5b558dc2a9..a612cd45ddb3 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -1071,7 +1071,7 @@ def score_family(self, families, family2): if family2 in options: idx = options.index(family2) return ((0.1 * (float(idx) / len(options))) * - (float(i) / float(len(families)))) + (float(i + 1) / float(len(families)))) elif family1 == family2: # The score should be weighted by where in the # list the font was found. From 40747912469133bcc92071458dc95f1d079e36a4 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 27 Jan 2014 12:55:08 -0500 Subject: [PATCH 2/4] Add test --- lib/matplotlib/__init__.py | 1 + lib/matplotlib/tests/test_font_manager.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 lib/matplotlib/tests/test_font_manager.py diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index e66c7d4a6fa6..08447fc28260 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1322,6 +1322,7 @@ def tk_window_focus(): 'matplotlib.tests.test_dates', 'matplotlib.tests.test_delaunay', 'matplotlib.tests.test_figure', + 'matplotlib.tests.test_font_manager', 'matplotlib.tests.test_gridspec', 'matplotlib.tests.test_image', 'matplotlib.tests.test_legend', diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py new file mode 100644 index 000000000000..f8853ce924b4 --- /dev/null +++ b/lib/matplotlib/tests/test_font_manager.py @@ -0,0 +1,18 @@ +from __future__ import (absolute_import, division, print_function, + unicode_literals) + +import six + +import os + +from matplotlib.font_manager import findfont, FontProperties +from matplotlib import rc_context + + +def test_font_priority(): + with rc_context(rc={ + 'font.sans-serif': + ['cmmi10', 'Bitstream Vera Sans']}): + font = findfont( + FontProperties(family=["sans-serif"])) + assert os.path.basename(font) == 'cmmi10.ttf' From adbeaaf6e1770fdc925217d56c792b8b48464e9c Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 29 Jan 2014 09:41:39 -0500 Subject: [PATCH 3/4] Use assert_equal --- lib/matplotlib/tests/test_font_manager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index f8853ce924b4..df2675219023 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +from nose.tools import assert_equal import six import os @@ -15,4 +16,4 @@ def test_font_priority(): ['cmmi10', 'Bitstream Vera Sans']}): font = findfont( FontProperties(family=["sans-serif"])) - assert os.path.basename(font) == 'cmmi10.ttf' + assert_equal(os.path.basename(font), 'cmmi10.ttf') From 0a761eda6ec6cbac5c5f37435b441b06b9bf7818 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 24 Feb 2014 11:29:47 -0500 Subject: [PATCH 4/4] No need to cast to floats with future division --- lib/matplotlib/font_manager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index a612cd45ddb3..9bc5ee9ee712 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -1070,12 +1070,12 @@ def score_family(self, families, family2): options = [x.lower() for x in options] if family2 in options: idx = options.index(family2) - return ((0.1 * (float(idx) / len(options))) * - (float(i + 1) / float(len(families)))) + return ((0.1 * (idx / len(options))) * + ((i + 1) / len(families))) elif family1 == family2: # The score should be weighted by where in the # list the font was found. - return float(i) / float(len(families)) + return i / len(families) return 1.0 def score_style(self, style1, style2):