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 #include "modules/skottie/src/text/TextAnimator.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPoint.h"
12 #include "include/private/SkColorData.h"
13 #include "modules/skottie/src/SkottieValue.h"
14 #include "modules/skottie/src/animator/Animator.h"
15 #include "modules/skottie/src/text/RangeSelector.h"
16 #include "src/base/SkVx.h"
17 #include "src/core/SkSwizzlePriv.h"
18 #include "src/utils/SkJSON.h"
19
20 #include <cmath>
21
22 namespace skottie {
23 namespace internal {
24
25 /*
26 * Text layers can have optional text property animators.
27 *
28 * Each animator consists of
29 *
30 * 1) a list of animated properties (e.g. position, fill color, etc)
31 *
32 * 2) a list of range selectors
33 *
34 * Animated properties yield new values to be applied to the text, while range selectors
35 * determine the text subset these new values are applied to.
36 *
37 * The best way to think of range selectors is in terms of coverage: they combine to generate
38 * a coverage value [0..1] for each text fragment/glyph. This coverage is then used to modulate
39 * how the new property value is applied to a given fragment (interpolation weight).
40 *
41 * Note: Bodymovin currently only supports a single selector.
42 *
43 * JSON structure:
44 *
45 * "t": { // text node
46 * "a": [ // animators list
47 * { // animator node
48 * "s": {...}, // selector node
49 * "a": { // animator properties node
50 * "a": {} // optional anchor point value
51 * "p": {}, // optional position value
52 * "s": {}, // optional scale value
53 * "o": {}, // optional opacity
54 * "fc": {}, // optional fill color value
55 * "sc": {}, // optional stroke color value
56 *
57 * // TODO: more props?
58 * }
59 * },
60 * ...
61 * ],
62 * ...
63 * }
64 */
Make(const skjson::ObjectValue * janimator,const AnimationBuilder * abuilder,AnimatablePropertyContainer * acontainer)65 sk_sp<TextAnimator> TextAnimator::Make(const skjson::ObjectValue* janimator,
66 const AnimationBuilder* abuilder,
67 AnimatablePropertyContainer* acontainer) {
68 if (!janimator) {
69 return nullptr;
70 }
71
72 const skjson::ObjectValue* jprops = (*janimator)["a"];
73 if (!jprops) {
74 return nullptr;
75 }
76
77 std::vector<sk_sp<RangeSelector>> selectors;
78
79 // Depending on compat mode and whether more than one selector is present,
80 // BM exports either an array or a single object.
81 if (const skjson::ArrayValue* jselectors = (*janimator)["s"]) {
82 selectors.reserve(jselectors->size());
83 for (const skjson::ObjectValue* jselector : *jselectors) {
84 if (auto sel = RangeSelector::Make(*jselector, abuilder, acontainer)) {
85 selectors.push_back(std::move(sel));
86 }
87 }
88 } else {
89 if (auto sel = RangeSelector::Make((*janimator)["s"], abuilder, acontainer)) {
90 selectors.reserve(1);
91 selectors.push_back(std::move(sel));
92 }
93 }
94
95 return sk_sp<TextAnimator>(
96 new TextAnimator(std::move(selectors), *jprops, abuilder, acontainer));
97 }
98
modulateProps(const DomainMaps & maps,ModulatorBuffer & buf) const99 void TextAnimator::modulateProps(const DomainMaps& maps, ModulatorBuffer& buf) const {
100 // No selectors -> full coverage.
101 const auto initial_coverage = fSelectors.empty() ? 1.f : 0.f;
102
103 // Coverage is scoped per animator.
104 for (auto& mod : buf) {
105 mod.coverage = initial_coverage;
106 }
107
108 // Accumulate selector coverage.
109 for (const auto& selector : fSelectors) {
110 selector->modulateCoverage(maps, buf);
111 }
112
113 // Modulate animated props.
114 for (auto& mod : buf) {
115 mod.props = this->modulateProps(mod.props, mod.coverage);
116 }
117 }
118
modulateProps(const ResolvedProps & props,float amount) const119 TextAnimator::ResolvedProps TextAnimator::modulateProps(const ResolvedProps& props,
120 float amount) const {
121 auto modulated_props = props;
122
123 // Transform props compose.
124 modulated_props.position += static_cast<SkV3>(fTextProps.position) * amount;
125 modulated_props.rotation += fTextProps.rotation * amount;
126 modulated_props.tracking += fTextProps.tracking * amount;
127 modulated_props.scale *= SkV3{1,1,1} +
128 (static_cast<SkV3>(fTextProps.scale) * 0.01f - SkV3{1,1,1}) * amount;
129
130 // ... as do blur, line spacing, and stroke width.
131 modulated_props.blur += fTextProps.blur * amount;
132 modulated_props.line_spacing += fTextProps.line_spacing * amount;
133 modulated_props.stroke_width += fTextProps.stroke_width * amount;
134
135 const auto lerp = [](float v0, float v1, float t) {
136 return v0 + (v1 - v0)*t;
137 };
138 const auto lerp_color = [](SkColor c0, SkColor c1, float t) {
139 const auto c0_4f = Sk4f_fromL32(c0),
140 c1_4f = Sk4f_fromL32(c1),
141 c_4f = c0_4f + (c1_4f - c0_4f) * t;
142
143 return Sk4f_toL32(c_4f);
144 };
145
146 // Colors and opacity are interpolated, and use a clamped amount value.
147 const auto clamped_amount = std::max(amount, 0.0f);
148 if (fHasFillColor) {
149 modulated_props.fill_color = lerp_color(props.fill_color,
150 fTextProps.fill_color,
151 clamped_amount);
152 }
153 if (fHasStrokeColor) {
154 modulated_props.stroke_color = lerp_color(props.stroke_color,
155 fTextProps.stroke_color,
156 clamped_amount);
157 }
158 if (fHasFillOpacity) {
159 // 255-based
160 const auto alpha = lerp(SkColorGetA(props.fill_color),
161 fTextProps.fill_opacity*2.55f,
162 clamped_amount);
163 modulated_props.fill_color = SkColorSetA(modulated_props.fill_color,
164 static_cast<U8CPU>(std::round(alpha)));
165 }
166 if (fHasStrokeOpacity) {
167 // 255-based
168 const auto alpha = lerp(SkColorGetA(props.stroke_color),
169 fTextProps.stroke_opacity*2.55f,
170 clamped_amount);
171 modulated_props.stroke_color = SkColorSetA(modulated_props.stroke_color,
172 static_cast<U8CPU>(std::round(alpha)));
173 }
174 if (fHasOpacity) {
175 modulated_props.opacity = lerp(props.opacity, fTextProps.opacity*0.01f, clamped_amount);
176 }
177
178 return modulated_props;
179 }
180
TextAnimator(std::vector<sk_sp<RangeSelector>> && selectors,const skjson::ObjectValue & jprops,const AnimationBuilder * abuilder,AnimatablePropertyContainer * acontainer)181 TextAnimator::TextAnimator(std::vector<sk_sp<RangeSelector>>&& selectors,
182 const skjson::ObjectValue& jprops,
183 const AnimationBuilder* abuilder,
184 AnimatablePropertyContainer* acontainer)
185 : fSelectors(std::move(selectors))
186 , fRequiresAnchorPoint(false)
187 , fRequiresLineAdjustments(false) {
188
189 acontainer->bind(*abuilder, jprops["p" ], fTextProps.position);
190
191 // Tracking and line spacing affect all line fragments.
192 fRequiresLineAdjustments |= acontainer->bind(*abuilder, jprops["t" ], fTextProps.tracking);
193 fRequiresLineAdjustments |= acontainer->bind(*abuilder, jprops["ls"], fTextProps.line_spacing);
194
195 // Scale and rotation are anchor-point-dependent.
196 fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["s"], fTextProps.scale);
197
198 // Depending on whether we're in 2D/3D mode, some of these will stick and some will not.
199 // It's fine either way.
200 fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["rx"], fTextProps.rotation.x);
201 fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["ry"], fTextProps.rotation.y);
202 fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["r" ], fTextProps.rotation.z);
203
204 fHasFillColor = acontainer->bind(*abuilder, jprops["fc"], fTextProps.fill_color );
205 fHasStrokeColor = acontainer->bind(*abuilder, jprops["sc"], fTextProps.stroke_color );
206 fHasFillOpacity = acontainer->bind(*abuilder, jprops["fo"], fTextProps.fill_opacity );
207 fHasStrokeOpacity = acontainer->bind(*abuilder, jprops["so"], fTextProps.stroke_opacity);
208 fHasOpacity = acontainer->bind(*abuilder, jprops["o" ], fTextProps.opacity );
209 fHasBlur = acontainer->bind(*abuilder, jprops["bl"], fTextProps.blur );
210
211 acontainer->bind(*abuilder, jprops["sw"], fTextProps.stroke_width);
212 }
213
214 } // namespace internal
215 } // namespace skottie
216