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 "include/core/SkColorSpace.h"
9 #include "include/effects/SkGradientShader.h"
10 #include "modules/svg/include/SkSVGLinearGradient.h"
11 #include "modules/svg/include/SkSVGRenderContext.h"
12 #include "modules/svg/include/SkSVGValue.h"
13
SkSVGLinearGradient()14 SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {}
15
parseAndSetAttribute(const char * name,const char * value)16 bool SkSVGLinearGradient::parseAndSetAttribute(const char* name, const char* value) {
17 return INHERITED::parseAndSetAttribute(name, value) ||
18 this->setX1(SkSVGAttributeParser::parse<SkSVGLength>("x1", name, value)) ||
19 this->setY1(SkSVGAttributeParser::parse<SkSVGLength>("y1", name, value)) ||
20 this->setX2(SkSVGAttributeParser::parse<SkSVGLength>("x2", name, value)) ||
21 this->setY2(SkSVGAttributeParser::parse<SkSVGLength>("y2", name, value));
22 }
23
onMakeShader(const SkSVGRenderContext & ctx,const SkColor4f * colors,const SkScalar * pos,int count,SkTileMode tm,const SkMatrix & localMatrix) const24 sk_sp<SkShader> SkSVGLinearGradient::onMakeShader(const SkSVGRenderContext& ctx,
25 const SkColor4f* colors, const SkScalar* pos,
26 int count, SkTileMode tm,
27 const SkMatrix& localMatrix) const {
28 const SkSVGLengthContext lctx =
29 this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox
30 ? SkSVGLengthContext({1, 1})
31 : ctx.lengthContext();
32
33 const auto x1 = lctx.resolve(fX1, SkSVGLengthContext::LengthType::kHorizontal);
34 const auto y1 = lctx.resolve(fY1, SkSVGLengthContext::LengthType::kVertical);
35 const auto x2 = lctx.resolve(fX2, SkSVGLengthContext::LengthType::kHorizontal);
36 const auto y2 = lctx.resolve(fY2, SkSVGLengthContext::LengthType::kVertical);
37
38 const SkPoint pts[2] = { {x1, y1}, {x2, y2}};
39
40 return SkGradientShader::MakeLinear(pts, colors, nullptr, pos, count, tm, 0, &localMatrix);
41 }
42