20
20
found.
21
21
"""
22
22
23
- import six
24
-
25
- """
26
- KNOWN ISSUES
27
-
28
- - documentation
29
- - font variant is untested
30
- - font stretch is incomplete
31
- - font size is incomplete
32
- - default font algorithm needs improvement and testing
33
- - setWeights function needs improvement
34
- - 'light' is an invalid weight value, remove it.
35
- - update_fonts not implemented
36
-
37
- Authors : John Hunter <[email protected] >
38
-
39
- Michael Droettboom <[email protected] >
40
- Copyright : John Hunter (2004,2005), Paul Barrett (2004,2005)
41
- License : matplotlib license (PSF compatible)
42
- The font directory code is from ttfquery,
43
- see license/LICENSE_TTFQUERY.
44
- """
23
+ # KNOWN ISSUES
24
+ #
25
+ # - documentation
26
+ # - font variant is untested
27
+ # - font stretch is incomplete
28
+ # - font size is incomplete
29
+ # - default font algorithm needs improvement and testing
30
+ # - setWeights function needs improvement
31
+ # - 'light' is an invalid weight value, remove it.
32
+ # - update_fonts not implemented
45
33
46
34
from collections import Iterable
47
35
from functools import lru_cache
@@ -179,22 +167,17 @@ def win32FontDirectory():
179
167
180
168
If the key is not found, $WINDIR/Fonts will be returned.
181
169
"""
170
+ import winreg
182
171
try :
183
- from six .moves import winreg
184
- except ImportError :
185
- pass # Fall through to default
186
- else :
172
+ user = winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders )
187
173
try :
188
- user = winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders )
189
- try :
190
- try :
191
- return winreg .QueryValueEx (user , 'Fonts' )[0 ]
192
- except OSError :
193
- pass # Fall through to default
194
- finally :
195
- winreg .CloseKey (user )
174
+ return winreg .QueryValueEx (user , 'Fonts' )[0 ]
196
175
except OSError :
197
176
pass # Fall through to default
177
+ finally :
178
+ winreg .CloseKey (user )
179
+ except OSError :
180
+ pass # Fall through to default
198
181
return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
199
182
200
183
@@ -206,7 +189,8 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
206
189
'afm'.
207
190
"""
208
191
209
- from six .moves import winreg
192
+ import winreg
193
+
210
194
if directory is None :
211
195
directory = win32FontDirectory ()
212
196
@@ -224,7 +208,7 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
224
208
for j in range (winreg .QueryInfoKey (local )[1 ]):
225
209
try :
226
210
key , direc , tp = winreg .EnumValue (local , j )
227
- if not isinstance (direc , six . string_types ):
211
+ if not isinstance (direc , str ):
228
212
continue
229
213
# Work around for https://bugs.python.org/issue25778, which
230
214
# is fixed in Py>=3.6.1.
@@ -274,19 +258,12 @@ def _call_fc_list():
274
258
'This may take a moment.' ))
275
259
timer .start ()
276
260
try :
277
- out = subprocess .check_output ([str ( 'fc-list' ) , '--format=%{file}\\ n' ])
261
+ out = subprocess .check_output (['fc-list' , '--format=%{file}\\ n' ])
278
262
except (OSError , subprocess .CalledProcessError ):
279
263
return []
280
264
finally :
281
265
timer .cancel ()
282
- fnames = []
283
- for fname in out .split (b'\n ' ):
284
- try :
285
- fname = six .text_type (fname , sys .getfilesystemencoding ())
286
- except UnicodeDecodeError :
287
- continue
288
- fnames .append (fname )
289
- return fnames
266
+ return [os .fsdecode (fname ) for fname in out .split (b'\n ' )]
290
267
291
268
292
269
def get_fontconfig_fonts (fontext = 'ttf' ):
@@ -328,7 +305,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
328
305
for f in get_fontconfig_fonts (fontext ):
329
306
fontfiles .add (f )
330
307
331
- elif isinstance (fontpaths , six . string_types ):
308
+ elif isinstance (fontpaths , str ):
332
309
fontpaths = [fontpaths ]
333
310
334
311
for path in fontpaths :
@@ -478,9 +455,9 @@ def afmFontProperty(fontpath, font):
478
455
479
456
# Styles are: italic, oblique, and normal (default)
480
457
481
- if font .get_angle () != 0 or name . lower (). find ( 'italic' ) >= 0 :
458
+ if font .get_angle () != 0 or 'italic' in name . lower () :
482
459
style = 'italic'
483
- elif name . lower (). find ( 'oblique' ) >= 0 :
460
+ elif 'oblique' in name . lower () :
484
461
style = 'oblique'
485
462
else :
486
463
style = 'normal'
@@ -501,12 +478,11 @@ def afmFontProperty(fontpath, font):
501
478
# and ultra-expanded.
502
479
# Relative stretches are: wider, narrower
503
480
# Child value is: inherit
504
- if fontname .find ('narrow' ) >= 0 or fontname .find ('condensed' ) >= 0 or \
505
- fontname .find ('cond' ) >= 0 :
506
- stretch = 'condensed'
507
- elif fontname .find ('demi cond' ) >= 0 :
481
+ if 'demi cond' in fontname :
508
482
stretch = 'semi-condensed'
509
- elif fontname .find ('wide' ) >= 0 or fontname .find ('expanded' ) >= 0 :
483
+ elif 'narrow' in fontname or 'cond' in fontname :
484
+ stretch = 'condensed'
485
+ elif 'wide' in fontname or 'expanded' in fontname :
510
486
stretch = 'expanded'
511
487
else :
512
488
stretch = 'normal'
@@ -568,7 +544,7 @@ def createFontList(fontfiles, fontext='ttf'):
568
544
except UnicodeError :
569
545
_log .info ("Cannot handle unicode filenames" )
570
546
continue
571
- except IOError :
547
+ except OSError :
572
548
_log .info ("IO error - cannot open font file %s" , fpath )
573
549
continue
574
550
try :
@@ -646,7 +622,7 @@ def __init__(self,
646
622
weight = None ,
647
623
stretch = None ,
648
624
size = None ,
649
- fname = None , # if this is set, it's a hardcoded filename to use
625
+ fname = None , # if set, it's a hardcoded filename to use
650
626
_init = None # used only by copy()
651
627
):
652
628
self ._family = _normalize_font_family (rcParams ['font.family' ])
@@ -662,7 +638,7 @@ def __init__(self,
662
638
self .__dict__ .update (_init .__dict__ )
663
639
return
664
640
665
- if isinstance (family , six . string_types ):
641
+ if isinstance (family , str ):
666
642
# Treat family as a fontconfig pattern if it is the only
667
643
# parameter provided.
668
644
if (style is None and
@@ -712,23 +688,20 @@ def get_family(self):
712
688
713
689
def get_name (self ):
714
690
"""
715
- Return the name of the font that best matches the font
716
- properties.
691
+ Return the name of the font that best matches the font properties.
717
692
"""
718
693
return get_font (findfont (self )).family_name
719
694
720
695
def get_style (self ):
721
696
"""
722
- Return the font style. Values are: 'normal', 'italic' or
723
- 'oblique'.
697
+ Return the font style. Values are: 'normal', 'italic' or 'oblique'.
724
698
"""
725
699
return self ._slant
726
700
get_slant = get_style
727
701
728
702
def get_variant (self ):
729
703
"""
730
- Return the font variant. Values are: 'normal' or
731
- 'small-caps'.
704
+ Return the font variant. Values are: 'normal' or 'small-caps'.
732
705
"""
733
706
return self ._variant
734
707
@@ -793,8 +766,7 @@ def set_family(self, family):
793
766
794
767
def set_style (self , style ):
795
768
"""
796
- Set the font style. Values are: 'normal', 'italic' or
797
- 'oblique'.
769
+ Set the font style. Values are: 'normal', 'italic' or 'oblique'.
798
770
"""
799
771
if style is None :
800
772
style = rcParams ['font.style' ]
@@ -892,7 +864,7 @@ def set_fontconfig_pattern(self, pattern):
892
864
support for it to be enabled. We are merely borrowing its
893
865
pattern syntax for use here.
894
866
"""
895
- for key , val in six . iteritems ( self ._parse_fontconfig_pattern (pattern )):
867
+ for key , val in self ._parse_fontconfig_pattern (pattern ). items ( ):
896
868
if type (val ) == list :
897
869
getattr (self , "set_" + key )(val [0 ])
898
870
else :
@@ -936,9 +908,10 @@ def json_dump(data, filename):
936
908
with open (filename , 'w' ) as fh :
937
909
try :
938
910
json .dump (data , fh , cls = JSONEncoder , indent = 2 )
939
- except IOError as e :
911
+ except OSError as e :
940
912
warnings .warn ('Could not save font_manager cache ' , e )
941
913
914
+
942
915
def json_load (filename ):
943
916
"""Loads a data structure as JSON from the named file.
944
917
Handles FontManager and its fields."""
@@ -948,10 +921,8 @@ def json_load(filename):
948
921
949
922
950
923
def _normalize_font_family (family ):
951
- if isinstance (family , six .string_types ):
952
- family = [six .text_type (family )]
953
- elif isinstance (family , Iterable ):
954
- family = [six .text_type (f ) for f in family ]
924
+ if isinstance (family , str ):
925
+ family = [family ]
955
926
return family
956
927
957
928
@@ -1170,14 +1141,14 @@ def score_weight(self, weight1, weight2):
1170
1141
The result is 0.0 if both weight1 and weight 2 are given as strings
1171
1142
and have the same value.
1172
1143
1173
- Otherwise, the result is the absolute value of the difference between the
1174
- CSS numeric values of *weight1* and *weight2*, normalized
1175
- between 0.05 and 1.0.
1144
+ Otherwise, the result is the absolute value of the difference between
1145
+ the CSS numeric values of *weight1* and *weight2*, normalized between
1146
+ 0.05 and 1.0.
1176
1147
"""
1177
1148
1178
- # exact match of the weight names ( e.g. weight1 == weight2 == "regular")
1179
- if (isinstance (weight1 , six . string_types ) and
1180
- isinstance (weight2 , six . string_types ) and
1149
+ # exact match of the weight names, e.g. weight1 == weight2 == "regular"
1150
+ if (isinstance (weight1 , str ) and
1151
+ isinstance (weight2 , str ) and
1181
1152
weight1 == weight2 ):
1182
1153
return 0.0
1183
1154
try :
@@ -1364,26 +1335,22 @@ def fc_match(pattern, fontext):
1364
1335
stdout = subprocess .PIPE ,
1365
1336
stderr = subprocess .PIPE )
1366
1337
output = pipe .communicate ()[0 ]
1367
- except ( OSError , IOError ) :
1338
+ except OSError :
1368
1339
return None
1369
1340
1370
1341
# The bulk of the output from fc-list is ascii, so we keep the
1371
1342
# result in bytes and parse it as bytes, until we extract the
1372
1343
# filename, which is in sys.filesystemencoding().
1373
1344
if pipe .returncode == 0 :
1374
- for fname in output .split (b'\n ' ):
1375
- try :
1376
- fname = six .text_type (fname , sys .getfilesystemencoding ())
1377
- except UnicodeDecodeError :
1378
- continue
1345
+ for fname in map (os .fsdecode , output .split (b'\n ' )):
1379
1346
if os .path .splitext (fname )[1 ][1 :] in fontexts :
1380
1347
return fname
1381
1348
return None
1382
1349
1383
1350
_fc_match_cache = {}
1384
1351
1385
1352
def findfont (prop , fontext = 'ttf' ):
1386
- if not isinstance (prop , six . string_types ):
1353
+ if not isinstance (prop , str ):
1387
1354
prop = prop .get_fontconfig_pattern ()
1388
1355
cached = _fc_match_cache .get (prop )
1389
1356
if cached is not None :
0 commit comments