16
16
_log = logging .getLogger (__name__ )
17
17
18
18
19
- @functools .lru_cache (1 )
20
- def _get_adobe_standard_encoding ():
21
- enc_name = dviread .find_tex_file ('8a.enc' )
22
- enc = dviread .Encoding (enc_name )
23
- return {c : i for i , c in enumerate (enc .encoding )}
24
-
25
-
26
19
class TextToPath (object ):
27
20
"""
28
21
A class that convert a given text to a path using ttf fonts.
@@ -297,12 +290,8 @@ def get_texmanager(self):
297
290
298
291
def get_glyphs_tex (self , prop , s , glyph_map = None ,
299
292
return_new_glyphs_only = False ):
300
- """
301
- convert the string *s* to vertices and codes using matplotlib's usetex
302
- mode.
303
- """
304
-
305
- # codes are modstly borrowed from pdf backend.
293
+ """Convert the string *s* to vertices and codes using usetex mode."""
294
+ # Mostly borrowed from pdf backend.
306
295
307
296
dvifile = self .get_texmanager ().make_dvi (s , self .FONT_SCALE )
308
297
with dviread .Dvi (dvifile , self .DPI ) as dvi :
@@ -320,29 +309,20 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
320
309
321
310
# Gather font information and do some setup for combining
322
311
# characters into strings.
323
- # oldfont, seq = None, []
324
312
for x1 , y1 , dvifont , glyph , width in page .text :
325
313
font , enc = self ._get_ps_font_and_encoding (dvifont .texname )
326
314
char_id = self ._get_char_id_ps (font , glyph )
327
315
328
316
if char_id not in glyph_map :
329
317
font .clear ()
330
318
font .set_size (self .FONT_SCALE , self .DPI )
331
- if enc :
332
- charcode = enc .get (glyph , None )
333
- else :
334
- charcode = glyph
335
-
336
- ft2font_flag = LOAD_TARGET_LIGHT
337
- if charcode is not None :
338
- glyph0 = font .load_char (charcode , flags = ft2font_flag )
319
+ # See comments in _get_ps_font_and_encoding.
320
+ if enc is not None :
321
+ index = font .get_name_index (enc [glyph ])
322
+ font .load_glyph (index , flags = LOAD_TARGET_LIGHT )
339
323
else :
340
- _log .warning ("The glyph (%d) of font (%s) cannot be "
341
- "converted with the encoding. Glyph may "
342
- "be wrong." , glyph , font .fname )
343
-
344
- glyph0 = font .load_char (glyph , flags = ft2font_flag )
345
-
324
+ index = glyph
325
+ font .load_char (index , flags = LOAD_TARGET_LIGHT )
346
326
glyph_map_new [char_id ] = self .glyph_to_path (font )
347
327
348
328
glyph_ids .append (char_id )
@@ -370,31 +350,41 @@ def _get_ps_font_and_encoding(texname):
370
350
font_bunch = tex_font_map [texname ]
371
351
if font_bunch .filename is None :
372
352
raise ValueError (
373
- ( "No usable font file found for %s (%s ). "
374
- "The font may lack a Type-1 version." )
375
- % (font_bunch .psname , texname ))
353
+ "No usable font file found for {} ({} ). "
354
+ "The font may lack a Type-1 version."
355
+ . format (font_bunch .psname , texname ))
376
356
377
357
font = get_font (font_bunch .filename )
378
358
379
- for charmap_name , charmap_code in [("ADOBE_CUSTOM" , 1094992451 ),
380
- ("ADOBE_STANDARD" , 1094995778 )]:
381
- try :
382
- font .select_charmap (charmap_code )
383
- except (ValueError , RuntimeError ):
384
- pass
385
- else :
386
- break
359
+ if font_bunch .encoding :
360
+ # If psfonts.map specifies an encoding, use it: it gives us a
361
+ # mapping of glyph indices to Adobe glyph names; use it to convert
362
+ # dvi indices to glyph names and use the FreeType-synthesized
363
+ # unicode charmap to convert glyph names to glyph indices (with
364
+ # FT_Get_Name_Index/get_name_index), and load the glyph using
365
+ # FT_Load_Glyph/load_glyph. (That charmap has a coverage at least
366
+ # as good as, and possibly better than, the native charmaps.)
367
+ enc = dviread ._parse_enc (font_bunch .encoding )
387
368
else :
388
- charmap_name = ""
389
- _log .warning ("No supported encoding in font (%s)." ,
390
- font_bunch .filename )
391
-
392
- if charmap_name == "ADOBE_STANDARD" and font_bunch .encoding :
393
- enc0 = dviread .Encoding (font_bunch .encoding )
394
- enc = {i : _get_adobe_standard_encoding ().get (c , None )
395
- for i , c in enumerate (enc0 .encoding )}
396
- else :
397
- enc = {}
369
+ # If psfonts.map specifies no encoding, the indices directly map to
370
+ # the font's builtin charmap (see the pdftex manual, section 6.1
371
+ # -- Map files); so don't use the FreeType-synthesized charmap but
372
+ # the native ones (we can't directly identify it but it's typically
373
+ # an Adobe charmap), and directly load the dvi glyph indices using
374
+ # FT_Load_Char/load_char.
375
+ for charmap_name , charmap_code in [("ADOBE_CUSTOM" , 1094992451 ),
376
+ ("ADOBE_STANDARD" , 1094995778 )]:
377
+ try :
378
+ font .select_charmap (charmap_code )
379
+ except (ValueError , RuntimeError ):
380
+ pass
381
+ else :
382
+ break
383
+ else :
384
+ charmap_name = ""
385
+ _log .warning ("No supported encoding in font (%s)." ,
386
+ font_bunch .filename )
387
+ enc = None
398
388
399
389
return font , enc
400
390
0 commit comments