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 "modules/skottie/src/Adapter.h"
9 #include "modules/skottie/src/SkottieJson.h"
10 #include "modules/skottie/src/SkottiePriv.h"
11 #include "modules/skottie/src/SkottieValue.h"
12 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
13
14 namespace skottie {
15 namespace internal {
16
MergeGeometry(std::vector<sk_sp<sksg::GeometryNode>> && geos,sksg::Merge::Mode mode)17 sk_sp<sksg::Merge> ShapeBuilder::MergeGeometry(std::vector<sk_sp<sksg::GeometryNode>>&& geos,
18 sksg::Merge::Mode mode) {
19 std::vector<sksg::Merge::Rec> merge_recs;
20 merge_recs.reserve(geos.size());
21
22 for (auto& geo : geos) {
23 merge_recs.push_back(
24 {std::move(geo), merge_recs.empty() ? sksg::Merge::Mode::kMerge : mode});
25 }
26
27 return sksg::Merge::Make(std::move(merge_recs));
28 }
29
AttachMergeGeometryEffect(const skjson::ObjectValue & jmerge,const AnimationBuilder *,std::vector<sk_sp<sksg::GeometryNode>> && geos)30 std::vector<sk_sp<sksg::GeometryNode>> ShapeBuilder::AttachMergeGeometryEffect(
31 const skjson::ObjectValue& jmerge, const AnimationBuilder*,
32 std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
33 static constexpr sksg::Merge::Mode gModes[] = {
34 sksg::Merge::Mode::kMerge, // "mm": 1
35 sksg::Merge::Mode::kUnion, // "mm": 2
36 sksg::Merge::Mode::kDifference, // "mm": 3
37 sksg::Merge::Mode::kIntersect, // "mm": 4
38 sksg::Merge::Mode::kXOR , // "mm": 5
39 };
40
41 const auto mode = gModes[std::min<size_t>(ParseDefault<size_t>(jmerge["mm"], 1) - 1,
42 SK_ARRAY_COUNT(gModes) - 1)];
43
44 std::vector<sk_sp<sksg::GeometryNode>> merged;
45 merged.push_back(ShapeBuilder::MergeGeometry(std::move(geos), mode));
46
47 return merged;
48 }
49
50 } // namespace internal
51 } // namespace skottie
52