[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
This commit is contained in:
parent
36931c2b47
commit
dc4c2a9fb7
2
BUILD.gn
2
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",
|
||||
|
@ -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,
|
||||
|
58
experimental/svg/model/SkSVGCircle.cpp
Normal file
58
experimental/svg/model/SkSVGCircle.cpp
Normal file
@ -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);
|
||||
}
|
||||
}
|
38
experimental/svg/model/SkSVGCircle.h
Normal file
38
experimental/svg/model/SkSVGCircle.h
Normal file
@ -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
|
@ -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(); }},
|
||||
|
68
experimental/svg/model/SkSVGEllipse.cpp
Normal file
68
experimental/svg/model/SkSVGEllipse.cpp
Normal file
@ -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);
|
||||
}
|
||||
}
|
40
experimental/svg/model/SkSVGEllipse.h
Normal file
40
experimental/svg/model/SkSVGEllipse.h
Normal file
@ -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
|
@ -17,6 +17,8 @@ class SkSVGRenderContext;
|
||||
class SkSVGValue;
|
||||
|
||||
enum class SkSVGTag {
|
||||
kCircle,
|
||||
kEllipse,
|
||||
kG,
|
||||
kPath,
|
||||
kPolygon,
|
||||
|
@ -48,10 +48,14 @@
|
||||
'../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',
|
||||
|
Loading…
Reference in New Issue
Block a user