| 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/SkGradient.h" |
| 14 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 15 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 16 |
|
| 17 |
class SkMatrix; |
| 18 |
enum class SkTileMode; |
| 19 |
|
| 20 |
SkSVGRadialGradient::SkSVGRadialGradient() : INHERITED(SkSVGTag::kRadialGradient) {} |
| 21 |
|
| 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 |
|
| 31 |
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.has_value() ? lctx.resolve(*fFx, SkSVGLengthContext::LengthType::kHorizontal) |
| 46 |
: center.x(), |
| 47 |
fFy.has_value() ? 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 |
SkSpan<const float> positions; |
| 56 |
if (pos) { |
| 57 |
positions = {pos, (size_t)count}; |
| 58 |
} |
| 59 |
SkGradient grad = {{{colors, (size_t)count}, positions, tm, nullptr}, {}}; |
| 60 |
return center == focal |
| 61 |
? SkShaders::RadialGradient(center, r, grad, &m) |
| 62 |
: SkShaders::TwoPointConicalGradient(focal, 0, center, r, grad, &m); |
| 63 |
} |