Debian Codegraph @

modules/svg/src/SkSVGNode.cpp

raw
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 "modules/svg/include/SkSVGNode.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/pathops/SkPathOps.h"
15 #include "include/private/base/SkAssert.h"
16 #include "modules/svg/include/SkSVGRenderContext.h"
17
18 #include <algorithm>
19 #include <array>
20 #include <cstddef>
21 #include <optional>
22
23 SkSVGNode::SkSVGNode(SkSVGTag t) : fTag(t) {
24 // Uninherited presentation attributes need a non-null default value.
25 fPresentationAttributes.fStopColor.set(SkSVGColor(SK_ColorBLACK));
26 fPresentationAttributes.fStopOpacity.set(SkSVGNumberType(1.0f));
27 fPresentationAttributes.fFloodColor.set(SkSVGColor(SK_ColorBLACK));
28 fPresentationAttributes.fFloodOpacity.set(SkSVGNumberType(1.0f));
29 fPresentationAttributes.fLightingColor.set(SkSVGColor(SK_ColorWHITE));
30 }
31
32 SkSVGNode::~SkSVGNode() { }
33
34 void SkSVGNode::render(const SkSVGRenderContext& ctx) const {
35 SkSVGRenderContext localContext(ctx, this);
36
37 if (this->onPrepareToRender(&localContext)) {
38 this->onRender(localContext);
39 }
40 }
41
42 bool SkSVGNode::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
43 SkSVGRenderContext localContext(ctx);
44
45 return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
46 }
47
48 SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
49 SkSVGRenderContext localContext(ctx);
50 if (!this->onPrepareToRender(&localContext)) {
51 return SkPath();
52 }
53
54 SkPath path = this->onAsPath(localContext);
55
56 if (const auto* clipPath = localContext.clipPath()) {
57 // There is a clip-path present on the current node.
58 if (auto result = Op(path, *clipPath, kIntersect_SkPathOp)) {
59 path = *result;
60 }
61 }
62
63 return path;
64 }
65
66 SkRect SkSVGNode::objectBoundingBox(const SkSVGRenderContext& ctx) const {
67 return this->onObjectBoundingBox(ctx);
68 }
69
70 bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
71 ctx->applyPresentationAttributes(fPresentationAttributes,
72 this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
73
74 // visibility:hidden and display:none disable rendering.
75 // TODO: if display is not a value (true when display="inherit"), we currently
76 // ignore it. Eventually we should be able to add SkASSERT(display.isValue()).
77 const auto visibility = ctx->presentationContext().fInherited.fVisibility->type();
78 const auto display = fPresentationAttributes.fDisplay; // display is uninherited
79 return visibility != SkSVGVisibility::Type::kHidden &&
80 (!display.isValue() || *display != SkSVGDisplay::kNone);
81 }
82
83 void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
84 this->onSetAttribute(attr, v);
85 }
86
87 template <typename T>
88 void SetInheritedByDefault(std::optional<T>& presentation_attribute, const T& value) {
89 if (value.type() != T::Type::kInherit) {
90 presentation_attribute.set(value);
91 } else {
92 // kInherited values are semantically equivalent to
93 // the absence of a local presentation attribute.
94 presentation_attribute.reset();
95 }
96 }
97
98 bool SkSVGNode::parseAndSetAttribute(const char* n, const char* v) {
99 #define PARSE_AND_SET(svgName, attrName) \
100 this->set##attrName( \
101 SkSVGAttributeParser::parseProperty<decltype(fPresentationAttributes.f##attrName)>( \
102 svgName, n, v))
103
104 return PARSE_AND_SET( "clip-path" , ClipPath)
105 || PARSE_AND_SET("clip-rule" , ClipRule)
106 || PARSE_AND_SET("color" , Color)
107 || PARSE_AND_SET("color-interpolation" , ColorInterpolation)
108 || PARSE_AND_SET("color-interpolation-filters", ColorInterpolationFilters)
109 || PARSE_AND_SET("display" , Display)
110 || PARSE_AND_SET("fill" , Fill)
111 || PARSE_AND_SET("fill-opacity" , FillOpacity)
112 || PARSE_AND_SET("fill-rule" , FillRule)
113 || PARSE_AND_SET("filter" , Filter)
114 || PARSE_AND_SET("flood-color" , FloodColor)
115 || PARSE_AND_SET("flood-opacity" , FloodOpacity)
116 || PARSE_AND_SET("font-family" , FontFamily)
117 || PARSE_AND_SET("font-size" , FontSize)
118 || PARSE_AND_SET("font-style" , FontStyle)
119 || PARSE_AND_SET("font-weight" , FontWeight)
120 || PARSE_AND_SET("lighting-color" , LightingColor)
121 || PARSE_AND_SET("mask" , Mask)
122 || PARSE_AND_SET("opacity" , Opacity)
123 || PARSE_AND_SET("stop-color" , StopColor)
124 || PARSE_AND_SET("stop-opacity" , StopOpacity)
125 || PARSE_AND_SET("stroke" , Stroke)
126 || PARSE_AND_SET("stroke-dasharray" , StrokeDashArray)
127 || PARSE_AND_SET("stroke-dashoffset" , StrokeDashOffset)
128 || PARSE_AND_SET("stroke-linecap" , StrokeLineCap)
129 || PARSE_AND_SET("stroke-linejoin" , StrokeLineJoin)
130 || PARSE_AND_SET("stroke-miterlimit" , StrokeMiterLimit)
131 || PARSE_AND_SET("stroke-opacity" , StrokeOpacity)
132 || PARSE_AND_SET("stroke-width" , StrokeWidth)
133 || PARSE_AND_SET("text-anchor" , TextAnchor)
134 || PARSE_AND_SET("visibility" , Visibility);
135
136 #undef PARSE_AND_SET
137 }
138
139 // https://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute
140 SkMatrix SkSVGNode::ComputeViewboxMatrix(const SkRect& viewBox,
141 const SkRect& viewPort,
142 SkSVGPreserveAspectRatio par) {
143 if (viewBox.isEmpty() || viewPort.isEmpty()) {
144 return SkMatrix::Scale(0, 0);
145 }
146
147 auto compute_scale = [&]() -> SkV2 {
148 const auto sx = viewPort.width() / viewBox.width(),
149 sy = viewPort.height() / viewBox.height();
150
151 if (par.fAlign == SkSVGPreserveAspectRatio::kNone) {
152 // none -> anisotropic scaling, regardless of fScale
153 return {sx, sy};
154 }
155
156 // isotropic scaling
157 const auto s = par.fScale == SkSVGPreserveAspectRatio::kMeet
158 ? std::min(sx, sy)
159 : std::max(sx, sy);
160 return {s, s};
161 };
162
163 auto compute_trans = [&](const SkV2& scale) -> SkV2 {
164 static constexpr float gAlignCoeffs[] = {
165 0.0f, // Min
166 0.5f, // Mid
167 1.0f // Max
168 };
169
170 const size_t x_coeff = par.fAlign >> 0 & 0x03,
171 y_coeff = par.fAlign >> 2 & 0x03;
172
173 SkASSERT(x_coeff < std::size(gAlignCoeffs) &&
174 y_coeff < std::size(gAlignCoeffs));
175
176 const auto tx = -viewBox.x() * scale.x,
177 ty = -viewBox.y() * scale.y,
178 dx = viewPort.width() - viewBox.width() * scale.x,
179 dy = viewPort.height() - viewBox.height() * scale.y;
180
181 return {
182 tx + dx * gAlignCoeffs[x_coeff],
183 ty + dy * gAlignCoeffs[y_coeff]
184 };
185 };
186
187 const auto s = compute_scale(),
188 t = compute_trans(s);
189
190 return SkMatrix::Translate(t.x, t.y) *
191 SkMatrix::Scale(s.x, s.y);
192 }