1 /*
2 * Python bindings for lib2geom
3 *
4 * Copyright 2007 Aaron Spike <aaron@ekips.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 */
30
31 #include <boost/python.hpp>
32 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
33
34 #include "2geom/curve.h"
35 #include "2geom/bezier-curve.h"
36 #include "2geom/path.h"
37 #include "2geom/pathvector.h"
38 #include "2geom/sbasis-to-bezier.h"
39 #include "helpers.h"
40
41 #include "2geom/point.h"
42 #include "2geom/rect.h"
43 #include "2geom/d2.h"
44
45 using namespace boost::python;
46
path_getitem(Geom::Path const & p,int index)47 Geom::Curve const &path_getitem(Geom::Path const& p, int index)
48 {
49 unsigned size = p.size_default();
50 unsigned i = index;
51 if (index < 0)
52 {
53 i = index = size + index;
54 }
55 if ((index < 0) || (i > (size - 1))) {
56 PyErr_SetString(PyExc_IndexError, "index out of range");
57 boost::python::throw_error_already_set();
58 }
59 return p[i];
60 }
61
62 struct CurveWrap : Geom::Curve, wrapper<Geom::Curve>
63 {
initialPointCurveWrap64 Geom::Point initialPoint() const {return this->get_override("initialPoint")();}
finalPointCurveWrap65 Geom::Point finalPoint() const {return this->get_override("finalPoint")();}
isDegenerateCurveWrap66 bool isDegenerate() const {return this->get_override("isDegenerate")();}
duplicateCurveWrap67 CurveWrap *duplicate() const {return this->get_override("duplicate")();}
boundsFastCurveWrap68 Geom::Rect boundsFast() const {return this->get_override("boundsFast")();}
boundsExactCurveWrap69 Geom::Rect boundsExact() const {return this->get_override("boundsExact")();}
boundsLocalCurveWrap70 virtual Geom::OptRect boundsLocal(Geom::OptInterval const &i, unsigned deg) const {return this->get_override("boundsLocal")(i,deg);}
rootsCurveWrap71 std::vector<double> roots(double v, Geom::Dim2 d) const {return this->get_override("roots")(v,d);}
72
windingCurveWrap73 int winding(Geom::Point const &p) const {
74 if (override f = this->get_override("winding")) {
75 return f(p);
76 }
77 return Geom::Curve::winding(p);
78 }
default_windingCurveWrap79 int default_winding(Geom::Point p) const { return this->Geom::Curve::winding(p); }
80
portionCurveWrap81 Geom::Curve *portion(double f, double t) const { return this->get_override("portion")(f,t); }
reverseCurveWrap82 Geom::Curve *reverse() const {
83 if (override f = this->get_override("reverse")) {
84 return f();
85 }
86 return Geom::Curve::reverse();
87 }
default_reverseCurveWrap88 Geom::Curve *default_reverse() const { return this->Geom::Curve::reverse(); }
89
derivativeCurveWrap90 Geom::Curve *derivative() const { return this->get_override("derivative")(); }
91
92
transformedCurveWrap93 Geom::Curve *transformed(Geom::Affine const &m) const { return this->get_override("transformed")(m); }
94
pointAtCurveWrap95 Geom::Point pointAt(Geom::Coord t) const {
96 if (override f = this->get_override("pointAt")) {
97 return f(t);
98 }
99 return Geom::Curve::pointAt(t);
100 }
default_pointAtCurveWrap101 Geom::Point default_pointAt(Geom::Coord t) { return this->Geom::Curve::pointAt(t); }
pointAndDerivativesCurveWrap102 std::vector<Geom::Point> pointAndDerivatives(Geom::Coord t, unsigned n) const {
103 return this->get_override("pointAndDerivatives")(t, n);
104 }
105
toSBasisCurveWrap106 Geom::D2<Geom::SBasis> toSBasis() const {return this->get_override("sbasis")();}
107 };
108
109
110 /* pycairo stuff: */
111 #ifdef HAVE_PYCAIRO
112
113 #include "cairo-helpers.h"
114
py_cairo_curve(object cr,Geom::Curve const & c)115 void py_cairo_curve(object cr, Geom::Curve const &c) {
116 cairo_curve(cairo_t_from_object(cr), c);
117 }
py_cairo_rectangle(object cr,Geom::Rect const & r)118 void py_cairo_rectangle(object cr, Geom::Rect const &r) {
119 cairo_rectangle(cairo_t_from_object(cr), r);
120 }
121
py_cairo_convex_hull(object cr,Geom::ConvexHull const & r)122 void py_cairo_convex_hull(object cr, Geom::ConvexHull const &r) {
123 cairo_convex_hull(cairo_t_from_object(cr), r);
124 }
125 /*void py_cairo_path(object cr, Geom::Path const &p) {
126 cairo_path(cairo_t_from_object(cr), p);
127 }*/
128
py_cairo_path(object cr,Geom::Path const & p)129 void py_cairo_path(object cr, Geom::Path const &p) {
130 cairo_path(cairo_t_from_object(cr), p);
131 }
132
py_cairo_path(object cr,Geom::PathVector const & p)133 void py_cairo_path(object cr, Geom::PathVector const &p) {
134 cairo_path(cairo_t_from_object(cr), p);
135 }
py_cairo_path_stitches(object cr,Geom::Path const & p)136 void py_cairo_path_stitches(object cr, Geom::Path const &p) {
137 cairo_path_stitches(cairo_t_from_object(cr), p);
138 }
py_cairo_path_stitches(object cr,Geom::PathVector const & p)139 void py_cairo_path_stitches(object cr, Geom::PathVector const &p) {
140 cairo_path_stitches(cairo_t_from_object(cr), p);
141 }
142 void (*cp_1)(object, Geom::Path const &) = &py_cairo_path;
143 void (*cp_2)(object, Geom::PathVector const &) = &py_cairo_path;
144
145 void (*cps_1)(object, Geom::Path const &) = &py_cairo_path_stitches;
146 void (*cps_2)(object, Geom::PathVector const &) = &py_cairo_path_stitches;
147
148
py_cairo_d2_sb(object cr,Geom::D2<Geom::SBasis> const & p)149 void py_cairo_d2_sb(object cr, Geom::D2<Geom::SBasis> const &p) {
150 cairo_d2_sb(cairo_t_from_object(cr), p);
151 }
152
py_cairo_d2_pw_sb(object cr,Geom::D2<Geom::Piecewise<Geom::SBasis>> const & p)153 void py_cairo_d2_pw_sb(object cr, Geom::D2<Geom::Piecewise<Geom::SBasis> > const &p) {
154 cairo_d2_pw_sb(cairo_t_from_object(cr), p);
155 }
156
py_cairo_pw_d2_sb(object cr,Geom::Piecewise<Geom::D2<Geom::SBasis>> const & p)157 void py_cairo_pw_d2_sb(object cr, Geom::Piecewise<Geom::D2<Geom::SBasis> > const &p) {
158 cairo_pw_d2_sb(cairo_t_from_object(cr), p);
159 }
160
161 #endif // HAVE_PYCAIRO
162
163 Geom::Point (Geom::Path::*path_pointAt_time)(Geom::Coord) const = &Geom::Path::pointAt;
164 Geom::Coord (Geom::Path::*path_valueAt_time)(Geom::Coord, Geom::Dim2) const = &Geom::Path::valueAt;
165 void (Geom::Path::*appendPortionTo_time)(Geom::Path &, Geom::Coord, Geom::Coord) const = &Geom::Path::appendPortionTo;
166 //void (Geom::Path::*appendPortionTo_pos)(Geom::Path &, Geom::PathPosition const &, Geom::PathPosition const &, bool) const = &Geom::Path::appendPortionTo;
167
wrap_path()168 void wrap_path()
169 {
170 /* class_<CurveWrap, boost::noncopyable>("Curve")
171 .def("initalPoint", pure_virtual(&Geom::Curve::initialPoint))
172 .def("finalPoint", pure_virtual(&Geom::Curve::finalPoint))
173 .def("duplicate", pure_virtual(&Geom::Curve::duplicate), return_value_policy<manage_new_object>())
174 .def("boundsFast", pure_virtual(&Geom::Curve::boundsFast))
175 .def("boundsExact", pure_virtual(&Geom::Curve::boundsExact))
176 //.def("pointAt", &Geom::Curve::pointAt, &CurveWrap::default_pointAt)
177 //.def("winding", &Geom::Curve::winding, &CurveWrap::default_winding)
178 .def("pointAndDerivatives", pure_virtual(&Geom::Curve::pointAndDerivatives))
179 .def("toSBasis", pure_virtual(&Geom::Curve::toSBasis))
180 ;*/
181 /* class_<Geom::LineSegment, bases<CurveWrap> >("LineSegment")
182 .def("points", &Geom::LineSegment::points)
183 ;
184 class_<Geom::QuadraticBezier, bases<CurveWrap> >("QuadraticBezier")
185 .def("points", &Geom::QuadraticBezier::points)
186 ;
187 class_<Geom::CubicBezier, bases<CurveWrap> >("CubicBezier")
188 .def("points", &Geom::CubicBezier::points)
189 ;*/
190 class_<Geom::Path>("Path")
191 .def("__getitem__", path_getitem, return_value_policy<copy_const_reference>()) //or return_internal_reference see http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#question1
192 .def("empty", &Geom::Path::empty)
193 .def("closed", &Geom::Path::closed)
194 .def("close", &Geom::Path::close)
195 .def("boundsFast", &Geom::Path::boundsFast)
196 .def("boundsExact", &Geom::Path::boundsExact)
197 .def("toPwSb", &Geom::Path::toPwSb)
198 .def(self * Geom::Affine())
199 .def(self *= Geom::Affine())
200 .def("pointAt", path_pointAt_time)
201 .def("valueAt", path_valueAt_time)
202 .def("__call__", path_pointAt_time)
203 .def("roots", &Geom::Path::roots)
204 //.def("allNearestTimes", &Geom::Path::allNearestTimes)
205 //.def("nearestTime", &Geom::Path::nearestTime)
206 .def("appendPortionTo", appendPortionTo_time)
207 //.def("portion", &Geom::Path::portion)
208 .def("reversed", &Geom::Path::reversed)
209 //.def("insert", &Geom::Path::insert)
210 .def("clear", &Geom::Path::clear)
211 //.def("erase", &Geom::Path::erase)
212 .def("erase_last", &Geom::Path::erase_last)
213 //.def("replace", &Geom::Path::replace)
214 .def("start", &Geom::Path::start)
215 .def("initialPoint", &Geom::Path::initialPoint)
216 .def("finalPoint", &Geom::Path::finalPoint)
217 //.def("append", &Geom::Path::append)
218 //.def("appendNew", &Geom::Path::appendNew)
219 ;
220 def("paths_to_pw",Geom::paths_to_pw);
221 class_<Geom::PathVector >("PathVector")
222 .def(vector_indexing_suite<Geom::PathVector >())
223 .def(self * Geom::Affine())
224 .def(self *= Geom::Affine())
225 .def("reversed", &Geom::PathVector::reversed)
226 .def("reverse", &Geom::PathVector::reverse)
227 .def("boundsFast", &Geom::PathVector::boundsFast)
228 .def("boundsExact", &Geom::PathVector::boundsExact)
229 ;
230 def("path_from_piecewise", Geom::path_from_piecewise);
231 def("path_from_sbasis", Geom::path_from_sbasis);
232 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
233
234 #ifdef HAVE_PYCAIRO
235 void cairo_move_to(cairo_t *cr, Geom::Point p1);
236 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
237 void cairo_line_to(cairo_t *cr, Geom::Point p1);
238 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
239 void cairo_curve_to(cairo_t *cr, Geom::Point p1, Geom::Point p2, Geom::Point p3);
240 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
241
242 //def("cairo_curve", cairo_curve);
243 def("cairo_convex_hull", py_cairo_convex_hull);
244 def("cairo_path", cp_1);
245 def("cairo_path", cp_2);
246 def("cairo_path_stitches", cps_1);
247 def("cairo_path_stitches", cps_2);
248
249 def("cairo_d2_sb", py_cairo_d2_sb);
250 def("cairo_d2_pw_sb", py_cairo_d2_pw_sb);
251 def("cairo_pw_d2_sb", py_cairo_pw_d2_sb);
252 #endif // HAVE_PYCAIRO
253 }
254
255 /*
256 Local Variables:
257 mode:c++
258 c-file-style:"stroustrup"
259 c-file-offsets:((innamespace . 0)(substatement-open . 0))
260 indent-tabs-mode:nil
261 c-brace-offset:0
262 fill-column:99
263 End:
264 vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
265 */
266