fmalita | 28d5b72 | 2016-09-12 17:06:47 -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 | |
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/SkSVGLinearGradient.h" |
| 10 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 11 | #include "modules/svg/include/SkSVGValue.h" |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 12 | |
| 13 | SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {} |
| 14 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 15 | bool SkSVGLinearGradient::parseAndSetAttribute(const char* name, const char* value) { |
| 16 | return INHERITED::parseAndSetAttribute(name, value) || |
| 17 | this->setX1(SkSVGAttributeParser::parse<SkSVGLength>("x1", name, value)) || |
| 18 | this->setY1(SkSVGAttributeParser::parse<SkSVGLength>("y1", name, value)) || |
| 19 | this->setX2(SkSVGAttributeParser::parse<SkSVGLength>("x2", name, value)) || |
| 20 | this->setY2(SkSVGAttributeParser::parse<SkSVGLength>("y2", name, value)); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Florin Malita | df007e1 | 2017-10-09 15:14:13 -0400 | [diff] [blame] | 23 | sk_sp<SkShader> SkSVGLinearGradient::onMakeShader(const SkSVGRenderContext& ctx, |
Florin Malita | ea27de5 | 2021-01-19 12:51:12 -0500 | [diff] [blame] | 24 | const SkColor4f* colors, const SkScalar* pos, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 25 | int count, SkTileMode tm, |
Florin Malita | df007e1 | 2017-10-09 15:14:13 -0400 | [diff] [blame] | 26 | const SkMatrix& localMatrix) const { |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 27 | const SkSVGLengthContext lctx = |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 28 | this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox |
Tyler Denniston | f548a02 | 2020-10-27 15:02:02 -0400 | [diff] [blame] | 29 | ? SkSVGLengthContext({1, 1}) |
| 30 | : ctx.lengthContext(); |
| 31 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 32 | const auto x1 = lctx.resolve(fX1, SkSVGLengthContext::LengthType::kHorizontal); |
| 33 | const auto y1 = lctx.resolve(fY1, SkSVGLengthContext::LengthType::kVertical); |
| 34 | const auto x2 = lctx.resolve(fX2, SkSVGLengthContext::LengthType::kHorizontal); |
| 35 | const auto y2 = lctx.resolve(fY2, SkSVGLengthContext::LengthType::kVertical); |
| 36 | |
| 37 | const SkPoint pts[2] = { {x1, y1}, {x2, y2}}; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 38 | |
Florin Malita | ea27de5 | 2021-01-19 12:51:12 -0500 | [diff] [blame] | 39 | return SkGradientShader::MakeLinear(pts, colors, nullptr, pos, count, tm, 0, &localMatrix); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 40 | } |