Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b44556c

Browse files
committed
Convert ft2font extension to pybind11
1 parent 234f08a commit b44556c

File tree

6 files changed

+712
-1073
lines changed

6 files changed

+712
-1073
lines changed

lib/matplotlib/ft2font.pyi

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
from typing import BinaryIO, Literal, TypedDict, final, overload
2+
try:
3+
from collections.abc import Buffer
4+
except ImportError:
5+
from typing_extensions import Buffer # < Py 3.12
26

37
import numpy as np
48
from numpy.typing import NDArray
@@ -159,28 +163,7 @@ class _SfntPcltDict(TypedDict):
159163
serifStyle: int
160164

161165
@final
162-
class FT2Font:
163-
ascender: int
164-
bbox: tuple[int, int, int, int]
165-
descender: int
166-
face_flags: int
167-
family_name: str
168-
fname: str
169-
height: int
170-
max_advance_height: int
171-
max_advance_width: int
172-
num_charmaps: int
173-
num_faces: int
174-
num_fixed_sizes: int
175-
num_glyphs: int
176-
postscript_name: str
177-
scalable: bool
178-
style_flags: int
179-
style_name: str
180-
underline_position: int
181-
underline_thickness: int
182-
units_per_EM: int
183-
166+
class FT2Font(Buffer):
184167
def __init__(
185168
self,
186169
filename: str | BinaryIO,
@@ -189,6 +172,7 @@ class FT2Font:
189172
_fallback_list: list[FT2Font] | None = ...,
190173
_kerning_factor: int = ...
191174
) -> None: ...
175+
def __buffer__(self, flags: int) -> memoryview: ...
192176
def _get_fontmap(self, string: str) -> dict[str, FT2Font]: ...
193177
def clear(self) -> None: ...
194178
def draw_glyph_to_bitmap(
@@ -232,23 +216,72 @@ class FT2Font:
232216
def set_text(
233217
self, string: str, angle: float = ..., flags: int = ...
234218
) -> NDArray[np.float64]: ...
219+
@property
220+
def ascender(self) -> int: ...
221+
@property
222+
def bbox(self) -> tuple[int, int, int, int]: ...
223+
@property
224+
def descender(self) -> int: ...
225+
@property
226+
def face_flags(self) -> int: ...
227+
@property
228+
def family_name(self) -> str: ...
229+
@property
230+
def fname(self) -> str: ...
231+
@property
232+
def height(self) -> int: ...
233+
@property
234+
def max_advance_height(self) -> int: ...
235+
@property
236+
def max_advance_width(self) -> int: ...
237+
@property
238+
def num_charmaps(self) -> int: ...
239+
@property
240+
def num_faces(self) -> int: ...
241+
@property
242+
def num_fixed_sizes(self) -> int: ...
243+
@property
244+
def num_glyphs(self) -> int: ...
245+
@property
246+
def postscript_name(self) -> str: ...
247+
@property
248+
def scalable(self) -> bool: ...
249+
@property
250+
def style_flags(self) -> int: ...
251+
@property
252+
def style_name(self) -> str: ...
253+
@property
254+
def underline_position(self) -> int: ...
255+
@property
256+
def underline_thickness(self) -> int: ...
257+
@property
258+
def units_per_EM(self) -> int: ...
235259

236260
@final
237-
class FT2Image: # TODO: When updating mypy>=1.4, subclass from Buffer.
261+
class FT2Image(Buffer):
238262
def __init__(self, width: float, height: float) -> None: ...
239263
def draw_rect_filled(self, x0: float, y0: float, x1: float, y1: float) -> None: ...
264+
def __buffer__(self, flags: int) -> memoryview: ...
240265

241266
@final
242267
class Glyph:
243-
width: int
244-
height: int
245-
horiBearingX: int
246-
horiBearingY: int
247-
horiAdvance: int
248-
linearHoriAdvance: int
249-
vertBearingX: int
250-
vertBearingY: int
251-
vertAdvance: int
252-
268+
@property
269+
def width(self) -> int: ...
270+
@property
271+
def height(self) -> int: ...
272+
@property
273+
def horiBearingX(self) -> int: ...
274+
@property
275+
def horiBearingY(self) -> int: ...
276+
@property
277+
def horiAdvance(self) -> int: ...
278+
@property
279+
def linearHoriAdvance(self) -> int: ...
280+
@property
281+
def vertBearingX(self) -> int: ...
282+
@property
283+
def vertBearingY(self) -> int: ...
284+
@property
285+
def vertAdvance(self) -> int: ...
253286
@property
254287
def bbox(self) -> tuple[int, int, int, int]: ...

lib/matplotlib/tests/test_ft2font.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def test_ft2font_invalid_args(tmp_path):
130130
# filename argument.
131131
with pytest.raises(TypeError, match='to a font file or a binary-mode file object'):
132132
ft2font.FT2Font(None)
133+
with pytest.raises(TypeError, match='to a font file or a binary-mode file object'):
134+
ft2font.FT2Font(object()) # Not bytes or string, and has no read() method.
133135
file = tmp_path / 'invalid-font.ttf'
134136
file.write_text('This is not a valid font file.')
135137
with (pytest.raises(TypeError, match='to a font file or a binary-mode file object'),
@@ -145,7 +147,7 @@ def test_ft2font_invalid_args(tmp_path):
145147
file = fm.findfont('DejaVu Sans')
146148

147149
# hinting_factor argument.
148-
with pytest.raises(TypeError, match='cannot be interpreted as an integer'):
150+
with pytest.raises(TypeError, match='incompatible constructor arguments'):
149151
ft2font.FT2Font(file, 1.3)
150152
with pytest.raises(ValueError, match='hinting_factor must be greater than 0'):
151153
ft2font.FT2Font(file, 0)
@@ -157,7 +159,7 @@ def test_ft2font_invalid_args(tmp_path):
157159
ft2font.FT2Font(file, _fallback_list=[0]) # type: ignore[list-item]
158160

159161
# kerning_factor argument.
160-
with pytest.raises(TypeError, match='cannot be interpreted as an integer'):
162+
with pytest.raises(TypeError, match='incompatible constructor arguments'):
161163
ft2font.FT2Font(file, _kerning_factor=1.3)
162164

163165

requirements/testing/mypy.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Extra pip requirements for the GitHub Actions mypy build
22

33
mypy>=1.9
4-
typing-extensions>=4.1
4+
typing-extensions>=4.6
55

66
# Extra stubs distributed separately from the main pypi package
77
pandas-stubs

src/ft2font.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#ifndef MPL_FT2FONT_H
77
#define MPL_FT2FONT_H
88

9-
#define PY_SSIZE_T_CLEAN
10-
#include <Python.h>
11-
129
#include <cstdint>
1310
#include <set>
1411
#include <string>

0 commit comments

Comments
 (0)