23
23
# - setWeights function needs improvement
24
24
# - 'light' is an invalid weight value, remove it.
25
25
26
- from enum import IntEnum
27
26
from functools import lru_cache
28
27
import json
29
28
import logging
38
37
except ImportError :
39
38
from dummy_threading import Timer
40
39
41
- import numpy as np
42
-
43
40
import matplotlib as mpl
44
41
from matplotlib import afm , cbook , ft2font , rcParams
45
42
from matplotlib .fontconfig_pattern import (
90
87
'extra bold' : 800 ,
91
88
'black' : 900 ,
92
89
}
93
-
94
-
95
- class _Weight (IntEnum ):
96
- Thin = 0
97
- Extralight = Ultralight = 40
98
- Light = 50
99
- Demilight = Semilight = 55
100
- Book = 75
101
- Regular = Normal = 80
102
- Medium = 100
103
- Demibold = Semibold = 180
104
- Bold = 200
105
- Extrabold = Ultrabold = 205
106
- Black = Heavy = 210
107
- Extrablack = Ultrablack = 215
108
-
109
- @classmethod
110
- def from_opentype (cls , ot_weight ):
111
- fc_weights = [0 , 40 , 50 , 55 , 75 , 80 , 100 , 180 , 200 , 205 , 210 , 215 ]
112
- ot_weights = [
113
- 100 , 200 , 300 , 350 , 380 , 400 , 500 , 600 , 700 , 800 , 900 , 1000 ]
114
- weight = int (np .interp (ot_weight , ot_weights , fc_weights ) + .5 )
115
- try :
116
- return _Weight (weight )
117
- except ValueError :
118
- return weight
119
-
120
-
121
90
_weight_regexes = [
122
- ("thin" , _Weight .Thin ),
123
- ("extralight" , _Weight .Extralight ),
124
- ("ultralight" , _Weight .Ultralight ),
125
- ("demilight" , _Weight .Demilight ),
126
- ("semilight" , _Weight .Semilight ),
127
- ("light" , _Weight .Light ),
128
- ("book" , _Weight .Book ),
129
- ("regular" , _Weight .Regular ),
130
- ("normal" , _Weight .Normal ),
131
- ("medium" , _Weight .Medium ),
132
- ("demibold" , _Weight .Demibold ),
133
- ("demi" , _Weight .Demibold ),
134
- ("semibold" , _Weight .Semibold ),
135
- ("extrabold" , _Weight .Extrabold ),
136
- ("superbold" , _Weight .Extrabold ),
137
- ("ultrabold" , _Weight .Ultrabold ),
138
- ("bold" , _Weight .Bold ),
139
- ("ultrablack" , _Weight .Ultrablack ),
140
- ("superblack" , _Weight .Extrablack ),
141
- ("extrablack" , _Weight .Extrablack ),
142
- (r"\bultra" , _Weight .Ultrabold ),
143
- ("black" , _Weight .Black ),
144
- ("heavy" , _Weight .Heavy ),
91
+ # From fontconfig's FcFreeTypeQueryFaceInternal; not the same as
92
+ # weight_dict!
93
+ ("thin" , 100 ),
94
+ ("extralight" , 200 ),
95
+ ("ultralight" , 200 ),
96
+ ("demilight" , 350 ),
97
+ ("semilight" , 350 ),
98
+ ("light" , 300 ), # Needs to come *after* demi/semilight!
99
+ ("book" , 380 ),
100
+ ("regular" , 400 ),
101
+ ("normal" , 400 ),
102
+ ("medium" , 500 ),
103
+ ("demibold" , 600 ),
104
+ ("demi" , 600 ),
105
+ ("semibold" , 600 ),
106
+ ("extrabold" , 800 ),
107
+ ("superbold" , 800 ),
108
+ ("ultrabold" , 800 ),
109
+ ("bold" , 700 ), # Needs to come *after* extra/super/ultrabold!
110
+ ("ultrablack" , 1000 ),
111
+ ("superblack" , 1000 ),
112
+ ("extrablack" , 1000 ),
113
+ (r"\bultra" , 1000 ),
114
+ ("black" , 900 ), # Needs to come *after* ultra/super/extrablack!
115
+ ("heavy" , 900 ),
145
116
]
146
-
147
-
148
117
font_family_aliases = {
149
118
'serif' ,
150
119
'sans-serif' ,
@@ -493,11 +462,11 @@ def ttfFontProperty(font):
493
462
))]
494
463
or [font .style_name ])
495
464
496
- def get_weight ():
465
+ def get_weight (): # From fontconfig's FcFreeTypeQueryFaceInternal.
497
466
# OS/2 table weight.
498
467
os2 = font .get_sfnt_table ("OS/2" )
499
468
if os2 and os2 ["version" ] != 0xffff :
500
- return _Weight . from_opentype ( os2 ["usWeightClass" ])
469
+ return os2 ["usWeightClass" ]
501
470
# PostScript font info weight.
502
471
try :
503
472
ps_font_info = font .get_ps_font_info ()
@@ -513,8 +482,8 @@ def get_weight():
513
482
if re .search (regex , style , re .I ):
514
483
return weight
515
484
if font .style_flags & ft2font .BOLD :
516
- return _Weight . BOLD
517
- return _Weight . REGULAR
485
+ return 700 # "bold"
486
+ return 500 # "medium", not "regular"!
518
487
519
488
weight = int (get_weight ())
520
489
0 commit comments