| 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/SkSVGPattern.h" |
| 9 |
|
| 10 |
#include "include/core/SkPaint.h" |
| 11 |
#include "include/core/SkPicture.h" |
| 12 |
#include "include/core/SkPictureRecorder.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkSamplingOptions.h" |
| 15 |
#include "include/core/SkTileMode.h" |
| 16 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 17 |
#include "modules/svg/include/SkSVGContainer.h" |
| 18 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 19 |
|
| 20 |
class SkMatrix; |
| 21 |
|
| 22 |
SkSVGPattern::SkSVGPattern() : INHERITED(SkSVGTag::kPattern) {} |
| 23 |
|
| 24 |
bool SkSVGPattern::parseAndSetAttribute(const char* name, const char* value) { |
| 25 |
return INHERITED::parseAndSetAttribute(name, value) || |
| 26 |
this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", name, value)) || |
| 27 |
this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", name, value)) || |
| 28 |
this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", name, value)) || |
| 29 |
this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", name, value)) || |
| 30 |
this->setPatternTransform(SkSVGAttributeParser::parse<SkSVGTransformType>( |
| 31 |
"patternTransform", name, value)) || |
| 32 |
this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", name, value)); |
| 33 |
} |
| 34 |
|
| 35 |
const SkSVGPattern* SkSVGPattern::hrefTarget(const SkSVGRenderContext& ctx) const { |
| 36 |
if (fHref.iri().isEmpty()) { |
| 37 |
return nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
const auto href = ctx.findNodeById(fHref); |
| 41 |
if (!href || href->tag() != SkSVGTag::kPattern) { |
| 42 |
return nullptr; |
| 43 |
} |
| 44 |
|
| 45 |
return static_cast<const SkSVGPattern*>(href.get()); |
| 46 |
} |
| 47 |
|
| 48 |
template <typename T> |
| 49 |
int inherit_if_needed(const std::optional<T>& src, std::optional<T>& dst) { |
| 50 |
if (!dst.has_value()) { |
| 51 |
dst = src; |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
/* https://www.w3.org/TR/SVG11/pservers.html#PatternElementHrefAttribute |
| 59 |
* |
| 60 |
* Any attributes which are defined on the referenced element which are not defined on this element |
| 61 |
* are inherited by this element. If this element has no children, and the referenced element does |
| 62 |
* (possibly due to its own ‘xlink:href’ attribute), then this element inherits the children from |
| 63 |
* the referenced element. Inheritance can be indirect to an arbitrary level; thus, if the |
| 64 |
* referenced element inherits attributes or children due to its own ‘xlink:href’ attribute, then |
| 65 |
* the current element can inherit those attributes or children. |
| 66 |
*/ |
| 67 |
const SkSVGPattern* SkSVGPattern::resolveHref(const SkSVGRenderContext& ctx, |
| 68 |
PatternAttributes* attrs) const { |
| 69 |
const SkSVGPattern *currentNode = this, |
| 70 |
*contentNode = this; |
| 71 |
do { |
| 72 |
// Bitwise OR to avoid short-circuiting. |
| 73 |
const bool didInherit = |
| 74 |
inherit_if_needed(currentNode->fX , attrs->fX) | |
| 75 |
inherit_if_needed(currentNode->fY , attrs->fY) | |
| 76 |
inherit_if_needed(currentNode->fWidth , attrs->fWidth) | |
| 77 |
inherit_if_needed(currentNode->fHeight , attrs->fHeight) | |
| 78 |
inherit_if_needed(currentNode->fPatternTransform, attrs->fPatternTransform); |
| 79 |
|
| 80 |
if (!contentNode->hasChildren()) { |
| 81 |
contentNode = currentNode; |
| 82 |
} |
| 83 |
|
| 84 |
if (contentNode->hasChildren() && !didInherit) { |
| 85 |
// All attributes have been resolved, and a valid content node has been found. |
| 86 |
// We can terminate the href chain early. |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
// TODO: reference loop mitigation. |
| 91 |
currentNode = currentNode->hrefTarget(ctx); |
| 92 |
} while (currentNode); |
| 93 |
|
| 94 |
return contentNode; |
| 95 |
} |
| 96 |
|
| 97 |
bool SkSVGPattern::onAsPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const { |
| 98 |
PatternAttributes attrs; |
| 99 |
const auto* contentNode = this->resolveHref(ctx, &attrs); |
| 100 |
|
| 101 |
const auto tile = ctx.lengthContext().resolveRect( |
| 102 |
attrs.fX.has_value() ? *attrs.fX : SkSVGLength(0), |
| 103 |
attrs.fY.has_value() ? *attrs.fY : SkSVGLength(0), |
| 104 |
attrs.fWidth.has_value() ? *attrs.fWidth : SkSVGLength(0), |
| 105 |
attrs.fHeight.has_value() ? *attrs.fHeight : SkSVGLength(0)); |
| 106 |
|
| 107 |
if (tile.isEmpty()) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
const SkMatrix* patternTransform = SkOptAddressOrNull(attrs.fPatternTransform); |
| 112 |
|
| 113 |
SkPictureRecorder recorder; |
| 114 |
SkSVGRenderContext recordingContext(ctx, recorder.beginRecording(tile)); |
| 115 |
|
| 116 |
// Cannot call into INHERITED:: because SkSVGHiddenContainer skips rendering. |
| 117 |
contentNode->SkSVGContainer::onRender(recordingContext); |
| 118 |
|
| 119 |
paint->setShader(recorder.finishRecordingAsPicture()->makeShader( |
| 120 |
SkTileMode::kRepeat, |
| 121 |
SkTileMode::kRepeat, |
| 122 |
SkFilterMode::kLinear, |
| 123 |
patternTransform, |
| 124 |
&tile)); |
| 125 |
return true; |
| 126 |
} |