blob: 67310c8e9d82d6c1af1ab0467aef425e2cc0ccf0 [file] [log] [blame]
Tyler Denniston4c89481be2021-01-20 09:41:22 -05001/*
2 * Copyright 2020 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/effects/SkImageFilters.h"
9#include "modules/svg/include/SkSVGAttributeParser.h"
10#include "modules/svg/include/SkSVGFeMorphology.h"
11#include "modules/svg/include/SkSVGFilterContext.h"
12#include "modules/svg/include/SkSVGRenderContext.h"
13#include "modules/svg/include/SkSVGValue.h"
14
15bool SkSVGFeMorphology::parseAndSetAttribute(const char* name, const char* value) {
16 return INHERITED::parseAndSetAttribute(name, value) ||
17 this->setOperator(SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(
18 "operator", name, value)) ||
19 this->setRadius(SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(
20 "radius", name, value));
21}
22
23sk_sp<SkImageFilter> SkSVGFeMorphology::onMakeImageFilter(const SkSVGRenderContext& ctx,
24 const SkSVGFilterContext& fctx) const {
25 const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
Tyler Dennistonc7e48242021-01-20 17:31:36 -050026 const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
Tyler Denniston4c89481be2021-01-20 09:41:22 -050027 sk_sp<SkImageFilter> input = fctx.resolveInput(ctx, this->getIn(), colorspace);
28
29 SkScalar rx = fRadius.fX;
30 SkScalar ry = fRadius.fY;
31 if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
32 SkASSERT(ctx.node());
33 const SkRect objBounds = ctx.node()->objectBoundingBox(ctx);
34 rx *= objBounds.width();
35 ry *= objBounds.height();
36 }
37
38 switch (fOperator) {
39 case Operator::kErode:
40 return SkImageFilters::Erode(rx, ry, input, cropRect);
41 case Operator::kDilate:
42 return SkImageFilters::Dilate(rx, ry, input, cropRect);
43 }
44
45 SkUNREACHABLE;
46}
47
48template <>
49bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(SkSVGFeMorphology::Operator* op) {
50 static constexpr std::tuple<const char*, SkSVGFeMorphology::Operator> gMap[] = {
51 { "dilate", SkSVGFeMorphology::Operator::kDilate },
52 { "erode" , SkSVGFeMorphology::Operator::kErode },
53 };
54
55 return this->parseEnumMap(gMap, op) && this->parseEOSToken();
56}
57
58template <>
59bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(SkSVGFeMorphology::Radius* radius) {
60 std::vector<SkSVGNumberType> values;
61 if (!this->parse(&values)) {
62 return false;
63 }
64
65 radius->fX = values[0];
66 radius->fY = values.size() > 1 ? values[1] : values[0];
67 return true;
68}