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