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