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 "include/core/SkM44.h" 12 #include "include/core/SkPoint.h" 13 #include "include/core/SkRefCnt.h" 14 #include "modules/skottie/include/TextShaper.h" 15 #include "modules/skottie/src/SkottieValue.h" 16 #include "modules/skottie/src/animator/Animator.h" 17 #include "modules/skottie/src/text/Font.h" 18 #include "modules/skottie/src/text/TextAnimator.h" 19 #include "modules/skottie/src/text/TextValue.h" 20 #include "modules/sksg/include/SkSGPaint.h" 21 #include "modules/sksg/include/SkSGRenderEffect.h" 22 #include "modules/sksg/include/SkSGTransform.h" 23 24 #include <cstdint> 25 #include <memory> 26 #include <vector> 27 28 class SkFontMgr; 29 30 namespace skjson { 31 class ObjectValue; 32 } 33 namespace skottie { 34 class Logger; 35 } 36 namespace sksg { 37 class Group; 38 class RenderNode; 39 } // namespace sksg 40 41 namespace SkShapers { class Factory; } 42 43 namespace skottie { 44 namespace internal { 45 class AnimationBuilder; 46 47 class TextAdapter final : public AnimatablePropertyContainer { 48 public: 49 static sk_sp<TextAdapter> Make(const skjson::ObjectValue&, 50 const AnimationBuilder*, 51 sk_sp<SkFontMgr>, 52 sk_sp<CustomFont::GlyphCompMapper>, 53 sk_sp<Logger>, 54 sk_sp<::SkShapers::Factory>); 55 56 ~TextAdapter() override; 57 node()58 const sk_sp<sksg::Group>& node() const { return fRoot; } 59 getText()60 const TextValue& getText() const { return fText.fCurrentValue; } 61 void setText(const TextValue&); 62 63 protected: 64 void onSync() override; 65 66 private: 67 class GlyphDecoratorNode; 68 69 enum class AnchorPointGrouping : uint8_t { 70 kCharacter, 71 kWord, 72 kLine, 73 kAll, 74 }; 75 76 TextAdapter(sk_sp<SkFontMgr>, 77 sk_sp<CustomFont::GlyphCompMapper>, 78 sk_sp<Logger>, 79 sk_sp<SkShapers::Factory>, 80 AnchorPointGrouping); 81 82 struct FragmentRec { 83 SkPoint fOrigin; // fragment position 84 85 const Shaper::ShapedGlyphs* fGlyphs = nullptr; 86 sk_sp<sksg::Matrix<SkM44>> fMatrixNode; 87 sk_sp<sksg::Color> fFillColorNode, 88 fStrokeColorNode; 89 sk_sp<sksg::BlurImageFilter> fBlur; 90 91 float fAdvance, // used for transform anchor point calculations 92 fAscent; // ^ 93 }; 94 95 void reshape(); 96 void addFragment(Shaper::Fragment&, sksg::Group* container); 97 void buildDomainMaps(const Shaper::Result&); 98 std::vector<sk_sp<sksg::RenderNode>> buildGlyphCompNodes(Shaper::ShapedGlyphs&) const; 99 100 void pushPropsToFragment(const TextAnimator::ResolvedProps&, const FragmentRec&, 101 const SkV2& frag_offset, const SkV2& grouping_alignment, 102 const TextAnimator::DomainSpan*) const; 103 104 SkV2 fragmentAnchorPoint(const FragmentRec&, const SkV2&, 105 const TextAnimator::DomainSpan*) const; 106 uint32_t shaperFlags() const; 107 108 SkM44 fragmentMatrix(const TextAnimator::ResolvedProps&, const FragmentRec&, const SkV2&) const; 109 110 const sk_sp<sksg::Group> fRoot; 111 const sk_sp<SkFontMgr> fFontMgr; 112 const sk_sp<CustomFont::GlyphCompMapper> fCustomGlyphMapper; 113 sk_sp<Logger> fLogger; 114 sk_sp<SkShapers::Factory> fShapingFactory; 115 const AnchorPointGrouping fAnchorPointGrouping; 116 117 std::vector<sk_sp<TextAnimator>> fAnimators; 118 std::vector<FragmentRec> fFragments; 119 TextAnimator::DomainMaps fMaps; 120 121 // Helps detect external value changes. 122 struct TextValueTracker { 123 TextValue fCurrentValue; 124 hasChangedTextValueTracker125 bool hasChanged() const { 126 if (fCurrentValue != fPrevValue) { 127 fPrevValue = fCurrentValue; 128 return true; 129 } 130 return false; 131 } 132 133 const TextValue* operator->() const { return &fCurrentValue; } 134 135 private: 136 mutable TextValue fPrevValue; 137 }; 138 139 TextValueTracker fText; 140 Vec2Value fGroupingAlignment = {0,0}; 141 float fTextShapingScale = 1; // size adjustment from auto-scaling 142 143 // Optional text path. 144 struct PathInfo; 145 std::unique_ptr<PathInfo> fPathInfo; 146 147 bool fHasBlurAnimator : 1, 148 fRequiresAnchorPoint : 1, 149 fRequiresLineAdjustments : 1; 150 }; 151 152 } // namespace internal 153 } // namespace skottie 154 155 #endif // SkottieTextAdapter_DEFINED 156