Code

OCAL. Fix for Bug #638844 (Errors printed to console if openclipart search fails).
[inkscape.git] / src / sp-polygon.cpp
index 1fb43c03673de3f895c7723518a23bae8f3cd1a2..1067da72e7613ea1a6cab95d24f5a89afd6866d8 100644 (file)
@@ -1,10 +1,9 @@
-#define __SP_POLYGON_C__
-
 /*
  * SVG <polygon> implementation
  *
  * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2002 Lauris Kaplinski
  * Copyright (C) 2000-2001 Ximian, Inc.
 #include "sp-polygon.h"
 #include "display/curve.h"
 #include <glibmm/i18n.h>
-#include "libnr/n-art-bpath.h"
+#include <2geom/pathvector.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
+#include "helper/geom-curves.h"
 #include "svg/stringstream.h"
 #include "xml/repr.h"
 #include "document.h"
@@ -27,8 +29,7 @@ static void sp_polygon_class_init(SPPolygonClass *pc);
 static void sp_polygon_init(SPPolygon *polygon);
 
 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
-static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value);
+static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static gchar *sp_polygon_description(SPItem *item);
 
@@ -36,23 +37,25 @@ static SPShapeClass *parent_class;
 
 GType sp_polygon_get_type(void)
 {
-    static GType polygon_type = 0;
+    static GType type = 0;
 
-    if (!polygon_type) {
-        GTypeInfo polygon_info = {
+    if (!type) {
+        GTypeInfo info = {
             sizeof(SPPolygonClass),
-            NULL, NULL,
-            (GClassInitFunc) sp_polygon_class_init,
-            NULL, NULL,
+            0, // base_init
+            0, // base_finalize
+            (GClassInitFunc)sp_polygon_class_init,
+            0, // class_finalize
+            0, // class_data
             sizeof(SPPolygon),
-            16,
-            (GInstanceInitFunc) sp_polygon_init,
-            NULL,   /* value_table */
+            0, // n_preallocs
+            (GInstanceInitFunc)sp_polygon_init,
+            0 // value_table
         };
-        polygon_type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &polygon_info, (GTypeFlags) 0);
+        type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &info, static_cast<GTypeFlags>(0));
     }
 
-    return polygon_type;
+    return type;
 }
 
 static void sp_polygon_class_init(SPPolygonClass *pc)
@@ -80,59 +83,51 @@ static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::X
         ((SPObjectClass *) parent_class)->build(object, document, repr);
     }
 
-    sp_object_read_attr(object, "points");
+    object->readAttr( "points" );
 }
 
 
 /*
  * sp_svg_write_polygon: Write points attribute for polygon tag.
- * @bpath:
- *
+ * pathv may only contain paths with only straight line segments
  * Return value: points attribute string.
  */
-static gchar *sp_svg_write_polygon(const NArtBpath *bpath)
+static gchar *sp_svg_write_polygon(Geom::PathVector const & pathv)
 {
-    g_return_val_if_fail(bpath != NULL, NULL);
-
     Inkscape::SVGOStringStream os;
 
-    for (int i = 0; bpath[i].code != NR_END; i++) {
-        switch (bpath [i].code) {
-            case NR_LINETO:
-            case NR_MOVETO:
-            case NR_MOVETO_OPEN:
-                os << bpath [i].x3 << "," << bpath [i].y3 << " ";
-                break;
-
-            case NR_CURVETO:
-            default:
-                g_assert_not_reached();
+    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
+        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
+            if ( is_straight_curve(*cit) )
+            {
+                os << cit->finalPoint()[0] << "," << cit->finalPoint()[1] << " ";
+            } else {
+                g_error("sp_svg_write_polygon: polygon path contains non-straight line segments");
+            }
         }
     }
 
     return g_strdup(os.str().c_str());
 }
 
-static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPShape *shape = SP_SHAPE(object);
     // Tolerable workaround: we need to update the object's curve before we set points=
     // because it's out of sync when e.g. some extension attrs of the polygon or star are changed in XML editor
-    sp_shape_set_shape(shape);
+    shape->setShape();
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:polygon");
     }
 
     /* We can safely write points here, because all subclasses require it too (Lauris) */
-    NArtBpath *abp = sp_curve_first_bpath(shape->curve);
-    gchar *str = sp_svg_write_polygon(abp);
+    gchar *str = sp_svg_write_polygon(shape->curve->get_pathvector());
     repr->setAttribute("points", str);
     g_free(str);
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     return repr;
@@ -160,7 +155,7 @@ static gboolean polygon_get_value(gchar const **p, gdouble *v)
 }
 
 
-static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
+void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
 {
     SPPolygon *polygon = SP_POLYGON(object);
 
@@ -171,7 +166,7 @@ static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *valu
                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
                 break;
             }
-            SPCurve *curve = sp_curve_new();
+            SPCurve *curve = new SPCurve();
             gboolean hascpt = FALSE;
 
             gchar const *cptr = value;
@@ -198,9 +193,9 @@ static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *valu
                 }
 
                 if (hascpt) {
-                    sp_curve_lineto(curve, x, y);
+                    curve->lineto(x, y);
                 } else {
-                    sp_curve_moveto(curve, x, y);
+                    curve->moveto(x, y);
                     hascpt = TRUE;
                 }
             }
@@ -208,18 +203,13 @@ static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *valu
             if (has_error || *cptr != '\0') {
                 /* TODO: Flag the document as in error, as per
                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
-            } else if (curve->posSet) {
-                /* We've done a moveto but no lineto.  I'm not sure how we're supposed to represent
-                 * a single-point polygon in SPCurve: sp_curve_closepath at the time of writing
-                 * doesn't seem to like simply moveto followed by closepath.  The following works,
-                 * but won't round-trip properly: I believe it will write as two points rather than
-                 * one. */
-                sp_curve_lineto(curve, curve->movePos);
             } else if (hascpt) {
-                sp_curve_closepath(curve);
+                /* We might have done a moveto but no lineto.  I'm not sure how we're supposed to represent
+                 * a single-point polygon in SPCurve. TODO: add a testcase with only one coordinate pair */
+                curve->closepath();
             }
-            sp_shape_set_curve(SP_SHAPE(polygon), curve, TRUE);
-            sp_curve_unref(curve);
+            (SP_SHAPE(polygon))->setCurve(curve, TRUE);
+            curve->unref();
             break;
         }
         default: