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