Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/effects/SkGradientShader.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 9 | #include "modules/svg/include/SkSVGRadialGradient.h" |
| 10 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 11 | #include "modules/svg/include/SkSVGValue.h" |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 12 | |
| 13 | SkSVGRadialGradient::SkSVGRadialGradient() : INHERITED(SkSVGTag::kRadialGradient) {} |
| 14 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 15 | bool SkSVGRadialGradient::parseAndSetAttribute(const char* name, const char* value) { |
| 16 | return INHERITED::parseAndSetAttribute(name, value) || |
| 17 | this->setCx(SkSVGAttributeParser::parse<SkSVGLength>("cx", name, value)) || |
| 18 | this->setCy(SkSVGAttributeParser::parse<SkSVGLength>("cy", name, value)) || |
| 19 | this->setR(SkSVGAttributeParser::parse<SkSVGLength>("r", name, value)) || |
| 20 | this->setFx(SkSVGAttributeParser::parse<SkSVGLength>("fx", name, value)) || |
| 21 | this->setFy(SkSVGAttributeParser::parse<SkSVGLength>("fy", name, value)); |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | sk_sp<SkShader> SkSVGRadialGradient::onMakeShader(const SkSVGRenderContext& ctx, |
Florin Malita | ea27de5 | 2021-01-19 12:51:12 -0500 | [diff] [blame] | 25 | const SkColor4f* colors, const SkScalar* pos, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 26 | int count, SkTileMode tm, |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 27 | const SkMatrix& m) const { |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 28 | const SkSVGLengthContext lctx = |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 29 | this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 30 | ? SkSVGLengthContext({1, 1}) |
| 31 | : ctx.lengthContext(); |
| 32 | |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 33 | const auto r = lctx.resolve(fR , SkSVGLengthContext::LengthType::kOther); |
| 34 | const auto center = SkPoint::Make( |
| 35 | lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal), |
| 36 | lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical)); |
| 37 | const auto focal = SkPoint::Make( |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 38 | fFx.isValid() ? lctx.resolve(*fFx, SkSVGLengthContext::LengthType::kHorizontal) |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 39 | : center.x(), |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 40 | fFy.isValid() ? lctx.resolve(*fFy, SkSVGLengthContext::LengthType::kVertical) |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 41 | : center.y()); |
| 42 | |
Florin Malita | 9a5aa0a | 2020-11-20 15:35:09 -0500 | [diff] [blame] | 43 | if (r == 0) { |
Florin Malita | ea27de5 | 2021-01-19 12:51:12 -0500 | [diff] [blame] | 44 | const auto last_color = count > 0 ? colors[count - 1] : SkColors::kBlack; |
| 45 | return SkShaders::Color(last_color, nullptr); |
Florin Malita | 9a5aa0a | 2020-11-20 15:35:09 -0500 | [diff] [blame] | 46 | } |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 47 | |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 48 | return center == focal |
Florin Malita | ea27de5 | 2021-01-19 12:51:12 -0500 | [diff] [blame] | 49 | ? SkGradientShader::MakeRadial(center, r, colors, nullptr, pos, count, tm, 0, &m) |
| 50 | : SkGradientShader::MakeTwoPointConical(focal, 0, center, r, colors, nullptr, pos, |
| 51 | count, tm, 0, &m); |
Florin Malita | cc6cc29 | 2017-10-09 16:05:30 -0400 | [diff] [blame] | 52 | } |