diff options
author | fmalita <fmalita@chromium.org> | 2016-08-16 15:38:51 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-08-16 15:38:51 -0700 |
commit | dc4c2a9fb7210925df7cc5c142b691e6c817ca77 (patch) | |
tree | dced094ef0ebd63d084cbcb14d5cc67ac811f01e /experimental | |
parent | 36931c2b47f81db7ccf441937567c6fda72ad3e6 (diff) |
[SVGDom] Add <circle>, <ellipse> support
R=robertphillips@google.com,stephana@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2249033003
Review-Url: https://codereview.chromium.org/2249033003
Diffstat (limited to 'experimental')
-rw-r--r-- | experimental/svg/model/SkSVGAttribute.h | 7 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGCircle.cpp | 58 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGCircle.h | 38 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGDOM.cpp | 7 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGEllipse.cpp | 68 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGEllipse.h | 40 | ||||
-rw-r--r-- | experimental/svg/model/SkSVGNode.h | 2 |
7 files changed, 218 insertions, 2 deletions
diff --git a/experimental/svg/model/SkSVGAttribute.h b/experimental/svg/model/SkSVGAttribute.h index cc3e7a073c..c94a239712 100644 --- a/experimental/svg/model/SkSVGAttribute.h +++ b/experimental/svg/model/SkSVGAttribute.h @@ -14,14 +14,17 @@ 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 index 0000000000..692cd9ff54 --- /dev/null +++ b/experimental/svg/model/SkSVGCircle.cpp @@ -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 index 0000000000..ef1bd2aadb --- /dev/null +++ b/experimental/svg/model/SkSVGCircle.h @@ -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 diff --git a/experimental/svg/model/SkSVGDOM.cpp b/experimental/svg/model/SkSVGDOM.cpp index 8e2667ad20..ef1bb0d0ae 100644 --- a/experimental/svg/model/SkSVGDOM.cpp +++ b/experimental/svg/model/SkSVGDOM.cpp @@ -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 index 0000000000..a70a8e019c --- /dev/null +++ b/experimental/svg/model/SkSVGEllipse.cpp @@ -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 index 0000000000..640d7f6f26 --- /dev/null +++ b/experimental/svg/model/SkSVGEllipse.h @@ -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 diff --git a/experimental/svg/model/SkSVGNode.h b/experimental/svg/model/SkSVGNode.h index 0f95d77a6f..01ecdbe985 100644 --- a/experimental/svg/model/SkSVGNode.h +++ b/experimental/svg/model/SkSVGNode.h @@ -17,6 +17,8 @@ class SkSVGRenderContext; class SkSVGValue; enum class SkSVGTag { + kCircle, + kEllipse, kG, kPath, kPolygon, |