32
32
import fontTools .agl
33
33
import numpy as np
34
34
35
+ import matplotlib as mpl
35
36
from matplotlib import _api , cbook , font_manager
36
37
from matplotlib .ft2font import LoadFlags
37
38
@@ -127,7 +128,13 @@ def glyph_name_or_index(self):
127
128
def _as_unicode_or_name (self ):
128
129
if self .font .subfont :
129
130
raise NotImplementedError ("Indexing TTC fonts is not supported yet" )
130
- face = font_manager .get_font (self .font .resolve_path ())
131
+ path = self .font .resolve_path ()
132
+ if path .name .lower ().endswith ("pk" ):
133
+ # PK fonts have no encoding information; report glyphs as ASCII but
134
+ # with a "?" to indicate that this is just a guess.
135
+ return (f"{ chr (self .glyph )} ?" if chr (self .glyph ).isprintable () else
136
+ f"pk{ self .glyph :#02x} " )
137
+ face = font_manager .get_font (path )
131
138
glyph_name = face .get_glyph_name (self .index )
132
139
glyph_str = fontTools .agl .toUnicode (glyph_name )
133
140
return glyph_str or glyph_name
@@ -758,13 +765,24 @@ def _height_depth_of(self, char):
758
765
759
766
def resolve_path (self ):
760
767
if self ._path is None :
761
- psfont = PsfontsMap (find_tex_file ("pdftex.map" ))[self .texname ]
762
- if psfont .filename is None :
763
- raise ValueError ("No usable font file found for {} ({}); "
764
- "the font may lack a Type-1 version"
765
- .format (psfont .psname .decode ("ascii" ),
766
- psfont .texname .decode ("ascii" )))
767
- self ._path = Path (psfont .filename )
768
+ fontmap = PsfontsMap (find_tex_file ("pdftex.map" ))
769
+ try :
770
+ psfont = fontmap [self .texname ]
771
+ except LookupError as exc :
772
+ try :
773
+ find_tex_file (f"{ self .texname .decode ('ascii' )} .mf" )
774
+ except FileNotFoundError :
775
+ raise exc from None
776
+ else :
777
+ self ._path = Path (find_tex_file (
778
+ f"{ self .texname .decode ('ascii' )} .600pk" ))
779
+ else :
780
+ if psfont .filename is None :
781
+ raise ValueError ("No usable font file found for {} ({}); "
782
+ "the font may lack a Type-1 version"
783
+ .format (psfont .psname .decode ("ascii" ),
784
+ psfont .texname .decode ("ascii" )))
785
+ self ._path = Path (psfont .filename )
768
786
return self ._path
769
787
770
788
@cached_property
@@ -773,6 +791,8 @@ def subfont(self):
773
791
774
792
@cached_property
775
793
def effects (self ):
794
+ if self .resolve_path ().match ("*.600pk" ):
795
+ return {}
776
796
return PsfontsMap (find_tex_file ("pdftex.map" ))[self .texname ].effects
777
797
778
798
def _index_dvi_to_freetype (self , idx ):
@@ -1233,9 +1253,12 @@ def __new__(cls):
1233
1253
1234
1254
def _new_proc (self ):
1235
1255
return subprocess .Popen (
1236
- ["luatex" , "--luaonly" ,
1237
- str (cbook ._get_data_path ("kpsewhich.lua" ))],
1238
- stdin = subprocess .PIPE , stdout = subprocess .PIPE )
1256
+ ["luatex" , "--luaonly" , str (cbook ._get_data_path ("kpsewhich.lua" ))],
1257
+ # mktexpk logs to stderr; suppress that.
1258
+ stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .DEVNULL ,
1259
+ # Store generated pk fonts in our own cache.
1260
+ env = {"MT_VARTEXFONTS" : str (Path (mpl .get_cachedir (), "vartexfonts" )),
1261
+ ** os .environ })
1239
1262
1240
1263
def search (self , filename ):
1241
1264
if self ._proc .poll () is not None : # Dead, restart it.
@@ -1287,13 +1310,16 @@ def find_tex_file(filename):
1287
1310
kwargs = {'env' : {** os .environ , 'command_line_encoding' : 'utf-8' },
1288
1311
'encoding' : 'utf-8' }
1289
1312
else : # On POSIX, run through the equivalent of os.fsdecode().
1290
- kwargs = {'encoding' : sys .getfilesystemencoding (),
1313
+ kwargs = {'env' : {** os .environ },
1314
+ 'encoding' : sys .getfilesystemencoding (),
1291
1315
'errors' : 'surrogateescape' }
1316
+ kwargs ['env' ].update (
1317
+ MT_VARTEXFONTS = str (Path (mpl .get_cachedir (), "vartexfonts" )))
1292
1318
1293
1319
try :
1294
- path = ( cbook ._check_and_log_subprocess ([ 'kpsewhich' , filename ],
1295
- _log , ** kwargs )
1296
- .rstrip ('\n ' ) )
1320
+ path = cbook ._check_and_log_subprocess (
1321
+ [ 'kpsewhich' , '-mktex=pk' , filename ], _log , ** kwargs ,
1322
+ ) .rstrip ('\n ' )
1297
1323
except (FileNotFoundError , RuntimeError ):
1298
1324
path = None
1299
1325
@@ -1327,7 +1353,6 @@ def _print_fields(*args):
1327
1353
print (" " .join (map ("{:>11}" .format , args )))
1328
1354
1329
1355
with Dvi (args .filename , args .dpi ) as dvi :
1330
- fontmap = PsfontsMap (find_tex_file ('pdftex.map' ))
1331
1356
for page in dvi :
1332
1357
print (f"=== NEW PAGE === "
1333
1358
f"(w: { page .width } , h: { page .height } , d: { page .descent } )" )
0 commit comments