1 /* 2 * Copyright 2019 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 SkottieTextAnimator_DEFINED 9 #define SkottieTextAnimator_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "modules/skottie/src/SkottieAdapter.h" 13 #include "modules/skottie/src/SkottiePriv.h" 14 #include "modules/sksg/include/SkSGScene.h" 15 16 #include <memory> 17 #include <vector> 18 19 namespace skottie { 20 namespace internal { 21 22 class AnimationBuilder; 23 class RangeSelector; 24 class TextAdapter; 25 26 class TextAnimator final : public SkNVRefCnt<TextAnimator> { 27 public: 28 static sk_sp<TextAnimator> Make(const skjson::ObjectValue*, 29 const AnimationBuilder*); 30 31 struct AnimatedProps { 32 SkPoint position = { 0, 0 }; 33 float opacity = 1, 34 scale = 1, 35 rotation = 0, 36 tracking = 0; 37 SkColor fill_color = SK_ColorTRANSPARENT, 38 stroke_color = SK_ColorTRANSPARENT; 39 }; 40 41 struct AnimatedPropsModulator { 42 AnimatedProps props; // accumulates properties across *all* animators 43 float coverage; // accumulates range selector coverage for a given animator 44 }; 45 using ModulatorBuffer = std::vector<AnimatedPropsModulator>; 46 47 // Domain maps describe how a given index domain (words, lines, etc) relates 48 // to the full fragment index range. 49 // 50 // Each domain[i] represents a [domain[i].fOffset.. domain[i].fOffset+domain[i].fCount-1] 51 // fragment subset. 52 struct DomainSpan { 53 size_t fOffset, fCount; 54 }; 55 using DomainMap = std::vector<DomainSpan>; 56 57 struct DomainMaps { 58 DomainMap fNonWhitespaceMap, 59 fWordsMap, 60 fLinesMap; 61 }; 62 63 void modulateProps(const DomainMaps&, ModulatorBuffer&) const; 64 65 private: 66 TextAnimator(std::vector<sk_sp<RangeSelector>>&& selectors, 67 const skjson::ObjectValue& jprops, 68 const AnimationBuilder* abuilder); 69 70 AnimatedProps modulateProps(const AnimatedProps&, float amount) const; 71 72 const std::vector<sk_sp<RangeSelector>> fSelectors; 73 74 AnimatedProps fTextProps; 75 bool fHasFillColor : 1, 76 fHasStrokeColor : 1; 77 }; 78 79 class TextAnimatorList final : public sksg::GroupAnimator { 80 public: 81 static sk_sp<TextAnimatorList> Make(const skjson::ArrayValue&, 82 const AnimationBuilder*, 83 sk_sp<TextAdapter>); 84 ~TextAnimatorList() override; 85 86 protected: 87 void onTick(float) override; 88 89 private: 90 TextAnimatorList(sk_sp<TextAdapter>, sksg::AnimatorList&&, std::vector<sk_sp<TextAnimator>>&&); 91 92 const std::vector<sk_sp<TextAnimator>> fAnimators; 93 const sk_sp<TextAdapter> fAdapter; 94 95 using INHERITED = sksg::GroupAnimator; 96 }; 97 98 } // namespace internal 99 } // namespace skottie 100 101 #endif // SkottieTextAnimator_DEFINED 102