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

Skip to content

Correctly get weight & style hints from certain newer Microsoft fonts #12945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference:
https://www.freetype.org/freetype2/docs/reference/ft2-truetype_tables.html
3 = TT_PLATFORM_MICROSOFT
0x0409 = TT_MS_LANGID_ENGLISH_UNITED_STATES

utf-16-be encoding is specified at https://www.microsoft.com/typography/otspec/name.htm


if sfnt4.find('oblique') >= 0:
style = 'oblique'
elif sfnt4.find('italic') >= 0:
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
import shutil
import sys
import warnings

import numpy as np
Expand Down Expand Up @@ -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"