-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole thing should be rewritten (I have some plans for that) but in the meantime I think the PR's still a plus.
lib/matplotlib/font_manager.py
Outdated
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 \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use parentheses instead of backslashes for line continuations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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()) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach seems sensible to me. Can you add a test that at least on AppVeyor tests with a font that only has an MS table?
Well, I'm trying to, but to be honest I've spent about six or seven hours total at this point trying to get the tests to run on my machine so I can test my test... The other problem is that I'm not sure where to legally get a font to test against other than having Windows installed, which I assume your test runners aren't running. |
https://matplotlib.org/devel/testing.html does not work for you? If there are specific problems feel free to ask. |
Well, I deleted everything and started over, following the directions here. So now I'm getting |
For testing, you should install in developer mode (https://matplotlib.org/devel/contributing.html#installing-matplotlib-in-developer-mode); replace the |
It runs now, but every test errors out with:
|
You need a recent enough pytest (pip install --upgrade pytest should get you the last version). |
The test as it stands will only work on a Windows system; if I get time tomorrow I'll try to look for (or create?) a favorably-licensed font that can be used for the test. |
A windows-only test is fine (if you can find a free font it's even better but only if it's available online, we probably won't want to include it in the repository just for testing this feature). Looks like you need to rebase, though. |
4773ec8
to
5969311
Compare
5969311
to
72d9a86
Compare
Alright, uh, I think I've done it. May have made a few mistakes along the way but it's there now. |
e4d2c48
to
fd45203
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pending CI.
* upstream/master: (1723 commits) Correctly get weight & style hints from certain newer Microsoft fonts (matplotlib#12945) Remove some checks for Py<3.6 in the test suite. (matplotlib#12974) Fail-fast when trying to run tests with too-old pytest. Include scatter plots in Qt figure options editor. (matplotlib#12779) ENH: replace deprecated numpy header Minor simplifications. tickminorvisible-fix (matplotlib#12938) Remove animated=True from animation docs Update the documentation of Cursor Misc. cleanups. Add test for 3d conversion of empty PolyCollection Support ~ as nonbreaking space in mathtext. Deprecate public use of Formatter.pprint_val. MAINT: Unify calculation of normal vectors from polygons (matplotlib#12136) Fix the title of testing_api More table documentation Simplify bachelors degree example using new features. Avoid pyplot in showcase examples. Simplify argument checking in Table.__getitem__. (matplotlib#12932) Minor updates following bump to Py3.6+. ... # Conflicts: # lib/matplotlib/widgets.py
PR Summary
We've been having some internationalization issues caused by MPL not loading our international font, Microsoft YaHei. Part of the problem is that YaHei got turned in to a .TTC by a Windows update, which we rectified by overlaying this PR. But we still weren't getting what we wanted because matplotlib was displaying using YaHei Light instead of regular YaHei, and it looked really bad.
We tracked the issue down to the fact that the font manager wasn't identifying the light and regular versions as having different weights, making them appear identical and leading to plots using whichever one was higher up in the list.
We rectified this issue by giving
font_manager.ttfFontProperty
the ability to read Microsoft's SFNT tables. This allows matplotlib to correctly interpret the properties of a bunch of Windows default fonts including the one we were specifically interested in.We just did this:
and it started working (the utf_16_be lines are the new ones.)
I haven't written a test for this yet since, honestly, we're just wondering if there's some obvious reason that this is a bad idea and hasn't been done already. If the community think it's a worthwhile addition I can finish it up as a proper PR though.
PR Checklist