1313namespace py = pybind11;
1414using namespace pybind11 ::literals;
1515
16+ template <typename T>
17+ using double_or_ = std::variant<double , T>;
18+
19+ template <typename T>
20+ static T
21+ _double_to_ (const char *name, double_or_<T> &var)
22+ {
23+ if (auto value = std::get_if<double >(&var)) {
24+ auto api = py::module_::import (" matplotlib._api" );
25+ auto warn = api.attr (" warn_deprecated" );
26+ warn (" since" _a=" 3.10" , " name" _a=name, " obj_type" _a=" parameter as float" ,
27+ " alternative" _a=" int({})" _s.format (name));
28+ return static_cast <T>(*value);
29+ } else if (auto value = std::get_if<T>(&var)) {
30+ return *value;
31+ } else {
32+ // pybind11 will have only allowed types that match the variant, so this `else`
33+ // can't happen. We only have this case because older macOS doesn't support
34+ // `std::get` and using the conditional `std::get_if` means an `else` to silence
35+ // compiler warnings about "unhandled" cases.
36+ throw std::runtime_error (" Should not happen" );
37+ }
38+ }
39+
1640/* *********************************************************************
1741 * Enumerations
1842 * */
@@ -227,8 +251,15 @@ const char *PyFT2Image_draw_rect_filled__doc__ = R"""(
227251)""" ;
228252
229253static void
230- PyFT2Image_draw_rect_filled (FT2Image *self, double x0, double y0, double x1, double y1)
254+ PyFT2Image_draw_rect_filled (FT2Image *self,
255+ double_or_<long > vx0, double_or_<long > vy0,
256+ double_or_<long > vx1, double_or_<long > vy1)
231257{
258+ auto x0 = _double_to_<long >(" x0" , vx0);
259+ auto y0 = _double_to_<long >(" y0" , vy0);
260+ auto x1 = _double_to_<long >(" x1" , vx1);
261+ auto y1 = _double_to_<long >(" y1" , vy1);
262+
232263 self->draw_rect_filled (x0, y0, x1, y1);
233264}
234265
@@ -920,7 +951,7 @@ const char *PyFT2Font_draw_glyph_to_bitmap__doc__ = R"""(
920951 ----------
921952 image : FT2Image
922953 The image buffer on which to draw the glyph.
923- x, y : float
954+ x, y : int
924955 The pixel location at which to draw the glyph.
925956 glyph : Glyph
926957 The glyph to draw.
@@ -933,9 +964,13 @@ const char *PyFT2Font_draw_glyph_to_bitmap__doc__ = R"""(
933964)""" ;
934965
935966static void
936- PyFT2Font_draw_glyph_to_bitmap (PyFT2Font *self, FT2Image &image, double xd, double yd,
967+ PyFT2Font_draw_glyph_to_bitmap (PyFT2Font *self, FT2Image &image,
968+ double_or_<int > vxd, double_or_<int > vyd,
937969 PyGlyph *glyph, bool antialiased = true )
938970{
971+ auto xd = _double_to_<int >(" x" , vxd);
972+ auto yd = _double_to_<int >(" y" , vyd);
973+
939974 self->x ->draw_glyph_to_bitmap (image, xd, yd, glyph->glyphInd , antialiased);
940975}
941976
@@ -1625,7 +1660,14 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
16251660
16261661 py::class_<FT2Image>(m, " FT2Image" , py::is_final (), py::buffer_protocol (),
16271662 PyFT2Image__doc__)
1628- .def (py::init<double , double >(), " width" _a, " height" _a, PyFT2Image_init__doc__)
1663+ .def (py::init (
1664+ [](double_or_<long > width, double_or_<long > height) {
1665+ return new FT2Image (
1666+ _double_to_<long >(" width" , width),
1667+ _double_to_<long >(" height" , height)
1668+ );
1669+ }),
1670+ " width" _a, " height" _a, PyFT2Image_init__doc__)
16291671 .def (" draw_rect_filled" , &PyFT2Image_draw_rect_filled,
16301672 " x0" _a, " y0" _a, " x1" _a, " y1" _a,
16311673 PyFT2Image_draw_rect_filled__doc__)
0 commit comments