1
1
from collections import OrderedDict
2
+ import functools
2
3
import urllib .parse
3
4
import warnings
4
5
5
6
import numpy as np
6
7
7
- from matplotlib .path import Path
8
- from matplotlib import rcParams
9
- import matplotlib .font_manager as font_manager
10
- from matplotlib .ft2font import KERNING_DEFAULT , LOAD_NO_HINTING
11
- from matplotlib .ft2font import LOAD_TARGET_LIGHT
12
- from matplotlib .mathtext import MathTextParser
13
- import matplotlib .dviread as dviread
8
+ from matplotlib import cbook , dviread , font_manager , rcParams
14
9
from matplotlib .font_manager import FontProperties , get_font
10
+ from matplotlib .ft2font import (
11
+ KERNING_DEFAULT , LOAD_NO_HINTING , LOAD_TARGET_LIGHT )
12
+ from matplotlib .mathtext import MathTextParser
13
+ from matplotlib .path import Path
15
14
from matplotlib .transforms import Affine2D
16
15
17
16
17
+ @functools .lru_cache (1 )
18
+ def _get_adobe_standard_encoding ():
19
+ enc_name = dviread .find_tex_file ('8a.enc' )
20
+ enc = dviread .Encoding (enc_name )
21
+ return {c : i for i , c in enumerate (enc .encoding )}
22
+
23
+
18
24
class TextToPath (object ):
19
25
"""
20
26
A class that convert a given text to a path using ttf fonts.
@@ -25,19 +31,11 @@ class TextToPath(object):
25
31
26
32
def __init__ (self ):
27
33
self .mathtext_parser = MathTextParser ('path' )
28
- self .tex_font_map = None
29
-
30
- from matplotlib .cbook import maxdict
31
- self ._ps_fontd = maxdict (50 )
32
-
33
34
self ._texmanager = None
34
35
35
- self ._adobe_standard_encoding = None
36
-
37
- def _get_adobe_standard_encoding (self ):
38
- enc_name = dviread .find_tex_file ('8a.enc' )
39
- enc = dviread .Encoding (enc_name )
40
- return {c : i for i , c in enumerate (enc .encoding )}
36
+ @cbook .deprecated ("3.0" )
37
+ def tex_font_map (self ):
38
+ return dviread .PsfontsMap (dviread .find_tex_file ('pdftex.map' ))
41
39
42
40
def _get_font (self , prop ):
43
41
"""
@@ -287,13 +285,6 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
287
285
288
286
texmanager = self .get_texmanager ()
289
287
290
- if self .tex_font_map is None :
291
- self .tex_font_map = dviread .PsfontsMap (
292
- dviread .find_tex_file ('pdftex.map' ))
293
-
294
- if self ._adobe_standard_encoding is None :
295
- self ._adobe_standard_encoding = self ._get_adobe_standard_encoding ()
296
-
297
288
fontsize = prop .get_size_in_points ()
298
289
if hasattr (texmanager , "get_dvi" ):
299
290
dvifilelike = texmanager .get_dvi (s , self .FONT_SCALE )
@@ -318,46 +309,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
318
309
# characters into strings.
319
310
# oldfont, seq = None, []
320
311
for x1 , y1 , dvifont , glyph , width in page .text :
321
- font_and_encoding = self ._ps_fontd .get (dvifont .texname )
322
- font_bunch = self .tex_font_map [dvifont .texname ]
323
-
324
- if font_and_encoding is None :
325
- if font_bunch .filename is None :
326
- raise ValueError (
327
- ("No usable font file found for %s (%s). "
328
- "The font may lack a Type-1 version." )
329
- % (font_bunch .psname , dvifont .texname ))
330
-
331
- font = get_font (font_bunch .filename )
332
-
333
- for charmap_name , charmap_code in [("ADOBE_CUSTOM" ,
334
- 1094992451 ),
335
- ("ADOBE_STANDARD" ,
336
- 1094995778 )]:
337
- try :
338
- font .select_charmap (charmap_code )
339
- except (ValueError , RuntimeError ):
340
- pass
341
- else :
342
- break
343
- else :
344
- charmap_name = ""
345
- warnings .warn ("No supported encoding in font (%s)." %
346
- font_bunch .filename )
347
-
348
- if charmap_name == "ADOBE_STANDARD" and font_bunch .encoding :
349
- enc0 = dviread .Encoding (font_bunch .encoding )
350
- enc = {i : self ._adobe_standard_encoding .get (c , None )
351
- for i , c in enumerate (enc0 .encoding )}
352
- else :
353
- enc = {}
354
- self ._ps_fontd [dvifont .texname ] = font , enc
355
-
356
- else :
357
- font , enc = font_and_encoding
358
-
359
- ft2font_flag = LOAD_TARGET_LIGHT
360
-
312
+ font , enc = self ._get_ps_font_and_encoding (dvifont .texname )
361
313
char_id = self ._get_char_id_ps (font , glyph )
362
314
363
315
if char_id not in glyph_map :
@@ -368,12 +320,13 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
368
320
else :
369
321
charcode = glyph
370
322
323
+ ft2font_flag = LOAD_TARGET_LIGHT
371
324
if charcode is not None :
372
325
glyph0 = font .load_char (charcode , flags = ft2font_flag )
373
326
else :
374
327
warnings .warn ("The glyph (%d) of font (%s) cannot be "
375
328
"converted with the encoding. Glyph may "
376
- "be wrong" % (glyph , font_bunch . filename ))
329
+ "be wrong" % (glyph , font . fname ))
377
330
378
331
glyph0 = font .load_char (glyph , flags = ft2font_flag )
379
332
@@ -397,6 +350,41 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
397
350
return (list (zip (glyph_ids , xpositions , ypositions , sizes )),
398
351
glyph_map_new , myrects )
399
352
353
+ @staticmethod
354
+ @functools .lru_cache (50 )
355
+ def _get_ps_font_and_encoding (texname ):
356
+ tex_font_map = dviread .PsfontsMap (dviread .find_tex_file ('pdftex.map' ))
357
+ font_bunch = tex_font_map [texname ]
358
+ if font_bunch .filename is None :
359
+ raise ValueError (
360
+ ("No usable font file found for %s (%s). "
361
+ "The font may lack a Type-1 version." )
362
+ % (font_bunch .psname , texname ))
363
+
364
+ font = get_font (font_bunch .filename )
365
+
366
+ for charmap_name , charmap_code in [("ADOBE_CUSTOM" , 1094992451 ),
367
+ ("ADOBE_STANDARD" , 1094995778 )]:
368
+ try :
369
+ font .select_charmap (charmap_code )
370
+ except (ValueError , RuntimeError ):
371
+ pass
372
+ else :
373
+ break
374
+ else :
375
+ charmap_name = ""
376
+ warnings .warn ("No supported encoding in font (%s)." %
377
+ font_bunch .filename )
378
+
379
+ if charmap_name == "ADOBE_STANDARD" and font_bunch .encoding :
380
+ enc0 = dviread .Encoding (font_bunch .encoding )
381
+ enc = {i : _get_adobe_standard_encoding ().get (c , None )
382
+ for i , c in enumerate (enc0 .encoding )}
383
+ else :
384
+ enc = {}
385
+
386
+ return font , enc
387
+
400
388
401
389
text_to_path = TextToPath ()
402
390
0 commit comments