Skip to content

Commit ee7082b

Browse files
committed
ft2font: Convert flag properties to enums
1 parent 2c0c213 commit ee7082b

File tree

5 files changed

+205
-56
lines changed

5 files changed

+205
-56
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from matplotlib.figure import Figure
3636
from matplotlib.font_manager import get_font, fontManager as _fontManager
3737
from matplotlib._afm import AFM
38-
from matplotlib.ft2font import FIXED_WIDTH, ITALIC, FT2Font, Kerning, LoadFlags
38+
from matplotlib.ft2font import FT2Font, FaceFlags, Kerning, LoadFlags, StyleFlags
3939
from matplotlib.transforms import Affine2D, BboxBase
4040
from matplotlib.path import Path
4141
from matplotlib.dates import UTC
@@ -1416,15 +1416,15 @@ def embedTTFType42(font, characters, descriptor):
14161416

14171417
flags = 0
14181418
symbolic = False # ps_name.name in ('Cmsy10', 'Cmmi10', 'Cmex10')
1419-
if ff & FIXED_WIDTH:
1419+
if FaceFlags.FIXED_WIDTH in ff:
14201420
flags |= 1 << 0
14211421
if 0: # TODO: serif
14221422
flags |= 1 << 1
14231423
if symbolic:
14241424
flags |= 1 << 2
14251425
else:
14261426
flags |= 1 << 5
1427-
if sf & ITALIC:
1427+
if StyleFlags.ITALIC in sf:
14281428
flags |= 1 << 6
14291429
if 0: # TODO: all caps
14301430
flags |= 1 << 16

lib/matplotlib/font_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def ttfFontProperty(font):
381381
style = 'italic'
382382
elif sfnt2.find('regular') >= 0:
383383
style = 'normal'
384-
elif font.style_flags & ft2font.ITALIC:
384+
elif ft2font.StyleFlags.ITALIC in font.style_flags:
385385
style = 'italic'
386386
else:
387387
style = 'normal'
@@ -430,7 +430,7 @@ def get_weight(): # From fontconfig's FcFreeTypeQueryFaceInternal.
430430
for regex, weight in _weight_regexes:
431431
if re.search(regex, style, re.I):
432432
return weight
433-
if font.style_flags & ft2font.BOLD:
433+
if ft2font.StyleFlags.BOLD in font.style_flags:
434434
return 700 # "bold"
435435
return 500 # "medium", not "regular"!
436436

lib/matplotlib/ft2font.pyi

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@ from numpy.typing import NDArray
88

99
__freetype_build_type__: str
1010
__freetype_version__: str
11-
BOLD: int
12-
EXTERNAL_STREAM: int
13-
FAST_GLYPHS: int
14-
FIXED_SIZES: int
15-
FIXED_WIDTH: int
16-
GLYPH_NAMES: int
17-
HORIZONTAL: int
18-
ITALIC: int
19-
KERNING: int
20-
MULTIPLE_MASTERS: int
21-
SCALABLE: int
22-
SFNT: int
23-
VERTICAL: int
11+
12+
class FaceFlags(Enum):
13+
SCALABLE: FaceFlags
14+
FIXED_SIZES: FaceFlags
15+
FIXED_WIDTH: FaceFlags
16+
SFNT: FaceFlags
17+
HORIZONTAL: FaceFlags
18+
VERTICAL: FaceFlags
19+
KERNING: FaceFlags
20+
FAST_GLYPHS: FaceFlags
21+
MULTIPLE_MASTERS: FaceFlags
22+
GLYPH_NAMES: FaceFlags
23+
EXTERNAL_STREAM: FaceFlags
24+
HINTER: FaceFlags
25+
CID_KEYED: FaceFlags
26+
TRICKY: FaceFlags
27+
COLOR: FaceFlags
28+
# VARIATION: FaceFlags # FT 2.9
29+
# SVG: FaceFlags # FT 2.12
30+
# SBIX: FaceFlags # FT 2.12
31+
# SBIX_OVERLAY: FaceFlags # FT 2.12
32+
__members__: dict[str, FaceFlags]
33+
def __and__(self, other: FaceFlags) -> FaceFlags: ...
34+
def __contains__(self, other: FaceFlags) -> bool: ...
35+
def __ge__(self, other: FaceFlags) -> bool: ...
36+
def __gt__(self, other: FaceFlags) -> bool: ...
37+
def __index__(self) -> int: ...
38+
def __int__(self) -> int: ...
39+
def __invert__(self) -> FaceFlags: ...
40+
def __le__(self, other: FaceFlags) -> bool: ...
41+
def __lt__(self, other: FaceFlags) -> bool: ...
42+
def __or__(self, other: FaceFlags) -> FaceFlags: ...
43+
def __xor__(self, other: FaceFlags) -> FaceFlags: ...
2444

2545
class Kerning(Enum):
2646
DEFAULT: Kerning
@@ -69,6 +89,23 @@ class LoadFlags(Enum):
6989
def __or__(self, other: LoadFlags) -> LoadFlags: ...
7090
def __xor__(self, other: LoadFlags) -> LoadFlags: ...
7191

92+
class StyleFlags(Enum):
93+
NORMAL: StyleFlags
94+
ITALIC: StyleFlags
95+
BOLD: StyleFlags
96+
__members__: dict[str, StyleFlags]
97+
def __and__(self, other: StyleFlags) -> StyleFlags: ...
98+
def __contains__(self, other: StyleFlags) -> bool: ...
99+
def __ge__(self, other: StyleFlags) -> bool: ...
100+
def __gt__(self, other: StyleFlags) -> bool: ...
101+
def __index__(self) -> int: ...
102+
def __int__(self) -> int: ...
103+
def __invert__(self) -> StyleFlags: ...
104+
def __le__(self, other: StyleFlags) -> bool: ...
105+
def __lt__(self, other: StyleFlags) -> bool: ...
106+
def __or__(self, other: StyleFlags) -> StyleFlags: ...
107+
def __xor__(self, other: StyleFlags) -> StyleFlags: ...
108+
72109
class _SfntHeadDict(TypedDict):
73110
version: tuple[int, int]
74111
fontRevision: tuple[int, int]
@@ -247,7 +284,7 @@ class FT2Font(Buffer):
247284
@property
248285
def descender(self) -> int: ...
249286
@property
250-
def face_flags(self) -> int: ...
287+
def face_flags(self) -> FaceFlags: ...
251288
@property
252289
def family_name(self) -> str: ...
253290
@property
@@ -271,7 +308,7 @@ class FT2Font(Buffer):
271308
@property
272309
def scalable(self) -> bool: ...
273310
@property
274-
def style_flags(self) -> int: ...
311+
def style_flags(self) -> StyleFlags: ...
275312
@property
276313
def style_name(self) -> str: ...
277314
@property

lib/matplotlib/tests/test_ft2font.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ def test_ft2font_dejavu_attrs():
4444
assert font.num_fixed_sizes == 0 # All glyphs are scalable.
4545
assert font.num_charmaps == 5
4646
# Other internal flags are set, so only check the ones we're allowed to test.
47-
expected_flags = (ft2font.SCALABLE | ft2font.SFNT | ft2font.HORIZONTAL |
48-
ft2font.KERNING | ft2font.GLYPH_NAMES)
49-
assert (font.face_flags & expected_flags) == expected_flags
50-
assert font.style_flags == 0 # Not italic or bold.
47+
expected_flags = (ft2font.FaceFlags.SCALABLE | ft2font.FaceFlags.SFNT |
48+
ft2font.FaceFlags.HORIZONTAL | ft2font.FaceFlags.KERNING |
49+
ft2font.FaceFlags.GLYPH_NAMES)
50+
assert expected_flags in font.face_flags
51+
assert font.style_flags == ft2font.StyleFlags.NORMAL
5152
assert font.scalable
5253
# From FontForge: Font Information → General tab → entry name below.
5354
assert font.units_per_EM == 2048 # Em Size.
@@ -76,10 +77,10 @@ def test_ft2font_cm_attrs():
7677
assert font.num_fixed_sizes == 0 # All glyphs are scalable.
7778
assert font.num_charmaps == 2
7879
# Other internal flags are set, so only check the ones we're allowed to test.
79-
expected_flags = (ft2font.SCALABLE | ft2font.SFNT | ft2font.HORIZONTAL |
80-
ft2font.GLYPH_NAMES)
81-
assert (font.face_flags & expected_flags) == expected_flags, font.face_flags
82-
assert font.style_flags == 0 # Not italic or bold.
80+
expected_flags = (ft2font.FaceFlags.SCALABLE | ft2font.FaceFlags.SFNT |
81+
ft2font.FaceFlags.HORIZONTAL | ft2font.FaceFlags.GLYPH_NAMES)
82+
assert expected_flags in font.face_flags
83+
assert font.style_flags == ft2font.StyleFlags.NORMAL
8384
assert font.scalable
8485
# From FontForge: Font Information → General tab → entry name below.
8586
assert font.units_per_EM == 2048 # Em Size.
@@ -108,10 +109,10 @@ def test_ft2font_stix_bold_attrs():
108109
assert font.num_fixed_sizes == 0 # All glyphs are scalable.
109110
assert font.num_charmaps == 3
110111
# Other internal flags are set, so only check the ones we're allowed to test.
111-
expected_flags = (ft2font.SCALABLE | ft2font.SFNT | ft2font.HORIZONTAL |
112-
ft2font.GLYPH_NAMES)
113-
assert (font.face_flags & expected_flags) == expected_flags, font.face_flags
114-
assert font.style_flags == ft2font.BOLD
112+
expected_flags = (ft2font.FaceFlags.SCALABLE | ft2font.FaceFlags.SFNT |
113+
ft2font.FaceFlags.HORIZONTAL | ft2font.FaceFlags.GLYPH_NAMES)
114+
assert expected_flags in font.face_flags
115+
assert font.style_flags == ft2font.StyleFlags.BOLD
115116
assert font.scalable
116117
# From FontForge: Font Information → General tab → entry name below.
117118
assert font.units_per_EM == 1000 # Em Size.

0 commit comments

Comments
 (0)