blob: fd2a4e3f9a2641b412cfb5b9aaa19fde8e91bd53 [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkMatrix.h"
10#include "include/pathops/SkPathOps.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050011#include "include/private/SkTPin.h"
Florin Malitab3418102020-10-15 18:10:29 -040012#include "modules/svg/include/SkSVGNode.h"
13#include "modules/svg/include/SkSVGRenderContext.h"
14#include "modules/svg/include/SkSVGValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTLazy.h"
fmalita6ceef3d2016-07-26 18:46:34 -070016
Tyler Denniston04e03bc2020-12-09 14:16:25 -050017SkSVGNode::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));
Tyler Denniston8ed04432020-12-10 15:51:04 -050021 fPresentationAttributes.fFloodColor.set(SkSVGColor(SK_ColorBLACK));
22 fPresentationAttributes.fFloodOpacity.set(SkSVGNumberType(1.0f));
Tyler Denniston04e03bc2020-12-09 14:16:25 -050023}
fmalita6ceef3d2016-07-26 18:46:34 -070024
25SkSVGNode::~SkSVGNode() { }
26
fmalita397a5172016-08-08 11:38:55 -070027void SkSVGNode::render(const SkSVGRenderContext& ctx) const {
Tyler Denniston53281c72020-10-22 15:54:24 -040028 SkSVGRenderContext localContext(ctx, this);
fmalita6ceef3d2016-07-26 18:46:34 -070029
fmalita397a5172016-08-08 11:38:55 -070030 if (this->onPrepareToRender(&localContext)) {
31 this->onRender(localContext);
fmalita6ceef3d2016-07-26 18:46:34 -070032 }
fmalita397a5172016-08-08 11:38:55 -070033}
fmalita6ceef3d2016-07-26 18:46:34 -070034
fmalita28d5b722016-09-12 17:06:47 -070035bool SkSVGNode::asPaint(const SkSVGRenderContext& ctx, SkPaint* paint) const {
36 SkSVGRenderContext localContext(ctx);
37
38 return this->onPrepareToRender(&localContext) && this->onAsPaint(localContext, paint);
39}
40
Florin Malitace8840e2016-12-08 09:26:47 -050041SkPath SkSVGNode::asPath(const SkSVGRenderContext& ctx) const {
42 SkSVGRenderContext localContext(ctx);
Florin Malita7d529882016-12-08 16:04:24 -050043 if (!this->onPrepareToRender(&localContext)) {
44 return SkPath();
45 }
46
47 SkPath path = this->onAsPath(localContext);
48
49 if (const auto* clipPath = localContext.clipPath()) {
50 // There is a clip-path present on the current node.
51 Op(path, *clipPath, kIntersect_SkPathOp, &path);
52 }
53
54 return path;
Florin Malitace8840e2016-12-08 09:26:47 -050055}
56
Tyler Dennistonf548a022020-10-27 15:02:02 -040057SkRect SkSVGNode::objectBoundingBox(const SkSVGRenderContext& ctx) const {
58 return this->onObjectBoundingBox(ctx);
Tyler Denniston53281c72020-10-22 15:54:24 -040059}
60
fmalita397a5172016-08-08 11:38:55 -070061bool SkSVGNode::onPrepareToRender(SkSVGRenderContext* ctx) const {
fmalitabef51c22016-09-20 15:45:57 -070062 ctx->applyPresentationAttributes(fPresentationAttributes,
63 this->hasChildren() ? 0 : SkSVGRenderContext::kLeaf);
Florin Malitaffe6ae42017-10-12 11:33:28 -040064
65 // visibility:hidden disables rendering
John Stilesa008b0f2020-08-16 08:48:02 -040066 const auto visibility = ctx->presentationContext().fInherited.fVisibility->type();
Florin Malitaffe6ae42017-10-12 11:33:28 -040067 return visibility != SkSVGVisibility::Type::kHidden;
fmalita6ceef3d2016-07-26 18:46:34 -070068}
69
Florin Malitaf4403e72020-04-10 14:14:04 +000070void SkSVGNode::setAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
fmalita6ceef3d2016-07-26 18:46:34 -070071 this->onSetAttribute(attr, v);
72}
73
Florin Malita09294252020-04-09 08:54:29 -040074template <typename T>
75void SetInheritedByDefault(SkTLazy<T>& presentation_attribute, const T& value) {
76 if (value.type() != T::Type::kInherit) {
77 presentation_attribute.set(value);
78 } else {
79 // kInherited values are semantically equivalent to
80 // the absence of a local presentation attribute.
81 presentation_attribute.reset();
82 }
83}
84
Florin Malita8c425672020-11-06 13:49:37 -050085bool SkSVGNode::parseAndSetAttribute(const char* n, const char* v) {
Tyler Denniston79832e32020-11-18 09:34:07 -050086#define PARSE_AND_SET(svgName, attrName) \
87 this->set##attrName( \
88 SkSVGAttributeParser::parseProperty<decltype(fPresentationAttributes.f##attrName)>( \
89 svgName, n, v))
90
Tyler Denniston7bb85db2021-01-13 12:08:04 -050091 return PARSE_AND_SET( "clip-path" , ClipPath)
92 || PARSE_AND_SET("clip-rule" , ClipRule)
93 || PARSE_AND_SET("color" , Color)
Florin Malita73d57bf2021-01-15 08:58:09 -050094 || PARSE_AND_SET("color-interpolation" , ColorInterpolation)
Tyler Denniston7bb85db2021-01-13 12:08:04 -050095 || PARSE_AND_SET("color-interpolation-filters", ColorInterpolationFilters)
96 || PARSE_AND_SET("fill" , Fill)
97 || PARSE_AND_SET("fill-opacity" , FillOpacity)
98 || PARSE_AND_SET("fill-rule" , FillRule)
99 || PARSE_AND_SET("filter" , Filter)
100 || PARSE_AND_SET("flood-color" , FloodColor)
101 || PARSE_AND_SET("flood-opacity" , FloodOpacity)
102 || PARSE_AND_SET("font-family" , FontFamily)
103 || PARSE_AND_SET("font-size" , FontSize)
104 || PARSE_AND_SET("font-style" , FontStyle)
105 || PARSE_AND_SET("font-weight" , FontWeight)
106 || PARSE_AND_SET("mask" , Mask)
107 || PARSE_AND_SET("opacity" , Opacity)
108 || PARSE_AND_SET("stop-color" , StopColor)
109 || PARSE_AND_SET("stop-opacity" , StopOpacity)
110 || PARSE_AND_SET("stroke" , Stroke)
111 || PARSE_AND_SET("stroke-dasharray" , StrokeDashArray)
112 || PARSE_AND_SET("stroke-dashoffset" , StrokeDashOffset)
113 || PARSE_AND_SET("stroke-linecap" , StrokeLineCap)
114 || PARSE_AND_SET("stroke-linejoin" , StrokeLineJoin)
115 || PARSE_AND_SET("stroke-miterlimit" , StrokeMiterLimit)
116 || PARSE_AND_SET("stroke-opacity" , StrokeOpacity)
117 || PARSE_AND_SET("stroke-width" , StrokeWidth)
118 || PARSE_AND_SET("text-anchor" , TextAnchor)
119 || PARSE_AND_SET("visibility" , Visibility);
Tyler Denniston79832e32020-11-18 09:34:07 -0500120
121#undef PARSE_AND_SET
Tyler Denniston57154992020-11-04 16:08:30 -0500122}