FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/lib/matplotlib/font_manager.py at v3.1.x · matplotlib/matplotlib · GitHub
matplotlib
/
matplotlib
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
8.5k
Star
23.3k
Code
Issues
1.1k
Pull requests
438
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
font_manager.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1363 lines (1142 loc) · 44.7 KB
Breadcrumbs
matplotlib
/
lib
/
matplotlib
/
font_manager.py
Copy path
File metadata and controls
1363 lines (1142 loc) · 44.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
A module for finding, managing, and using fonts across platforms.
This module provides a single :class:`FontManager` instance that can
be shared across backends and platforms. The :func:`findfont`
function returns the best TrueType (TTF) font file in the local or
system font path that matches the specified :class:`FontProperties`
instance. The :class:`FontManager` also handles Adobe Font Metrics
(AFM) font files for use by the PostScript backend.
The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
Future versions may implement the Level 2 or 2.1 specifications.
"""
# KNOWN ISSUES
#
# - documentation
# - font variant is untested
# - font stretch is incomplete
# - font size is incomplete
# - default font algorithm needs improvement and testing
# - setWeights function needs improvement
# - 'light' is an invalid weight value, remove it.
from
functools
import
lru_cache
import
json
import
logging
from
numbers
import
Number
import
os
from
pathlib
import
Path
import
subprocess
import
sys
try
:
from
threading
import
Timer
except
ImportError
:
from
dummy_threading
import
Timer
import
matplotlib
as
mpl
from
matplotlib
import
afm
,
cbook
,
ft2font
,
rcParams
from
matplotlib
.
fontconfig_pattern
import
(
parse_fontconfig_pattern
,
generate_fontconfig_pattern
)
_log
=
logging
.
getLogger
(
__name__
)
font_scalings
=
{
'xx-small'
:
0.579
,
'x-small'
:
0.694
,
'small'
:
0.833
,
'medium'
:
1.0
,
'large'
:
1.200
,
'x-large'
:
1.440
,
'xx-large'
:
1.728
,
'larger'
:
1.2
,
'smaller'
:
0.833
,
None
:
1.0
}
stretch_dict
=
{
'ultra-condensed'
:
100
,
'extra-condensed'
:
200
,
'condensed'
:
300
,
'semi-condensed'
:
400
,
'normal'
:
500
,
'semi-expanded'
:
600
,
'expanded'
:
700
,
'extra-expanded'
:
800
,
'ultra-expanded'
:
900
}
weight_dict
=
{
'ultralight'
:
100
,
'light'
:
200
,
'normal'
:
400
,
'regular'
:
400
,
'book'
:
400
,
'medium'
:
500
,
'roman'
:
500
,
'semibold'
:
600
,
'demibold'
:
600
,
'demi'
:
600
,
'bold'
:
700
,
'heavy'
:
800
,
'extra bold'
:
800
,
'black'
:
900
}
font_family_aliases
=
{
'serif'
,
'sans-serif'
,
'sans serif'
,
'cursive'
,
'fantasy'
,
'monospace'
,
'sans'
}
# OS Font paths
MSFolders
=
\
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
MSFontDirectories
=
[
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts'
,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts'
]
MSUserFontDirectories
=
[
os
.
path
.
join
(
str
(
Path
.
home
()),
r'AppData\Local\Microsoft\Windows\Fonts'
),
os
.
path
.
join
(
str
(
Path
.
home
()),
r'AppData\Roaming\Microsoft\Windows\Fonts'
)]
X11FontDirectories
=
[
# an old standard installation point
"/usr/X11R6/lib/X11/fonts/TTF/"
,
"/usr/X11/lib/X11/fonts"
,
# here is the new standard location for fonts
"/usr/share/fonts/"
,
# documented as a good place to install new fonts
"/usr/local/share/fonts/"
,
# common application, not really useful
"/usr/lib/openoffice/share/fonts/truetype/"
,
# user fonts
str
(
Path
(
os
.
environ
.
get
(
'XDG_DATA_HOME'
,
Path
.
home
()
/
".local/share"
))
/
"fonts"
),
str
(
Path
.
home
()
/
".fonts"
),
]
OSXFontDirectories
=
[
"/Library/Fonts/"
,
"/Network/Library/Fonts/"
,
"/System/Library/Fonts/"
,
# fonts installed via MacPorts
"/opt/local/share/fonts"
,
# user fonts
str
(
Path
.
home
()
/
"Library/Fonts"
),
]
def
get_fontext_synonyms
(
fontext
):
"""
Return a list of file extensions extensions that are synonyms for
the given file extension *fileext*.
"""
return
{
'afm'
: [
'afm'
],
'otf'
: [
'otf'
,
'ttc'
,
'ttf'
],
'ttc'
: [
'otf'
,
'ttc'
,
'ttf'
],
'ttf'
: [
'otf'
,
'ttc'
,
'ttf'
],
}[
fontext
]
def
list_fonts
(
directory
,
extensions
):
"""
Return a list of all fonts matching any of the extensions, found
recursively under the directory.
"""
extensions
=
[
"."
+
ext
for
ext
in
extensions
]
return
[
os
.
path
.
join
(
dirpath
,
filename
)
# os.walk ignores access errors, unlike Path.glob.
for
dirpath
,
_
,
filenames
in
os
.
walk
(
directory
)
for
filename
in
filenames
if
Path
(
filename
).
suffix
.
lower
()
in
extensions
]
def
win32FontDirectory
():
r"""
Return the user-specified font directory for Win32. This is
looked up from the registry key::
\\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
If the key is not found, ``%WINDIR%\Fonts`` will be returned.
"""
import
winreg
try
:
with
winreg
.
OpenKey
(
winreg
.
HKEY_CURRENT_USER
,
MSFolders
)
as
user
:
return
winreg
.
QueryValueEx
(
user
,
'Fonts'
)[
0
]
except
OSError
:
return
os
.
path
.
join
(
os
.
environ
[
'WINDIR'
],
'Fonts'
)
def
_win32RegistryFonts
(
reg_domain
,
base_dir
):
r"""
Searches for fonts in the Windows registry.
Parameters
----------
reg_domain : int
The top level registry domain (e.g. HKEY_LOCAL_MACHINE).
base_dir : str
The path to the folder where the font files are usually located (e.g.
C:\Windows\Fonts). If only the filename of the font is stored in the
registry, the absolute path is built relative to this base directory.
Returns
-------
`set`
`pathlib.Path` objects with the absolute path to the font files found.
"""
import
winreg
items
=
set
()
for
reg_path
in
MSFontDirectories
:
try
:
with
winreg
.
OpenKey
(
reg_domain
,
reg_path
)
as
local
:
for
j
in
range
(
winreg
.
QueryInfoKey
(
local
)[
1
]):
# value may contain the filename of the font or its
# absolute path.
key
,
value
,
tp
=
winreg
.
EnumValue
(
local
,
j
)
if
not
isinstance
(
value
,
str
):
continue
# Work around for https://bugs.python.org/issue25778, which
# is fixed in Py>=3.6.1.
value
=
value
.
split
(
"
\0
"
,
1
)[
0
]
try
:
# If value contains already an absolute path, then it
# is not changed further.
path
=
Path
(
base_dir
,
value
).
resolve
()
except
RuntimeError
:
# Don't fail with invalid entries.
continue
items
.
add
(
path
)
except
(
OSError
,
MemoryError
):
continue
return
items
def
win32InstalledFonts
(
directory
=
None
,
fontext
=
'ttf'
):
"""
Search for fonts in the specified font directory, or use the
system directories if none given. Additionally, it is searched for user
fonts installed. A list of TrueType font filenames are returned by default,
or AFM fonts if *fontext* == 'afm'.
"""
import
winreg
if
directory
is
None
:
directory
=
win32FontDirectory
()
fontext
=
[
'.'
+
ext
for
ext
in
get_fontext_synonyms
(
fontext
)]
items
=
set
()
# System fonts
items
.
update
(
_win32RegistryFonts
(
winreg
.
HKEY_LOCAL_MACHINE
,
directory
))
# User fonts
for
userdir
in
MSUserFontDirectories
:
items
.
update
(
_win32RegistryFonts
(
winreg
.
HKEY_CURRENT_USER
,
userdir
))
# Keep only paths with matching file extension.
return
[
str
(
path
)
for
path
in
items
if
path
.
suffix
.
lower
()
in
fontext
]
@
cbook
.
deprecated
(
"3.1"
)
def
OSXInstalledFonts
(
directories
=
None
,
fontext
=
'ttf'
):
"""Get list of font files on OS X."""
if
directories
is
None
:
directories
=
OSXFontDirectories
return
[
path
for
directory
in
directories
for
path
in
list_fonts
(
directory
,
get_fontext_synonyms
(
fontext
))]
@
lru_cache
()
def
_call_fc_list
():
"""Cache and list the font filenames known to `fc-list`.
"""
# Delay the warning by 5s.
timer
=
Timer
(
5
,
lambda
:
_log
.
warning
(
'Matplotlib is building the font cache using fc-list. '
'This may take a moment.'
))
timer
.
start
()
try
:
out
=
subprocess
.
check_output
([
'fc-list'
,
'--format=%{file}
\\
n'
])
except
(
OSError
,
subprocess
.
CalledProcessError
):
return
[]
finally
:
timer
.
cancel
()
return
[
os
.
fsdecode
(
fname
)
for
fname
in
out
.
split
(
b'
\n
'
)]
def
get_fontconfig_fonts
(
fontext
=
'ttf'
):
"""List the font filenames known to `fc-list` having the given extension.
"""
fontext
=
[
'.'
+
ext
for
ext
in
get_fontext_synonyms
(
fontext
)]
return
[
fname
for
fname
in
_call_fc_list
()
if
Path
(
fname
).
suffix
.
lower
()
in
fontext
]
def
findSystemFonts
(
fontpaths
=
None
,
fontext
=
'ttf'
):
"""
Search for fonts in the specified font paths. If no paths are
given, will use a standard set of system paths, as well as the
list of fonts tracked by fontconfig if fontconfig is installed and
available. A list of TrueType fonts are returned by default with
AFM fonts as an option.
"""
fontfiles
=
set
()
fontexts
=
get_fontext_synonyms
(
fontext
)
if
fontpaths
is
None
:
if
sys
.
platform
==
'win32'
:
fontpaths
=
MSUserFontDirectories
+
[
win32FontDirectory
()]
# now get all installed fonts directly...
fontfiles
.
update
(
win32InstalledFonts
(
fontext
=
fontext
))
else
:
fontpaths
=
X11FontDirectories
if
sys
.
platform
==
'darwin'
:
fontpaths
=
[
*
X11FontDirectories
,
*
OSXFontDirectories
]
fontfiles
.
update
(
get_fontconfig_fonts
(
fontext
))
elif
isinstance
(
fontpaths
,
str
):
fontpaths
=
[
fontpaths
]
for
path
in
fontpaths
:
fontfiles
.
update
(
map
(
os
.
path
.
abspath
,
list_fonts
(
path
,
fontexts
)))
return
[
fname
for
fname
in
fontfiles
if
os
.
path
.
exists
(
fname
)]
class
FontEntry
(
object
):
"""
A class for storing Font properties. It is used when populating
the font lookup dictionary.
"""
def
__init__
(
self
,
fname
=
''
,
name
=
''
,
style
=
'normal'
,
variant
=
'normal'
,
weight
=
'normal'
,
stretch
=
'normal'
,
size
=
'medium'
,
):
self
.
fname
=
fname
self
.
name
=
name
self
.
style
=
style
self
.
variant
=
variant
self
.
weight
=
weight
self
.
stretch
=
stretch
try
:
self
.
size
=
str
(
float
(
size
))
except
ValueError
:
self
.
size
=
size
def
__repr__
(
self
):
return
"<Font '%s' (%s) %s %s %s %s>"
%
(
self
.
name
,
os
.
path
.
basename
(
self
.
fname
),
self
.
style
,
self
.
variant
,
self
.
weight
,
self
.
stretch
)
def
ttfFontProperty
(
font
):
"""
Extract information from a TrueType font file.
Parameters
----------
font : `.FT2Font`
The TrueType font file from which information will be extracted.
Returns
-------
`FontEntry`
The extracted font properties.
"""
name
=
font
.
family_name
# Styles are: italic, oblique, and normal (default)
sfnt
=
font
.
get_sfnt
()
# These tables are actually mac_roman-encoded, but mac_roman support may be
# missing in some alternative Python implementations and we are only going
# to look for ASCII substrings, where any ASCII-compatible encoding works
# - or big-endian UTF-16, since important Microsoft fonts use that.
sfnt2
=
(
sfnt
.
get
((
1
,
0
,
0
,
2
),
b''
).
decode
(
'latin-1'
).
lower
()
or
sfnt
.
get
((
3
,
1
,
0x0409
,
2
),
b''
).
decode
(
'utf_16_be'
).
lower
())
sfnt4
=
(
sfnt
.
get
((
1
,
0
,
0
,
4
),
b''
).
decode
(
'latin-1'
).
lower
()
or
sfnt
.
get
((
3
,
1
,
0x0409
,
4
),
b''
).
decode
(
'utf_16_be'
).
lower
())
if
sfnt4
.
find
(
'oblique'
)
>=
0
:
style
=
'oblique'
elif
sfnt4
.
find
(
'italic'
)
>=
0
:
style
=
'italic'
elif
sfnt2
.
find
(
'regular'
)
>=
0
:
style
=
'normal'
elif
font
.
style_flags
&
ft2font
.
ITALIC
:
style
=
'italic'
else
:
style
=
'normal'
# Variants are: small-caps and normal (default)
# !!!! Untested
if
name
.
lower
()
in
[
'capitals'
,
'small-caps'
]:
variant
=
'small-caps'
else
:
variant
=
'normal'
weight
=
next
((
w
for
w
in
weight_dict
if
sfnt4
.
find
(
w
)
>=
0
),
None
)
if
not
weight
:
if
font
.
style_flags
&
ft2font
.
BOLD
:
weight
=
700
else
:
weight
=
400
# Stretch can be absolute and relative
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
# semi-condensed, normal, semi-expanded, expanded, extra-expanded,
# and ultra-expanded.
# Relative stretches are: wider, narrower
# Child value is: inherit
if
(
sfnt4
.
find
(
'narrow'
)
>=
0
or
sfnt4
.
find
(
'condensed'
)
>=
0
or
sfnt4
.
find
(
'cond'
)
>=
0
):
stretch
=
'condensed'
elif
sfnt4
.
find
(
'demi cond'
)
>=
0
:
stretch
=
'semi-condensed'
elif
sfnt4
.
find
(
'wide'
)
>=
0
or
sfnt4
.
find
(
'expanded'
)
>=
0
:
stretch
=
'expanded'
else
:
stretch
=
'normal'
# Sizes can be absolute and relative.
# Absolute sizes are: xx-small, x-small, small, medium, large, x-large,
# and xx-large.
# Relative sizes are: larger, smaller
# Length value is an absolute font size, e.g., 12pt
# Percentage values are in 'em's. Most robust specification.
if
not
font
.
scalable
:
raise
NotImplementedError
(
"Non-scalable fonts are not supported"
)
size
=
'scalable'
return
FontEntry
(
font
.
fname
,
name
,
style
,
variant
,
weight
,
stretch
,
size
)
def
afmFontProperty
(
fontpath
,
font
):
"""
Extract information from an AFM font file.
Parameters
----------
font : `.AFM`
The AFM font file from which information will be extracted.
Returns
-------
`FontEntry`
The extracted font properties.
"""
name
=
font
.
get_familyname
()
fontname
=
font
.
get_fontname
().
lower
()
# Styles are: italic, oblique, and normal (default)
if
font
.
get_angle
()
!=
0
or
'italic'
in
name
.
lower
():
style
=
'italic'
elif
'oblique'
in
name
.
lower
():
style
=
'oblique'
else
:
style
=
'normal'
# Variants are: small-caps and normal (default)
# !!!! Untested
if
name
.
lower
()
in
[
'capitals'
,
'small-caps'
]:
variant
=
'small-caps'
else
:
variant
=
'normal'
weight
=
font
.
get_weight
().
lower
()
if
weight
not
in
weight_dict
:
weight
=
'normal'
# Stretch can be absolute and relative
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
# semi-condensed, normal, semi-expanded, expanded, extra-expanded,
# and ultra-expanded.
# Relative stretches are: wider, narrower
# Child value is: inherit
if
'demi cond'
in
fontname
:
stretch
=
'semi-condensed'
elif
'narrow'
in
fontname
or
'cond'
in
fontname
:
stretch
=
'condensed'
elif
'wide'
in
fontname
or
'expanded'
in
fontname
:
stretch
=
'expanded'
else
:
stretch
=
'normal'
# Sizes can be absolute and relative.
# Absolute sizes are: xx-small, x-small, small, medium, large, x-large,
# and xx-large.
# Relative sizes are: larger, smaller
# Length value is an absolute font size, e.g., 12pt
# Percentage values are in 'em's. Most robust specification.
# All AFM fonts are apparently scalable.
size
=
'scalable'
return
FontEntry
(
fontpath
,
name
,
style
,
variant
,
weight
,
stretch
,
size
)
def
createFontList
(
fontfiles
,
fontext
=
'ttf'
):
"""
A function to create a font lookup list. The default is to create
a list of TrueType fonts. An AFM font list can optionally be
created.
"""
fontlist
=
[]
# Add fonts from list of known font files.
seen
=
set
()
for
fpath
in
fontfiles
:
_log
.
debug
(
'createFontDict: %s'
,
fpath
)
fname
=
os
.
path
.
split
(
fpath
)[
1
]
if
fname
in
seen
:
continue
if
fontext
==
'afm'
:
try
:
with
open
(
fpath
,
'rb'
)
as
fh
:
font
=
afm
.
AFM
(
fh
)
except
EnvironmentError
:
_log
.
info
(
"Could not open font file %s"
,
fpath
)
continue
except
RuntimeError
:
_log
.
info
(
"Could not parse font file %s"
,
fpath
)
continue
try
:
prop
=
afmFontProperty
(
fpath
,
font
)
except
KeyError
as
exc
:
_log
.
info
(
"Could not extract properties for %s: %s"
,
fpath
,
exc
)
continue
else
:
try
:
font
=
ft2font
.
FT2Font
(
fpath
)
except
(
OSError
,
RuntimeError
)
as
exc
:
_log
.
info
(
"Could not open font file %s: %s"
,
fpath
,
exc
)
continue
except
UnicodeError
:
_log
.
info
(
"Cannot handle unicode filenames"
)
continue
try
:
prop
=
ttfFontProperty
(
font
)
except
(
KeyError
,
RuntimeError
,
ValueError
,
NotImplementedError
)
as
exc
:
_log
.
info
(
"Could not extract properties for %s: %s"
,
fpath
,
exc
)
continue
fontlist
.
append
(
prop
)
seen
.
add
(
fname
)
return
fontlist
class
FontProperties
(
object
):
"""
A class for storing and manipulating font properties.
The font properties are those described in the `W3C Cascading
Style Sheet, Level 1
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ font
specification. The six properties are:
- family: A list of font names in decreasing order of priority.
The items may include a generic font family name, either
'serif', 'sans-serif', 'cursive', 'fantasy', or 'monospace'.
In that case, the actual font to be used will be looked up
from the associated rcParam.
- style: Either 'normal', 'italic' or 'oblique'.
- variant: Either 'normal' or 'small-caps'.
- stretch: A numeric value in the range 0-1000 or one of
'ultra-condensed', 'extra-condensed', 'condensed',
'semi-condensed', 'normal', 'semi-expanded', 'expanded',
'extra-expanded' or 'ultra-expanded'
- weight: A numeric value in the range 0-1000 or one of
'ultralight', 'light', 'normal', 'regular', 'book', 'medium',
'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
'extra bold', 'black'
- size: Either an relative value of 'xx-small', 'x-small',
'small', 'medium', 'large', 'x-large', 'xx-large' or an
absolute font size, e.g., 12
The default font property for TrueType fonts (as specified in the
default rcParams) is::
sans-serif, normal, normal, normal, normal, scalable.
Alternatively, a font may be specified using an absolute path to a
.ttf file, by using the *fname* kwarg.
The preferred usage of font sizes is to use the relative values,
e.g., 'large', instead of absolute font sizes, e.g., 12. This
approach allows all text sizes to be made larger or smaller based
on the font manager's default font size.
This class will also accept a fontconfig_ pattern_, if it is the only
argument provided. This support does not require fontconfig to be
installed. We are merely borrowing its pattern syntax for use here.
.. _fontconfig: https://www.freedesktop.org/wiki/Software/fontconfig/
.. _pattern:
https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
Note that Matplotlib's internal font manager and fontconfig use a
different algorithm to lookup fonts, so the results of the same pattern
may be different in Matplotlib than in other applications that use
fontconfig.
"""
def
__init__
(
self
,
family
=
None
,
style
=
None
,
variant
=
None
,
weight
=
None
,
stretch
=
None
,
size
=
None
,
fname
=
None
,
# if set, it's a hardcoded filename to use
):
self
.
_family
=
_normalize_font_family
(
rcParams
[
'font.family'
])
self
.
_slant
=
rcParams
[
'font.style'
]
self
.
_variant
=
rcParams
[
'font.variant'
]
self
.
_weight
=
rcParams
[
'font.weight'
]
self
.
_stretch
=
rcParams
[
'font.stretch'
]
self
.
_size
=
rcParams
[
'font.size'
]
self
.
_file
=
None
if
isinstance
(
family
,
str
):
# Treat family as a fontconfig pattern if it is the only
# parameter provided.
if
(
style
is
None
and
variant
is
None
and
weight
is
None
and
stretch
is
None
and
size
is
None
and
fname
is
None
):
self
.
set_fontconfig_pattern
(
family
)
return
self
.
set_family
(
family
)
self
.
set_style
(
style
)
self
.
set_variant
(
variant
)
self
.
set_weight
(
weight
)
self
.
set_stretch
(
stretch
)
self
.
set_file
(
fname
)
self
.
set_size
(
size
)
def
_parse_fontconfig_pattern
(
self
,
pattern
):
return
parse_fontconfig_pattern
(
pattern
)
def
__hash__
(
self
):
l
=
(
tuple
(
self
.
get_family
()),
self
.
get_slant
(),
self
.
get_variant
(),
self
.
get_weight
(),
self
.
get_stretch
(),
self
.
get_size_in_points
(),
self
.
get_file
())
return
hash
(
l
)
def
__eq__
(
self
,
other
):
return
hash
(
self
)
==
hash
(
other
)
def
__str__
(
self
):
return
self
.
get_fontconfig_pattern
()
def
get_family
(
self
):
"""
Return a list of font names that comprise the font family.
"""
return
self
.
_family
def
get_name
(
self
):
"""
Return the name of the font that best matches the font properties.
"""
return
get_font
(
findfont
(
self
)).
family_name
def
get_style
(
self
):
"""
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
"""
return
self
.
_slant
get_slant
=
get_style
def
get_variant
(
self
):
"""
Return the font variant. Values are: 'normal' or 'small-caps'.
"""
return
self
.
_variant
def
get_weight
(
self
):
"""
Set the font weight. Options are: A numeric value in the
range 0-1000 or one of 'light', 'normal', 'regular', 'book',
'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold',
'heavy', 'extra bold', 'black'
"""
return
self
.
_weight
def
get_stretch
(
self
):
"""
Return the font stretch or width. Options are: 'ultra-condensed',
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
"""
return
self
.
_stretch
def
get_size
(
self
):
"""
Return the font size.
"""
return
self
.
_size
def
get_size_in_points
(
self
):
return
self
.
_size
def
get_file
(
self
):
"""
Return the filename of the associated font.
"""
return
self
.
_file
def
get_fontconfig_pattern
(
self
):
"""
Get a fontconfig_ pattern_ suitable for looking up the font as
specified with fontconfig's ``fc-match`` utility.
This support does not require fontconfig to be installed or
support for it to be enabled. We are merely borrowing its
pattern syntax for use here.
"""
return
generate_fontconfig_pattern
(
self
)
def
set_family
(
self
,
family
):
"""
Change the font family. May be either an alias (generic name
is CSS parlance), such as: 'serif', 'sans-serif', 'cursive',
'fantasy', or 'monospace', a real font name or a list of real
font names. Real font names are not supported when
`text.usetex` is `True`.
"""
if
family
is
None
:
family
=
rcParams
[
'font.family'
]
self
.
_family
=
_normalize_font_family
(
family
)
set_name
=
set_family
def
set_style
(
self
,
style
):
"""
Set the font style. Values are: 'normal', 'italic' or 'oblique'.
"""
if
style
is
None
:
style
=
rcParams
[
'font.style'
]
cbook
.
_check_in_list
([
'normal'
,
'italic'
,
'oblique'
],
style
=
style
)
self
.
_slant
=
style
set_slant
=
set_style
def
set_variant
(
self
,
variant
):
"""
Set the font variant. Values are: 'normal' or 'small-caps'.
"""
if
variant
is
None
:
variant
=
rcParams
[
'font.variant'
]
cbook
.
_check_in_list
([
'normal'
,
'small-caps'
],
variant
=
variant
)
self
.
_variant
=
variant
def
set_weight
(
self
,
weight
):
"""
Set the font weight. May be either a numeric value in the
range 0-1000 or one of 'ultralight', 'light', 'normal',
'regular', 'book', 'medium', 'roman', 'semibold', 'demibold',
'demi', 'bold', 'heavy', 'extra bold', 'black'
"""
if
weight
is
None
:
weight
=
rcParams
[
'font.weight'
]
try
:
weight
=
int
(
weight
)
if
weight
<
0
or
weight
>
1000
:
raise
ValueError
()
except
ValueError
:
if
weight
not
in
weight_dict
:
raise
ValueError
(
"weight is invalid"
)
self
.
_weight
=
weight
def
set_stretch
(
self
,
stretch
):
"""
Set the font stretch or width. Options are: 'ultra-condensed',
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
'semi-expanded', 'expanded', 'extra-expanded' or
'ultra-expanded', or a numeric value in the range 0-1000.
"""
if
stretch
is
None
:
stretch
=
rcParams
[
'font.stretch'
]
try
:
stretch
=
int
(
stretch
)
if
stretch
<
0
or
stretch
>
1000
:
raise
ValueError
()
except
ValueError
:
if
stretch
not
in
stretch_dict
:
raise
ValueError
(
"stretch is invalid"
)
self
.
_stretch
=
stretch
def
set_size
(
self
,
size
):
"""
Set the font size. Either an relative value of 'xx-small',
'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
or an absolute font size, e.g., 12.
"""
if
size
is
None
:
size
=
rcParams
[
'font.size'
]
try
:
size
=
float
(
size
)
except
ValueError
:
try
:
scale
=
font_scalings
[
size
]
except
KeyError
:
raise
ValueError
(
"Size is invalid. Valid font size are "
+
", "
.
join
(
map
(
str
,
font_scalings
)))
else
:
size
=
scale
*
FontManager
.
get_default_size
()
if
size
<
1.0
:
_log
.
info
(
'Fontsize %1.2f < 1.0 pt not allowed by FreeType. '
'Setting fontsize = 1 pt'
,
size
)
size
=
1.0
self
.
_size
=
size
def
set_file
(
self
,
file
):
"""
Set the filename of the fontfile to use. In this case, all
other properties will be ignored.
"""
self
.
_file
=
file
def
set_fontconfig_pattern
(
self
,
pattern
):
"""
Set the properties by parsing a fontconfig_ *pattern*.
This support does not require fontconfig to be installed or
support for it to be enabled. We are merely borrowing its
pattern syntax for use here.
"""
for
key
,
val
in
self
.
_parse_fontconfig_pattern
(
pattern
).
items
():
if
type
(
val
)
==
list
:
getattr
(
self
,
"set_"
+
key
)(
val
[
0
])
else
:
getattr
(
self
,
"set_"
+
key
)(
val
)
def
copy
(
self
):
"""Return a copy of self."""
new
=
type
(
self
)()
vars
(
new
).
update
(
vars
(
self
))
return
new
class
JSONEncoder
(
json
.
JSONEncoder
):
def
default
(
self
,
o
):
if
isinstance
(
o
,
FontManager
):
return
dict
(
o
.
__dict__
,
__class__
=
'FontManager'
)
elif
isinstance
(
o
,
FontEntry
):
d
=
dict
(
o
.
__dict__
,
__class__
=
'FontEntry'
)
try
:
# Cache paths of fonts shipped with mpl relative to the mpl
# data path, which helps in the presence of venvs.
d
[
"fname"
]
=
str
(
Path
(
d
[
"fname"
]).
relative_to
(
mpl
.
get_data_path
()))
except
ValueError
:
pass
return
d
else
:
return
super
().
default
(
o
)
def
_json_decode
(
o
):
cls
=
o
.
pop
(
'__class__'
,
None
)
if
cls
is
None
:
return
o
elif
cls
==
'FontManager'
:
r
=
FontManager
.
__new__
(
FontManager
)
r
.
__dict__
.
update
(
o
)
return
r
elif
cls
==
'FontEntry'
:
r
=
FontEntry
.
__new__
(
FontEntry
)
r
.
__dict__
.
update
(
o
)
if
not
os
.
path
.
isabs
(
r
.
fname
):
r
.
fname
=
os
.
path
.
join
(
mpl
.
get_data_path
(),
r
.
fname
)
return
r
else
:
raise
ValueError
(
"don't know how to deserialize __class__=%s"
%
cls
)
def
json_dump
(
data
,
filename
):
"""
Dumps a data structure as JSON in the named file.
Handles FontManager and its fields. File paths that are children of the
Matplotlib data path (typically, fonts shipped with Matplotlib) are stored
relative to that data path (to remain valid across virtualenvs).
"""
with
open
(
filename
,
'w'
)
as
fh
:
try
:
json
.
dump
(
data
,
fh
,
cls
=
JSONEncoder
,
indent
=
2
)
except
OSError
as
e
:
_log
.
warning
(
'Could not save font_manager cache {}'
.
format
(
e
))
def
json_load
(
filename
):
"""
Loads a data structure as JSON from the named file.
Handles FontManager and its fields. Relative file paths are interpreted
as being relative to the Matplotlib data path, and transformed into
absolute paths.
"""
with
open
(
filename
,
'r'
)
as
fh
:
return
json
.
load
(
fh
,
object_hook
=
_json_decode
)
def
_normalize_font_family
(
family
):
if
isinstance
(
family
,
str
):
family
=
[
family
]
return
family
@
cbook
.
deprecated
(
"3.0"
)
class
TempCache
(
object
):
"""
A class to store temporary caches that are (a) not saved to disk
and (b) invalidated whenever certain font-related
rcParams---namely the family lookup lists---are changed or the
font cache is reloaded. This avoids the expensive linear search
through all fonts every time a font is looked up.
"""
# A list of rcparam names that, when changed, invalidated this
# cache.
invalidating_rcparams
=
(
'font.serif'
,
'font.sans-serif'
,
'font.cursive'
,
'font.fantasy'
,
'font.monospace'
)
def
__init__
(
self
):
self
.
_lookup_cache
=
{}
self
.
_last_rcParams
=
self
.
make_rcparams_key
()
def
make_rcparams_key
(
self
):
return
[
id
(
fontManager
)]
+
[
rcParams
[
param
]
for
param
in
self
.
invalidating_rcparams
]
def
get
(
self
,
prop
):
key
=
self
.
make_rcparams_key
()
if
key
!=
self
.
_last_rcParams
:
self
.
_lookup_cache
=
{}
self
.
_last_rcParams
=
key
return
self
.
_lookup_cache
.
get
(
prop
)
def
set
(
self
,
prop
,
value
):
key
=
self
.
make_rcparams_key
()
if
key
!=
self
.
_last_rcParams
:
self
.
_lookup_cache
=
{}
self
.
_last_rcParams
=
key
self
.
_lookup_cache
[
prop
]
=
value
class
FontManager
(
object
):
"""
On import, the :class:`FontManager` singleton instance creates a
list of TrueType fonts based on the font properties: name, style,
variant, weight, stretch, and size. The :meth:`findfont` method
does a nearest neighbor search to find the font that most closely
matches the specification. If no good enough match is found, a
default font is returned.
"""
# Increment this version number whenever the font cache data
# format or behavior has changed and requires a existing font
# cache files to be rebuilt.
__version__
=
310
def
__init__
(
self
,
size
=
None
,
weight
=
'normal'
):
self
.
_version
=
self
.
__version__
self
.
__default_weight
=
weight
self
.
default_size
=
size
paths
=
[
os
.
path
.
join
(
rcParams
[
'datapath'
],
'fonts'
,
'ttf'
),
os
.
path
.
join
(
rcParams
[
'datapath'
],
'fonts'
,
'afm'
),
os
.
path
.
join
(
rcParams
[
'datapath'
],
'fonts'
,
'pdfcorefonts'
)]
# Create list of font paths
for
pathname
in
[
'TTFPATH'
,
'AFMPATH'
]:
if
pathname
in
os
.
environ
:
ttfpath
=
os
.
environ
[
pathname
]
if
ttfpath
.
find
(
';'
)
>=
0
:
# win32 style
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL