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"