Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd45203

Browse files
Correctly get weight & style hints from certain newer Microsoft fonts
1 parent 345607a commit fd45203

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/matplotlib/font_manager.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,13 @@ def ttfFontProperty(font):
321321
sfnt = font.get_sfnt()
322322
# These tables are actually mac_roman-encoded, but mac_roman support may be
323323
# missing in some alternative Python implementations and we are only going
324-
# to look for ASCII substrings, where any ASCII-compatible encoding works.
325-
sfnt2 = sfnt.get((1, 0, 0, 2), b'').decode('latin-1').lower()
326-
sfnt4 = sfnt.get((1, 0, 0, 4), b'').decode('latin-1').lower()
324+
# to look for ASCII substrings, where any ASCII-compatible encoding works
325+
# - or big-endian UTF-16, since important Microsoft fonts use that.
326+
sfnt2 = (sfnt.get((1, 0, 0, 2), b'').decode('latin-1').lower() or
327+
sfnt.get((3, 1, 0x0409, 2), b'').decode('utf_16_be').lower())
328+
sfnt4 = (sfnt.get((1, 0, 0, 4), b'').decode('latin-1').lower() or
329+
sfnt.get((3, 1, 0x0409, 4), b'').decode('utf_16_be').lower())
330+
327331
if sfnt4.find('oblique') >= 0:
328332
style = 'oblique'
329333
elif sfnt4.find('italic') >= 0:

lib/matplotlib/tests/test_font_manager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22
import shutil
3+
import sys
34
import warnings
45

56
import numpy as np
@@ -87,3 +88,20 @@ def test_hinting_factor(factor):
8788
# Check that hinting only changes text layout by a small (10%) amount.
8889
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
8990
rtol=0.1)
91+
92+
93+
@pytest.mark.skipif(sys.platform != "win32",
94+
reason="Need Windows font to test against")
95+
def test_utf16m_sfnt():
96+
segoe_ui_semibold = None
97+
for f in fontManager.ttflist:
98+
# seguisbi = Microsoft Segoe UI Semibold
99+
if f.fname[-12:] == "seguisbi.ttf":
100+
segoe_ui_semibold = f
101+
break
102+
else:
103+
pytest.xfail(reason="Couldn't find font to test against.")
104+
105+
# Check that we successfully read the "semibold" from the font's
106+
# sfnt table and set its weight accordingly
107+
assert segoe_ui_semibold.weight == "semibold"

0 commit comments

Comments
 (0)