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 "include/core/SkCanvas.h"
9 #include "modules/svg/include/SkSVGCircle.h"
10 #include "modules/svg/include/SkSVGRenderContext.h"
11 #include "modules/svg/include/SkSVGValue.h"
12
SkSVGCircle()13 SkSVGCircle::SkSVGCircle() : INHERITED(SkSVGTag::kCircle) {}
14
parseAndSetAttribute(const char * n,const char * v)15 bool SkSVGCircle::parseAndSetAttribute(const char* n, const char* v) {
16 return INHERITED::parseAndSetAttribute(n, v) ||
17 this->setCx(SkSVGAttributeParser::parse<SkSVGLength>("cx", n, v)) ||
18 this->setCy(SkSVGAttributeParser::parse<SkSVGLength>("cy", n, v)) ||
19 this->setR(SkSVGAttributeParser::parse<SkSVGLength>("r", n, v));
20 }
21
resolve(const SkSVGLengthContext & lctx) const22 std::tuple<SkPoint, SkScalar> SkSVGCircle::resolve(const SkSVGLengthContext& lctx) const {
23 const auto cx = lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal);
24 const auto cy = lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical);
25 const auto r = lctx.resolve(fR , SkSVGLengthContext::LengthType::kOther);
26
27 return std::make_tuple(SkPoint::Make(cx, cy), r);
28 }
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPathFillType) const29 void SkSVGCircle::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
30 const SkPaint& paint, SkPathFillType) const {
31 SkPoint pos;
32 SkScalar r;
33 std::tie(pos, r) = this->resolve(lctx);
34
35 if (r > 0) {
36 canvas->drawCircle(pos.x(), pos.y(), r, paint);
37 }
38 }
39
onAsPath(const SkSVGRenderContext & ctx) const40 SkPath SkSVGCircle::onAsPath(const SkSVGRenderContext& ctx) const {
41 SkPoint pos;
42 SkScalar r;
43 std::tie(pos, r) = this->resolve(ctx.lengthContext());
44
45 SkPath path = SkPath::Circle(pos.x(), pos.y(), r);
46 this->mapToParent(&path);
47
48 return path;
49 }
50
onObjectBoundingBox(const SkSVGRenderContext & ctx) const51 SkRect SkSVGCircle::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
52 const auto [pos, r] = this->resolve(ctx.lengthContext());
53 return SkRect::MakeXYWH(pos.fX - r, pos.fY - r, 2 * r, 2 * r);
54 }
55