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

Skip to content

Commit efd1c4b

Browse files
committed
textpath tries the adobe standard encoding as a default for fonts
1 parent f8da96a commit efd1c4b

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

lib/matplotlib/textpath.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import numpy as np
1414

15+
import warnings
16+
1517
class TextToPath(object):
1618
"""
1719
A class that convert a given text to a path using ttf fonts.
@@ -32,6 +34,14 @@ def __init__(self):
3234

3335
self._texmanager = None
3436

37+
self._adobe_standard_encoding = self._get_adobe_standard_encoding()
38+
39+
40+
def _get_adobe_standard_encoding(self):
41+
enc_name = dviread.find_tex_file('8a.enc')
42+
enc = dviread.Encoding(enc_name)
43+
return dict([(c, i) for i, c in enumerate(enc.encoding)])
44+
3545
def _get_font(self, prop):
3646
"""
3747
find a ttf font.
@@ -312,14 +322,25 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
312322
if font_and_encoding is None:
313323
font_bunch = self.tex_font_map[dvifont.texname]
314324
font = FT2Font(str(font_bunch.filename))
315-
try:
316-
font.select_charmap(1094992451) # select ADOBE_CUSTOM
317-
except ValueError:
318-
font.set_charmap(0)
319-
if font_bunch.encoding:
320-
enc = dviread.Encoding(font_bunch.encoding)
325+
326+
for charmap_name, charmap_code in [("ADOBE_CUSTOM", 1094992451),
327+
("ADOBE_STANDARD", 1094995778)]:
328+
try:
329+
font.select_charmap(charmap_code)
330+
except ValueError:
331+
pass
332+
else:
333+
break
321334
else:
322-
enc = None
335+
charmap_name = ""
336+
warnings.warn("No supported encoding in font (%s)." % font_bunch.filename)
337+
338+
if charmap_name == "ADOBE_STANDARD" and font_bunch.encoding:
339+
enc0 = dviread.Encoding(font_bunch.encoding)
340+
enc = dict([(i, self._adobe_standard_encoding.get(c, None)) \
341+
for i, c in enumerate(enc0.encoding)])
342+
else:
343+
enc = dict()
323344
self._ps_fontd[dvifont.texname] = font, enc
324345

325346
else:
@@ -332,11 +353,19 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
332353
if not char_id in glyph_map:
333354
font.clear()
334355
font.set_size(self.FONT_SCALE, self.DPI)
356+
if enc: charcode = enc.get(glyph, None)
357+
else: charcode = glyph
335358

336-
glyph0 = font.load_char(glyph, flags=ft2font_flag)
359+
if charcode:
360+
glyph0 = font.load_char(charcode, flags=ft2font_flag)
361+
else:
362+
warnings.warn("The glyph (%d) of font (%s) cannot be converted with the encoding. Glyph may be wrong" % (glyph, font_bunch.filename))
363+
364+
glyph0 = font.load_char(glyph, flags=ft2font_flag)
337365

338366
glyph_map_new[char_id] = self.glyph_to_path(glyph0)
339367

368+
340369
glyph_ids.append(char_id)
341370
xpositions.append(x1)
342371
ypositions.append(y1)

0 commit comments

Comments
 (0)