Skip to content

Commit

Permalink
Add a pybind11 type caster for SketchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
QuLogic committed Jan 6, 2024
1 parent 09ff6c2 commit 25fbfe7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "_path.h"

#include "_backend_agg_basic_types.h"
#include "py_adaptors.h"
#include "py_converters.h"
#include "py_converters_11.h"
Expand Down Expand Up @@ -302,14 +303,8 @@ Py_convert_path_to_polygons(mpl::PathIterator path, agg::trans_affine trans,
static py::tuple
Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nans,
agg::rect_d clip_rect, e_snap_mode snap_mode, double stroke_width,
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
std::optional<bool> simplify, bool return_curves, SketchParams sketch)
{
SketchParams sketch;

if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
throw py::error_already_set();
}

if (!simplify.has_value()) {
simplify = path.should_simplify();
}
Expand Down Expand Up @@ -367,18 +362,13 @@ postfix : bool
static py::object
Py_convert_to_string(mpl::PathIterator path, agg::trans_affine trans,
agg::rect_d cliprect, std::optional<bool> simplify,
py::object sketch_obj, int precision,
SketchParams sketch, int precision,
std::array<std::string, 5> codes_obj, bool postfix)
{
SketchParams sketch;
char *codes[5];
std::string buffer;
bool status;

if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
throw py::error_already_set();
}

for(auto i = 0; i < 5; ++i) {
codes[i] = const_cast<char *>(codes_obj[i].c_str());
}
Expand Down
20 changes: 20 additions & 0 deletions src/py_converters_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ namespace PYBIND11_NAMESPACE { namespace detail {
}
};
#endif

/* Remove all this macro magic after dropping NumPy usage and just include `_backend_agg_basic_types.h`. */
#ifdef MPL_BACKEND_AGG_BASIC_TYPES_H
template <> struct type_caster<SketchParams> {
public:
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));

bool load(handle src, bool) {
if (src.is_none()) {
value.scale = 0.0;
return true;
}

auto params = src.cast<std::tuple<double, double, double>>();
std::tie(value.scale, value.length, value.randomness) = params;

return true;
}
};
#endif
}} // namespace PYBIND11_NAMESPACE::detail

#endif /* MPL_PY_CONVERTERS_11_H */

0 comments on commit 25fbfe7

Please sign in to comment.