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