fmalita | bffc256 | 2016-08-03 10:21:11 -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 | |
Florin Malita | c49c7f5 | 2021-03-29 13:38:38 -0400 | [diff] [blame] | 8 | #include <tuple> |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkRect.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 12 | #include "modules/svg/include/SkSVGRect.h" |
| 13 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 14 | #include "modules/svg/include/SkSVGValue.h" |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 15 | |
| 16 | SkSVGRect::SkSVGRect() : INHERITED(SkSVGTag::kRect) {} |
| 17 | |
Tyler Denniston | 52d9475 | 2021-02-05 10:23:34 -0500 | [diff] [blame] | 18 | bool SkSVGRect::parseAndSetAttribute(const char* n, const char* v) { |
| 19 | return INHERITED::parseAndSetAttribute(n, v) || |
| 20 | this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) || |
| 21 | this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) || |
| 22 | this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) || |
| 23 | this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) || |
| 24 | this->setRx(SkSVGAttributeParser::parse<SkSVGLength>("rx", n, v)) || |
| 25 | this->setRy(SkSVGAttributeParser::parse<SkSVGLength>("ry", n, v)); |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 28 | SkRRect SkSVGRect::resolve(const SkSVGLengthContext& lctx) const { |
Florin Malita | c49c7f5 | 2021-03-29 13:38:38 -0400 | [diff] [blame] | 29 | const auto rect = lctx.resolveRect(fX, fY, fWidth, fHeight); |
fmalita | 286a865 | 2016-08-10 17:11:29 -0700 | [diff] [blame] | 30 | |
Florin Malita | c49c7f5 | 2021-03-29 13:38:38 -0400 | [diff] [blame] | 31 | // https://www.w3.org/TR/SVG11/shapes.html#RectElementRXAttribute: |
| 32 | // |
| 33 | // - Let rx and ry be length values. |
| 34 | // - If neither ‘rx’ nor ‘ry’ are properly specified, then set both rx and ry to 0. |
| 35 | // - Otherwise, if a properly specified value is provided for ‘rx’, but not for ‘ry’, |
| 36 | // then set both rx and ry to the value of ‘rx’. |
| 37 | // - Otherwise, if a properly specified value is provided for ‘ry’, but not for ‘rx’, |
| 38 | // then set both rx and ry to the value of ‘ry’. |
| 39 | // - Otherwise, both ‘rx’ and ‘ry’ were specified properly. Set rx to the value of ‘rx’ |
| 40 | // and ry to the value of ‘ry’. |
| 41 | // - If rx is greater than half of ‘width’, then set rx to half of ‘width’. |
| 42 | // - If ry is greater than half of ‘height’, then set ry to half of ‘height’. |
| 43 | // - The effective values of ‘rx’ and ‘ry’ are rx and ry, respectively. |
| 44 | // |
| 45 | auto radii = [this]() { |
| 46 | return fRx.isValid() |
| 47 | ? fRy.isValid() |
| 48 | ? std::make_tuple(*fRx, *fRy) |
| 49 | : std::make_tuple(*fRx, *fRx) |
| 50 | : fRy.isValid() |
| 51 | ? std::make_tuple(*fRy, *fRy) |
| 52 | : std::make_tuple(SkSVGLength(0), SkSVGLength(0)); |
| 53 | }; |
| 54 | |
| 55 | const auto [ rxlen, rylen ] = radii(); |
| 56 | const auto rx = std::min(lctx.resolve(rxlen, SkSVGLengthContext::LengthType::kHorizontal), |
| 57 | rect.width() / 2), |
| 58 | ry = std::min(lctx.resolve(rylen, SkSVGLengthContext::LengthType::kVertical), |
| 59 | rect.height() / 2); |
| 60 | |
| 61 | return SkRRect::MakeRectXY(rect, rx, ry); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx, |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 65 | const SkPaint& paint, SkPathFillType) const { |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 66 | canvas->drawRRect(this->resolve(lctx), paint); |
| 67 | } |
| 68 | |
| 69 | SkPath SkSVGRect::onAsPath(const SkSVGRenderContext& ctx) const { |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 70 | SkPath path = SkPath::RRect(this->resolve(ctx.lengthContext())); |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 71 | |
| 72 | this->mapToParent(&path); |
| 73 | |
| 74 | return path; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 75 | } |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 76 | |
| 77 | SkRect SkSVGRect::onObjectBoundingBox(const SkSVGRenderContext& ctx) const { |
| 78 | return ctx.lengthContext().resolveRect(fX, fY, fWidth, fHeight); |
| 79 | } |