fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | SkSVGLinearGradient::SkSVGLinearGradient() : INHERITED(SkSVGTag::kLinearGradient) {} |
| 15 | |
| 16 | void SkSVGLinearGradient::setHref(const SkSVGStringType& href) { |
| 17 | fHref = std::move(href); |
| 18 | } |
| 19 | |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame^] | 20 | void SkSVGLinearGradient::setSpreadMethod(const SkSVGSpreadMethod& spread) { |
| 21 | fSpreadMethod = spread; |
| 22 | } |
| 23 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 24 | void SkSVGLinearGradient::setX1(const SkSVGLength& x1) { |
| 25 | fX1 = x1; |
| 26 | } |
| 27 | |
| 28 | void SkSVGLinearGradient::setY1(const SkSVGLength& y1) { |
| 29 | fY1 = y1; |
| 30 | } |
| 31 | |
| 32 | void SkSVGLinearGradient::setX2(const SkSVGLength& x2) { |
| 33 | fX2 = x2; |
| 34 | } |
| 35 | |
| 36 | void SkSVGLinearGradient::setY2(const SkSVGLength& y2) { |
| 37 | fY2 = y2; |
| 38 | } |
| 39 | |
| 40 | void 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; |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame^] | 47 | case SkSVGAttribute::kSpreadMethod: |
| 48 | if (const auto* spread = v.as<SkSVGSpreadMethodValue>()) { |
| 49 | this->setSpreadMethod(*spread); |
| 50 | } |
| 51 | break; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 52 | 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 |
| 78 | void 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 | |
| 106 | bool 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) |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 122 | // * objectBoundingBox units support |
| 123 | |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame^] | 124 | 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 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 132 | paint->setShader(SkGradientShader::MakeLinear(pts, colors.begin(), pos.begin(), colors.count(), |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame^] | 133 | tileMode)); |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 134 | return true; |
| 135 | } |