blob: 49348795abe2ddaff8804e831aeaab226cb58568 [file] [log] [blame]
fmalitadc4c2a92016-08-16 15:38:51 -07001/*
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 "SkCanvas.h"
9#include "SkSVGCircle.h"
10#include "SkSVGRenderContext.h"
11#include "SkSVGValue.h"
12
13SkSVGCircle::SkSVGCircle() : INHERITED(SkSVGTag::kCircle) {}
14
15void SkSVGCircle::setCx(const SkSVGLength& cx) {
16 fCx = cx;
17}
18
19void SkSVGCircle::setCy(const SkSVGLength& cy) {
20 fCy = cy;
21}
22
23void SkSVGCircle::setR(const SkSVGLength& r) {
24 fR = r;
25}
26
27void 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
Florin Malitace8840e2016-12-08 09:26:47 -050049std::tuple<SkPoint, SkScalar> SkSVGCircle::resolve(const SkSVGLengthContext& lctx) const {
fmalitadc4c2a92016-08-16 15:38:51 -070050 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
Florin Malitace8840e2016-12-08 09:26:47 -050054 return std::make_tuple(SkPoint::Make(cx, cy), r);
55}
56void 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
fmalitadc4c2a92016-08-16 15:38:51 -070062 if (r > 0) {
Florin Malitace8840e2016-12-08 09:26:47 -050063 canvas->drawCircle(pos.x(), pos.y(), r, paint);
fmalitadc4c2a92016-08-16 15:38:51 -070064 }
65}
Florin Malitace8840e2016-12-08 09:26:47 -050066
67SkPath 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}