Skip to content

Commit 25fbfe7

Browse files
committed
Add a pybind11 type caster for SketchParams
1 parent 09ff6c2 commit 25fbfe7

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/_path_wrapper.cpp

+3-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "_path.h"
1313

14+
#include "_backend_agg_basic_types.h"
1415
#include "py_adaptors.h"
1516
#include "py_converters.h"
1617
#include "py_converters_11.h"
@@ -302,14 +303,8 @@ Py_convert_path_to_polygons(mpl::PathIterator path, agg::trans_affine trans,
302303
static py::tuple
303304
Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nans,
304305
agg::rect_d clip_rect, e_snap_mode snap_mode, double stroke_width,
305-
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
306+
std::optional<bool> simplify, bool return_curves, SketchParams sketch)
306307
{
307-
SketchParams sketch;
308-
309-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
310-
throw py::error_already_set();
311-
}
312-
313308
if (!simplify.has_value()) {
314309
simplify = path.should_simplify();
315310
}
@@ -367,18 +362,13 @@ postfix : bool
367362
static py::object
368363
Py_convert_to_string(mpl::PathIterator path, agg::trans_affine trans,
369364
agg::rect_d cliprect, std::optional<bool> simplify,
370-
py::object sketch_obj, int precision,
365+
SketchParams sketch, int precision,
371366
std::array<std::string, 5> codes_obj, bool postfix)
372367
{
373-
SketchParams sketch;
374368
char *codes[5];
375369
std::string buffer;
376370
bool status;
377371

378-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
379-
throw py::error_already_set();
380-
}
381-
382372
for(auto i = 0; i < 5; ++i) {
383373
codes[i] = const_cast<char *>(codes_obj[i].c_str());
384374
}

src/py_converters_11.h

+20
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ namespace PYBIND11_NAMESPACE { namespace detail {
127127
}
128128
};
129129
#endif
130+
131+
/* Remove all this macro magic after dropping NumPy usage and just include `_backend_agg_basic_types.h`. */
132+
#ifdef MPL_BACKEND_AGG_BASIC_TYPES_H
133+
template <> struct type_caster<SketchParams> {
134+
public:
135+
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));
136+
137+
bool load(handle src, bool) {
138+
if (src.is_none()) {
139+
value.scale = 0.0;
140+
return true;
141+
}
142+
143+
auto params = src.cast<std::tuple<double, double, double>>();
144+
std::tie(value.scale, value.length, value.randomness) = params;
145+
146+
return true;
147+
}
148+
};
149+
#endif
130150
}} // namespace PYBIND11_NAMESPACE::detail
131151

132152
#endif /* MPL_PY_CONVERTERS_11_H */

0 commit comments

Comments
 (0)