12
12
13
13
import numpy as np
14
14
15
+ import warnings
16
+
15
17
class TextToPath (object ):
16
18
"""
17
19
A class that convert a given text to a path using ttf fonts.
@@ -32,6 +34,14 @@ def __init__(self):
32
34
33
35
self ._texmanager = None
34
36
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
+
35
45
def _get_font (self , prop ):
36
46
"""
37
47
find a ttf font.
@@ -308,14 +318,25 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
308
318
if font_and_encoding is None :
309
319
font_bunch = self .tex_font_map [dvifont .texname ]
310
320
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
317
330
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 ()
319
340
self ._ps_fontd [dvifont .texname ] = font , enc
320
341
321
342
else :
@@ -328,11 +349,19 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
328
349
if not char_id in glyph_map :
329
350
font .clear ()
330
351
font .set_size (self .FONT_SCALE , self .DPI )
352
+ if enc : charcode = enc .get (glyph , None )
353
+ else : charcode = glyph
331
354
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 )
333
361
334
362
glyph_map_new [char_id ] = self .glyph_to_path (font )
335
363
364
+
336
365
glyph_ids .append (char_id )
337
366
xpositions .append (x1 )
338
367
ypositions .append (y1 )
0 commit comments