8000 Deprecate passing floats to FT2Image · matplotlib/matplotlib@3974323 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3974323

Browse files
committed
Deprecate passing floats to FT2Image
These were silently truncated to int anyway, so we should make the types explicit.
1 parent 1078c87 commit 3974323

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

doc/api/next_api_changes/deprecations/288XX-ES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ Passing floating-point values to ``RendererAgg.draw_text_image``
33

44
Any floating-point values passed to the *x* and *y* parameters were truncated to integers
55
silently. This behaviour is now deprecated, and only `int` values should be used.
6+
7+
Passing floating-point values to ``FT2Image``
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
10+
Any floating-point values passed to the `.FT2Image` constructor, or the *x0*, *y0*, *x1*,
11+
and *y1* parameters of `.FT2Image.draw_rect_filled` were truncated to integers silently.
12+
This behaviour is now deprecated, and only `int` values should be used.

lib/matplotlib/_mathtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def to_raster(self, *, antialiased: bool) -> RasterParse:
153153
w = xmax - xmin
154154
h = ymax - ymin - self.box.depth
155155
d = ymax - ymin - self.box.height
156-
image = FT2Image(np.ceil(w), np.ceil(h + max(d, 0)))
156+
image = FT2Image(int(np.ceil(w)), int(np.ceil(h + max(d, 0))))
157157

158158
# Ideally, we could just use self.glyphs and self.rects here, shifting
159159
# their coordinates by (-xmin, -ymin), but this yields slightly
@@ -172,7 +172,7 @@ def to_raster(self, *, antialiased: bool) -> RasterParse:
172172
y = int(center - (height + 1) / 2)
173173
else:
174174
y = int(y1)
175-
image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height)
175+
image.draw_rect_filled(int(x1), y, int(np.ceil(x2)), y + height)
176176
return RasterParse(0, 0, w, h + d, d, image)
177177

178178

lib/matplotlib/ft2font.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ class FT2Font(Buffer):
281281

282282
@final
283283
class FT2Image(Buffer):
284-
def __init__(self, width: float, height: float) -> None: ...
285-
def draw_rect_filled(self, x0: float, y0: float, x1: float, y1: float) -> None: ...
284+
def __init__(self, width: int, height: int) -> None: ...
285+
def draw_rect_filled(self, x0: int, y0: int, x1: int, y1: int) -> None: ...
286286
if sys.version_info[:2] >= (3, 12):
287287
def __buffer__(self, flags: int) -> memoryview: ...
288288

src/ft2font_wrapper.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ P11X_DECLARE_ENUM(
206206
* FT2Image
207207
* */
208208

209+
using double_or_long = std::variant<double, long>;
210+
209211
const char *PyFT2Image__doc__ = R"""(
210212
An image buffer for drawing glyphs.
211213
)""";
@@ -226,9 +228,32 @@ const char *PyFT2Image_draw_rect_filled__doc__ = R"""(
226228
The bounds of the rectangle from (x0, y0) to (x1, y1).
227229
)""";
228230

231+
static long
232+
_double_to_long(const char *name, double_or_long &var)
233+
{
234+
if (auto value = std::get_if<double>(&var)) {
235+
auto api = py::module_::import("matplotlib._api");
236+
auto warn = api.attr("warn_deprecated");
237+
warn("since"_a="3.10", "name"_a=name, "obj_type"_a="parameter as float",
238+
"alternative"_a="int({})"_s.format(name));
239+
return static_cast<long>(*value);
240+
} else if (auto value = std::get_if<long>(&var)) {
241+
return *value;
242+
} else {
243+
throw std::runtime_error("Should not happen");
244+
}
245+
}
246+
229247
static void
230-
PyFT2Image_draw_rect_filled(FT2Image *self, double x0, double y0, double x1, double y1)
248+
PyFT2Image_draw_rect_filled(FT2Image *self,
249+
double_or_long vx0, double_or_long vy0,
250+
double_or_long vx1, double_or_long vy1)
231251
{
252+
auto x0 = _double_to_long("x0", vx0);
253+
auto y0 = _double_to_long("y0", vy0);
254+
auto x1 = _double_to_long("x1", vx1);
255+
auto y1 = _double_to_long("y1", vy1);
256+
232257
self->draw_rect_filled(x0, y0, x1, y1);
233258
}
234259

@@ -1625,7 +1650,14 @@ PYBIND11_MODULE(ft2font, m)
16251650

16261651
py::class_<FT2Image>(m, "FT2Image", py::is_final(), py::buffer_protocol(),
16271652
PyFT2Image__doc__)
1628-
.def(py::init<double, double>(), "width"_a, "height"_a, PyFT2Image_init__doc__)
1653+
.def(py::init(
1654+
[](double_or_long width, double_or_long height) {
1655+
return new FT2Image(
1656+
_double_to_long("width", width),
1657+
_double_to_long("height", height)
1658+
);
1659+
}),
1660+
"width"_a, "height"_a, PyFT2Image_init__doc__)
16291661
.def("draw_rect_filled", &PyFT2Image_draw_rect_filled,
16301662
"x0"_a, "y0"_a, "x1"_a, "y1"_a,
16311663
PyFT2Image_draw_rect_filled__doc__)

0 commit comments

Comments
 (0)
0