vector: add length() function in vpath 79/185279/1
authorJaeun Choi <jaeun12.choi@samsung.com>
Fri, 27 Jul 2018 08:04:49 +0000 (17:04 +0900)
committerJaeun Choi <jaeun12.choi@samsung.com>
Fri, 27 Jul 2018 08:04:49 +0000 (17:04 +0900)
Change-Id: I94e2a3d7a1441f250ca722f53de018efce191c74

src/vector/vpath.cpp
src/vector/vpath.h

index cfe64ee..1724f8f 100644 (file)
@@ -26,6 +26,41 @@ void VPath::VPathData::transform(const VMatrix &m)
     }
 }
 
+float VPath::VPathData::length() const
+{
+   float len = 0.0;
+   int i = 0;
+   for (auto e : m_elements) {
+        switch (e) {
+           case VPath::Element::MoveTo:
+              i++;
+              break;
+           case VPath::Element::LineTo:
+                {
+                   VPointF p0 = m_points[i - 1];
+                   VPointF p = m_points[i++];
+                   VBezier b = VBezier::fromPoints(p0, p0, p, p);
+                   len += b.length();
+                   break;
+                }
+           case VPath::Element::CubicTo:
+                {
+                   VPointF p0 = m_points[i - 1];
+                   VPointF p = m_points[i++];
+                   VPointF p1 = m_points[i++];
+                   VPointF p2 = m_points[i++];
+                   VBezier b = VBezier::fromPoints(p0, p, p1, p2);
+                   len += b.length();
+                   break;
+                }
+           case VPath::Element::Close:
+              break;
+        }
+   }
+
+   return len;
+}
+
 void VPath::VPathData::checkNewSegment()
 {
     if (mNewSegment) {
index 14a29bd..2782861 100644 (file)
@@ -45,6 +45,7 @@ public:
     void addPolygon(float points, float radius, float roundness,
                     float startAngle, float cx, float cy, VPath::Direction dir = Direction::CW);
     void transform(const VMatrix &m);
+    float length() const;
     const std::vector<VPath::Element> &elements() const;
     const std::vector<VPointF> &points() const;
 private:
@@ -61,6 +62,7 @@ private:
         void checkNewSegment();
         int  segments() const;
         void transform(const VMatrix &m);
+        float length() const;
         void addRoundRect(const VRectF &, float, float, VPath::Direction);
         void addRect(const VRectF &, VPath::Direction);
         void arcTo(const VRectF&, float, float, bool);
@@ -118,6 +120,12 @@ inline int VPath::segments() const
     return d->segments();
 }
 
+inline float VPath::length() const
+{
+    //TODO re-calculate when there is a change
+    return d->length();
+}
+
 inline void VPath::cubicTo(const VPointF &c1, const VPointF &c2, const VPointF &e)
 {
     d.write().cubicTo(c1, c2, e);