[SVGDom] Add <circle>, <ellipse> support
authorfmalita <fmalita@chromium.org>
Tue, 16 Aug 2016 22:38:51 +0000 (15:38 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 16 Aug 2016 22:38:51 +0000 (15:38 -0700)
R=robertphillips@google.com,stephana@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2249033003

Review-Url: https://codereview.chromium.org/2249033003

BUILD.gn
experimental/svg/model/SkSVGAttribute.h
experimental/svg/model/SkSVGCircle.cpp [new file with mode: 0644]
experimental/svg/model/SkSVGCircle.h [new file with mode: 0644]
experimental/svg/model/SkSVGDOM.cpp
experimental/svg/model/SkSVGEllipse.cpp [new file with mode: 0644]
experimental/svg/model/SkSVGEllipse.h [new file with mode: 0644]
experimental/svg/model/SkSVGNode.h
gyp/svg.gyp

index d7dd484..8bfc984 100644 (file)
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -521,7 +521,9 @@ test_lib("experimental_svg_model") {
     "experimental/svg/model/SkSVGAttribute.cpp",
     "experimental/svg/model/SkSVGAttributeParser.cpp",
     "experimental/svg/model/SkSVGContainer.cpp",
+    "experimental/svg/model/SkSVGCircle.cpp",
     "experimental/svg/model/SkSVGDOM.cpp",
+    "experimental/svg/model/SkSVGEllipse.cpp",
     "experimental/svg/model/SkSVGNode.cpp",
     "experimental/svg/model/SkSVGPath.cpp",
     "experimental/svg/model/SkSVGPoly.cpp",
index cc3e7a0..c94a239 100644 (file)
 class SkSVGRenderContext;
 
 enum class SkSVGAttribute {
+    kCx, // <circle>,<ellipse>: center x position
+    kCy, // <circle>,<ellipse>: center y position
     kD,
     kFill,
     kFillOpacity,
     kHeight,
     kOpacity,
     kPoints,
-    kRx,
-    kRy,
+    kR,  // <circle>: radius
+    kRx, // <ellipse>,<rect>: horizontal (corner) radius
+    kRy, // <ellipse>,<rect>: vertical (corner) radius
     kStroke,
     kStrokeOpacity,
     kStrokeLineCap,
diff --git a/experimental/svg/model/SkSVGCircle.cpp b/experimental/svg/model/SkSVGCircle.cpp
new file mode 100644 (file)
index 0000000..692cd9f
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCanvas.h"
+#include "SkSVGCircle.h"
+#include "SkSVGRenderContext.h"
+#include "SkSVGValue.h"
+
+SkSVGCircle::SkSVGCircle() : INHERITED(SkSVGTag::kCircle) {}
+
+void SkSVGCircle::setCx(const SkSVGLength& cx) {
+    fCx = cx;
+}
+
+void SkSVGCircle::setCy(const SkSVGLength& cy) {
+    fCy = cy;
+}
+
+void SkSVGCircle::setR(const SkSVGLength& r) {
+    fR = r;
+}
+
+void SkSVGCircle::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
+    switch (attr) {
+    case SkSVGAttribute::kCx:
+        if (const auto* cx = v.as<SkSVGLengthValue>()) {
+            this->setCx(*cx);
+        }
+        break;
+    case SkSVGAttribute::kCy:
+        if (const auto* cy = v.as<SkSVGLengthValue>()) {
+            this->setCy(*cy);
+        }
+        break;
+    case SkSVGAttribute::kR:
+        if (const auto* r = v.as<SkSVGLengthValue>()) {
+            this->setR(*r);
+        }
+        break;
+    default:
+        this->INHERITED::onSetAttribute(attr, v);
+    }
+}
+
+void SkSVGCircle::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
+                         const SkPaint& paint) const {
+    const auto cx = lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal);
+    const auto cy = lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical);
+    const auto  r = lctx.resolve(fR , SkSVGLengthContext::LengthType::kOther);
+
+    if (r > 0) {
+        canvas->drawCircle(cx, cy, r, paint);
+    }
+}
diff --git a/experimental/svg/model/SkSVGCircle.h b/experimental/svg/model/SkSVGCircle.h
new file mode 100644 (file)
index 0000000..ef1bd2a
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSVGCircle_DEFINED
+#define SkSVGCircle_DEFINED
+
+#include "SkSVGShape.h"
+#include "SkSVGTypes.h"
+
+class SkSVGCircle final : public SkSVGShape {
+public:
+    virtual ~SkSVGCircle() = default;
+    static sk_sp<SkSVGCircle> Make() { return sk_sp<SkSVGCircle>(new SkSVGCircle()); }
+
+    void setCx(const SkSVGLength&);
+    void setCy(const SkSVGLength&);
+    void setR(const SkSVGLength&);
+
+protected:
+    void onSetAttribute(SkSVGAttribute, const SkSVGValue&) override;
+
+    void onDraw(SkCanvas*, const SkSVGLengthContext&, const SkPaint&) const override;
+
+private:
+    SkSVGCircle();
+
+    SkSVGLength fCx = SkSVGLength(0);
+    SkSVGLength fCy = SkSVGLength(0);
+    SkSVGLength fR  = SkSVGLength(0);
+
+    typedef SkSVGShape INHERITED;
+};
+
+#endif // SkSVGCircle_DEFINED
index 8e2667a..ef1bb0d 100644 (file)
@@ -10,7 +10,9 @@
 #include "SkParsePath.h"
 #include "SkString.h"
 #include "SkSVGAttributeParser.h"
+#include "SkSVGCircle.h"
 #include "SkSVGDOM.h"
+#include "SkSVGEllipse.h"
 #include "SkSVGG.h"
 #include "SkSVGNode.h"
 #include "SkSVGPath.h"
@@ -213,12 +215,15 @@ struct AttrParseInfo {
 };
 
 SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
+    { "cx"             , { SkSVGAttribute::kCx            , SetLengthAttribute    }},
+    { "cy"             , { SkSVGAttribute::kCy            , SetLengthAttribute    }},
     { "d"              , { SkSVGAttribute::kD             , SetPathDataAttribute  }},
     { "fill"           , { SkSVGAttribute::kFill          , SetPaintAttribute     }},
     { "fill-opacity"   , { SkSVGAttribute::kFillOpacity   , SetNumberAttribute    }},
     { "height"         , { SkSVGAttribute::kHeight        , SetLengthAttribute    }},
     { "opacity"        , { SkSVGAttribute::kOpacity       , SetNumberAttribute    }},
     { "points"         , { SkSVGAttribute::kPoints        , SetPointsAttribute    }},
+    { "r"              , { SkSVGAttribute::kR             , SetLengthAttribute    }},
     { "rx"             , { SkSVGAttribute::kRx            , SetLengthAttribute    }},
     { "ry"             , { SkSVGAttribute::kRy            , SetLengthAttribute    }},
     { "stroke"         , { SkSVGAttribute::kStroke        , SetPaintAttribute     }},
@@ -235,6 +240,8 @@ SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
 };
 
 SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
+    { "circle"  , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make();       }},
+    { "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make();      }},
     { "g"       , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make();            }},
     { "path"    , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make();         }},
     { "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon();  }},
diff --git a/experimental/svg/model/SkSVGEllipse.cpp b/experimental/svg/model/SkSVGEllipse.cpp
new file mode 100644 (file)
index 0000000..a70a8e0
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCanvas.h"
+#include "SkSVGEllipse.h"
+#include "SkSVGRenderContext.h"
+#include "SkSVGValue.h"
+
+SkSVGEllipse::SkSVGEllipse() : INHERITED(SkSVGTag::kEllipse) {}
+
+void SkSVGEllipse::setCx(const SkSVGLength& cx) {
+    fCx = cx;
+}
+
+void SkSVGEllipse::setCy(const SkSVGLength& cy) {
+    fCy = cy;
+}
+
+void SkSVGEllipse::setRx(const SkSVGLength& rx) {
+    fRx = rx;
+}
+
+void SkSVGEllipse::setRy(const SkSVGLength& ry) {
+    fRy = ry;
+}
+
+void SkSVGEllipse::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
+    switch (attr) {
+    case SkSVGAttribute::kCx:
+        if (const auto* cx = v.as<SkSVGLengthValue>()) {
+            this->setCx(*cx);
+        }
+        break;
+    case SkSVGAttribute::kCy:
+        if (const auto* cy = v.as<SkSVGLengthValue>()) {
+            this->setCy(*cy);
+        }
+        break;
+    case SkSVGAttribute::kRx:
+        if (const auto* rx = v.as<SkSVGLengthValue>()) {
+            this->setRx(*rx);
+        }
+        break;
+    case SkSVGAttribute::kRy:
+        if (const auto* ry = v.as<SkSVGLengthValue>()) {
+            this->setRy(*ry);
+        }
+        break;
+    default:
+        this->INHERITED::onSetAttribute(attr, v);
+    }
+}
+
+void SkSVGEllipse::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
+                          const SkPaint& paint) const {
+    const auto cx = lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal);
+    const auto cy = lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical);
+    const auto rx = lctx.resolve(fRx, SkSVGLengthContext::LengthType::kHorizontal);
+    const auto ry = lctx.resolve(fRy, SkSVGLengthContext::LengthType::kVertical);
+
+    if (rx > 0 && ry > 0) {
+        canvas->drawOval(SkRect::MakeXYWH(cx - rx / 2, cy - ry / 2, rx * 2, ry * 2), paint);
+    }
+}
diff --git a/experimental/svg/model/SkSVGEllipse.h b/experimental/svg/model/SkSVGEllipse.h
new file mode 100644 (file)
index 0000000..640d7f6
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSVGEllipse_DEFINED
+#define SkSVGEllipse_DEFINED
+
+#include "SkSVGShape.h"
+#include "SkSVGTypes.h"
+
+class SkSVGEllipse final : public SkSVGShape {
+public:
+    virtual ~SkSVGEllipse() = default;
+    static sk_sp<SkSVGEllipse> Make() { return sk_sp<SkSVGEllipse>(new SkSVGEllipse()); }
+
+    void setCx(const SkSVGLength&);
+    void setCy(const SkSVGLength&);
+    void setRx(const SkSVGLength&);
+    void setRy(const SkSVGLength&);
+
+protected:
+    void onSetAttribute(SkSVGAttribute, const SkSVGValue&) override;
+
+    void onDraw(SkCanvas*, const SkSVGLengthContext&, const SkPaint&) const override;
+
+private:
+    SkSVGEllipse();
+
+    SkSVGLength fCx = SkSVGLength(0);
+    SkSVGLength fCy = SkSVGLength(0);
+    SkSVGLength fRx = SkSVGLength(0);
+    SkSVGLength fRy = SkSVGLength(0);
+
+    typedef SkSVGShape INHERITED;
+};
+
+#endif // SkSVGEllipse_DEFINED
index 0f95d77..01ecdbe 100644 (file)
@@ -17,6 +17,8 @@ class SkSVGRenderContext;
 class SkSVGValue;
 
 enum class SkSVGTag {
+    kCircle,
+    kEllipse,
     kG,
     kPath,
     kPolygon,
index 50e4dbe..0006903 100644 (file)
         '../experimental/svg/model/SkSVGAttribute.cpp',
         '../experimental/svg/model/SkSVGAttributeParser.h',
         '../experimental/svg/model/SkSVGAttributeParser.cpp',
+        '../experimental/svg/model/SkSVGCircle.h',
+        '../experimental/svg/model/SkSVGCircle.cpp',
         '../experimental/svg/model/SkSVGContainer.h',
         '../experimental/svg/model/SkSVGContainer.cpp',
         '../experimental/svg/model/SkSVGDOM.h',
         '../experimental/svg/model/SkSVGDOM.cpp',
+        '../experimental/svg/model/SkSVGEllipse.h',
+        '../experimental/svg/model/SkSVGEllipse.cpp',
         '../experimental/svg/model/SkSVGG.h',
         '../experimental/svg/model/SkSVGNode.h',
         '../experimental/svg/model/SkSVGNode.cpp',