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