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 | ScalarValue opacity = 100, |
43 | tracking = 0; |
44 | }; |
45 | |
46 | struct ResolvedProps { |
47 | SkV3 position = { 0, 0, 0 }, |
48 | scale = { 1, 1, 1 }, |
49 | rotation = { 0, 0, 0 }; |
50 | float opacity = 1, |
51 | tracking = 0; |
52 | SkColor fill_color = SK_ColorTRANSPARENT, |
53 | stroke_color = SK_ColorTRANSPARENT; |
54 | SkV2 blur = { 0, 0 }; |
55 | }; |
56 | |
57 | struct AnimatedPropsModulator { |
58 | ResolvedProps props; // accumulates properties across *all* animators |
59 | float coverage; // accumulates range selector coverage for a given animator |
60 | }; |
61 | using ModulatorBuffer = std::vector<AnimatedPropsModulator>; |
62 | |
63 | // Domain maps describe how a given index domain (words, lines, etc) relates |
64 | // to the full fragment index range. |
65 | // |
66 | // Each domain[i] represents a [domain[i].fOffset.. domain[i].fOffset+domain[i].fCount-1] |
67 | // fragment subset. |
68 | struct DomainSpan { |
69 | size_t fOffset, |
70 | fCount; |
71 | float fAdvance, // cumulative advance for all fragments in span |
72 | fAscent; // max ascent for all fragments in span |
73 | }; |
74 | using DomainMap = std::vector<DomainSpan>; |
75 | |
76 | struct DomainMaps { |
77 | DomainMap fNonWhitespaceMap, |
78 | fWordsMap, |
79 | fLinesMap; |
80 | }; |
81 | |
82 | void modulateProps(const DomainMaps&, ModulatorBuffer&) const; |
83 | |
84 | bool hasBlur() const { return fHasBlur; } |
85 | |
86 | bool requiresAnchorPoint() const { return fRequiresAnchorPoint; } |
87 | |
88 | private: |
89 | TextAnimator(std::vector<sk_sp<RangeSelector>>&&, |
90 | const skjson::ObjectValue&, |
91 | const AnimationBuilder*, |
92 | AnimatablePropertyContainer*); |
93 | |
94 | ResolvedProps modulateProps(const ResolvedProps&, float amount) const; |
95 | |
96 | const std::vector<sk_sp<RangeSelector>> fSelectors; |
97 | |
98 | AnimatedProps fTextProps; |
99 | bool fHasFillColor : 1, |
100 | fHasStrokeColor : 1, |
101 | fHasBlur : 1, |
102 | fRequiresAnchorPoint : 1; // animator sensitive to transform origin? |
103 | }; |
104 | |
105 | } // namespace internal |
106 | } // namespace skottie |
107 | |
108 | #endif // SkottieTextAnimator_DEFINED |
109 | |