From fd452032d24d2a7906e8e1321f839449ce4c5f80 Mon Sep 17 00:00:00 2001 From: Stephen-Chilcote Date: Thu, 6 Dec 2018 13:51:41 -0500 Subject: [PATCH] Correctly get weight & style hints from certain newer Microsoft fonts --- lib/matplotlib/font_manager.py | 10 +++++++--- lib/matplotlib/tests/test_font_manager.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index b3bf781422ae..cdf3d7b85dae 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -321,9 +321,13 @@ def ttfFontProperty(font): sfnt = font.get_sfnt() # These tables are actually mac_roman-encoded, but mac_roman support may be # missing in some alternative Python implementations and we are only going - # to look for ASCII substrings, where any ASCII-compatible encoding works. - sfnt2 = sfnt.get((1, 0, 0, 2), b'').decode('latin-1').lower() - sfnt4 = sfnt.get((1, 0, 0, 4), b'').decode('latin-1').lower() + # to look for ASCII substrings, where any ASCII-compatible encoding works + # - or big-endian UTF-16, since important Microsoft fonts use that. + sfnt2 = (sfnt.get((1, 0, 0, 2), b'').decode('latin-1').lower() or + sfnt.get((3, 1, 0x0409, 2), b'').decode('utf_16_be').lower()) + sfnt4 = (sfnt.get((1, 0, 0, 4), b'').decode('latin-1').lower() or + sfnt.get((3, 1, 0x0409, 4), b'').decode('utf_16_be').lower()) + if sfnt4.find('oblique') >= 0: style = 'oblique' elif sfnt4.find('italic') >= 0: diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index 3dca7e6f2ce6..7ba02954ab05 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -1,5 +1,6 @@ from pathlib import Path import shutil +import sys import warnings import numpy as np @@ -87,3 +88,20 @@ def test_hinting_factor(factor): # Check that hinting only changes text layout by a small (10%) amount. np.testing.assert_allclose(hinted_font.get_width_height(), expected, rtol=0.1) + + +@pytest.mark.skipif(sys.platform != "win32", + reason="Need Windows font to test against") +def test_utf16m_sfnt(): + segoe_ui_semibold = None + for f in fontManager.ttflist: + # seguisbi = Microsoft Segoe UI Semibold + if f.fname[-12:] == "seguisbi.ttf": + segoe_ui_semibold = f + break + else: + pytest.xfail(reason="Couldn't find font to test against.") + + # Check that we successfully read the "semibold" from the font's + # sfnt table and set its weight accordingly + assert segoe_ui_semibold.weight == "semibold" pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy