blob: e74149c81e661ea4de8b5369a76776e2998f6702 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
Florin Malitab3418102020-10-15 18:10:29 -04009#include "modules/svg/include/SkSVGCircle.h"
10#include "modules/svg/include/SkSVGRenderContext.h"
11#include "modules/svg/include/SkSVGValue.h"
fmalitadc4c2a92016-08-16 15:38:51 -070012
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
Florin Malitaf4403e72020-04-10 14:14:04 +000027void SkSVGCircle::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalitadc4c2a92016-08-16 15:38:51 -070028 switch (attr) {
29 case SkSVGAttribute::kCx:
Florin Malitaf4403e72020-04-10 14:14:04 +000030 if (const auto* cx = v.as<SkSVGLengthValue>()) {
fmalitadc4c2a92016-08-16 15:38:51 -070031 this->setCx(*cx);
32 }
33 break;
34 case SkSVGAttribute::kCy:
Florin Malitaf4403e72020-04-10 14:14:04 +000035 if (const auto* cy = v.as<SkSVGLengthValue>()) {
fmalitadc4c2a92016-08-16 15:38:51 -070036 this->setCy(*cy);
37 }
38 break;
39 case SkSVGAttribute::kR:
Florin Malitaf4403e72020-04-10 14:14:04 +000040 if (const auto* r = v.as<SkSVGLengthValue>()) {
fmalitadc4c2a92016-08-16 15:38:51 -070041 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,
Mike Reed7d34dc72019-11-26 12:17:17 -050057 const SkPaint& paint, SkPathFillType) const {
Florin Malitace8840e2016-12-08 09:26:47 -050058 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
Mike Reed58cc97a2020-08-24 15:15:01 -040072 SkPath path = SkPath::Circle(pos.x(), pos.y(), r);
Florin Malitace8840e2016-12-08 09:26:47 -050073 this->mapToParent(&path);
74
75 return path;
76}
Tyler Denniston3a92f772021-01-12 09:28:22 -050077
78SkRect SkSVGCircle::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
79 const auto [pos, r] = this->resolve(ctx.lengthContext());
80 return SkRect::MakeXYWH(pos.fX - r, pos.fY - r, 2 * r, 2 * r);
81}