fmalita | dc4c2a9 | 2016-08-16 15:38:51 -0700 | [diff] [blame] | 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 "SkCanvas.h" |
| 9 | #include "SkSVGCircle.h" |
| 10 | #include "SkSVGRenderContext.h" |
| 11 | #include "SkSVGValue.h" |
| 12 | |
| 13 | SkSVGCircle::SkSVGCircle() : INHERITED(SkSVGTag::kCircle) {} |
| 14 | |
| 15 | void SkSVGCircle::setCx(const SkSVGLength& cx) { |
| 16 | fCx = cx; |
| 17 | } |
| 18 | |
| 19 | void SkSVGCircle::setCy(const SkSVGLength& cy) { |
| 20 | fCy = cy; |
| 21 | } |
| 22 | |
| 23 | void SkSVGCircle::setR(const SkSVGLength& r) { |
| 24 | fR = r; |
| 25 | } |
| 26 | |
| 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 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 49 | std::tuple<SkPoint, SkScalar> SkSVGCircle::resolve(const SkSVGLengthContext& lctx) const { |
fmalita | dc4c2a9 | 2016-08-16 15:38:51 -0700 | [diff] [blame] | 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 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 54 | return std::make_tuple(SkPoint::Make(cx, cy), r); |
| 55 | } |
| 56 | 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 | |
fmalita | dc4c2a9 | 2016-08-16 15:38:51 -0700 | [diff] [blame] | 62 | if (r > 0) { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 63 | canvas->drawCircle(pos.x(), pos.y(), r, paint); |
fmalita | dc4c2a9 | 2016-08-16 15:38:51 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 66 | |
| 67 | 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 | } |