diff --git a/src/display/curve.cpp b/src/display/curve.cpp
index bcf5e92a01ac2004dcfd7d5d275623390a86625d..c229ec28a533ebc5b9b2ff28c7a4b366d50bead3 100644 (file)
--- a/src/display/curve.cpp
+++ b/src/display/curve.cpp
#define __CURVE_C__
/** \file
- * Routines for SPCurve and for NArtBpath arrays in general.
+ * Routines for SPCurve and for NArtBpath arrays / Geom::PathVector in general.
*/
/*
- * Author:
+ * Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
+ * Johan Engelen
*
* Copyright (C) 2000 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
* Copyright (C) 2002 Lauris Kaplinski
+ * Copyright (C) 2008 Johan Engelen
*
* Released under GNU GPL
*/
+#include "display/curve.h"
+#include <string.h>
#include <glib/gmem.h>
-#include <display/curve.h>
+#include "libnr/nr-point.h"
+#include "libnr/nr-rect.h"
#include <libnr/n-art-bpath.h>
#include <libnr/nr-point-matrix-ops.h>
#include <libnr/nr-translate-ops.h>
+#include <libnr/n-art-bpath-2geom.h>
+#include <libnr/nr-convert2geom.h>
+#include <cstring>
+#include <string>
+#include <2geom/pathvector.h>
+#include <2geom/sbasis-geometric.h>
+#include <2geom/sbasis-to-bezier.h>
+#include "svg/svg.h"
+#include <2geom/point.h>
-#define SP_CURVE_LENSTEP 32
-
-static bool sp_bpath_good(NArtBpath const bpath[]);
-static NArtBpath *sp_bpath_clean(NArtBpath const bpath[]);
-static NArtBpath const *sp_bpath_check_subpath(NArtBpath const bpath[]);
static unsigned sp_bpath_length(NArtBpath const bpath[]);
static bool sp_bpath_closed(NArtBpath const bpath[]);
/* Constructors */
/**
- * The returned curve's state is as if sp_curve_reset has just been called on it.
+ * The returned curve's state is as if SPCurve::reset has just been called on it.
+ * \param length Initial number of NArtBpath elements allocated for bpath (including NR_END
+ * element).
+ * 2GEOMproof
*/
-SPCurve *
-sp_curve_new()
+SPCurve::SPCurve(guint length)
+ : _refcount(1),
+ _bpath(NULL),
+ _pathv(),
+ _end(0),
+ _length(length),
+ _substart(0),
+ _hascpt(false),
+ _posSet(false),
+ _moving(false),
+ _closed(false)
{
- return sp_curve_new_sized(SP_CURVE_LENSTEP);
+ if (length <= 0) {
+ g_error("SPCurve::SPCurve called with invalid length parameter");
+ throw;
+ }
+
+ _bpath = g_new(NArtBpath, length);
+ _bpath->code = NR_END;
+
+ _pathv.clear();
}
-/**
- * Like sp_curve_new, but overriding the default initial capacity.
- *
- * The returned curve's state is as if sp_curve_reset has just been called on it.
- *
- * \param length Initial number of NArtBpath elements allocated for bpath (including NR_END
- * element).
- */
+SPCurve::SPCurve(Geom::PathVector const& pathv)
+ : _refcount(1),
+ _bpath(NULL),
+ _pathv(pathv),
+ _end(0),
+ _length(0),
+ _substart(0),
+ _hascpt(false),
+ _posSet(false),
+ _moving(false),
+ _closed(false)
+{
+ // temporary code to convert to _bpath as well:
+ _bpath = BPath_from_2GeomPath(_pathv);
+ unsigned const len = sp_bpath_length(_bpath);
+ _length = len;
+ _end = _length - 1;
+ gint i = _end;
+ for (; i > 0; i--)
+ if ((_bpath[i].code == NR_MOVETO) ||
+ (_bpath[i].code == NR_MOVETO_OPEN))
+ break;
+ _substart = i;
+ _closed = sp_bpath_closed(_bpath);
+}
+
+// * 2GEOMproof
SPCurve *
-sp_curve_new_sized(gint length)
+SPCurve::new_from_foreign_bpath(NArtBpath const *bpath)
{
- g_return_val_if_fail(length > 0, NULL);
+ g_return_val_if_fail(bpath != NULL, NULL);
+
+ NArtBpath *new_bpath;
+ unsigned const len = sp_bpath_length(bpath);
+ new_bpath = g_new(NArtBpath, len);
+ memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
+
+ SPCurve *curve = new SPCurve();
- SPCurve *curve = g_new(SPCurve, 1);
+ curve->_bpath = new_bpath;
+ curve->_length = len;
+ curve->_end = curve->_length - 1;
+ gint i = curve->_end;
+ for (; i > 0; i--)
+ if ((curve->_bpath[i].code == NR_MOVETO) ||
+ (curve->_bpath[i].code == NR_MOVETO_OPEN))
+ break;
+ curve->_substart = i;
+ curve->_closed = sp_bpath_closed(new_bpath);
- curve->refcount = 1;
- curve->_bpath = g_new(NArtBpath, length);
- curve->_bpath->code = NR_END;
- curve->end = 0;
- curve->length = length;
- curve->substart = 0;
- curve->hascpt = false;
- curve->posSet = false;
- curve->moving = false;
- curve->closed = false;
+ curve->_pathv = BPath_to_2GeomPath(curve->_bpath);
return curve;
}
* Convert NArtBpath object to SPCurve object.
*
* \return new SPCurve, or NULL if the curve was not created for some reason.
+ * 2GEOMproof
*/
SPCurve *
-sp_curve_new_from_bpath(NArtBpath *bpath)
+SPCurve::new_from_bpath(NArtBpath *bpath)
{
g_return_val_if_fail(bpath != NULL, NULL);
- SPCurve *curve = sp_curve_new_from_foreign_bpath(bpath);
+ SPCurve *curve = SPCurve::new_from_foreign_bpath(bpath);
g_free(bpath);
+
return curve;
}
-/**
- * Convert const NArtBpath array to SPCurve.
- *
- * \return new SPCurve, or NULL if the curve was not created for some reason.
- */
-SPCurve *sp_curve_new_from_foreign_bpath(NArtBpath const bpath[])
+// * 2GEOMproof
+SPCurve *
+SPCurve::new_from_rect(Geom::Rect const &rect)
{
- g_return_val_if_fail(bpath != NULL, NULL);
+ SPCurve *c = new SPCurve();
- NArtBpath *new_bpath;
- if (!sp_bpath_good(bpath)) {
- new_bpath = sp_bpath_clean(bpath);
- g_return_val_if_fail(new_bpath != NULL, NULL);
- } else {
- unsigned const len = sp_bpath_length(bpath);
- new_bpath = g_new(NArtBpath, len);
- memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
+ NR::Point p = rect.corner(0);
+ c->moveto(p);
+
+ for (int i=3; i>=0; i--) {
+ c->lineto(rect.corner(i));
}
+ c->closepath_current();
- SPCurve *curve = g_new(SPCurve, 1);
+ return c;
+}
- curve->refcount = 1;
- curve->_bpath = new_bpath;
- curve->length = sp_bpath_length(new_bpath);
- curve->end = curve->length - 1;
- gint i = curve->end;
+// * 2GEOMproof
+SPCurve::~SPCurve()
+{
+ if (_bpath) {
+ g_free(_bpath);
+ _bpath = NULL;
+ }
+}
+
+/* Methods */
+
+void
+SPCurve::set_pathvector(Geom::PathVector const & new_pathv)
+{
+ _pathv = new_pathv;
+
+ _hascpt = false;
+ _posSet = false;
+ _moving = false;
+
+ // temporary code to convert to _bpath as well:
+ if (_bpath) {
+ g_free(_bpath);
+ _bpath = NULL;
+ }
+ _bpath = BPath_from_2GeomPath(_pathv);
+ unsigned const len = sp_bpath_length(_bpath);
+ _length = len;
+ _end = _length - 1;
+ gint i = _end;
for (; i > 0; i--)
- if ((curve->_bpath[i].code == NR_MOVETO) ||
- (curve->_bpath[i].code == NR_MOVETO_OPEN))
+ if ((_bpath[i].code == NR_MOVETO) ||
+ (_bpath[i].code == NR_MOVETO_OPEN))
break;
- curve->substart = i;
- curve->hascpt = false;
- curve->posSet = false;
- curve->moving = false;
- curve->closed = sp_bpath_closed(new_bpath);
+ _substart = i;
+ _closed = sp_bpath_closed(_bpath);
+}
- return curve;
+/**
+ * Get pointer to bpath data. Don't keep this reference too long, because the path might change by another function.
+ */
+NArtBpath const *
+SPCurve::get_bpath() const
+{
+ return _bpath;
+};
+
+Geom::PathVector const &
+SPCurve::get_pathvector() const
+{
+ return _pathv;
}
-SPCurve *sp_curve_new_from_rect(NR::Maybe<NR::Rect> const &rect)
+/**
+ *Returns index in bpath[] of NR_END element.
+ * remove for 2geom
+ */
+guint
+SPCurve::get_length() const
{
- g_return_val_if_fail(rect, NULL);
+// g_message("SPCurve::get_length must be removed");
- SPCurve *c = sp_curve_new();
+ return _end;
+}
- NR::Point p = rect->corner(0);
- sp_curve_moveto(c, p);
+/*
+ * Returns the number of segments of all paths summed
+ */
+guint
+SPCurve::get_segment_count() const
+{
+ guint nr = 0;
+ for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
+ nr += (*it).size();
- for (int i=3; i>=0; i--) {
- sp_curve_lineto(c, rect->corner(i));
+ if (it->closed()) nr += 1;
}
- sp_curve_closepath_current(c);
-
- return c;
+ return nr;
}
/**
- * Increase refcount of curve.
+ * Increase _refcount of curve.
*
* \todo should this be shared with other refcounting code?
*/
SPCurve *
-sp_curve_ref(SPCurve *curve)
+SPCurve::ref()
{
- g_return_val_if_fail(curve != NULL, NULL);
+ _refcount += 1;
- curve->refcount += 1;
-
- return curve;
+ return this;
}
/**
* \todo should this be shared with other refcounting code?
*/
SPCurve *
-sp_curve_unref(SPCurve *curve)
+SPCurve::unref()
{
- g_return_val_if_fail(curve != NULL, NULL);
-
- curve->refcount -= 1;
+ _refcount -= 1;
- if (curve->refcount < 1) {
- if (curve->_bpath) {
- g_free(curve->_bpath);
- }
- g_free(curve);
+ if (_refcount < 1) {
+ delete this;
}
return NULL;
/**
* Add space for more paths in curve.
+ * This function has no meaning for 2geom representation, other than maybe for optimization issues (enlargening the vector for what is to come)
+ * 2GEOMproof
*/
-static void
-sp_curve_ensure_space(SPCurve *curve, gint space)
+void
+SPCurve::ensure_space(guint space)
{
- g_return_if_fail(curve != NULL);
+ g_return_if_fail(this != NULL);
g_return_if_fail(space > 0);
- if (curve->end + space < curve->length)
+ if (_end + space < _length)
return;
if (space < SP_CURVE_LENSTEP)
space = SP_CURVE_LENSTEP;
- curve->_bpath = g_renew(NArtBpath, curve->_bpath, curve->length + space);
+ _bpath = g_renew(NArtBpath, _bpath, _length + space);
- curve->length += space;
+ _length += space;
}
/**
* Create new curve from its own bpath array.
+ * 2GEOMproof
*/
SPCurve *
-sp_curve_copy(SPCurve *curve)
+SPCurve::copy() const
{
- g_return_val_if_fail(curve != NULL, NULL);
-
- return sp_curve_new_from_foreign_bpath(curve->_bpath);
+ return SPCurve::new_from_foreign_bpath(_bpath);
}
/**
* Return new curve that is the concatenation of all curves in list.
+ * 2GEOMified
*/
SPCurve *
-sp_curve_concat(GSList const *list)
+SPCurve::concat(GSList const *list)
{
- g_return_val_if_fail(list != NULL, NULL);
-
gint length = 0;
for (GSList const *l = list; l != NULL; l = l->next) {
SPCurve *c = (SPCurve *) l->data;
- length += c->end;
+ length += c->_end;
}
- SPCurve *new_curve = sp_curve_new_sized(length + 1);
+ SPCurve *new_curve = new SPCurve(length + 1);
NArtBpath *bp = new_curve->_bpath;
for (GSList const *l = list; l != NULL; l = l->next) {
SPCurve *c = (SPCurve *) l->data;
- memcpy(bp, c->_bpath, c->end * sizeof(NArtBpath));
- bp += c->end;
+ memcpy(bp, c->_bpath, c->_end * sizeof(NArtBpath));
+ bp += c->_end;
}
bp->code = NR_END;
- new_curve->end = length;
+ new_curve->_end = length;
gint i;
- for (i = new_curve->end; i > 0; i--) {
+ for (i = new_curve->_end; i > 0; i--) {
if ((new_curve->_bpath[i].code == NR_MOVETO) ||
(new_curve->_bpath[i].code == NR_MOVETO_OPEN) )
break;
}
- new_curve->substart = i;
+ new_curve->_substart = i;
+
+ for (GSList const *l = list; l != NULL; l = l->next) {
+ SPCurve *c = (SPCurve *) l->data;
+ new_curve->_pathv.insert( new_curve->_pathv.end(), c->get_pathvector().begin(), c->get_pathvector().end() );
+ }
return new_curve;
}
/**
* Returns a list of new curves corresponding to the subpaths in \a curve.
+ * 2geomified
*/
GSList *
-sp_curve_split(SPCurve const *curve)
+SPCurve::split() const
{
- g_return_val_if_fail(curve != NULL, NULL);
-
- gint p = 0;
+ guint p = 0;
GSList *l = NULL;
- while (p < curve->end) {
+ gint pathnr = 0;
+ while (p < _end) {
gint i = 1;
- while ((curve->_bpath[p + i].code == NR_LINETO) ||
- (curve->_bpath[p + i].code == NR_CURVETO))
+ while ((_bpath[p + i].code == NR_LINETO) ||
+ (_bpath[p + i].code == NR_CURVETO))
i++;
- SPCurve *new_curve = sp_curve_new_sized(i + 1);
- memcpy(new_curve->_bpath, curve->_bpath + p, i * sizeof(NArtBpath));
- new_curve->end = i;
+ SPCurve *new_curve = new SPCurve(i + 1);
+ memcpy(new_curve->_bpath, _bpath + p, i * sizeof(NArtBpath));
+ new_curve->_end = i;
new_curve->_bpath[i].code = NR_END;
- new_curve->substart = 0;
- new_curve->closed = (new_curve->_bpath->code == NR_MOVETO);
- new_curve->hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
+ new_curve->_substart = 0;
+ new_curve->_closed = (new_curve->_bpath->code == NR_MOVETO);
+ new_curve->_hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
+ new_curve->_pathv = Geom::PathVector(1, _pathv[pathnr]);
l = g_slist_prepend(l, new_curve);
p += i;
+ pathnr++;
}
return l;
*/
template<class M>
static void
-tmpl_curve_transform(SPCurve *const curve, M const &m)
+tmpl_curve_transform(SPCurve * curve, M const &m)
{
g_return_if_fail(curve != NULL);
- for (gint i = 0; i < curve->end; i++) {
+ for (guint i = 0; i < curve->_end; i++) {
NArtBpath *p = curve->_bpath + i;
switch (p->code) {
case NR_MOVETO:
}
}
+
+void
+SPCurve::transform(NR::Matrix const &m)
+{
+ transform(to_2geom(m));
+};
+
+
/**
* Transform all paths in curve using matrix.
*/
void
-sp_curve_transform(SPCurve *const curve, NR::Matrix const &m)
+SPCurve::transform(Geom::Matrix const &m)
{
- tmpl_curve_transform<NR::Matrix>(curve, m);
+ tmpl_curve_transform<NR::Matrix>(this, from_2geom(m));
+
+ _pathv = _pathv * m;
}
/**
- * Transform all paths in curve using NR::translate.
+ * Set curve to empty curve.
+ * 2GEOMified
*/
void
-sp_curve_transform(SPCurve *const curve, NR::translate const &m)
+SPCurve::reset()
{
- tmpl_curve_transform<NR::translate>(curve, m);
+ _bpath->code = NR_END;
+ _end = 0;
+ _substart = 0;
+ _hascpt = false;
+ _posSet = false;
+ _moving = false;
+ _closed = false;
+
+ _pathv.clear();
}
-
-/* Methods */
+/* Several consecutive movetos are ALLOWED */
/**
- * Set curve to empty curve.
+ * Calls SPCurve::moveto() with point made of given coordinates.
*/
void
-sp_curve_reset(SPCurve *curve)
+SPCurve::moveto(gdouble x, gdouble y)
{
- g_return_if_fail(curve != NULL);
-
- curve->_bpath->code = NR_END;
- curve->end = 0;
- curve->substart = 0;
- curve->hascpt = false;
- curve->posSet = false;
- curve->moving = false;
- curve->closed = false;
+ moveto(NR::Point(x, y));
}
-
-/* Several consecutive movetos are ALLOWED */
-
/**
- * Calls sp_curve_moveto() with point made of given coordinates.
+ * Calls SPCurve::moveto() with point made of given coordinates.
*/
void
-sp_curve_moveto(SPCurve *curve, gdouble x, gdouble y)
+SPCurve::moveto(Geom::Point const &p)
{
- sp_curve_moveto(curve, NR::Point(x, y));
+ moveto(from_2geom(p));
}
-
/**
* Perform a moveto to a point, thus starting a new subpath.
+ * 2GEOMified
*/
void
-sp_curve_moveto(SPCurve *curve, NR::Point const &p)
+SPCurve::moveto(NR::Point const &p)
{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(!curve->moving);
-
- curve->substart = curve->end;
- curve->hascpt = true;
- curve->posSet = true;
- curve->movePos = p;
+ g_return_if_fail(!_moving);
+
+ _substart = _end;
+ _hascpt = true;
+ _posSet = true;
+ _movePos = p;
+ _pathv.push_back( Geom::Path() ); // for some reason Geom::Path(p) does not work...
+ _pathv.back().start(to_2geom(p));
}
/**
- * Calls sp_curve_lineto() with a point's coordinates.
+ * Calls SPCurve::lineto() with a point's coordinates.
*/
void
-sp_curve_lineto(SPCurve *curve, NR::Point const &p)
+SPCurve::lineto(Geom::Point const &p)
{
- sp_curve_lineto(curve, p[NR::X], p[NR::Y]);
+ lineto(p[Geom::X], p[Geom::Y]);
+}
+/**
+ * Calls SPCurve::lineto() with a point's coordinates.
+ */
+void
+SPCurve::lineto(NR::Point const &p)
+{
+ lineto(p[NR::X], p[NR::Y]);
}
-
/**
* Adds a line to the current subpath.
+ * 2GEOMified
*/
void
-sp_curve_lineto(SPCurve *curve, gdouble x, gdouble y)
+SPCurve::lineto(gdouble x, gdouble y)
{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(curve->hascpt);
+ g_return_if_fail(_hascpt);
- if (curve->moving) {
+ if (_moving) {
/* fix endpoint */
- g_return_if_fail(!curve->posSet);
- g_return_if_fail(curve->end > 1);
- NArtBpath *bp = curve->_bpath + curve->end - 1;
+ g_return_if_fail(!_posSet);
+ g_return_if_fail(_end > 1);
+ NArtBpath *bp = _bpath + _end - 1;
g_return_if_fail(bp->code == NR_LINETO);
bp->x3 = x;
bp->y3 = y;
- curve->moving = false;
- return;
- }
+ _moving = false;
- if (curve->posSet) {
+ Geom::Path::iterator it = _pathv.back().end();
+ if ( Geom::LineSegment const *last_line_segment = dynamic_cast<Geom::LineSegment const *>( &(*it) )) {
+ Geom::LineSegment new_seg( *last_line_segment );
+ new_seg.setFinal( Geom::Point(x,y) );
+ _pathv.back().replace(it, new_seg);
+ }
+ } else if (_posSet) {
/* start a new segment */
- sp_curve_ensure_space(curve, 2);
- NArtBpath *bp = curve->_bpath + curve->end;
+ ensure_space(2);
+ NArtBpath *bp = _bpath + _end;
bp->code = NR_MOVETO_OPEN;
- bp->setC(3, curve->movePos);
+ bp->setC(3, _movePos);
bp++;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
- curve->end += 2;
- curve->posSet = false;
- curve->closed = false;
- return;
- }
-
- /* add line */
-
- g_return_if_fail(curve->end > 1);
- sp_curve_ensure_space(curve, 1);
- NArtBpath *bp = curve->_bpath + curve->end;
- bp->code = NR_LINETO;
- bp->x3 = x;
- bp->y3 = y;
- bp++;
- bp->code = NR_END;
- curve->end++;
-}
+ _end += 2;
+ _posSet = false;
+ _closed = false;
-/// Unused
-void
-sp_curve_lineto_moving(SPCurve *curve, gdouble x, gdouble y)
-{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(curve->hascpt);
-
- if (curve->moving) {
- /* change endpoint */
- g_return_if_fail(!curve->posSet);
- g_return_if_fail(curve->end > 1);
- NArtBpath *bp = curve->_bpath + curve->end - 1;
- g_return_if_fail(bp->code == NR_LINETO);
- bp->x3 = x;
- bp->y3 = y;
+ _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
return;
- }
+ } else {
+ /* add line */
- if (curve->posSet) {
- /* start a new segment */
- sp_curve_ensure_space(curve, 2);
- NArtBpath *bp = curve->_bpath + curve->end;
- bp->code = NR_MOVETO_OPEN;
- bp->setC(3, curve->movePos);
- bp++;
+ g_return_if_fail(_end > 1);
+ ensure_space(1);
+ NArtBpath *bp = _bpath + _end;
bp->code = NR_LINETO;
bp->x3 = x;
bp->y3 = y;
bp++;
bp->code = NR_END;
- curve->end += 2;
- curve->posSet = false;
- curve->moving = true;
- curve->closed = false;
- return;
+ _end++;
+ _pathv.back().appendNew<Geom::LineSegment>( Geom::Point(x,y) );
}
-
- /* add line */
-
- g_return_if_fail(curve->end > 1);
- sp_curve_ensure_space(curve, 1);
- NArtBpath *bp = curve->_bpath + curve->end;
- bp->code = NR_LINETO;
- bp->x3 = x;
- bp->y3 = y;
- bp++;
- bp->code = NR_END;
- curve->end++;
- curve->moving = true;
}
/**
- * Calls sp_curve_curveto() with coordinates of three points.
+ * Calls SPCurve::curveto() with coordinates of three points.
*/
void
-sp_curve_curveto(SPCurve *curve, NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
+SPCurve::curveto(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2)
+{
+ using Geom::X;
+ using Geom::Y;
+ curveto( p0[X], p0[Y],
+ p1[X], p1[Y],
+ p2[X], p2[Y] );
+}
+/**
+ * Calls SPCurve::curveto() with coordinates of three points.
+ */
+void
+SPCurve::curveto(NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
{
using NR::X;
using NR::Y;
- sp_curve_curveto(curve,
- p0[X], p0[Y],
- p1[X], p1[Y],
- p2[X], p2[Y]);
+ curveto( p0[X], p0[Y],
+ p1[X], p1[Y],
+ p2[X], p2[Y] );
}
-
/**
* Adds a bezier segment to the current subpath.
+ * 2GEOMified
*/
void
-sp_curve_curveto(SPCurve *curve, gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
+SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(curve->hascpt);
- g_return_if_fail(!curve->moving);
+ g_return_if_fail(_hascpt);
+ g_return_if_fail(!_moving);
- if (curve->posSet) {
+ if (_posSet) {
/* start a new segment */
- sp_curve_ensure_space(curve, 2);
- NArtBpath *bp = curve->_bpath + curve->end;
+ ensure_space(2);
+ NArtBpath *bp = _bpath + _end;
bp->code = NR_MOVETO_OPEN;
- bp->setC(3, curve->movePos);
+ bp->setC(3, _movePos);
bp++;
bp->code = NR_CURVETO;
bp->x1 = x0;
@@ -525,211 +604,314 @@ sp_curve_curveto(SPCurve *curve, gdouble x0, gdouble y0, gdouble x1, gdouble y1,
bp->y3 = y2;
bp++;
bp->code = NR_END;
- curve->end += 2;
- curve->posSet = false;
- curve->closed = false;
- return;
- }
+ _end += 2;
+ _posSet = false;
+ _closed = false;
+ _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
+ } else {
+ /* add curve */
- /* add curve */
-
- g_return_if_fail(curve->end > 1);
- sp_curve_ensure_space(curve, 1);
- NArtBpath *bp = curve->_bpath + curve->end;
- bp->code = NR_CURVETO;
- bp->x1 = x0;
- bp->y1 = y0;
- bp->x2 = x1;
- bp->y2 = y1;
- bp->x3 = x2;
- bp->y3 = y2;
- bp++;
- bp->code = NR_END;
- curve->end++;
+ g_return_if_fail(_end > 1);
+ ensure_space(1);
+ NArtBpath *bp = _bpath + _end;
+ bp->code = NR_CURVETO;
+ bp->x1 = x0;
+ bp->y1 = y0;
+ bp->x2 = x1;
+ bp->y2 = y1;
+ bp->x3 = x2;
+ bp->y3 = y2;
+ bp++;
+ bp->code = NR_END;
+ _end++;
+ if (_pathv.empty()) g_message("leeg");
+ else _pathv.back().appendNew<Geom::CubicBezier>( Geom::Point(x0,y0), Geom::Point(x1,y1), Geom::Point(x2,y2) );
+ }
}
/**
* Close current subpath by possibly adding a line between start and end.
+ * 2GEOMified
*/
void
-sp_curve_closepath(SPCurve *curve)
+SPCurve::closepath()
{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(curve->hascpt);
- g_return_if_fail(!curve->posSet);
- g_return_if_fail(!curve->moving);
- g_return_if_fail(!curve->closed);
+ g_return_if_fail(_hascpt);
+ g_return_if_fail(!_posSet);
+ g_return_if_fail(!_moving);
+ g_return_if_fail(!_closed);
/* We need at least moveto, curveto, end. */
- g_return_if_fail(curve->end - curve->substart > 1);
+ g_return_if_fail(_end - _substart > 1);
{
- NArtBpath *bs = curve->_bpath + curve->substart;
- NArtBpath *be = curve->_bpath + curve->end - 1;
+ NArtBpath *bs = _bpath + _substart;
+ NArtBpath *be = _bpath + _end - 1;
if (bs->c(3) != be->c(3)) {
- sp_curve_lineto(curve, bs->c(3));
- bs = curve->_bpath + curve->substart;
+ lineto(bs->c(3));
+ bs = _bpath + _substart;
}
bs->code = NR_MOVETO;
}
- curve->closed = true;
+ // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
+ // This lineto is removed in the writing function for NArtBpath,
+ // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
+ // TODO: fix behavior in Inkscape!
+ if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const *>(&_pathv.back().back())) {
+ _pathv.back().erase_last();
+ }
+ _pathv.back().close(true);
+ _closed = true;
- for (NArtBpath const *bp = curve->_bpath; bp->code != NR_END; bp++) {
+ for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
+ if ( ! it->closed() ) {
+ _closed = false;
+ break;
+ }
+ }
+
+ for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
/** \todo
* effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
* the closed boolean).
*/
if (bp->code == NR_MOVETO_OPEN) {
- curve->closed = false;
+ _closed = false;
break;
}
}
- curve->hascpt = false;
+ _hascpt = false;
}
-/** Like sp_curve_closepath() but sets the end point of the current
+/** Like SPCurve::closepath() but sets the end point of the current
command to the subpath start point instead of adding a new lineto.
Used for freehand drawing when the user draws back to the start point.
+
+ 2GEOMified
**/
void
-sp_curve_closepath_current(SPCurve *curve)
+SPCurve::closepath_current()
{
- g_return_if_fail(curve != NULL);
- g_return_if_fail(curve->hascpt);
- g_return_if_fail(!curve->posSet);
- g_return_if_fail(!curve->closed);
+ g_return_if_fail(_hascpt);
+ g_return_if_fail(!_posSet);
+ g_return_if_fail(!_closed);
/* We need at least moveto, curveto, end. */
- g_return_if_fail(curve->end - curve->substart > 1);
+ g_return_if_fail(_end - _substart > 1);
{
- NArtBpath *bs = curve->_bpath + curve->substart;
- NArtBpath *be = curve->_bpath + curve->end - 1;
+ NArtBpath *bs = _bpath + _substart;
+ NArtBpath *be = _bpath + _end - 1;
be->x3 = bs->x3;
be->y3 = bs->y3;
bs->code = NR_MOVETO;
}
- curve->closed = true;
+ // Inkscape always manually adds the closing line segment to SPCurve with a lineto.
+ // This lineto is removed in the writing function for NArtBpath,
+ // so when path is closed and the last segment is a lineto, the closing line segment must really be removed first!
+ // TODO: fix behavior in Inkscape!
+ if ( /*Geom::LineSegment const *line_segment = */ dynamic_cast<Geom::LineSegment const *>(&_pathv.back().back())) {
+ _pathv.back().erase_last();
+ }
+ _pathv.back().close(true);
+ _closed = true;
+
+ for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
+ if ( ! it->closed() ) {
+ _closed = false;
+ break;
+ }
+ }
- for (NArtBpath const *bp = curve->_bpath; bp->code != NR_END; bp++) {
+ for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
/** \todo
* effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
* the closed boolean).
*/
if (bp->code == NR_MOVETO_OPEN) {
- curve->closed = false;
+ _closed = false;
break;
}
}
- curve->hascpt = false;
- curve->moving = false;
+ _hascpt = false;
+ _moving = false;
}
/**
- * True if no paths are in curve.
+ * True if no paths are in curve. If it only contains a path with only a moveto, the path is considered NON-empty
*/
bool
-sp_curve_empty(SPCurve *curve)
+SPCurve::is_empty() const
{
- g_return_val_if_fail(curve != NULL, TRUE);
+ bool empty = _pathv.empty();
+
+ return empty;
+}
+
+/**
+ * True iff all subpaths are closed.
+ */
+bool
+SPCurve::is_closed() const
+{
+ bool closed = true;
+ for (Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); it++) {
+ if ( ! it->closed() ) {
+ closed = false;
+ break;
+ }
+ }
- return (curve->_bpath->code == NR_END);
+ return closed;
}
/**
* Return last subpath or NULL.
*/
-NArtBpath *
-sp_curve_last_bpath(SPCurve const *curve)
+NArtBpath const *
+SPCurve::last_bpath() const
{
- g_return_val_if_fail(curve != NULL, NULL);
+ if (_end == 0) {
+ return NULL;
+ }
- if (curve->end == 0) {
+ return _bpath + _end - 1;
+}
+
+/**
+ * Return last pathsegment (possibly the closing path segment) of the last path in PathVector or NULL.
+ * If the last path is empty (contains only a moveto), the function returns NULL
+ */
+Geom::Curve const *
+SPCurve::last_segment() const
+{
+ if (is_empty()) {
+ return NULL;
+ }
+ if (_pathv.back().empty()) {
return NULL;
}
- return curve->_bpath + curve->end - 1;
+ return &_pathv.back().back_default();
}
/**
- * Return first subpath or NULL.
+ * Return last path in PathVector or NULL.
*/
-NArtBpath *
-sp_curve_first_bpath(SPCurve const *curve)
+Geom::Path const *
+SPCurve::last_path() const
{
- g_return_val_if_fail(curve != NULL, NULL);
+ if (is_empty()) {
+ return NULL;
+ }
+
+ return &_pathv.back();
+}
- if (curve->end == 0) {
+/**
+ * Return first pathsegment in PathVector or NULL.
+ * equal in functionality to SPCurve::first_bpath()
+ */
+Geom::Curve const *
+SPCurve::first_segment() const
+{
+ if (is_empty()) {
+ return NULL;
+ }
+ if (_pathv.front().empty()) {
return NULL;
}
- return curve->_bpath;
+ return &_pathv.front().front();
}
/**
- * Return first point of first subpath or (0,0).
+ * Return first path in PathVector or NULL.
*/
-NR::Point
-sp_curve_first_point(SPCurve const *const curve)
+Geom::Path const *
+SPCurve::first_path() const
{
- NArtBpath *const bpath = sp_curve_first_bpath(curve);
- g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
- return bpath->c(3);
+ if (is_empty()) {
+ return NULL;
+ }
+
+ return &_pathv.front();
}
/**
- * Return the second point of first subpath or curve->movePos if curve too short.
+ * Return first point of first subpath or (0,0). TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ?
*/
NR::Point
-sp_curve_second_point(SPCurve const *const curve)
+SPCurve::first_point() const
{
- g_return_val_if_fail(curve != NULL, NR::Point(0, 0));
+ if (is_empty())
+ return NR::Point(0, 0);
- if (curve->end < 1) {
- return curve->movePos;
- }
+ return from_2geom( _pathv.front().initialPoint() );
+}
- NArtBpath *bpath = NULL;
- if (curve->end < 2) {
- bpath = curve->_bpath;
- } else {
- bpath = curve->_bpath + 1;
+/**
+ * Return the second point of first subpath or _movePos if curve too short.
+ * If the pathvector is empty, this returns (0,0). If the first path is only a moveto, this method
+ * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
+ * first point of the first path.
+ *
+ * FIXME: for empty paths shouldn't this return (NR_HUGE,NR_HUGE)
+ */
+NR::Point
+SPCurve::second_point() const
+{
+ if (is_empty()) {
+ return NR::Point(0,0);
}
- g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
- return bpath->c(3);
+ else if (_pathv.front().empty()) {
+ // first path is only a moveto
+ // check if there is second path
+ if (_pathv.size() > 1) {
+ return _pathv[1].initialPoint();
+ } else {
+ return _pathv[0].initialPoint();
+ }
+ }
+ else
+ return _pathv.front()[0].finalPoint();
}
/**
- * Return the second-last point of last subpath or curve->movePos if curve too short.
+ * Return the second-last point of last subpath or _movePos if curve too short.
*/
NR::Point
-sp_curve_penultimate_point(SPCurve const *const curve)
+SPCurve::penultimate_point() const
{
- g_return_val_if_fail(curve != NULL, NR::Point(0, 0));
-
- if (curve->end < 2) {
- return curve->movePos;
+ if (_end < 2) {
+ return _movePos;
}
- NArtBpath *const bpath = curve->_bpath + curve->end - 2;
+ NArtBpath *const bpath = _bpath + _end - 2;
g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
- return bpath->c(3);
+
+
+ Geom::Curve const& back = _pathv.back().back_default();
+ Geom::Point p = back.initialPoint();
+ return from_2geom(p);
}
/**
- * Return last point of last subpath or (0,0).
+ * Return last point of last subpath or (0,0). TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ?
+ * If the last path is only a moveto, then return that point.
*/
NR::Point
-sp_curve_last_point(SPCurve const *const curve)
+SPCurve::last_point() const
{
- NArtBpath *const bpath = sp_curve_last_bpath(curve);
- g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
- return bpath->c(3);
+ if (is_empty())
+ return NR::Point(0, 0);
+
+ return from_2geom( _pathv.back().finalPoint() );
}
inline static bool
}
/**
- * Returns \a curve but drawn in the opposite direction.
+ * Returns a *new* \a curve but drawn in the opposite direction.
* Should result in the same shape, but
* with all its markers drawn facing the other direction.
+ * Reverses the order of subpaths as well
+ * 2GEOMified
**/
SPCurve *
-sp_curve_reverse(SPCurve const *curve)
+SPCurve::create_reverse() const
{
/* We need at least moveto, curveto, end. */
- g_return_val_if_fail(curve->end - curve->substart > 1, NULL);
+ g_return_val_if_fail(_end - _substart > 1, NULL);
- NArtBpath const *be = curve->_bpath + curve->end - 1;
+ NArtBpath const *be = _bpath + _end - 1;
- g_assert(is_moveto(curve->_bpath[curve->substart].code));
- g_assert(is_moveto(curve->_bpath[0].code));
+ g_assert(is_moveto(_bpath[_substart].code));
+ g_assert(is_moveto(_bpath[0].code));
g_assert((be+1)->code == NR_END);
- SPCurve *new_curve = sp_curve_new_sized(curve->length);
- sp_curve_moveto(new_curve, be->c(3));
+ SPCurve *new_curve = new SPCurve(_length);
+ new_curve->moveto(be->c(3));
for (NArtBpath const *bp = be; ; --bp) {
switch (bp->code) {
case NR_MOVETO:
- g_assert(new_curve->_bpath[new_curve->substart].code == NR_MOVETO_OPEN);
- new_curve->_bpath[new_curve->substart].code = NR_MOVETO;
+ g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
+ new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
/* FALL-THROUGH */
case NR_MOVETO_OPEN:
- if (bp == curve->_bpath) {
+ if (bp == _bpath) {
return new_curve;
}
- sp_curve_moveto(new_curve, (bp-1)->c(3));
+ new_curve->moveto((bp-1)->c(3));
break;
case NR_LINETO:
- sp_curve_lineto(new_curve, (bp-1)->c(3));
+ new_curve->lineto((bp-1)->c(3));
break;
case NR_CURVETO:
- sp_curve_curveto(new_curve, bp->c(2), bp->c(1), (bp-1)->c(3));
+ new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
break;
default:
g_assert_not_reached();
}
}
+
+ new_curve->_pathv = Geom::reverse_paths_and_order(_pathv);
}
/**
- * Append \a curve2 to \a curve.
+ * Append \a curve2 to \a this.
+ * If \a use_lineto is false, simply add all paths in \a curve2 to \a this;
+ * if \a use_lineto is true, combine \a this's last path and \a curve2's first path and add the rest of the paths in \a curve2 to \a this.
+ * 2GEOMified
*/
void
-sp_curve_append(SPCurve *curve,
- SPCurve const *curve2,
+SPCurve::append(SPCurve const *curve2,
bool use_lineto)
{
- g_return_if_fail(curve != NULL);
g_return_if_fail(curve2 != NULL);
- if (curve2->end < 1)
+ if (curve2->is_empty())
+ return;
+ if (curve2->_end < 1)
return;
NArtBpath const *bs = curve2->_bpath;
- bool closed = curve->closed;
+ bool closed = this->_closed;
for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
switch (bp->code) {
case NR_MOVETO_OPEN:
- if (use_lineto && curve->hascpt) {
- sp_curve_lineto(curve, bp->x3, bp->y3);
- use_lineto = FALSE;
+ if (use_lineto && _hascpt) {
+ lineto(bp->x3, bp->y3);
+ use_lineto = false;
} else {
- if (closed) sp_curve_closepath(curve);
- sp_curve_moveto(curve, bp->x3, bp->y3);
+ if (closed && _hascpt) closepath();
+ moveto(bp->x3, bp->y3);
}
closed = false;
break;
case NR_MOVETO:
- if (use_lineto && curve->hascpt) {
- sp_curve_lineto(curve, bp->x3, bp->y3);
+ if (use_lineto && _hascpt) {
+ lineto(bp->x3, bp->y3);
use_lineto = FALSE;
} else {
- if (closed) sp_curve_closepath(curve);
- sp_curve_moveto(curve, bp->x3, bp->y3);
+ if (closed && _hascpt) closepath();
+ moveto(bp->x3, bp->y3);
}
closed = true;
break;
case NR_LINETO:
- sp_curve_lineto(curve, bp->x3, bp->y3);
+ lineto(bp->x3, bp->y3);
break;
case NR_CURVETO:
- sp_curve_curveto(curve, bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
+ curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
break;
case NR_END:
}
if (closed) {
- sp_curve_closepath(curve);
+ closepath();
}
+
+ /* 2GEOM code when code above is removed:
+ if (use_lineto) {
+ Geom::PathVector::const_iterator it = curve2->_pathv.begin();
+ if ( ! _pathv.empty() ) {
+ Geom::Path & lastpath = _pathv.back();
+ lastpath.appendNew<Geom::LineSegment>( (*it).initialPoint() );
+ lastpath.append( (*it) );
+ } else {
+ _pathv.push_back( (*it) );
+ }
+
+ for (it++; it != curve2->_pathv.end(); it++) {
+ _pathv.push_back( (*it) );
+ }
+ } else {
+ for (Geom::PathVector::const_iterator it = curve2->_pathv.begin(); it != curve2->_pathv.end(); it++) {
+ _pathv.push_back( (*it) );
+ }
+ }
+ */
}
/**
- * Append \a c1 to \a c0 with possible fusing of close endpoints.
+ * Append \a c1 to \a this with possible fusing of close endpoints.
+ * 2GEOMproof. Needs to be recoded when NArtBpath is no longer there. Right now, it applies the same changes to bpath and pathv depending on bpath
*/
SPCurve *
-sp_curve_append_continuous(SPCurve *c0, SPCurve const *c1, gdouble tolerance)
+SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
{
- g_return_val_if_fail(c0 != NULL, NULL);
g_return_val_if_fail(c1 != NULL, NULL);
- g_return_val_if_fail(!c0->closed, NULL);
- g_return_val_if_fail(!c1->closed, NULL);
+ g_return_val_if_fail(!_closed, NULL);
+ g_return_val_if_fail(!c1->_closed, NULL);
- if (c1->end < 1) {
- return c0;
+ if (c1->_end < 1) {
+ return this;
}
- NArtBpath *be = sp_curve_last_bpath(c0);
+ NArtBpath const *be = last_bpath();
if (be) {
- NArtBpath const *bs = sp_curve_first_bpath(c1);
+ NArtBpath const *bs = c1->get_bpath();
if ( bs
&& ( fabs( bs->x3 - be->x3 ) <= tolerance )
&& ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
@@ -875,163 +1085,70 @@ sp_curve_append_continuous(SPCurve *c0, SPCurve const *c1, gdouble tolerance)
for (bs = bs + 1; bs->code != NR_END; bs++) {
switch (bs->code) {
case NR_MOVETO_OPEN:
- if (closed) sp_curve_closepath(c0);
- sp_curve_moveto(c0, bs->x3, bs->y3);
+ if (closed) closepath();
+ moveto(bs->x3, bs->y3);
closed = false;
break;
case NR_MOVETO:
- if (closed) sp_curve_closepath(c0);
- sp_curve_moveto(c0, bs->x3, bs->y3);
+ if (closed) closepath();
+ moveto(bs->x3, bs->y3);
closed = true;
break;
case NR_LINETO:
- sp_curve_lineto(c0, bs->x3, bs->y3);
+ lineto(bs->x3, bs->y3);
break;
case NR_CURVETO:
- sp_curve_curveto(c0, bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
+ curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
break;
case NR_END:
g_assert_not_reached();
}
}
} else {
- sp_curve_append(c0, c1, TRUE);
+ append(c1, TRUE);
}
} else {
- sp_curve_append(c0, c1, TRUE);
+ append(c1, TRUE);
}
- return c0;
+ return this;
}
/**
* Remove last segment of curve.
+ * (Only used once in /src/pen-context.cpp)
+ * 2GEOMified
*/
void
-sp_curve_backspace(SPCurve *curve)
+SPCurve::backspace()
{
- g_return_if_fail(curve != NULL);
+ if ( is_empty() )
+ return;
- if (curve->end > 0) {
- curve->end -= 1;
- if (curve->end > 0) {
- NArtBpath *bp = curve->_bpath + curve->end - 1;
+ if (_end > 0) {
+ _end -= 1;
+ if (_end > 0) {
+ NArtBpath *bp = _bpath + _end - 1;
if ((bp->code == NR_MOVETO) ||
(bp->code == NR_MOVETO_OPEN) )
{
- curve->hascpt = true;
- curve->posSet = true;
- curve->closed = false;
- curve->movePos = bp->c(3);
- curve->end -= 1;
+ _hascpt = true;
+ _posSet = true;
+ _closed = false;
+ _movePos = bp->c(3);
+ _end -= 1;
}
}
- curve->_bpath[curve->end].code = NR_END;
+ _bpath[_end].code = NR_END;
}
-}
-/* Private methods */
-
-/**
- * True if all subpaths in bpath array pass consistency check.
- */
-static bool sp_bpath_good(NArtBpath const bpath[])
-{
- g_return_val_if_fail(bpath != NULL, FALSE);
-
- NArtBpath const *bp = bpath;
- while (bp->code != NR_END) {
- bp = sp_bpath_check_subpath(bp);
- if (bp == NULL)
- return false;
+ if ( !_pathv.back().empty() ) {
+ _pathv.back().erase_last();
+ _pathv.back().close(false);
}
-
- return true;
}
-/**
- * Return copy of a bpath array, discarding any inconsistencies.
- */
-static NArtBpath *sp_bpath_clean(NArtBpath const bpath[])
-{
- NArtBpath *new_bpath = g_new(NArtBpath, sp_bpath_length(bpath));
-
- NArtBpath const *bp = bpath;
- NArtBpath *np = new_bpath;
-
- while (bp->code != NR_END) {
- if (sp_bpath_check_subpath(bp)) {
- *np++ = *bp++;
- while ((bp->code == NR_LINETO) ||
- (bp->code == NR_CURVETO))
- *np++ = *bp++;
- } else {
- bp++;
- while ((bp->code == NR_LINETO) ||
- (bp->code == NR_CURVETO))
- bp++;
- }
- }
-
- if (np == new_bpath) {
- g_free(new_bpath);
- return NULL;
- }
-
- np->code = NR_END;
- np += 1;
-
- new_bpath = g_renew(NArtBpath, new_bpath, np - new_bpath);
-
- return new_bpath;
-}
-
-/**
- * Perform consistency check of bpath array.
- * \return Address of NR_END node or NULL.
- */
-static NArtBpath const *sp_bpath_check_subpath(NArtBpath const bpath[])
-{
- g_return_val_if_fail(bpath != NULL, NULL);
-
- bool closed;
- if (bpath->code == NR_MOVETO) {
- closed = true;
- } else if (bpath->code == NR_MOVETO_OPEN) {
- closed = false;
- } else {
- return NULL;
- }
-
- gint len = 0;
- gint i;
- /** \todo
- * effic: consider checking for END/MOVE/MOVETO inside switch block
- */
- for (i = 1; (bpath[i].code != NR_END) && (bpath[i].code != NR_MOVETO) && (bpath[i].code != NR_MOVETO_OPEN); i++) {
- switch (bpath[i].code) {
- case NR_LINETO:
- case NR_CURVETO:
- len++;
- break;
- default:
- return NULL;
- }
- }
-
- if (closed) {
- if (len < 1)
- return NULL;
-
- if ((bpath->x3 != bpath[i-1].x3) || (bpath->y3 != bpath[i-1].y3))
- return NULL;
- } else {
- if (len < 1)
- return NULL;
- }
-
- return bpath + i;
-}
+/* Private methods */
/**
* Returns index of first NR_END bpath in array.
/**
* Returns total length of curve, excluding length of closepath segments.
*/
-static double
+double
sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
{
g_return_val_if_fail(curve != NULL, 0.);
@@ -1124,7 +1241,7 @@ sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
}
NR::Point prev(curve->_bpath->c(3));
- for (gint i = 1; i < curve->end; ++i) {
+ for (guint i = 1; i < curve->_end; ++i) {
NArtBpath &p = curve->_bpath[i];
double seg_len = 0;
switch (p.code) {
@@ -1153,7 +1270,7 @@ sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
* Like sp_curve_distance_including_space(), but ensures that the
* result >= 1e-18: uses 1 per segment if necessary.
*/
-static double
+double
sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
{
double const real_dist(sp_curve_distance_including_space(curve, seg2len));
@@ -1168,24 +1285,27 @@ sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg
}
}
+/**
+ * 2GEOMified
+ */
void
-sp_curve_stretch_endpoints(SPCurve *curve, NR::Point const &new_p0, NR::Point const &new_p1)
+SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
{
- if (sp_curve_empty(curve)) {
+ if (is_empty()) {
return;
}
- g_assert(unsigned(SP_CURVE_LENGTH(curve)) + 1 == sp_bpath_length(curve->_bpath));
- unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
+ g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
+ unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
g_assert(nSegs != 0);
double *const seg2len = new double[nSegs];
- double const tot_len = sp_curve_nonzero_distance_including_space(curve, seg2len);
- NR::Point const offset0( new_p0 - sp_curve_first_point(curve) );
- NR::Point const offset1( new_p1 - sp_curve_last_point(curve) );
- curve->_bpath->setC(3, new_p0);
+ double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
+ NR::Point const offset0( new_p0 - first_point() );
+ NR::Point const offset1( new_p1 - last_point() );
+ _bpath->setC(3, new_p0);
double begin_dist = 0.;
for (unsigned si = 0; si < nSegs; ++si) {
double const end_dist = begin_dist + seg2len[si];
- NArtBpath &p = curve->_bpath[1 + si];
+ NArtBpath &p = _bpath[1 + si];
switch (p.code) {
case NR_LINETO:
case NR_MOVETO:
@@ -1205,26 +1325,103 @@ sp_curve_stretch_endpoints(SPCurve *curve, NR::Point const &new_p0, NR::Point co
begin_dist = end_dist;
}
- g_assert(L1(curve->_bpath[nSegs].c(3) - new_p1) < 1.);
+ g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
/* Explicit set for better numerical properties. */
- curve->_bpath[nSegs].setC(3, new_p1);
+ _bpath[nSegs].setC(3, new_p1);
delete [] seg2len;
+
+ Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = _pathv.front().toPwSb();
+ Geom::Piecewise<Geom::SBasis> arclength = Geom::arcLengthSb(pwd2);
+ if ( arclength.lastValue() <= 0 ) {
+ g_error("SPCurve::stretch_endpoints - arclength <= 0");
+ throw;
+ }
+ arclength *= 1./arclength.lastValue();
+ Geom::Point const A( to_2geom(offset0) );
+ Geom::Point const B( to_2geom(offset1) );
+ Geom::Piecewise<Geom::SBasis> offsetx = (arclength*-1.+1)*A[0] + arclength*B[0];
+ Geom::Piecewise<Geom::SBasis> offsety = (arclength*-1.+1)*A[1] + arclength*B[1];
+ Geom::Piecewise<Geom::D2<Geom::SBasis> > offsetpath = Geom::sectionize( Geom::D2<Geom::Piecewise<Geom::SBasis> >(offsetx, offsety) );
+ pwd2 += offsetpath;
+ _pathv = Geom::path_from_piecewise( pwd2, 0.001 );
}
+/**
+ * sets start of first path to new_p0, and end of first path to new_p1
+ * 2GEOMified
+ */
void
-sp_curve_move_endpoints(SPCurve *curve, NR::Point const &new_p0,
- NR::Point const &new_p1)
+SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
{
- if (sp_curve_empty(curve)) {
+ if (is_empty()) {
return;
}
- unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
+ unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
g_assert(nSegs != 0);
- curve->_bpath->setC(3, new_p0);
- curve->_bpath[nSegs].setC(3, new_p1);
+ _bpath->setC(3, new_p0);
+ _bpath[nSegs].setC(3, new_p1);
+
+ _pathv.front().setInitial(to_2geom(new_p0));
+ _pathv.front().setFinal(to_2geom(new_p1));
}
+/**
+ * returns the number of nodes in a path, used for statusbar text when selecting an spcurve.
+ * 2GEOMified
+ */
+guint
+SPCurve::nodes_in_path() const
+{
+ gint r = _end;
+ gint i = _length - 1;
+ if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
+ for (; i >= 0; i --)
+ if (_bpath[i].code == NR_MOVETO)
+ r --;
+
+ guint nr = 0;
+ for(Geom::PathVector::const_iterator it = _pathv.begin(); it != _pathv.end(); ++it) {
+ nr += (*it).size();
+
+ nr++; // count last node (this works also for closed paths because although they don't have a 'last node', they do have an extra segment
+ }
+
+ return r;
+}
+
+/**
+ * Adds p to the last point (and last handle if present) of the last path
+ * 2GEOMified
+ */
+void
+SPCurve::last_point_additive_move(Geom::Point const & p)
+{
+ if (is_empty()) {
+ return;
+ }
+ if (_end == 0) {
+ return;
+ }
+ NArtBpath * path = _bpath + _end - 1;
+
+ if (path->code == NR_CURVETO) {
+ path->x2 += p[Geom::X];
+ path->y2 += p[Geom::Y];
+ }
+ path->x3 += p[Geom::X];
+ path->y3 += p[Geom::Y];
+
+ _pathv.back().setFinal( _pathv.back().finalPoint() + p );
+
+ // Move handle as well when the last segment is a cubic bezier segment:
+ // TODO: what to do for quadratic beziers?
+ if ( Geom::CubicBezier const *lastcube = dynamic_cast<Geom::CubicBezier const *>(&_pathv.back().back()) ) {
+ Geom::CubicBezier newcube( *lastcube );
+ newcube.setPoint(2, newcube[2] + p);
+ _pathv.back().replace( --_pathv.back().end(), newcube );
+ }
+}
/*
Local Variables: