1 /*
2 * Copyright 2017 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/SkSVGRadialGradient.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkShader.h"
13 #include "include/effects/SkGradientShader.h"
14 #include "modules/svg/include/SkSVGAttributeParser.h"
15 #include "modules/svg/include/SkSVGRenderContext.h"
16
17 class SkMatrix;
18 enum class SkTileMode;
19
SkSVGRadialGradient()20 SkSVGRadialGradient::SkSVGRadialGradient() : INHERITED(SkSVGTag::kRadialGradient) {}
21
parseAndSetAttribute(const char * name,const char * value)22 bool SkSVGRadialGradient::parseAndSetAttribute(const char* name, const char* value) {
23 return INHERITED::parseAndSetAttribute(name, value) ||
24 this->setCx(SkSVGAttributeParser::parse<SkSVGLength>("cx", name, value)) ||
25 this->setCy(SkSVGAttributeParser::parse<SkSVGLength>("cy", name, value)) ||
26 this->setR(SkSVGAttributeParser::parse<SkSVGLength>("r", name, value)) ||
27 this->setFx(SkSVGAttributeParser::parse<SkSVGLength>("fx", name, value)) ||
28 this->setFy(SkSVGAttributeParser::parse<SkSVGLength>("fy", name, value));
29 }
30
onMakeShader(const SkSVGRenderContext & ctx,const SkColor4f * colors,const SkScalar * pos,int count,SkTileMode tm,const SkMatrix & m) const31 sk_sp<SkShader> SkSVGRadialGradient::onMakeShader(const SkSVGRenderContext& ctx,
32 const SkColor4f* colors, const SkScalar* pos,
33 int count, SkTileMode tm,
34 const SkMatrix& m) const {
35 const SkSVGLengthContext lctx =
36 this->getGradientUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox
37 ? SkSVGLengthContext({1, 1})
38 : ctx.lengthContext();
39
40 const auto r = lctx.resolve(fR , SkSVGLengthContext::LengthType::kOther);
41 const auto center = SkPoint::Make(
42 lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal),
43 lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical));
44 const auto focal = SkPoint::Make(
45 fFx.isValid() ? lctx.resolve(*fFx, SkSVGLengthContext::LengthType::kHorizontal)
46 : center.x(),
47 fFy.isValid() ? lctx.resolve(*fFy, SkSVGLengthContext::LengthType::kVertical)
48 : center.y());
49
50 if (r == 0) {
51 const auto last_color = count > 0 ? colors[count - 1] : SkColors::kBlack;
52 return SkShaders::Color(last_color, nullptr);
53 }
54
55 return center == focal
56 ? SkGradientShader::MakeRadial(center, r, colors, nullptr, pos, count, tm, 0, &m)
57 : SkGradientShader::MakeTwoPointConical(focal, 0, center, r, colors, nullptr, pos,
58 count, tm, 0, &m);
59 }
60