2323# - setWeights function needs improvement
2424# - 'light' is an invalid weight value, remove it.
2525
26- from enum import IntEnum
2726from functools import lru_cache
2827import json
2928import logging
3837except ImportError :
3938 from dummy_threading import Timer
4039
41- import numpy as np
42-
4340import matplotlib as mpl
4441from matplotlib import afm , cbook , ft2font , rcParams
4542from matplotlib .fontconfig_pattern import (
9087 'extra bold' : 800 ,
9188 'black' : 900 ,
9289}
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-
12190_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 ),
145116]
146-
147-
148117font_family_aliases = {
149118 'serif' ,
150119 'sans-serif' ,
@@ -467,11 +436,11 @@ def ttfFontProperty(font):
467436 ]
468437 styles = [* filter (None , styles )] or [font .style_name ]
469438
470- def get_weight ():
439+ def get_weight (): # From fontconfig's FcFreeTypeQueryFaceInternal.
471440 # OS/2 table weight.
472441 os2 = font .get_sfnt_table ("OS/2" )
473442 if os2 and os2 ["version" ] != 0xffff :
474- return _Weight . from_opentype ( os2 ["usWeightClass" ])
443+ return os2 ["usWeightClass" ]
475444 # PostScript font info weight.
476445 try :
477446 ps_font_info_weight = (
@@ -489,8 +458,8 @@ def get_weight():
489458 if re .search (regex , style , re .I ):
490459 return weight
491460 if font .style_flags & ft2font .BOLD :
492- return _Weight . BOLD
493- return _Weight . REGULAR
461+ return 700 # "bold"
462+ return 500 # "medium", not "regular"!
494463
495464 weight = int (get_weight ())
496465
0 commit comments