• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/private/SkTPin.h"
9 #include "modules/skottie/src/Adapter.h"
10 #include "modules/skottie/src/SkottieJson.h"
11 #include "modules/skottie/src/SkottiePriv.h"
12 #include "modules/skottie/src/SkottieValue.h"
13 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
14 #include "modules/sksg/include/SkSGGeometryEffect.h"
15 
16 namespace skottie::internal {
17 
18 namespace  {
19 
20 class OffsetPathsAdapter final : public DiscardableAdapterBase<OffsetPathsAdapter,
21                                                                sksg::OffsetEffect> {
22 public:
OffsetPathsAdapter(const skjson::ObjectValue & joffset,const AnimationBuilder & abuilder,sk_sp<sksg::GeometryNode> child)23     OffsetPathsAdapter(const skjson::ObjectValue& joffset,
24                        const AnimationBuilder& abuilder,
25                        sk_sp<sksg::GeometryNode> child)
26         : INHERITED(sksg::OffsetEffect::Make(std::move(child))) {
27         static constexpr SkPaint::Join gJoinMap[] = {
28             SkPaint::kMiter_Join,  // 'lj': 1
29             SkPaint::kRound_Join,  // 'lj': 2
30             SkPaint::kBevel_Join,  // 'lj': 3
31         };
32 
33         const auto join = ParseDefault<int>(joffset["lj"], 1) - 1;
34         this->node()->setJoin(gJoinMap[SkTPin<int>(join, 0, SK_ARRAY_COUNT(gJoinMap) - 1)]);
35 
36         this->bind(abuilder, joffset["a" ], fAmount);
37         this->bind(abuilder, joffset["ml"], fMiterLimit);
38     }
39 
40 private:
onSync()41     void onSync() override {
42         this->node()->setOffset(fAmount);
43         this->node()->setMiterLimit(fMiterLimit);
44     }
45 
46     ScalarValue fAmount     = 0,
47                 fMiterLimit = 0;
48 
49     using INHERITED = DiscardableAdapterBase<OffsetPathsAdapter, sksg::OffsetEffect>;
50 };
51 
52 } // namespace
53 
AttachOffsetGeometryEffect(const skjson::ObjectValue & jround,const AnimationBuilder * abuilder,std::vector<sk_sp<sksg::GeometryNode>> && geos)54 std::vector<sk_sp<sksg::GeometryNode>> ShapeBuilder::AttachOffsetGeometryEffect(
55         const skjson::ObjectValue& jround, const AnimationBuilder* abuilder,
56         std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
57     std::vector<sk_sp<sksg::GeometryNode>> offsetted;
58     offsetted.reserve(geos.size());
59 
60     for (auto& g : geos) {
61         offsetted.push_back(abuilder->attachDiscardableAdapter<OffsetPathsAdapter>
62                                         (jround, *abuilder, std::move(g)));
63     }
64 
65     return offsetted;
66 }
67 
68 } // namespace skottie::internal
69