Debian Codegraph @

modules/skottie/src/text/TextAnimator.cpp

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