8000 Go back to using OpenType weight values. · matplotlib/matplotlib@e4b065d · GitHub
[go: up one dir, main page]

Skip to content

Commit e4b065d

Browse files
committed
Go back to using OpenType weight values.
1 parent 19f167f commit e4b065d

File tree

2 files changed

+30
-61
lines changed

2 files changed

+30
-61
lines changed

lib/matplotlib/font_manager.py

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# - setWeights function needs improvement
2424
# - 'light' is an invalid weight value, remove it.
2525

26-
from enum import IntEnum
2726
from functools import lru_cache
2827
import json
2928
import logging
@@ -38,8 +37,6 @@
3837
except ImportError:
3938
from dummy_threading import Timer
4039

41-
import numpy as np
42-
4340
import matplotlib as mpl
4441
from matplotlib import afm, cbook, ft2font, rcParams
4542
from matplotlib.fontconfig_pattern import (
@@ -90,61 +87,33 @@
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-
148117
font_family_aliases = {
149118
'serif',
150119
'sans-serif',
@@ -482,11 +451,11 @@ def ttfFontProperty(font):
482451
))]
483452
or [font.style_name])
484453

485-
def get_weight():
454+
def get_weight(): # From fontconfig's FcFreeTypeQueryFaceInternal.
486455
# OS/2 table weight.
487456
os2 = font.get_sfnt_table("OS/2")
488457
if os2 and os2["version"] != 0xffff:
489-
return _Weight.from_opentype(os2["usWeightClass"])
458+
return os2["usWeightClass"]
490459
# PostScript font info weight.
491460
try:
492461
ps_font_info = font.get_ps_font_info()
@@ -502,8 +471,8 @@ def get_weight():
502471
if re.search(regex, style, re.I):
503472
return weight
504473
if font.style_flags & ft2font.BOLD:
505-
return _Weight.BOLD
506-
return _Weight.REGULAR
474+
return 700 # "bold"
475+
return 500 # "medium", not "regular"!
507476

508477
weight = int(get_weight())
509478

lib/matplotlib/tests/test_font_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_utf16m_sfnt():
105105
else:
106106
# Check that we successfully read "semibold" from the font's sfnt table
107107
# and set its weight accordingly.
108-
assert entry.weight == "semibold"
108+
assert segoe_ui_semibold.weight == 600
109109

110110

111111
def test_find_ttc():

0 commit comments

Comments
 (0)
0