| 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/SkNx.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/utils/SkJSON.h" |
| 17 | |
| 18 | namespace skottie { |
| 19 | namespace internal { |
| 20 | |
| 21 | /* |
| 22 | * Text layers can have optional text property animators. |
| 23 | * |
| 24 | * Each animator consists of |
| 25 | * |
| 26 | * 1) a list of animated properties (e.g. position, fill color, etc) |
| 27 | * |
| 28 | * 2) a list of range selectors |
| 29 | * |
| 30 | * Animated properties yield new values to be applied to the text, while range selectors |
| 31 | * determine the text subset these new values are applied to. |
| 32 | * |
| 33 | * The best way to think of range selectors is in terms of coverage: they combine to generate |
| 34 | * a coverage value [0..1] for each text fragment/glyph. This coverage is then used to modulate |
| 35 | * how the new property value is applied to a given fragment (interpolation weight). |
| 36 | * |
| 37 | * Note: Bodymovin currently only supports a single selector. |
| 38 | * |
| 39 | * JSON structure: |
| 40 | * |
| 41 | * "t": { // text node |
| 42 | * "a": [ // animators list |
| 43 | * { // animator node |
| 44 | * "s": {...}, // selector node |
| 45 | * "a": { // animator properties node |
| 46 | * "a": {} // optional anchor point value |
| 47 | * "p": {}, // optional position value |
| 48 | * "s": {}, // optional scale value |
| 49 | * "o": {}, // optional opacity |
| 50 | * "fc": {}, // optional fill color value |
| 51 | * "sc": {}, // optional stroke color value |
| 52 | * |
| 53 | * // TODO: more props? |
| 54 | * } |
| 55 | * }, |
| 56 | * ... |
| 57 | * ], |
| 58 | * ... |
| 59 | * } |
| 60 | */ |
| 61 | sk_sp<TextAnimator> TextAnimator::Make(const skjson::ObjectValue* janimator, |
| 62 | const AnimationBuilder* abuilder, |
| 63 | AnimatablePropertyContainer* acontainer) { |
| 64 | if (!janimator) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | const skjson::ObjectValue* jprops = (*janimator)["a" ]; |
| 69 | if (!jprops) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | std::vector<sk_sp<RangeSelector>> selectors; |
| 74 | |
| 75 | // Depending on compat mode and whether more than one selector is present, |
| 76 | // BM exports either an array or a single object. |
| 77 | if (const skjson::ArrayValue* jselectors = (*janimator)["s" ]) { |
| 78 | selectors.reserve(jselectors->size()); |
| 79 | for (const skjson::ObjectValue* jselector : *jselectors) { |
| 80 | if (auto sel = RangeSelector::Make(*jselector, abuilder, acontainer)) { |
| 81 | selectors.push_back(std::move(sel)); |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | if (auto sel = RangeSelector::Make((*janimator)["s" ], abuilder, acontainer)) { |
| 86 | selectors.reserve(1); |
| 87 | selectors.push_back(std::move(sel)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return sk_sp<TextAnimator>( |
| 92 | new TextAnimator(std::move(selectors), *jprops, abuilder, acontainer)); |
| 93 | } |
| 94 | |
| 95 | void TextAnimator::modulateProps(const DomainMaps& maps, ModulatorBuffer& buf) const { |
| 96 | // No selectors -> full coverage. |
| 97 | const auto initial_coverage = fSelectors.empty() ? 1.f : 0.f; |
| 98 | |
| 99 | // Coverage is scoped per animator. |
| 100 | for (auto& mod : buf) { |
| 101 | mod.coverage = initial_coverage; |
| 102 | } |
| 103 | |
| 104 | // Accumulate selector coverage. |
| 105 | for (const auto& selector : fSelectors) { |
| 106 | selector->modulateCoverage(maps, buf); |
| 107 | } |
| 108 | |
| 109 | // Modulate animated props. |
| 110 | for (auto& mod : buf) { |
| 111 | mod.props = this->modulateProps(mod.props, mod.coverage); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | TextAnimator::ResolvedProps TextAnimator::modulateProps(const ResolvedProps& props, |
| 116 | float amount) const { |
| 117 | auto modulated_props = props; |
| 118 | |
| 119 | // Transform props compose. |
| 120 | modulated_props.position += static_cast<SkV3>(fTextProps.position) * amount; |
| 121 | modulated_props.rotation += fTextProps.rotation * amount; |
| 122 | modulated_props.tracking += fTextProps.tracking * amount; |
| 123 | modulated_props.scale *= SkV3{1,1,1} + |
| 124 | (static_cast<SkV3>(fTextProps.scale) * 0.01f - SkV3{1,1,1}) * amount; |
| 125 | |
| 126 | // ... as does blur |
| 127 | modulated_props.blur += fTextProps.blur * amount; |
| 128 | |
| 129 | const auto lerp_color = [](SkColor c0, SkColor c1, float t) { |
| 130 | const auto c0_4f = SkNx_cast<float>(Sk4b::Load(&c0)), |
| 131 | c1_4f = SkNx_cast<float>(Sk4b::Load(&c1)), |
| 132 | c_4f = c0_4f + (c1_4f - c0_4f) * t; |
| 133 | |
| 134 | SkColor c; |
| 135 | SkNx_cast<uint8_t>(Sk4f_round(c_4f)).store(&c); |
| 136 | return c; |
| 137 | }; |
| 138 | |
| 139 | // Colors and opacity are overridden, and use a clamped amount value. |
| 140 | const auto clamped_amount = std::max(amount, 0.0f); |
| 141 | if (fHasFillColor) { |
| 142 | const auto fc = static_cast<SkColor>(fTextProps.fill_color); |
| 143 | modulated_props.fill_color = lerp_color(props.fill_color, fc, clamped_amount); |
| 144 | } |
| 145 | if (fHasStrokeColor) { |
| 146 | const auto sc = static_cast<SkColor>(fTextProps.stroke_color); |
| 147 | modulated_props.stroke_color = lerp_color(props.stroke_color, sc, clamped_amount); |
| 148 | } |
| 149 | modulated_props.opacity *= 1 + (fTextProps.opacity * 0.01f - 1) * clamped_amount; // 100-based |
| 150 | |
| 151 | return modulated_props; |
| 152 | } |
| 153 | |
| 154 | TextAnimator::TextAnimator(std::vector<sk_sp<RangeSelector>>&& selectors, |
| 155 | const skjson::ObjectValue& jprops, |
| 156 | const AnimationBuilder* abuilder, |
| 157 | AnimatablePropertyContainer* acontainer) |
| 158 | : fSelectors(std::move(selectors)) |
| 159 | , fRequiresAnchorPoint(false) { |
| 160 | |
| 161 | acontainer->bind(*abuilder, jprops["p" ], fTextProps.position); |
| 162 | acontainer->bind(*abuilder, jprops["o" ], fTextProps.opacity ); |
| 163 | acontainer->bind(*abuilder, jprops["t" ], fTextProps.tracking); |
| 164 | |
| 165 | // Scale and rotation are anchor-point-dependent. |
| 166 | fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["s" ], fTextProps.scale); |
| 167 | |
| 168 | // Depending on whether we're in 2D/3D mode, some of these will stick and some will not. |
| 169 | // It's fine either way. |
| 170 | fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["rx" ], fTextProps.rotation.x); |
| 171 | fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["ry" ], fTextProps.rotation.y); |
| 172 | fRequiresAnchorPoint |= acontainer->bind(*abuilder, jprops["r" ], fTextProps.rotation.z); |
| 173 | |
| 174 | fHasFillColor = acontainer->bind(*abuilder, jprops["fc" ], fTextProps.fill_color ); |
| 175 | fHasStrokeColor = acontainer->bind(*abuilder, jprops["sc" ], fTextProps.stroke_color); |
| 176 | fHasBlur = acontainer->bind(*abuilder, jprops["bl" ], fTextProps.blur ); |
| 177 | } |
| 178 | |
| 179 | } // namespace internal |
| 180 | } // namespace skottie |
| 181 | |