Fix font encoding issue in a better way. matplotlib/matplotlib#11277

This commit is contained in:
Antony Lee 2018-05-21 13:47:46 -04:00 committed by Michael Droettboom
parent 12b05ad373
commit 77266efdd0
1 changed files with 83 additions and 21 deletions

View File

@ -1,23 +1,85 @@
diff -ur matplotlib-2.2.2/lib/matplotlib/font_manager.py matplotlib-2.2.2/lib/matplotlib/font_manager.py
--- a/lib/matplotlib/font_manager.py 2018-03-17 14:03:23.000000000 -0400
+++ b/lib/matplotlib/font_manager.py 2018-05-16 16:56:49.399466649 -0400
@@ -416,11 +419,17 @@
sfnt2 = sfnt.get((1,0,0,2))
sfnt4 = sfnt.get((1,0,0,4))
if sfnt2:
diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py
index 4f248fde9..0fc17ea37 100644
--- a/lib/matplotlib/backends/backend_pdf.py
+++ b/lib/matplotlib/backends/backend_pdf.py
@@ -1135,15 +1135,7 @@ end"""
# Beginning of main embedTTF function...
- # You are lost in a maze of TrueType tables, all different...
- sfnt = font.get_sfnt()
- try:
- ps_name = sfnt[1, 0, 0, 6].decode('mac_roman') # Macintosh scheme
- except KeyError:
- # Microsoft scheme:
- ps_name = sfnt[3, 1, 0x0409, 6].decode('utf-16be')
- # (see freetype/ttnameid.h)
- ps_name = ps_name.encode('ascii', 'replace')
+ ps_name = font.postscript_name.encode('ascii', 'replace')
ps_name = Name(ps_name)
pclt = font.get_sfnt_table('pclt') or {'capHeight': 0, 'xHeight': 0}
post = font.get_sfnt_table('post') or {'italicAngle': (0, 0)}
diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py
index 1aeee39a2..3d44b6f7f 100644
--- a/lib/matplotlib/backends/backend_ps.py
+++ b/lib/matplotlib/backends/backend_ps.py
@@ -714,12 +714,8 @@ grestore
self.track_characters(font, s)
self.set_color(*gc.get_rgb())
- sfnt = font.get_sfnt()
- try:
- ps_name = sfnt[1, 0, 0, 6].decode('mac_roman')
- except KeyError:
- ps_name = sfnt[3, 1, 0x0409, 6].decode('utf-16be')
- ps_name = ps_name.encode('ascii', 'replace').decode('ascii')
+ ps_name = (font.postscript_name
+ .encode('ascii', 'replace').decode('ascii'))
self.set_font(ps_name, prop.get_size_in_points())
lastgind = None
diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py
index cdc82edbf..2ea6efc2d 100644
--- a/lib/matplotlib/font_manager.py
+++ b/lib/matplotlib/font_manager.py
@@ -413,16 +413,11 @@ def ttfFontProperty(font):
# Styles are: italic, oblique, and normal (default)
sfnt = font.get_sfnt()
- sfnt2 = sfnt.get((1,0,0,2))
- sfnt4 = sfnt.get((1,0,0,4))
- if sfnt2:
- sfnt2 = sfnt2.decode('mac_roman').lower()
+ try:
+ sfnt2 = sfnt2.decode('mac_roman').lower()
+ except LookupError:
+ sfnt2 = sfnt2.decode('latin1').lower()
else:
sfnt2 = ''
if sfnt4:
- else:
- sfnt2 = ''
- if sfnt4:
- sfnt4 = sfnt4.decode('mac_roman').lower()
+ try:
+ sfnt4 = sfnt4.decode('mac_roman').lower()
+ except LookupError:
+ sfnt4 = sfnt4.decode('latin1').lower()
else:
sfnt4 = ''
if sfnt4.find('oblique') >= 0:
- else:
- sfnt4 = ''
+ # 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()
if sfnt4.find('oblique') >= 0:
style = 'oblique'
elif sfnt4.find('italic') >= 0:
diff --git a/lib/matplotlib/textpath.py b/lib/matplotlib/textpath.py
index 5ee356774..89fcfb79c 100644
--- a/lib/matplotlib/textpath.py
+++ b/lib/matplotlib/textpath.py
@@ -68,13 +68,7 @@ class TextToPath(object):
"""
Return a unique id for the given font and character-code set.
"""
- sfnt = font.get_sfnt()
- try:
- ps_name = sfnt[1, 0, 0, 6].decode('mac_roman')
- except KeyError:
- ps_name = sfnt[3, 1, 0x0409, 6].decode('utf-16be')
- char_id = urllib_quote('%s-%x' % (ps_name, ccode))
- return char_id
+ return urllib_quote('%s-%x' % (font.postscript_name, ccode))
def _get_char_id_ps(self, font, ccode):
"""