Flutter Engine
The Flutter Engine
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 this->forEachChild<SkSVGStop>([&](const SkSVGStop* stop) {
34 colors->push_back(this->resolveStopColor(ctx, *stop));
35 pos->push_back(
36 SkTPin(ltx.resolve(stop->getOffset(), SkSVGLengthContext::LengthType::kOther),
37 0.f, 1.f));
38 });
39
40 SkASSERT(colors->size() == pos->size());
41
42 if (pos->empty() && !fHref.iri().isEmpty()) {
43 const auto ref = ctx.findNodeById(fHref);
44 if (ref && (ref->tag() == SkSVGTag::kLinearGradient ||
45 ref->tag() == SkSVGTag::kRadialGradient)) {
46 static_cast<const SkSVGGradient*>(ref.get())->collectColorStops(ctx, pos, colors);
47 }
48 }
49}
50
51SkColor4f SkSVGGradient::resolveStopColor(const SkSVGRenderContext& ctx,
52 const SkSVGStop& stop) const {
53 const auto& stopColor = stop.getStopColor();
54 const auto& stopOpacity = stop.getStopOpacity();
55 // Uninherited presentation attrs should have a concrete value at this point.
56 if (!stopColor.isValue() || !stopOpacity.isValue()) {
57 SkDebugf("unhandled: stop-color or stop-opacity has no value\n");
58 return SkColors::kBlack;
59 }
60
61 const auto color = SkColor4f::FromColor(ctx.resolveSvgColor(*stopColor));
62
63 return { color.fR, color.fG, color.fB, *stopOpacity * color.fA };
64}
65
69
70 this->collectColorStops(ctx, &pos, &colors);
71
72 // TODO:
73 // * stop (lazy?) sorting
74 // * href loop detection
75 // * href attribute inheritance (not just color stops)
76 // * objectBoundingBox units support
77
78 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kPad) ==
79 SkTileMode::kClamp, "SkSVGSpreadMethod::Type is out of sync");
80 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kRepeat) ==
81 SkTileMode::kRepeat, "SkSVGSpreadMethod::Type is out of sync");
82 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kReflect) ==
83 SkTileMode::kMirror, "SkSVGSpreadMethod::Type is out of sync");
84 const auto tileMode = static_cast<SkTileMode>(fSpreadMethod.type());
85
86 const auto obbt = ctx.transformForCurrentOBB(fGradientUnits);
87 const auto localMatrix = SkMatrix::Translate(obbt.offset.x, obbt.offset.y)
88 * SkMatrix::Scale(obbt.scale.x, obbt.scale.y)
89 * fGradientTransform;
90
91 paint->setShader(this->onMakeShader(ctx, colors.begin(), pos.begin(), colors.size(), tileMode,
92 localMatrix));
93 return true;
94}
95
96// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementSpreadMethodAttribute
97template <>
99 static const struct {
101 const char* fName;
102 } gSpreadInfo[] = {
106 };
107
108 bool parsedValue = false;
109 for (size_t i = 0; i < std::size(gSpreadInfo); ++i) {
110 if (this->parseExpectedStringToken(gSpreadInfo[i].fName)) {
111 *spread = SkSVGSpreadMethod(gSpreadInfo[i].fType);
112 parsedValue = true;
113 break;
114 }
115 }
116
117 return parsedValue && this->parseEOSToken();
118}
const char * fName
SkPoint pos
#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)
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
Definition: color_source.cc:38
DlColor color
uint8_t value
constexpr SkColor4f kBlack
Definition: SkColor.h:435
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56