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/SkM44.h" 12 #include "include/core/SkRefCnt.h" 13 #include "modules/skottie/src/SkottiePriv.h" 14 #include "modules/skottie/src/SkottieValue.h" 15 #include "modules/sksg/include/SkSGScene.h" 16 17 #include <memory> 18 #include <vector> 19 20 namespace skottie { 21 namespace internal { 22 23 class AnimatablePropertyContainer; 24 class AnimationBuilder; 25 class RangeSelector; 26 27 class TextAnimator final : public SkNVRefCnt<TextAnimator> { 28 public: 29 static sk_sp<TextAnimator> Make(const skjson::ObjectValue*, 30 const AnimationBuilder*, 31 AnimatablePropertyContainer* acontainer); 32 33 // Direct mapping of AE properties. 34 struct AnimatedProps { 35 VectorValue position, 36 scale = { 100, 100, 100 }, 37 fill_color, 38 stroke_color; 39 // unlike pos/scale which are animated vectors, rotation is separated in each dimension. 40 SkV3 rotation = { 0, 0, 0 }; 41 Vec2Value blur = { 0, 0 }, 42 line_spacing = { 0, 0 }; 43 ScalarValue opacity = 100, 44 fill_opacity = 100, 45 stroke_opacity = 100, 46 tracking = 0; 47 }; 48 49 struct ResolvedProps { 50 SkV3 position = { 0, 0, 0 }, 51 scale = { 1, 1, 1 }, 52 rotation = { 0, 0, 0 }; 53 float opacity = 1, 54 tracking = 0; 55 SkColor fill_color = SK_ColorTRANSPARENT, 56 stroke_color = SK_ColorTRANSPARENT; 57 SkV2 blur = { 0, 0 }, 58 line_spacing = { 0, 0 }; 59 }; 60 61 struct AnimatedPropsModulator { 62 ResolvedProps props; // accumulates properties across *all* animators 63 float coverage; // accumulates range selector coverage for a given animator 64 }; 65 using ModulatorBuffer = std::vector<AnimatedPropsModulator>; 66 67 // Domain maps describe how a given index domain (words, lines, etc) relates 68 // to the full fragment index range. 69 // 70 // Each domain[i] represents a [domain[i].fOffset.. domain[i].fOffset+domain[i].fCount-1] 71 // fragment subset. 72 struct DomainSpan { 73 size_t fOffset, 74 fCount; 75 float fAdvance, // cumulative advance for all fragments in span 76 fAscent; // max ascent for all fragments in span 77 }; 78 using DomainMap = std::vector<DomainSpan>; 79 80 struct DomainMaps { 81 DomainMap fNonWhitespaceMap, 82 fWordsMap, 83 fLinesMap; 84 }; 85 86 void modulateProps(const DomainMaps&, ModulatorBuffer&) const; 87 hasBlur()88 bool hasBlur() const { return fHasBlur; } 89 requiresAnchorPoint()90 bool requiresAnchorPoint() const { return fRequiresAnchorPoint; } 91 92 private: 93 TextAnimator(std::vector<sk_sp<RangeSelector>>&&, 94 const skjson::ObjectValue&, 95 const AnimationBuilder*, 96 AnimatablePropertyContainer*); 97 98 ResolvedProps modulateProps(const ResolvedProps&, float amount) const; 99 100 const std::vector<sk_sp<RangeSelector>> fSelectors; 101 102 AnimatedProps fTextProps; 103 bool fHasFillColor : 1, 104 fHasStrokeColor : 1, 105 fHasBlur : 1, 106 fRequiresAnchorPoint : 1; // animator sensitive to transform origin? 107 }; 108 109 } // namespace internal 110 } // namespace skottie 111 112 #endif // SkottieTextAnimator_DEFINED 113