2 * Copyright 2020 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkPathBuilder.h"
9 #include "include/private/SkTPin.h"
10 #include "modules/skottie/src/Adapter.h"
11 #include "modules/skottie/src/SkottieJson.h"
12 #include "modules/skottie/src/SkottiePriv.h"
13 #include "modules/skottie/src/SkottieValue.h"
14 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
15 #include "modules/sksg/include/SkSGPath.h"
22 class PolystarGeometryAdapter final :
23 public DiscardableAdapterBase<PolystarGeometryAdapter, sksg::Path> {
29 PolystarGeometryAdapter(const skjson::ObjectValue& jstar,
30 const AnimationBuilder* abuilder, Type t)
32 this->bind(*abuilder, jstar["pt"], fPointCount );
33 this->bind(*abuilder, jstar["p" ], fPosition );
34 this->bind(*abuilder, jstar["r" ], fRotation );
35 this->bind(*abuilder, jstar["ir"], fInnerRadius );
36 this->bind(*abuilder, jstar["or"], fOuterRadius );
37 this->bind(*abuilder, jstar["is"], fInnerRoundness);
38 this->bind(*abuilder, jstar["os"], fOuterRoundness);
42 void onSync() override {
43 static constexpr int kMaxPointCount = 100000;
44 const auto count = SkToUInt(SkTPin(SkScalarRoundToInt(fPointCount), 0, kMaxPointCount));
45 const auto arc = sk_ieee_float_divide(SK_ScalarPI * 2, count);
47 const auto pt_on_circle = [](const SkV2& c, SkScalar r, SkScalar a) {
48 return SkPoint::Make(c.x + r * std::cos(a),
49 c.y + r * std::sin(a));
52 // TODO: inner/outer "roundness"?
56 auto angle = SkDegreesToRadians(fRotation - 90);
57 poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle));
58 poly.incReserve(fType == Type::kStar ? count * 2 : count);
60 for (unsigned i = 0; i < count; ++i) {
61 if (fType == Type::kStar) {
62 poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f));
65 poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle));
69 this->node()->setPath(poly.detach());
74 Vec2Value fPosition = {0,0};
75 ScalarValue fPointCount = 0,
85 sk_sp<sksg::GeometryNode> ShapeBuilder::AttachPolystarGeometry(const skjson::ObjectValue& jstar,
86 const AnimationBuilder* abuilder) {
87 static constexpr PolystarGeometryAdapter::Type gTypes[] = {
88 PolystarGeometryAdapter::Type::kStar, // "sy": 1
89 PolystarGeometryAdapter::Type::kPoly, // "sy": 2
92 const auto type = ParseDefault<size_t>(jstar["sy"], 0) - 1;
93 if (type >= SK_ARRAY_COUNT(gTypes)) {
94 abuilder->log(Logger::Level::kError, &jstar, "Unknown polystar type.");
98 return abuilder->attachDiscardableAdapter<PolystarGeometryAdapter>
99 (jstar, abuilder, gTypes[type]);
102 } // namespace internal
103 } // namespace skottie