8000 Merge pull request #112 from leejjoon/fix_textpath · matplotlib/matplotlib@11d2c52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11d2c52

Browse files
committed
Merge pull request #112 from leejjoon/fix_textpath
textpath tries the adobe standard encoding as a default for fonts
2 parents 2ca68c1 + efd1c4b commit 11d2c52

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

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

8000
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.
@@ -308,14 +318,25 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
308318
if font_and_encoding is None:
309319
font_bunch = self.tex_font_map[dvifont.texname]
310320
font = FT2Font(str(font_bunch.filename))
311-
try:
312-
font.select_charmap(1094992451) # select ADOBE_CUSTOM
313-
except ValueError:
314-
font.set_charmap(0)
315-
if font_bunch.encoding:
316-
enc = dviread.Encoding(font_bunch.encoding)
321+
322+
for charmap_name, charmap_code in [("ADOBE_CUSTOM", 1094992451),
323+
("ADOBE_STANDARD", 1094995778)]:
324+
try:
325+
font.select_charmap(charmap_code)
326+
except ValueError:
327+
pass
328+
else:
329+
break
317330
else:
318-
enc = None
331+
charmap_name = ""
332+
warnings.warn("No supported encoding in font (%s)." % font_bunch.filename)
333+
334+
if charmap_name == "ADOBE_STANDARD" and font_bunch.encoding:
335+
enc0 = dviread.Encoding(font_bunch.encoding)
336+
enc = dict([(i, self._adobe_standard_encoding.get(c, None)) \
337+
for i, c in enumerate(enc0.encoding)])
338+
else:
339+
enc = dict()
319340
self._ps_fontd[dvifont.texname] = font, enc
320341

321342
else:
@@ -328,11 +349,19 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
328349
if not char_id in glyph_map:
329350
font.clear()
330351
font.set_size(self.FONT_SCALE, self.DPI)
352+
if enc: charcode = enc.get(glyph, None)
353+
else: charcode = glyph
331354

332-
glyph0 = font.load_char(glyph, flags=ft2font_flag)
355+
if charcode:
356+
glyph0 = font.load_char(charcode, flags=ft2font_flag)
357+
else:
358+
warnings.warn("The glyph (%d) of font (%s) cannot be converted with the encoding. Glyph may be wrong" % (glyph, font_bunch.filename))
359+
360+
glyph0 = font.load_char(glyph, flags=ft2font_flag)
333361

334362
glyph_map_new[char_id] = self.glyph_to_path(font)
335363

364+
336365
glyph_ids.append(char_id)
337366
xpositions.append(x1)
338367
ypositions.append(y1)

0 commit comments

Comments
 (0)
0