122
122
MSFolders = \
123
123
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
124
124
125
- MSFontDirectories = [
125
+
126
+ MSFontDirectories = [
126
127
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' ,
127
128
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts' ]
128
129
129
- X11FontDirectories = [
130
+
131
+ X11FontDirectories = [
130
132
# an old standard installation point
131
133
"/usr/X11R6/lib/X11/fonts/TTF/" ,
132
134
"/usr/X11/lib/X11/fonts" ,
156
158
path = os .path .join (home , '.fonts' )
157
159
X11FontDirectories .append (path )
158
160
161
+
159
162
def get_fontext_synonyms (fontext ):
160
163
"""
161
164
Return a list of file extensions extensions that are synonyms for
@@ -165,6 +168,7 @@ def get_fontext_synonyms(fontext):
165
168
'otf' : ('ttf' , 'otf' ),
166
169
'afm' : ('afm' ,)}[fontext ]
167
170
171
+
168
172
def list_fonts (directory , extensions ):
169
173
"""
170
174
Return a list of all fonts matching any of the extensions,
@@ -174,6 +178,7 @@ def list_fonts(directory, extensions):
174
178
for ext in extensions ])
175
179
return cbook .listFiles (directory , pattern )
176
180
181
+
177
182
def win32FontDirectory ():
178
183
"""
179
184
Return the user-specified font directory for Win32. This is
@@ -186,21 +191,22 @@ def win32FontDirectory():
186
191
try :
187
192
from six .moves import winreg
188
193
except ImportError :
189
- pass # Fall through to default
194
+ pass # Fall through to default
190
195
else :
191
196
try :
192
197
user = winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders )
193
198
try :
194
199
try :
195
200
return winreg .QueryValueEx (user , 'Fonts' )[0 ]
196
201
except OSError :
197
- pass # Fall through to default
202
+ pass # Fall through to default
198
203
finally :
199
204
winreg .CloseKey (user )
200
205
except OSError :
201
- pass # Fall through to default
206
+ pass # Fall through to default
202
207
return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
203
208
209
+
204
210
def win32InstalledFonts (directory = None , fontext = 'ttf' ):
205
211
"""
206
212
Search for fonts in the specified font directory, or use the
@@ -246,6 +252,7 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
246
252
winreg .CloseKey (local )
247
253
return None
248
254
255
+
249
256
def OSXInstalledFonts (directories = None , fontext = 'ttf' ):
250
257
"""
251
258
Get list of font files on OS X - ignores font suffix by default.
@@ -297,6 +304,7 @@ def get_fontconfig_fonts(fontext='ttf'):
297
304
if os .path .splitext (fname )[1 ][1 :] in fontext ]
298
305
299
306
307
+
300
308
def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
301
309
"""
302
310
Search for fonts in the specified font paths. If no paths are
@@ -338,6 +346,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
338
346
339
347
return [fname for fname in fontfiles if os .path .exists (fname )]
340
348
349
+
341
350
def weight_as_number (weight ):
342
351
"""
343
352
Return the weight property as a numeric value. String values
@@ -419,7 +428,6 @@ def ttfFontProperty(font):
419
428
else :
420
429
style = 'normal'
421
430
422
-
423
431
# Variants are: small-caps and normal (default)
424
432
425
433
# !!!! Untested
@@ -451,8 +459,8 @@ def ttfFontProperty(font):
451
459
# Relative stretches are: wider, narrower
452
460
# Child value is: inherit
453
461
454
- if sfnt4 .find ('narrow' ) >= 0 or sfnt4 .find ('condensed' ) >= 0 or \
455
- sfnt4 .find ('cond' ) >= 0 :
462
+ if ( sfnt4 .find ('narrow' ) >= 0 or sfnt4 .find ('condensed' ) >= 0 or
463
+ sfnt4 .find ('cond' ) >= 0 ) :
456
464
stretch = 'condensed'
457
465
elif sfnt4 .find ('demi cond' ) >= 0 :
458
466
stretch = 'semi-condensed'
@@ -673,12 +681,12 @@ def __init__(self,
673
681
fname = None , # if this is set, it's a hardcoded filename to use
674
682
_init = None # used only by copy()
675
683
):
676
- self ._family = None
677
- self ._slant = None
678
- self ._variant = None
679
- self ._weight = None
680
- self ._stretch = None
681
- self ._size = None
684
+ self ._family = _normalize_font_family ( rcParams [ 'font.family' ])
685
+ self ._slant = rcParams [ 'font.style' ]
686
+ self ._variant = rcParams [ 'font.variant' ]
687
+ self ._weight = rcParams [ 'font.weight' ]
688
+ self ._stretch = rcParams [ 'font.stretch' ]
689
+ self ._size = rcParams [ 'font.size' ]
682
690
self ._file = None
683
691
684
692
# This is used only by copy()
@@ -732,11 +740,6 @@ def get_family(self):
732
740
"""
733
741
Return a list of font names that comprise the font family.
734
742
"""
735
- if self ._family is None :
736
- family = rcParams ['font.family' ]
737
- if is_string_like (family ):
738
- return [family ]
739
- return family
740
743
return self ._family
741
744
742
745
def get_name (self ):
@@ -751,8 +754,6 @@ def get_style(self):
751
754
Return the font style. Values are: 'normal', 'italic' or
752
755
'oblique'.
753
756
"""
754
- if self ._slant is None :
755
- return rcParams ['font.style' ]
756
757
return self ._slant
757
758
get_slant = get_style
758
759
@@ -761,8 +762,6 @@ def get_variant(self):
761
762
Return the font variant. Values are: 'normal' or
762
763
'small-caps'.
763
764
"""
764
- if self ._variant is None :
765
- return rcParams ['font.variant' ]
766
765
return self ._variant
767
766
768
767
def get_weight (self ):
@@ -772,8 +771,6 @@ def get_weight(self):
772
771
'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold',
773
772
'heavy', 'extra bold', 'black'
774
773
"""
775
- if self ._weight is None :
776
- return rcParams ['font.weight' ]
777
774
return self ._weight
778
775
779
776
def get_stretch (self ):
@@ -782,26 +779,16 @@ def get_stretch(self):
782
779
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
783
780
'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
784
781
"""
785
- if self ._stretch is None :
786
- return rcParams ['font.stretch' ]
787
782
return self ._stretch
788
783
789
784
def get_size (self ):
790
785
"""
791
786
Return the font size.
792
787
"""
793
- if self ._size is None :
794
- return rcParams ['font.size' ]
795
788
return self ._size
796
789
797
790
def get_size_in_points (self ):
798
- if self ._size is not None :
799
- try :
800
- return float (self ._size )
801
- except ValueError :
802
- pass
803
- default_size = FontManager .get_default_size ()
804
- return default_size * font_scalings .get (self ._size )
791
+ return self ._size
805
792
806
793
def get_file (self ):
807
794
"""
@@ -833,11 +820,7 @@ def set_family(self, family):
833
820
"""
834
821
if family is None :
835
822
family = rcParams ['font.family' ]
836
- if is_string_like (family ):
837
- family = [six .text_type (family )]
838
- elif not is_string_like (family ) and isinstance (family , Iterable ):
839
- family = [six .text_type (f ) for f in family ]
840
- self ._family = family
823
+ self ._family = _normalize_font_family (family )
841
824
set_name = set_family
842
825
843
826
def set_style (self , style ):
@@ -847,7 +830,7 @@ def set_style(self, style):
847
830
"""
848
831
if style is None :
849
832
style = rcParams ['font.style' ]
850
- if style not in ('normal' , 'italic' , 'oblique' , None ):
833
+ if style not in ('normal' , 'italic' , 'oblique' ):
851
834
raise ValueError ("style must be normal, italic or oblique" )
852
835
self ._slant = style
853
836
set_slant = set_style
@@ -858,7 +841,7 @@ def set_variant(self, variant):
858
841
"""
859
842
if variant is None :
860
843
variant = rcParams ['font.variant' ]
861
- if variant not in ('normal' , 'small-caps' , None ):
844
+ if variant not in ('normal' , 'small-caps' ):
862
845
raise ValueError ("variant must be normal or small-caps" )
863
846
self ._variant = variant
864
847
@@ -910,10 +893,15 @@ def set_size(self, size):
910
893
try :
911
894
size = float (size )
912
895
except ValueError :
913
- if size is not None and size not in font_scalings :
896
+ try :
897
+ scale = font_scalings [size ]
898
+ except KeyError :
914
899
raise ValueError (
915
900
"Size is invalid. Valid font size are " + ", " .join (
916
901
str (i ) for i in font_scalings .keys ()))
902
+ else :
903
+ size = scale * FontManager .get_default_size ()
904
+
917
905
self ._size = size
918
906
919
907
def set_file (self , file ):
@@ -942,7 +930,8 @@ def set_fontconfig_pattern(self, pattern):
942
930
943
931
def copy (self ):
944
932
"""Return a deep copy of self"""
945
- return FontProperties (_init = self )
933
+ return FontProperties (_init = self )
934
+
946
935
947
936
def ttfdict_to_fnames (d ):
948
937
"""
@@ -958,6 +947,7 @@ def ttfdict_to_fnames(d):
958
947
fnames .append (fname )
959
948
return fnames
960
949
950
+
961
951
def pickle_dump (data , filename ):
962
952
"""
963
953
Equivalent to pickle.dump(data, open(filename, 'w'))
@@ -966,6 +956,7 @@ def pickle_dump(data, filename):
966
956
with open (filename , 'wb' ) as fh :
967
957
pickle .dump (data , fh )
968
958
959
+
969
960
def pickle_load (filename ):
970
961
"""
971
962
Equivalent to pickle.load(open(filename, 'r'))
@@ -976,6 +967,14 @@ def pickle_load(filename):
976
967
return data
977
968
978
969
970
+ def _normalize_font_family (family ):
971
+ if is_string_like (family ):
972
+ family = [six .text_type (family )]
973
+ elif isinstance (family , Iterable ):
974
+ family = [six .text_type (f ) for f in family ]
975
+ return family
976
+
977
+
979
978
class TempCache (object ):
980
979
"""
981
980
A class to store temporary caches that are (a) not saved to disk
0 commit comments