Skip to content

Commit ed46569

Browse files
committed
ft2font: Convert flag properties to enums
1 parent 719b2e1 commit ed46569

File tree

7 files changed

+189
-66
lines changed

7 files changed

+189
-66
lines changed

doc/api/next_api_changes/deprecations/28842-ES.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,22 @@ prefix):
3434
- ``LOAD_TARGET_MONO``
3535
- ``LOAD_TARGET_LCD``
3636
- ``LOAD_TARGET_LCD_V``
37+
38+
The following constants are now part of `.ft2font.FaceFlags`:
39+
40+
- ``EXTERNAL_STREAM``
41+
- ``FAST_GLYPHS``
42+
- ``FIXED_SIZES``
43+
- ``FIXED_WIDTH``
44+
- ``GLYPH_NAMES``
45+
- ``HORIZONTAL``
46+
- ``KERNING``
47+
- ``MULTIPLE_MASTERS``
48+
- ``SCALABLE``
49+
- ``SFNT``
50+
- ``VERTICAL``
51+
52+
The following constants are now part of `.ft2font.StyleFlags`:
53+
54+
- ``ITALIC``
55+
- ``BOLD``

galleries/examples/misc/ftface_props.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,10 @@
4646
# vertical thickness of the underline
4747
print('Underline thickness:', font.underline_thickness)
4848

49-
for style in ('Italic',
50-
'Bold',
51-
'Scalable',
52-
'Fixed sizes',
53-
'Fixed width',
54-
'SFNT',
55-
'Horizontal',
56-
'Vertical',
57-
'Kerning',
58-
'Fast glyphs',
59-
'Multiple masters',
60-
'Glyph names',
61-
'External stream'):
62-
bitpos = getattr(ft, style.replace(' ', '_').upper()) - 1
63-
print(f"{style+':':17}", bool(font.style_flags & (1 << bitpos)))
49+
for flag in ft.StyleFlags:
50+
name = flag.name.replace('_', ' ').title() + ':'
51+
print(f"{name:17}", flag in font.style_flags)
52+
53+
for flag in ft.FaceFlags:
54+
name = flag.name.replace('_', ' ').title() + ':'
55+
print(f"{name:17}", flag in font.face_flags)

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: 27 additions & 15 deletions
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(Flag):
13+
SCALABLE: int
14+
FIXED_SIZES: int
15+
FIXED_WIDTH: int
16+
SFNT: int
17+
HORIZONTAL: int
18+
VERTICAL: int
19+
KERNING: int
20+
FAST_GLYPHS: int
21+
MULTIPLE_MASTERS: int
22+
GLYPH_NAMES: int
23+
EXTERNAL_STREAM: int
24+
HINTER: int
25+
CID_KEYED: int
26+
TRICKY: int
27+
COLOR: int
28+
# VARIATION: int # FT 2.9
29+
# SVG: int # FT 2.12
30+
# SBIX: int # FT 2.12
31+
# SBIX_OVERLAY: int # FT 2.12
2432

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

65+
class StyleFlags(Flag):
66+
ITALIC: int
67+
BOLD: int
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

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)