• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkottieTextAdapter_DEFINED
9 #define SkottieTextAdapter_DEFINED
10 
11 #include "modules/skottie/src/Animator.h"
12 #include "modules/skottie/src/text/SkottieShaper.h"
13 #include "modules/skottie/src/text/TextAnimator.h"
14 #include "modules/skottie/src/text/TextValue.h"
15 
16 #include <vector>
17 
18 class SkFontMgr;
19 
20 namespace sksg {
21 class Group;
22 template <typename T>
23 class Matrix;
24 } // namespace sksg
25 
26 namespace skottie {
27 namespace internal {
28 
29 class TextAdapter final : public AnimatablePropertyContainer {
30 public:
31     static sk_sp<TextAdapter> Make(const skjson::ObjectValue&, const AnimationBuilder*,
32                                    sk_sp<SkFontMgr>, sk_sp<Logger>);
33 
34     ~TextAdapter() override;
35 
node()36     const sk_sp<sksg::Group>& node() const { return fRoot; }
37 
getText()38     const TextValue& getText() const { return fText.fCurrentValue; }
39     void setText(const TextValue&);
40 
41 protected:
42     void onSync() override;
43 
44 private:
45     TextAdapter(sk_sp<SkFontMgr>, sk_sp<Logger>);
46 
47     struct FragmentRec {
48         SkPoint                    fOrigin; // fragment position
49 
50         sk_sp<sksg::Matrix<SkM44>> fMatrixNode;
51         sk_sp<sksg::Color>         fFillColorNode,
52                                    fStrokeColorNode;
53     };
54 
55     void reshape();
56     void addFragment(const Shaper::Fragment&);
57     void buildDomainMaps(const Shaper::Result&);
58 
59     void pushPropsToFragment(const TextAnimator::ResolvedProps&, const FragmentRec&) const;
60 
61     void adjustLineTracking(const TextAnimator::ModulatorBuffer&,
62                             const TextAnimator::DomainSpan&,
63                             float line_tracking) const;
64 
65     const sk_sp<sksg::Group>         fRoot;
66     const sk_sp<SkFontMgr>           fFontMgr;
67     sk_sp<Logger>                    fLogger;
68 
69     std::vector<sk_sp<TextAnimator>> fAnimators;
70     std::vector<FragmentRec>         fFragments;
71     TextAnimator::DomainMaps         fMaps;
72 
73     // Helps detect external value changes.
74     struct TextValueTracker {
75         TextValue fCurrentValue;
76 
hasChangedTextValueTracker77         bool hasChanged() const {
78             if (fCurrentValue != fPrevValue) {
79                 fPrevValue = fCurrentValue;
80                 return true;
81             }
82             return false;
83         }
84 
85         const TextValue* operator->() const { return &fCurrentValue; }
86 
87     private:
88         mutable TextValue fPrevValue;
89     };
90 
91     TextValueTracker fText;
92 };
93 
94 } // namespace internal
95 } // namespace skottie
96 
97 #endif // SkottieTextAdapter_DEFINED
98