1 /* 2 * Copyright 2018 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 SkottieProperty_DEFINED 9 #define SkottieProperty_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkPoint.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkTypeface.h" 15 #include "include/utils/SkTextUtils.h" 16 #include "modules/skottie/src/text/SkottieShaper.h" 17 18 #include <functional> 19 20 class SkMatrix; 21 22 namespace sksg { 23 24 class Color; 25 class OpacityEffect; 26 27 } // namespace sksg 28 29 namespace skottie { 30 31 using ColorPropertyValue = SkColor; 32 using OpacityPropertyValue = float; 33 34 enum class TextPaintOrder : uint8_t { 35 kFillStroke, 36 kStrokeFill, 37 }; 38 39 struct TextPropertyValue { 40 sk_sp<SkTypeface> fTypeface; 41 SkString fText; 42 float fTextSize = 0, 43 fMinTextSize = 0, // when auto-sizing 44 fMaxTextSize = std::numeric_limits<float>::max(), // when auto-sizing 45 fStrokeWidth = 0, 46 fLineHeight = 0, 47 fLineShift = 0, 48 fAscent = 0; 49 SkTextUtils::Align fHAlign = SkTextUtils::kLeft_Align; 50 Shaper::VAlign fVAlign = Shaper::VAlign::kTop; 51 Shaper::ResizePolicy fResize = Shaper::ResizePolicy::kNone; 52 Shaper::LinebreakPolicy fLineBreak = Shaper::LinebreakPolicy::kExplicit; 53 Shaper::Direction fDirection = Shaper::Direction::kLTR; 54 Shaper::Capitalization fCapitalization = Shaper::Capitalization::kNone; 55 SkRect fBox = SkRect::MakeEmpty(); 56 SkColor fFillColor = SK_ColorTRANSPARENT, 57 fStrokeColor = SK_ColorTRANSPARENT; 58 TextPaintOrder fPaintOrder = TextPaintOrder::kFillStroke; 59 bool fHasFill = false, 60 fHasStroke = false; 61 62 bool operator==(const TextPropertyValue& other) const; 63 bool operator!=(const TextPropertyValue& other) const; 64 }; 65 66 struct TransformPropertyValue { 67 SkPoint fAnchorPoint, 68 fPosition; 69 SkVector fScale; 70 SkScalar fRotation, 71 fSkew, 72 fSkewAxis; 73 74 bool operator==(const TransformPropertyValue& other) const; 75 bool operator!=(const TransformPropertyValue& other) const; 76 }; 77 78 namespace internal { class AnimationBuilder; } 79 80 /** 81 * Property handles are adapters between user-facing AE model/values 82 * and the internal scene-graph representation. 83 */ 84 template <typename ValueT, typename NodeT> 85 class SK_API PropertyHandle final { 86 public: PropertyHandle(sk_sp<NodeT> node)87 explicit PropertyHandle(sk_sp<NodeT> node) : fNode(std::move(node)) {} 88 ~PropertyHandle(); 89 90 ValueT get() const; 91 void set(const ValueT&); 92 93 private: 94 const sk_sp<NodeT> fNode; 95 }; 96 97 namespace internal { 98 99 class TextAdapter; 100 class TransformAdapter2D; 101 102 } // namespace internal 103 104 using ColorPropertyHandle = PropertyHandle<ColorPropertyValue, 105 sksg::Color>; 106 using OpacityPropertyHandle = PropertyHandle<OpacityPropertyValue, 107 sksg::OpacityEffect>; 108 using TextPropertyHandle = PropertyHandle<TextPropertyValue, 109 internal::TextAdapter>; 110 using TransformPropertyHandle = PropertyHandle<TransformPropertyValue, 111 internal::TransformAdapter2D>; 112 113 /** 114 * A PropertyObserver can be used to track and manipulate certain properties of "interesting" 115 * Lottie nodes. 116 * 117 * When registered with an animation builder, PropertyObserver receives notifications for 118 * various properties of layer and shape nodes. The |node_name| argument corresponds to the 119 * name ("nm") node property. 120 */ 121 class SK_API PropertyObserver : public SkRefCnt { 122 public: 123 enum class NodeType {COMPOSITION, LAYER, EFFECT, OTHER}; 124 125 template <typename T> 126 using LazyHandle = std::function<std::unique_ptr<T>()>; 127 128 virtual void onColorProperty (const char node_name[], 129 const LazyHandle<ColorPropertyHandle>&); 130 virtual void onOpacityProperty (const char node_name[], 131 const LazyHandle<OpacityPropertyHandle>&); 132 virtual void onTextProperty (const char node_name[], 133 const LazyHandle<TextPropertyHandle>&); 134 virtual void onTransformProperty(const char node_name[], 135 const LazyHandle<TransformPropertyHandle>&); 136 virtual void onEnterNode(const char node_name[], NodeType node_type); 137 virtual void onLeavingNode(const char node_name[], NodeType node_type); 138 }; 139 140 } // namespace skottie 141 142 #endif // SkottieProperty_DEFINED 143