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/SkSVGGradient.h"
9
10 #include "include/core/SkM44.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h" // IWYU pragma: keep
13 #include "include/core/SkSize.h"
14 #include "include/core/SkTileMode.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/base/SkDebug.h"
17 #include "include/private/base/SkTPin.h"
18 #include "modules/svg/include/SkSVGAttributeParser.h"
19 #include "modules/svg/include/SkSVGRenderContext.h"
20 #include "modules/svg/include/SkSVGStop.h"
21
22 #include <array>
23 #include <cstddef>
24
parseAndSetAttribute(const char * name,const char * value)25 bool SkSVGGradient::parseAndSetAttribute(const char* name, const char* value) {
26 return INHERITED::parseAndSetAttribute(name, value) ||
27 this->setGradientTransform(SkSVGAttributeParser::parse<SkSVGTransformType>(
28 "gradientTransform", name, value)) ||
29 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", name, value)) ||
30 this->setSpreadMethod(
31 SkSVGAttributeParser::parse<SkSVGSpreadMethod>("spreadMethod", name, value)) ||
32 this->setGradientUnits(SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(
33 "gradientUnits", name, value));
34 }
35
36 // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementHrefAttribute
collectColorStops(const SkSVGRenderContext & ctx,StopPositionArray * pos,StopColorArray * colors) const37 void SkSVGGradient::collectColorStops(const SkSVGRenderContext& ctx,
38 StopPositionArray* pos,
39 StopColorArray* colors) const {
40 // Used to resolve percentage offsets.
41 const SkSVGLengthContext ltx(SkSize::Make(1, 1));
42
43 this->forEachChild<SkSVGStop>([&](const SkSVGStop* stop) {
44 colors->push_back(this->resolveStopColor(ctx, *stop));
45 pos->push_back(
46 SkTPin(ltx.resolve(stop->getOffset(), SkSVGLengthContext::LengthType::kOther),
47 0.f, 1.f));
48 });
49
50 SkASSERT(colors->size() == pos->size());
51
52 if (pos->empty() && !fHref.iri().isEmpty()) {
53 const auto ref = ctx.findNodeById(fHref);
54 if (ref && (ref->tag() == SkSVGTag::kLinearGradient ||
55 ref->tag() == SkSVGTag::kRadialGradient)) {
56 static_cast<const SkSVGGradient*>(ref.get())->collectColorStops(ctx, pos, colors);
57 }
58 }
59 }
60
resolveStopColor(const SkSVGRenderContext & ctx,const SkSVGStop & stop) const61 SkColor4f SkSVGGradient::resolveStopColor(const SkSVGRenderContext& ctx,
62 const SkSVGStop& stop) const {
63 const auto& stopColor = stop.getStopColor();
64 const auto& stopOpacity = stop.getStopOpacity();
65 // Uninherited presentation attrs should have a concrete value at this point.
66 if (!stopColor.isValue() || !stopOpacity.isValue()) {
67 SkDEBUGF("unhandled: stop-color or stop-opacity has no value\n");
68 return SkColors::kBlack;
69 }
70
71 const auto color = SkColor4f::FromColor(ctx.resolveSvgColor(*stopColor));
72
73 return { color.fR, color.fG, color.fB, *stopOpacity * color.fA };
74 }
75
onAsPaint(const SkSVGRenderContext & ctx,SkPaint * paint) const76 bool SkSVGGradient::onAsPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
77 StopColorArray colors;
78 StopPositionArray pos;
79
80 this->collectColorStops(ctx, &pos, &colors);
81
82 // TODO:
83 // * stop (lazy?) sorting
84 // * href loop detection
85 // * href attribute inheritance (not just color stops)
86 // * objectBoundingBox units support
87
88 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kPad) ==
89 SkTileMode::kClamp, "SkSVGSpreadMethod::Type is out of sync");
90 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kRepeat) ==
91 SkTileMode::kRepeat, "SkSVGSpreadMethod::Type is out of sync");
92 static_assert(static_cast<SkTileMode>(SkSVGSpreadMethod::Type::kReflect) ==
93 SkTileMode::kMirror, "SkSVGSpreadMethod::Type is out of sync");
94 const auto tileMode = static_cast<SkTileMode>(fSpreadMethod.type());
95
96 const auto obbt = ctx.transformForCurrentOBB(fGradientUnits);
97 const auto localMatrix = SkMatrix::Translate(obbt.offset.x, obbt.offset.y)
98 * SkMatrix::Scale(obbt.scale.x, obbt.scale.y)
99 * fGradientTransform;
100
101 paint->setShader(this->onMakeShader(ctx, colors.begin(), pos.begin(), colors.size(), tileMode,
102 localMatrix));
103 return true;
104 }
105
106 // https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementSpreadMethodAttribute
107 template <>
parse(SkSVGSpreadMethod * spread)108 bool SkSVGAttributeParser::parse(SkSVGSpreadMethod* spread) {
109 static const struct {
110 SkSVGSpreadMethod::Type fType;
111 const char* fName;
112 } gSpreadInfo[] = {
113 { SkSVGSpreadMethod::Type::kPad , "pad" },
114 { SkSVGSpreadMethod::Type::kReflect, "reflect" },
115 { SkSVGSpreadMethod::Type::kRepeat , "repeat" },
116 };
117
118 bool parsedValue = false;
119 for (size_t i = 0; i < std::size(gSpreadInfo); ++i) {
120 if (this->parseExpectedStringToken(gSpreadInfo[i].fName)) {
121 *spread = SkSVGSpreadMethod(gSpreadInfo[i].fType);
122 parsedValue = true;
123 break;
124 }
125 }
126
127 return parsedValue && this->parseEOSToken();
128 }
129