1/*
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#ifndef SkSGDashEffect_DEFINED
9#define SkSGDashEffect_DEFINED
10
11#include "include/core/SkPath.h"
12#include "modules/sksg/include/SkSGGeometryNode.h"
13
14#include <vector>
15
16namespace sksg {
17
18/**
19 * Apply a dash effect to the child geometry.
20 *
21 * Follows the same semantics as SkDashPathEffect, with one minor tweak: when the number of
22 * intervals is odd, they are repeated once more to attain an even sequence (same as SVG
23 * stroke-dasharray: https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty).
24 */
25class DashEffect final : public GeometryNode {
26public:
27 static sk_sp<DashEffect> Make(sk_sp<GeometryNode> child) {
28 return child ? sk_sp<DashEffect>(new DashEffect(std::move(child))) : nullptr;
29 }
30
31 ~DashEffect() override;
32
33 SG_ATTRIBUTE(Intervals, std::vector<float>, fIntervals)
34 SG_ATTRIBUTE(Phase, float , fPhase )
35
36protected:
37 void onClip(SkCanvas*, bool antiAlias) const override;
38 void onDraw(SkCanvas*, const SkPaint&) const override;
39 bool onContains(const SkPoint&) const override;
40
41 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
42 SkPath onAsPath() const override;
43
44private:
45 explicit DashEffect(sk_sp<GeometryNode>);
46
47 const sk_sp<GeometryNode> fChild;
48
49 SkPath fDashedPath; // cache
50
51 std::vector<float> fIntervals;
52 float fPhase;
53};
54
55} // namespace sksg
56
57#endif // SkSGDashEffect_DEFINED
58