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/effects/SkGradientShader.h"
9 #include "modules/svg/include/SkSVGLinearGradient.h"
10 #include "modules/svg/include/SkSVGRenderContext.h"
11 #include "modules/svg/include/SkSVGValue.h"
12
SkSVGLinearGradient()13 SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {}
14
parseAndSetAttribute(const char * name,const char * value)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));
21 }
22
onMakeShader(const SkSVGRenderContext & ctx,const SkColor4f * colors,const SkScalar * pos,int count,SkTileMode tm,const SkMatrix & localMatrix) const23 sk_sp<SkShader> SkSVGLinearGradient::onMakeShader(const SkSVGRenderContext& ctx,
24 const SkColor4f* colors, const SkScalar* pos,
25 int count, SkTileMode tm,
26 const SkMatrix& localMatrix) const {
27 const SkSVGLengthContext lctx =
28 this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox
29 ? SkSVGLengthContext({1, 1})
30 : ctx.lengthContext();
31
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}};
38
39 return SkGradientShader::MakeLinear(pts, colors, nullptr, pos, count, tm, 0, &localMatrix);
40 }
41