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
SkSVGPattern()22 SkSVGPattern::SkSVGPattern() : INHERITED(SkSVGTag::kPattern) {}
23
parseAndSetAttribute(const char * name,const char * value)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
hrefTarget(const SkSVGRenderContext & ctx) const35 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>
inherit_if_needed(const SkTLazy<T> & src,SkTLazy<T> & dst)49 int inherit_if_needed(const SkTLazy<T>& src, SkTLazy<T>& dst) {
50 if (!dst.isValid()) {
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 */
resolveHref(const SkSVGRenderContext & ctx,PatternAttributes * attrs) const67 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
onAsPaint(const SkSVGRenderContext & ctx,SkPaint * paint) const97 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.isValid() ? *attrs.fX : SkSVGLength(0),
103 attrs.fY.isValid() ? *attrs.fY : SkSVGLength(0),
104 attrs.fWidth.isValid() ? *attrs.fWidth : SkSVGLength(0),
105 attrs.fHeight.isValid() ? *attrs.fHeight : SkSVGLength(0));
106
107 if (tile.isEmpty()) {
108 return false;
109 }
110
111 const SkMatrix* patternTransform = attrs.fPatternTransform.isValid()
112 ? attrs.fPatternTransform.get()
113 : nullptr;
114
115 SkPictureRecorder recorder;
116 SkSVGRenderContext recordingContext(ctx, recorder.beginRecording(tile));
117
118 // Cannot call into INHERITED:: because SkSVGHiddenContainer skips rendering.
119 contentNode->SkSVGContainer::onRender(recordingContext);
120
121 paint->setShader(recorder.finishRecordingAsPicture()->makeShader(
122 SkTileMode::kRepeat,
123 SkTileMode::kRepeat,
124 SkFilterMode::kLinear,
125 patternTransform,
126 &tile));
127 return true;
128 }
129