Code

Fixed crash when draw height was zero.
[inkscape.git] / src / sp-ellipse.cpp
index b2b0f7b0565dd7fbe467fe8e15043272725d3b7d..6ab49711607fc6f878ed0e52065a33cbde57726f 100644 (file)
 #include "libnr/nr-path.h"
 #include "libnr/nr-matrix-fns.h"
 #include "svg/svg.h"
-#include "svg/stringstream.h"
+#include "svg/path-string.h"
 #include "xml/repr.h"
 #include "attributes.h"
 #include "style.h"
 #include "display/curve.h"
 #include <glibmm/i18n.h>
 
+#include "document.h"
 #include "sp-ellipse.h"
 
 #include "prefs-utils.h"
@@ -284,7 +285,8 @@ static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::
 
     if (flags & SP_OBJECT_WRITE_EXT) {
         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-            repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+            repr = xml_doc->createElement("svg:path");
         }
 
         sp_repr_set_svg_double(repr, "sodipodi:cx", ellipse->cx.computed);
@@ -376,7 +378,8 @@ sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     ellipse = SP_GENERICELLIPSE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:ellipse");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+        repr = xml_doc->createElement("svg:ellipse");
     }
 
     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
@@ -523,7 +526,8 @@ sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     ellipse = SP_GENERICELLIPSE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:circle");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+        repr = xml_doc->createElement("svg:circle");
     }
 
     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
@@ -658,42 +662,34 @@ sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
 static gboolean
 sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr)
 {
-    gint fa, fs;
-    gdouble  dt;
-    Inkscape::SVGOStringStream os;
-
     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
 
+    Inkscape::SVG::PathString str;
+
     NR::Point p1 = sp_arc_get_xy(arc, ge->start);
     NR::Point p2 = sp_arc_get_xy(arc, ge->end);
+    double rx = ge->rx.computed;
+    double ry = ge->ry.computed;
 
-    dt = fmod(ge->end - ge->start, SP_2PI);
+    str.moveTo(p1);
+
+    double dt = fmod(ge->end - ge->start, SP_2PI);
     if (fabs(dt) < 1e-6) {
         NR::Point ph = sp_arc_get_xy(arc, (ge->start + ge->end) / 2.0);
-        os << "M " << p1[NR::X] << " " <<  p1[NR::Y]
-           << " A " << ge->rx.computed << " " << ge->ry.computed
-           << " 0 1 1 " << " " << ph[NR::X] << "," << ph[NR::Y]
-           << " A " << ge->rx.computed << " " << ge->ry.computed
-           << " 0 1 1 " << " " << p2[NR::X] << " " << p2[NR::Y] << " z";
+        str.arcTo(rx, ry, 0, true, true, ph)
+           .arcTo(rx, ry, 0, true, true, p2)
+           .closePath();
     } else {
-        fa = (fabs(dt) > M_PI) ? 1 : 0;
-        fs = (dt > 0) ? 1 : 0;
-#ifdef ARC_VERBOSE
-        g_print("start:%g end:%g fa=%d fs=%d\n", ge->start, ge->end, fa, fs);
-#endif
+        bool fa = (fabs(dt) > M_PI);
+        bool fs = (dt > 0);
+        str.arcTo(rx, ry, 0, fa, fs, p2);
         if (ge->closed) {
-            os << "M " << p1[NR::X] << "," << p1[NR::Y]
-               << " A " << ge->rx.computed << "," << ge->ry.computed
-               << " 0 " << fa << " " << fs << " " << p2[NR::X] << "," << p2[NR::Y]
-               << " L " << ge->cx.computed << "," << ge->cy.computed << " z";
-        } else {
-            os << "M " << p1[NR::X] << "," << p1[NR::Y]
-               << " A " << ge->rx.computed << "," << ge->ry.computed
-               << " 0 " << fa << " " << fs << " " << p2[NR::X] << "," << p2[NR::Y];
-
+            NR::Point center = NR::Point(ge->cx.computed, ge->cy.computed);
+            str.lineTo(center).closePath();
         }
     }
-    repr->setAttribute("d", os.str().c_str());
+
+    repr->setAttribute("d", str.c_str());
     return true;
 }
 
@@ -704,7 +700,8 @@ sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     SPArc *arc = SP_ARC(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:path");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+        repr = xml_doc->createElement("svg:path");
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {