Skip to content

Commit 2217d5f

Browse files
committed
ft2font: Convert flag properties to enums
1 parent 12095ca commit 2217d5f

File tree

5 files changed

+180
-56
lines changed

5 files changed

+180
-56
lines changed

lib/matplotlib/backends/backend_pdf.py

+3-3
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

+2-2
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

+27-15
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ 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 = ...
14+
FIXED_SIZES = ...
15+
FIXED_WIDTH = ...
16+
SFNT = ...
17+
HORIZONTAL = ...
18+
VERTICAL = ...
19+
KERNING = ...
20+
FAST_GLYPHS = ...
21+
MULTIPLE_MASTERS = ...
22+
GLYPH_NAMES = ...
23+
EXTERNAL_STREAM = ...
24+
HINTER = ...
25+
CID_KEYED = ...
26+
TRICKY = ...
27+
COLOR = ...
28+
# VARIATION = ... # FT 2.9
29+
# SVG = ... # FT 2.12
30+
# SBIX = ... # FT 2.12
31+
# SBIX_OVERLAY = ... # FT 2.12
2432

2533
class Kerning(Enum):
2634
DEFAULT = ...
@@ -54,6 +62,10 @@ class LoadFlags(Enum):
5462
TARGET_LCD = ...
5563
TARGET_LCD_V = ...
5664

65+
class StyleFlags(Enum):
66+
ITALIC = ...
67+
BOLD = ...
68+
5769
class _SfntHeadDict(TypedDict):
5870
version: tuple[int, int]
5971
fontRevision: tuple[int, int]
@@ -232,7 +244,7 @@ class FT2Font(Buffer):
232244
@property
233245
def descender(self) -> int: ...
234246
@property
235-
def face_flags(self) -> int: ...
247+
def face_flags(self) -> FaceFlags: ...
236248
@property
237249
def family_name(self) -> str: ...
238250
@property
@@ -256,7 +268,7 @@ class FT2Font(Buffer):
256268
@property
257269
def scalable(self) -> bool: ...
258270
@property
259-
def style_flags(self) -> int: ...
271+
def style_flags(self) -> StyleFlags: ...
260272
@property
261273
def style_name(self) -> str: ...
262274
@property

lib/matplotlib/tests/test_ft2font.py

+13-12
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)