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 #include "modules/svg/include/SkSVGGradient.h"
8
9 #include "include/core/SkTileMode.h"
10 #include "include/private/base/SkTPin.h"
11 #include "modules/svg/include/SkSVGRenderContext.h"
12 #include "modules/svg/include/SkSVGStop.h"
13 #include "modules/svg/include/SkSVGValue.h"
14
parseAndSetAttribute(const char * name,const char * value)15 bool SkSVGGradient::parseAndSetAttribute(const char* name, const char* value) {
16 return INHERITED::parseAndSetAttribute(name, 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
collectColorStops(const SkSVGRenderContext & ctx,StopPositionArray * pos,StopColorArray * colors) const27 void 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
resolveStopColor(const SkSVGRenderContext & ctx,const SkSVGStop & stop) const51 SkColor4f 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
onAsPaint(const SkSVGRenderContext & ctx,SkPaint * paint) const66 bool SkSVGGradient::onAsPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
67 StopColorArray colors;
68 StopPositionArray pos;
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
97 template <>
parse(SkSVGSpreadMethod * spread)98 bool SkSVGAttributeParser::parse(SkSVGSpreadMethod* spread) {
99 static const struct {
100 SkSVGSpreadMethod::Type fType;
101 const char* fName;
102 } gSpreadInfo[] = {
103 { SkSVGSpreadMethod::Type::kPad , "pad" },
104 { SkSVGSpreadMethod::Type::kReflect, "reflect" },
105 { SkSVGSpreadMethod::Type::kRepeat , "repeat" },
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 }
119