Debian Codegraph @

modules/svg/src/SkSVGLinearGradient.cpp

raw
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/SkGradient.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
20 SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {}
21
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
30 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 SkSpan<const float> positions;
47 if (pos) {
48 positions = {pos, (size_t)count};
49 }
50 SkGradient grad = {{{colors, (size_t)count}, positions, tm, nullptr}, {}};
51 return SkShaders::LinearGradient(pts, grad, &localMatrix);
52 }