Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSVGGradient.cpp
Go to the documentation of this file.
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 */
8
14
15bool SkSVGGradient::parseAndSetAttribute(const char* name, const char* value) {
17 this->setGradientTransform(SkSVGAttributeParser::parse<SkSVGTransformType>(
18 "gradientTransform", name, value)) ||
19 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", name, value)) ||
20 this->setSpreadMethod(
21 SkSVGAttributeParser::parse<SkSVGSpreadMethod>("spreadMethod", name, value)) ||
22 this->setGradientUnits(SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(
23 "gradientUnits", name, value));
24}
25
26// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementHrefAttribute
27void SkSVGGradient::collectColorStops(const SkSVGRenderContext& ctx,
28 StopPositionArray* pos,
29 StopColorArray* colors) const {
30 // Used to resolve percentage offsets.
31 const SkSVGLengthContext ltx(SkSize::Make(1, 1));
32
33 for (const auto& child : fChildren) {
34 if (child->tag() != SkSVGTag::kStop) {
35 continue;
36 }
37
38 const auto& stop = static_cast<const SkSVGStop&>(*child);
39 colors->push_back(this->resolveStopColor(ctx, stop));
40 pos->push_back(SkTPin(ltx.resolve(stop.getOffset(), SkSVGLengthContext::LengthType::kOther),
41 0.f, 1.f));
42 }
43
44 SkASSERT(colors->size() == pos->size());
45
46 if (pos->empty() && !fHref.iri().isEmpty()) {
47 const auto ref = ctx.findNodeById(fHref);
48 if (ref && (ref->tag() == SkSVGTag::kLinearGradient ||
49 ref->tag() == SkSVGTag::kRadialGradient)) {
50 static_cast<const SkSVGGradient*>(ref.get())->collectColorStops(ctx, pos, colors);
51 }
52 }
53}
54
55SkColor4f SkSVGGradient::resolveStopColor(const SkSVGRenderContext& ctx,
56 const SkSVGStop& stop) const {
57 const auto& stopColor = stop.getStopColor();
58 const auto& stopOpacity = stop.getStopOpacity();
59 // Uninherited presentation attrs should have a concrete value at this point.
60 if (!stopColor.isValue() || !stopOpacity.isValue()) {
61 SkDebugf("unhandled: stop-color or stop-opacity has no value\n");
62 return SkColors::kBlack;
63 }
64
65 const auto color = SkColor4f::FromColor(ctx.resolveSvgColor(*stopColor));
66
67 return { color.fR, color.fG, color.fB, *stopOpacity * color.fA };
68}
69
71 StopColorArray colors;
73
74 this->collectColorStops(ctx, &pos, &colors);
75
76 // TODO:
77 // * stop (lazy?) sorting
78 // * href loop detection
79 // * href attribute inheritance (not just color stops)
80 // * objectBoundingBox units support
81
82 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kPad) ==
83 SkTileMode::kClamp, "SkSVGSpreadMethod::Type is out of sync");
84 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kRepeat) ==
85 SkTileMode::kRepeat, "SkSVGSpreadMethod::Type is out of sync");
86 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kReflect) ==
87 SkTileMode::kMirror, "SkSVGSpreadMethod::Type is out of sync");
88 const auto tileMode = static_cast<SkTileMode>(fSpreadMethod.type());
89
90 const auto obbt = ctx.transformForCurrentOBB(fGradientUnits);
91 const auto localMatrix = SkMatrix::Translate(obbt.offset.x, obbt.offset.y)
92 * SkMatrix::Scale(obbt.scale.x, obbt.scale.y)
93 * fGradientTransform;
94
95 paint->setShader(this->onMakeShader(ctx, colors.begin(), pos.begin(), colors.size(), tileMode,
96 localMatrix));
97 return true;
98}
99
100// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementSpreadMethodAttribute
101template <>
103 static const struct {
105 const char* fName;
106 } gSpreadInfo[] = {
110 };
111
112 bool parsedValue = false;
113 for (size_t i = 0; i < std::size(gSpreadInfo); ++i) {
114 if (this->parseExpectedStringToken(gSpreadInfo[i].fName)) {
115 *spread = SkSVGSpreadMethod(gSpreadInfo[i].fType);
116 parsedValue = true;
117 break;
118 }
119 }
120
121 return parsedValue && this->parseEOSToken();
122}
const char * fName
SkPoint pos
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
@ kLinearGradient
@ kRadialGradient
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
SkTileMode
Definition SkTileMode.h:13
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
void ref() const
Definition SkRefCnt.h:62
bool parse(SkSVGIntegerType *v)
skia_private::STArray< 1, sk_sp< SkSVGNode >, true > fChildren
virtual sk_sp< SkShader > onMakeShader(const SkSVGRenderContext &, const SkColor4f *, const SkScalar *, int count, SkTileMode, const SkMatrix &localMatrix) const =0
bool onAsPaint(const SkSVGRenderContext &, SkPaint *) const final
bool parseAndSetAttribute(const char *, const char *) override
virtual bool parseAndSetAttribute(const char *name, const char *value)
Definition SkSVGNode.cpp:90
SkSVGColorType resolveSvgColor(const SkSVGColor &) const
OBBTransform transformForCurrentOBB(SkSVGObjectBoundingBoxUnits) const
BorrowedNode findNodeById(const SkSVGIRI &) const
const Paint & paint
uint8_t value
const char * name
Definition fuchsia.cc:50
constexpr SkColor4f kBlack
Definition SkColor.h:435
PODArray< SkColor > colors
Definition SkRecords.h:276
static SkRGBA4f FromColor(SkColor color)
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56