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

Skip to content

Commit 13aa334

Browse files
committed
Add a pybind11 type caster for SketchParams
1 parent f2bfbf5 commit 13aa334

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/_path_wrapper.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,8 @@ Py_convert_path_to_polygons(mpl::PathIterator path, agg::trans_affine trans,
302302
static py::tuple
303303
Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nans,
304304
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)
305+
std::optional<bool> simplify, bool return_curves, SketchParams sketch)
306306
{
307-
SketchParams sketch;
308-
309-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
310-
throw py::error_already_set();
311-
}
312-
313307
if (!simplify.has_value()) {
314308
simplify = path.should_simplify();
315309
}
@@ -367,18 +361,13 @@ postfix : bool
367361
static py::object
368362
Py_convert_to_string(mpl::PathIterator path, agg::trans_affine trans,
369363
agg::rect_d cliprect, std::optional<bool> simplify,
370-
py::object sketch_obj, int precision,
364+
SketchParams sketch, int precision,
371365
std::array<std::string, 5> codes_obj, bool postfix)
372366
{
373-
SketchParams sketch;
374367
char *codes[5];
375368
std::string buffer;
376369
bool status;
377370

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

src/py_converters_11.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace py = pybind11;
1818
#define NO_IMPORT_ARRAY
1919
#endif
2020
#include "py_adaptors.h"
21+
#include "_backend_agg_basic_types.h"
2122
#ifdef KEEP_NO_IMPORT_ARRAY
2223
#undef _KEEP_NO_IMPORT_ARRAY
2324
#else
@@ -136,6 +137,25 @@ namespace PYBIND11_NAMESPACE { namespace detail {
136137
return true;
137138
}
138139
};
140+
141+
template <> struct type_caster<SketchParams> {
142+
public:
143+
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));
144+
145+
bool load(handle src, bool) {
146+
if (src.is_none()) {
147+
value.scale = 0.0;
148+
return true;
149+
}
150+
151+
auto params = src.cast<std::tuple<double, double, double>>();
152+
value.scale = std::get<0>(params);
153+
value.length = std::get<1>(params);
154+
value.randomness = std::get<2>(params);
155+
156+
return true;
157+
}
158+
};
139159
}} // namespace PYBIND11_NAMESPACE::detail
140160

141161
#endif /* MPL_PY_CONVERTERS_11_H */

0 commit comments

Comments
 (0)