Code

New grids are almost ready to fly!
[inkscape.git] / src / display / curve.cpp
index 3ea4e60dfd7756fa51b65a2256805a21d906d952..9e571fdd0518305801b71b09c8a85f71ebd33919 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 
+#include <glib/gmem.h>
 #include <display/curve.h>
 #include <libnr/n-art-bpath.h>
 #include <libnr/nr-point-matrix-ops.h>
@@ -56,12 +57,11 @@ sp_curve_new_sized(gint length)
     SPCurve *curve = g_new(SPCurve, 1);
 
     curve->refcount = 1;
-    curve->_bpath = nr_new(NArtBpath, length);
+    curve->_bpath = g_new(NArtBpath, length);
     curve->_bpath->code = NR_END;
     curve->end = 0;
     curve->length = length;
     curve->substart = 0;
-    curve->sbpath = false;
     curve->hascpt = false;
     curve->posSet = false;
     curve->moving = false;
@@ -80,62 +80,35 @@ sp_curve_new_from_bpath(NArtBpath *bpath)
 {
     g_return_val_if_fail(bpath != NULL, NULL);
 
-    if (!sp_bpath_good(bpath)) {
-        NArtBpath *new_bpath = sp_bpath_clean(bpath);
-        if (new_bpath == NULL) {
-            return NULL;
-        }
-        nr_free(bpath);
-        bpath = new_bpath;
-    }
-
-    SPCurve *curve = g_new(SPCurve, 1);
-
-    curve->refcount = 1;
-    curve->_bpath = bpath;
-    curve->length = sp_bpath_length(bpath);
-    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->sbpath = false;
-    curve->hascpt = false;
-    curve->posSet = false;
-    curve->moving = false;
-    curve->closed = sp_bpath_closed(bpath);
-
+    SPCurve *curve = sp_curve_new_from_foreign_bpath(bpath);
+    g_free(bpath);
     return curve;
 }
 
-/** 
- * Construct an SPCurve from read-only, static storage.
+/**
+ * Convert const NArtBpath array to SPCurve.
  *
- *  We could treat read-onliness and staticness (i.e. can't call free on bpath) as orthogonal
- *  attributes, but at the time of writing we have only one caller.
+ * \return new SPCurve, or NULL if the curve was not created for some reason.
  */
-SPCurve *
-sp_curve_new_from_static_bpath(NArtBpath const *bpath)
+SPCurve *sp_curve_new_from_foreign_bpath(NArtBpath const bpath[])
 {
     g_return_val_if_fail(bpath != NULL, NULL);
 
-    bool sbpath;
+    NArtBpath *new_bpath;
     if (!sp_bpath_good(bpath)) {
-        NArtBpath *new_bpath = sp_bpath_clean(bpath);
+        new_bpath = sp_bpath_clean(bpath);
         g_return_val_if_fail(new_bpath != NULL, NULL);
-        sbpath = false;
-        bpath = new_bpath;
     } else {
-        sbpath = true;
+        unsigned const len = sp_bpath_length(bpath);
+        new_bpath = g_new(NArtBpath, len);
+        memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
     }
 
     SPCurve *curve = g_new(SPCurve, 1);
 
     curve->refcount = 1;
-    curve->_bpath = const_cast<NArtBpath *>(bpath);
-    curve->length = sp_bpath_length(bpath);
+    curve->_bpath = new_bpath;
+    curve->length = sp_bpath_length(new_bpath);
     curve->end = curve->length - 1;
     gint i = curve->end;
     for (; i > 0; i--)
@@ -143,38 +116,10 @@ sp_curve_new_from_static_bpath(NArtBpath const *bpath)
             (curve->_bpath[i].code == NR_MOVETO_OPEN))
             break;
     curve->substart = i;
-    curve->sbpath = sbpath;
     curve->hascpt = false;
     curve->posSet = false;
     curve->moving = false;
-    curve->closed = sp_bpath_closed(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[])
-{
-    g_return_val_if_fail(bpath != NULL, NULL);
-
-    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 = nr_new(NArtBpath, len);
-        memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
-    }
-
-    SPCurve *curve = sp_curve_new_from_bpath(new_bpath);
-
-    if (!curve)
-        nr_free(new_bpath);
+    curve->closed = sp_bpath_closed(new_bpath);
 
     return curve;
 }
@@ -207,8 +152,8 @@ sp_curve_unref(SPCurve *curve)
     curve->refcount -= 1;
 
     if (curve->refcount < 1) {
-        if ((!curve->sbpath) && (curve->_bpath)) {
-            nr_free(curve->_bpath);
+        if (curve->_bpath) {
+            g_free(curve->_bpath);
         }
         g_free(curve);
     }
@@ -231,7 +176,7 @@ sp_curve_ensure_space(SPCurve *curve, gint space)
     if (space < SP_CURVE_LENSTEP)
         space = SP_CURVE_LENSTEP;
 
-    curve->_bpath = nr_renew(curve->_bpath, NArtBpath, curve->length + space);
+    curve->_bpath = g_renew(NArtBpath, curve->_bpath, curve->length + space);
 
     curve->length += space;
 }
@@ -310,12 +255,7 @@ sp_curve_split(SPCurve const *curve)
         new_curve->substart = 0;
         new_curve->closed = (new_curve->_bpath->code == NR_MOVETO);
         new_curve->hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
-        l = g_slist_append(l, new_curve);
-        /** \todo
-         * effic: Use g_slist_prepend instead.  Either work backwards from 
-         * the end of curve, or work forwards as at present but do
-         * g_slist_reverse before returning.
-         */
+        l = g_slist_prepend(l, new_curve);
         p += i;
     }
 
@@ -330,7 +270,6 @@ static void
 tmpl_curve_transform(SPCurve *const curve, M const &m)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
 
     for (gint i = 0; i < curve->end; i++) {
         NArtBpath *p = curve->_bpath + i;
@@ -381,7 +320,6 @@ void
 sp_curve_reset(SPCurve *curve)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
 
     curve->_bpath->code = NR_END;
     curve->end = 0;
@@ -410,7 +348,6 @@ void
 sp_curve_moveto(SPCurve *curve, NR::Point const &p)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(!curve->moving);
 
     curve->substart = curve->end;
@@ -435,7 +372,6 @@ void
 sp_curve_lineto(SPCurve *curve, gdouble x, gdouble y)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(curve->hascpt);
 
     if (curve->moving) {
@@ -486,7 +422,6 @@ void
 sp_curve_lineto_moving(SPCurve *curve, gdouble x, gdouble y)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(curve->hascpt);
 
     if (curve->moving) {
@@ -554,7 +489,6 @@ void
 sp_curve_curveto(SPCurve *curve, gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(curve->hascpt);
     g_return_if_fail(!curve->moving);
 
@@ -604,7 +538,6 @@ void
 sp_curve_closepath(SPCurve *curve)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(curve->hascpt);
     g_return_if_fail(!curve->posSet);
     g_return_if_fail(!curve->moving);
@@ -648,7 +581,6 @@ void
 sp_curve_closepath_current(SPCurve *curve)
 {
     g_return_if_fail(curve != NULL);
-    g_return_if_fail(!curve->sbpath);
     g_return_if_fail(curve->hascpt);
     g_return_if_fail(!curve->posSet);
     g_return_if_fail(!curve->closed);
@@ -1005,7 +937,7 @@ static bool sp_bpath_good(NArtBpath const bpath[])
  */
 static NArtBpath *sp_bpath_clean(NArtBpath const bpath[])
 {
-    NArtBpath *new_bpath = nr_new(NArtBpath, sp_bpath_length(bpath));
+    NArtBpath *new_bpath = g_new(NArtBpath, sp_bpath_length(bpath));
 
     NArtBpath const *bp = bpath;
     NArtBpath *np = new_bpath;
@@ -1025,14 +957,14 @@ static NArtBpath *sp_bpath_clean(NArtBpath const bpath[])
     }
 
     if (np == new_bpath) {
-        nr_free(new_bpath);
+        g_free(new_bpath);
         return NULL;
     }
 
     np->code = NR_END;
     np += 1;
 
-    new_bpath = nr_renew(new_bpath, NArtBpath, np - new_bpath);
+    new_bpath = g_renew(NArtBpath, new_bpath, np - new_bpath);
 
     return new_bpath;
 }