blob: 289c5e327602c615e984e38716eb3c12c8a24439 [file] [log] [blame]
fmalita28d5b722016-09-12 17:06:47 -07001/*
2 * Copyright 2016 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 "SkGradientShader.h"
9#include "SkSVGLinearGradient.h"
10#include "SkSVGRenderContext.h"
11#include "SkSVGStop.h"
12#include "SkSVGValue.h"
13
14SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {}
15
16void SkSVGLinearGradient::setHref(const SkSVGStringType& href) {
17 fHref = std::move(href);
18}
19
fmalitacecd6172016-09-13 12:56:11 -070020void SkSVGLinearGradient::setSpreadMethod(const SkSVGSpreadMethod& spread) {
21 fSpreadMethod = spread;
22}
23
fmalita28d5b722016-09-12 17:06:47 -070024void SkSVGLinearGradient::setX1(const SkSVGLength& x1) {
25 fX1 = x1;
26}
27
28void SkSVGLinearGradient::setY1(const SkSVGLength& y1) {
29 fY1 = y1;
30}
31
32void SkSVGLinearGradient::setX2(const SkSVGLength& x2) {
33 fX2 = x2;
34}
35
36void SkSVGLinearGradient::setY2(const SkSVGLength& y2) {
37 fY2 = y2;
38}
39
40void SkSVGLinearGradient::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
41 switch (attr) {
42 case SkSVGAttribute::kHref:
43 if (const auto* href = v.as<SkSVGStringValue>()) {
44 this->setHref(*href);
45 }
46 break;
fmalitacecd6172016-09-13 12:56:11 -070047 case SkSVGAttribute::kSpreadMethod:
48 if (const auto* spread = v.as<SkSVGSpreadMethodValue>()) {
49 this->setSpreadMethod(*spread);
50 }
51 break;
fmalita28d5b722016-09-12 17:06:47 -070052 case SkSVGAttribute::kX1:
53 if (const auto* x1 = v.as<SkSVGLengthValue>()) {
54 this->setX1(*x1);
55 }
56 break;
57 case SkSVGAttribute::kY1:
58 if (const auto* y1 = v.as<SkSVGLengthValue>()) {
59 this->setY1(*y1);
60 }
61 break;
62 case SkSVGAttribute::kX2:
63 if (const auto* x2 = v.as<SkSVGLengthValue>()) {
64 this->setX2(*x2);
65 }
66 break;
67 case SkSVGAttribute::kY2:
68 if (const auto* y2 = v.as<SkSVGLengthValue>()) {
69 this->setY2(*y2);
70 }
71 break;
72 default:
73 this->INHERITED::onSetAttribute(attr, v);
74 }
75}
76
77// https://www.w3.org/TR/SVG/pservers.html#LinearGradientElementHrefAttribute
78void SkSVGLinearGradient::collectColorStops(const SkSVGRenderContext& ctx,
79 SkSTArray<2, SkScalar, true>* pos,
80 SkSTArray<2, SkColor, true>* colors) const {
81 // Used to resolve percentage offsets.
82 const SkSVGLengthContext ltx(SkSize::Make(1, 1));
83
84 for (const auto& child : fChildren) {
85 if (child->tag() != SkSVGTag::kStop) {
86 continue;
87 }
88
89 const auto& stop = static_cast<const SkSVGStop&>(*child);
90 colors->push_back(SkColorSetA(stop.stopColor(),
91 SkScalarRoundToInt(stop.stopOpacity() * 255)));
92 pos->push_back(SkTPin(ltx.resolve(stop.offset(), SkSVGLengthContext::LengthType::kOther),
93 0.f, 1.f));
94 }
95
96 SkASSERT(colors->count() == pos->count());
97
98 if (pos->empty() && !fHref.value().isEmpty()) {
99 const auto* ref = ctx.findNodeById(fHref);
100 if (ref && ref->tag() == SkSVGTag::kLinearGradient) {
101 static_cast<const SkSVGLinearGradient*>(ref)->collectColorStops(ctx, pos, colors);
102 }
103 }
104}
105
106bool SkSVGLinearGradient::onAsPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
107 const auto& lctx = ctx.lengthContext();
108 const auto x1 = lctx.resolve(fX1, SkSVGLengthContext::LengthType::kHorizontal);
109 const auto y1 = lctx.resolve(fY1, SkSVGLengthContext::LengthType::kVertical);
110 const auto x2 = lctx.resolve(fX2, SkSVGLengthContext::LengthType::kHorizontal);
111 const auto y2 = lctx.resolve(fY2, SkSVGLengthContext::LengthType::kVertical);
112
113 const SkPoint pts[2] = { {x1, y1}, {x2, y2}};
114 SkSTArray<2, SkColor , true> colors;
115 SkSTArray<2, SkScalar, true> pos;
116
117 this->collectColorStops(ctx, &pos, &colors);
118 // TODO:
119 // * stop (lazy?) sorting
120 // * href loop detection
121 // * href attribute inheritance (not just color stops)
fmalita28d5b722016-09-12 17:06:47 -0700122 // * objectBoundingBox units support
123
fmalitacecd6172016-09-13 12:56:11 -0700124 static_assert(static_cast<SkShader::TileMode>(SkSVGSpreadMethod::Type::kPad) ==
125 SkShader::kClamp_TileMode, "SkSVGSpreadMethod::Type is out of sync");
126 static_assert(static_cast<SkShader::TileMode>(SkSVGSpreadMethod::Type::kRepeat) ==
127 SkShader::kRepeat_TileMode, "SkSVGSpreadMethod::Type is out of sync");
128 static_assert(static_cast<SkShader::TileMode>(SkSVGSpreadMethod::Type::kReflect) ==
129 SkShader::kMirror_TileMode, "SkSVGSpreadMethod::Type is out of sync");
130 const auto tileMode = static_cast<SkShader::TileMode>(fSpreadMethod.type());
131
fmalita28d5b722016-09-12 17:06:47 -0700132 paint->setShader(SkGradientShader::MakeLinear(pts, colors.begin(), pos.begin(), colors.count(),
fmalitacecd6172016-09-13 12:56:11 -0700133 tileMode));
fmalita28d5b722016-09-12 17:06:47 -0700134 return true;
135}