@@ -301,7 +301,7 @@ def get_fontconfig_fonts(fontext='ttf'):
301
301
except OSError :
302
302
# Calling fc-list did not work, so we'll just return nothing
303
303
return fontfiles
304
-
304
+
305
305
if pipe .returncode == 0 :
306
306
for line in output .split ('\n ' ):
307
307
fname = line .split (':' )[0 ]
@@ -463,8 +463,7 @@ def ttfFontProperty(font):
463
463
# Relative stretches are: wider, narrower
464
464
# Child value is: inherit
465
465
466
- # !!!! Incomplete
467
- if sfnt4 .find ('narrow' ) >= 0 or sfnt4 .find ('condensed' ) >= 0 or \
466
+ if sfnt4 .find ('narrow' ) >= 0 or sfnt4 .find ('condensed' ) >= 0 or \
468
467
sfnt4 .find ('cond' ) >= 0 :
469
468
stretch = 'condensed'
470
469
elif sfnt4 .find ('demi cond' ) >= 0 :
@@ -502,6 +501,7 @@ def afmFontProperty(fontpath, font):
502
501
"""
503
502
504
503
name = font .get_familyname ()
504
+ fontname = font .get_fontname ().lower ()
505
505
506
506
# Styles are: italic, oblique, and normal (default)
507
507
@@ -532,9 +532,15 @@ def afmFontProperty(fontpath, font):
532
532
# and ultra-expanded.
533
533
# Relative stretches are: wider, narrower
534
534
# Child value is: inherit
535
-
536
- # !!!! Incomplete
537
- stretch = 'normal'
535
+ if fontname .find ('narrow' ) >= 0 or fontname .find ('condensed' ) >= 0 or \
536
+ fontname .find ('cond' ) >= 0 :
537
+ stretch = 'condensed'
538
+ elif fontname .find ('demi cond' ) >= 0 :
539
+ stretch = 'semi-condensed'
540
+ elif fontname .find ('wide' ) >= 0 or fontname .find ('expanded' ) >= 0 :
541
+ stretch = 'expanded'
542
+ else :
543
+ stretch = 'normal'
538
544
539
545
# Sizes can be absolute and relative.
540
546
# Absolute sizes are: xx-small, x-small, small, medium, large, x-large,
@@ -960,12 +966,20 @@ class FontManager:
960
966
matches the specification. If no good enough match is found, a
961
967
default font is returned.
962
968
"""
969
+ # Increment this version number whenever the font cache data
970
+ # format or behavior has changed and requires a existing font
971
+ # cache files to be rebuilt.
972
+ __version__ = 5
973
+
963
974
def __init__ (self , size = None , weight = 'normal' ):
975
+ self ._version = self .__version__
976
+
964
977
self .__default_weight = weight
965
978
self .default_size = size
966
979
967
980
paths = [os .path .join (rcParams ['datapath' ], 'fonts' , 'ttf' ),
968
- os .path .join (rcParams ['datapath' ], 'fonts' , 'afm' )]
981
+ os .path .join (rcParams ['datapath' ], 'fonts' , 'afm' ),
982
+ os .path .join (rcParams ['datapath' ], 'fonts' , 'pdfcorefonts' )]
969
983
970
984
# Create list of font paths
971
985
for pathname in ['TTFPATH' , 'AFMPATH' ]:
@@ -982,32 +996,23 @@ def __init__(self, size=None, weight='normal'):
982
996
# Load TrueType fonts and create font dictionary.
983
997
984
998
self .ttffiles = findSystemFonts (paths ) + findSystemFonts ()
999
+ self .defaultFont = {}
985
1000
986
1001
for fname in self .ttffiles :
987
1002
verbose .report ('trying fontname %s' % fname , 'debug' )
988
1003
if fname .lower ().find ('vera.ttf' )>= 0 :
989
- self .defaultFont = fname
1004
+ self .defaultFont [ 'ttf' ] = fname
990
1005
break
991
1006
else :
992
1007
# use anything
993
- self .defaultFont = self .ttffiles [0 ]
1008
+ self .defaultFont [ 'ttf' ] = self .ttffiles [0 ]
994
1009
995
1010
self .ttflist = createFontList (self .ttffiles )
996
1011
997
- if rcParams ['pdf.use14corefonts' ]:
998
- # Load only the 14 PDF core fonts. These fonts do not need to be
999
- # embedded; every PDF viewing application is required to have them:
1000
- # Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique,
1001
- # Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique,
1002
- # Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol,
1003
- # ZapfDingbats.
1004
- afmpath = os .path .join (rcParams ['datapath' ],'fonts' ,'pdfcorefonts' )
1005
- afmfiles = findSystemFonts (afmpath , fontext = 'afm' )
1006
- self .afmlist = createFontList (afmfiles , fontext = 'afm' )
1007
- else :
1008
- self .afmfiles = findSystemFonts (paths , fontext = 'afm' ) + \
1009
- findSystemFonts (fontext = 'afm' )
1010
- self .afmlist = createFontList (self .afmfiles , fontext = 'afm' )
1012
+ self .afmfiles = findSystemFonts (paths , fontext = 'afm' ) + \
1013
+ findSystemFonts (fontext = 'afm' )
1014
+ self .afmlist = createFontList (self .afmfiles , fontext = 'afm' )
1015
+ self .defaultFont ['afm' ] = None
1011
1016
1012
1017
self .ttf_lookup_cache = {}
1013
1018
self .afm_lookup_cache = {}
@@ -1151,7 +1156,7 @@ def score_size(self, size1, size2):
1151
1156
return 1.0
1152
1157
return abs (sizeval1 - sizeval2 ) / 72.0
1153
1158
1154
- def findfont (self , prop , fontext = 'ttf' ):
1159
+ def findfont (self , prop , fontext = 'ttf' , directory = None ):
1155
1160
"""
1156
1161
Search the font list for the font that most closely matches
1157
1162
the :class:`FontProperties` *prop*.
@@ -1162,6 +1167,9 @@ def findfont(self, prop, fontext='ttf'):
1162
1167
returned. If no matches below a certain threshold are found,
1163
1168
the default font (usually Vera Sans) is returned.
1164
1169
1170
+ `directory`, is specified, will only return fonts from the
1171
+ given directory (or subdirectory of that directory).
1172
+
1165
1173
The result is cached, so subsequent lookups don't have to
1166
1174
perform the O(n) nearest neighbor search.
1167
1175
@@ -1194,6 +1202,10 @@ def findfont(self, prop, fontext='ttf'):
1194
1202
best_font = None
1195
1203
1196
1204
for font in fontlist :
1205
+ if (directory is not None and
1206
+ os .path .commonprefix ([font .fname , directory ]) != directory ):
1207
+ print directory , font .fname , os .path .commonprefix ([font .fname , directory ])
1208
+ continue
1197
1209
# Matching family should have highest priority, so it is multiplied
1198
1210
# by 10.0
1199
1211
score = \
@@ -1211,8 +1223,8 @@ def findfont(self, prop, fontext='ttf'):
1211
1223
1212
1224
if best_font is None or best_score >= 10.0 :
1213
1225
verbose .report ('findfont: Could not match %s. Returning %s' %
1214
- (prop , self .defaultFont ))
1215
- result = self .defaultFont
1226
+ (prop , self .defaultFont [ fontext ] ))
1227
+ result = self .defaultFont [ fontext ]
1216
1228
else :
1217
1229
verbose .report ('findfont: Matching %s to %s (%s) with score of %f' %
1218
1230
(prop , best_font .name , best_font .fname , best_score ))
@@ -1289,16 +1301,16 @@ def _rebuild():
1289
1301
1290
1302
try :
1291
1303
fontManager = pickle_load (_fmcache )
1292
- fontManager .default_size = None
1293
- verbose .report ("Using fontManager instance from %s" % _fmcache )
1304
+ if (not hasattr (fontManager , '_version' ) or
1305
+ fontManager ._version != FontManager .__version__ ):
1306
+ _rebuild ()
1307
+ else :
1308
+ fontManager .default_size = None
1309
+ verbose .report ("Using fontManager instance from %s" % _fmcache )
1294
1310
except :
1295
1311
_rebuild ()
1296
1312
1297
1313
def findfont (prop , ** kw ):
1298
1314
global fontManager
1299
1315
font = fontManager .findfont (prop , ** kw )
1300
- if not os .path .exists (font ):
1301
- verbose .report ("%s returned by pickled fontManager does not exist" % font )
1302
- _rebuild ()
1303
- font = fontManager .findfont (prop , ** kw )
1304
1316
return font
0 commit comments