blob: f2a83faa04130bc750026d4c86240624c717c1d5 [file] [log] [blame]
fmalita28d5b722016-09-12 17:06:47 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/effects/SkGradientShader.h"
Florin Malitab3418102020-10-15 18:10:29 -04009#include "modules/svg/include/SkSVGLinearGradient.h"
10#include "modules/svg/include/SkSVGRenderContext.h"
11#include "modules/svg/include/SkSVGValue.h"
fmalita28d5b722016-09-12 17:06:47 -070012
13SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {}
14
Tyler Dennistona0a51462020-11-10 13:13:28 -050015bool 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));
fmalita28d5b722016-09-12 17:06:47 -070021}
22
Florin Malitadf007e12017-10-09 15:14:13 -040023sk_sp<SkShader> SkSVGLinearGradient::onMakeShader(const SkSVGRenderContext& ctx,
Florin Malitaea27de52021-01-19 12:51:12 -050024 const SkColor4f* colors, const SkScalar* pos,
Mike Reedfae8fce2019-04-03 10:27:45 -040025 int count, SkTileMode tm,
Florin Malitadf007e12017-10-09 15:14:13 -040026 const SkMatrix& localMatrix) const {
Tyler Dennistonf548a022020-10-27 15:02:02 -040027 const SkSVGLengthContext lctx =
Tyler Dennistona0a51462020-11-10 13:13:28 -050028 this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox
Tyler Dennistonf548a022020-10-27 15:02:02 -040029 ? SkSVGLengthContext({1, 1})
30 : ctx.lengthContext();
31
fmalita28d5b722016-09-12 17:06:47 -070032 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}};
fmalita28d5b722016-09-12 17:06:47 -070038
Florin Malitaea27de52021-01-19 12:51:12 -050039 return SkGradientShader::MakeLinear(pts, colors, nullptr, pos, count, tm, 0, &localMatrix);
fmalita28d5b722016-09-12 17:06:47 -070040}