13
13
namespace py = pybind11;
14
14
using namespace pybind11 ::literals;
15
15
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
+
16
40
/* *********************************************************************
17
41
* Enumerations
18
42
* */
@@ -227,8 +251,15 @@ const char *PyFT2Image_draw_rect_filled__doc__ = R"""(
227
251
)""" ;
228
252
229
253
static 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)
231
257
{
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
+
232
263
self->draw_rect_filled (x0, y0 , x1, y1 );
233
264
}
234
265
@@ -920,7 +951,7 @@ const char *PyFT2Font_draw_glyph_to_bitmap__doc__ = R"""(
920
951
----------
921
952
image : FT2Image
922
953
The image buffer on which to draw the glyph.
923
- x, y : float
954
+ x, y : int
924
955
The pixel location at which to draw the glyph.
925
956
glyph : Glyph
926
957
The glyph to draw.
@@ -933,9 +964,13 @@ const char *PyFT2Font_draw_glyph_to_bitmap__doc__ = R"""(
933
964
)""" ;
934
965
935
966
static 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,
937
969
PyGlyph *glyph, bool antialiased = true )
938
970
{
971
+ auto xd = _double_to_<int >(" x" , vxd);
972
+ auto yd = _double_to_<int >(" y" , vyd);
973
+
939
974
self->x ->draw_glyph_to_bitmap (image, xd, yd, glyph->glyphInd , antialiased);
940
975
}
941
976
@@ -1625,7 +1660,14 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
1625
1660
1626
1661
py::class_<FT2Image>(m, " FT2Image" , py::is_final (), py::buffer_protocol (),
1627
1662
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__)
1629
1671
.def (" draw_rect_filled" , &PyFT2Image_draw_rect_filled,
1630
1672
" x0" _a, " y0" _a, " x1" _a, " y1" _a,
1631
1673
PyFT2Image_draw_rect_filled__doc__)
0 commit comments