1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "experimental/svg/model/SkSVGCircle.h"
9 #include "experimental/svg/model/SkSVGRenderContext.h"
10 #include "experimental/svg/model/SkSVGValue.h"
11 #include "include/core/SkCanvas.h"
12
SkSVGCircle()13 SkSVGCircle::SkSVGCircle() : INHERITED(SkSVGTag::kCircle) {}
14
setCx(const SkSVGLength & cx)15 void SkSVGCircle::setCx(const SkSVGLength& cx) {
16 fCx = cx;
17 }
18
setCy(const SkSVGLength & cy)19 void SkSVGCircle::setCy(const SkSVGLength& cy) {
20 fCy = cy;
21 }
22
setR(const SkSVGLength & r)23 void SkSVGCircle::setR(const SkSVGLength& r) {
24 fR = r;
25 }
26
onSetAttribute(SkSVGAttribute attr,const SkSVGValue & v)27 void SkSVGCircle::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
28 switch (attr) {
29 case SkSVGAttribute::kCx:
30 if (const auto* cx = v.as<SkSVGLengthValue>()) {
31 this->setCx(*cx);
32 }
33 break;
34 case SkSVGAttribute::kCy:
35 if (const auto* cy = v.as<SkSVGLengthValue>()) {
36 this->setCy(*cy);
37 }
38 break;
39 case SkSVGAttribute::kR:
40 if (const auto* r = v.as<SkSVGLengthValue>()) {
41 this->setR(*r);
42 }
43 break;
44 default:
45 this->INHERITED::onSetAttribute(attr, v);
46 }
47 }
48
resolve(const SkSVGLengthContext & lctx) const49 std::tuple<SkPoint, SkScalar> SkSVGCircle::resolve(const SkSVGLengthContext& lctx) const {
50 const auto cx = lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal);
51 const auto cy = lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical);
52 const auto r = lctx.resolve(fR , SkSVGLengthContext::LengthType::kOther);
53
54 return std::make_tuple(SkPoint::Make(cx, cy), r);
55 }
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPath::FillType) const56 void SkSVGCircle::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
57 const SkPaint& paint, SkPath::FillType) const {
58 SkPoint pos;
59 SkScalar r;
60 std::tie(pos, r) = this->resolve(lctx);
61
62 if (r > 0) {
63 canvas->drawCircle(pos.x(), pos.y(), r, paint);
64 }
65 }
66
onAsPath(const SkSVGRenderContext & ctx) const67 SkPath SkSVGCircle::onAsPath(const SkSVGRenderContext& ctx) const {
68 SkPoint pos;
69 SkScalar r;
70 std::tie(pos, r) = this->resolve(ctx.lengthContext());
71
72 SkPath path;
73 path.addCircle(pos.x(), pos.y(), r);
74 this->mapToParent(&path);
75
76 return path;
77 }
78