Skip to content

Commit cd0e236

Browse files
committed
Add a pybind11 type caster for SketchParams
1 parent 7a99055 commit cd0e236

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/_path_wrapper.cpp

Lines changed: 3 additions & 13 deletions
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"
@@ -296,14 +297,8 @@ Py_convert_path_to_polygons(mpl::PathIterator path, agg::trans_affine trans,
296297
static py::tuple
297298
Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nans,
298299
agg::rect_d clip_rect, e_snap_mode snap_mode, double stroke_width,
299-
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
300+
std::optional<bool> simplify, bool return_curves, SketchParams sketch)
300301
{
301-
SketchParams sketch;
302-
303-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
304-
throw py::error_already_set();
305-
}
306-
307302
if (!simplify.has_value()) {
308303
simplify = path.should_simplify();
309304
}
@@ -361,18 +356,13 @@ postfix : bool
361356
static py::object
362357
Py_convert_to_string(mpl::PathIterator path, agg::trans_affine trans,
363358
agg::rect_d cliprect, std::optional<bool> simplify,
364-
py::object sketch_obj, int precision,
359+
SketchParams sketch, int precision,
365360
std::array<std::string, 5> codes_obj, bool postfix)
366361
{
367-
SketchParams sketch;
368362
char *codes[5];
369363
std::string buffer;
370364
bool status;
371365

372-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
373-
throw py::error_already_set();
374-
}
375-
376366
for(auto i = 0; i < 5; ++i) {
377367
codes[i] = const_cast<char *>(codes_obj[i].c_str());
378368
}

src/py_converters_11.h

Lines changed: 20 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy