1 /* 2 * Copyright 2022 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 SkottieFont_DEFINED 9 #define SkottieFont_DEFINED 10 11 #include "include/core/SkTypeface.h" 12 #include "include/private/base/SkNoncopyable.h" 13 #include "include/utils/SkCustomTypeface.h" 14 #include "modules/sksg/include/SkSGRenderNode.h" 15 #include "src/core/SkTHash.h" 16 17 #include <memory> 18 #include <vector> 19 20 class SkPath; 21 22 namespace skjson { class ObjectValue; } 23 24 namespace skottie::internal { 25 26 class AnimationBuilder; 27 28 // Font backed by Lottie character data (glyph paths and glyph compositions). 29 class CustomFont final : SkNoncopyable { 30 public: 31 ~CustomFont(); 32 33 using GlyphCompMap = SkTHashMap<SkGlyphID, sk_sp<sksg::RenderNode>>; 34 35 class Builder final : SkNoncopyable { 36 public: 37 bool parseGlyph(const AnimationBuilder*, const skjson::ObjectValue&); 38 std::unique_ptr<CustomFont> detach(); 39 40 private: 41 static bool ParseGlyphPath(const AnimationBuilder*, const skjson::ObjectValue&, SkPath*); 42 static sk_sp<sksg::RenderNode> ParseGlyphComp(const AnimationBuilder*, 43 const skjson::ObjectValue&); 44 45 GlyphCompMap fGlyphComps; 46 SkCustomTypefaceBuilder fCustomBuilder; 47 }; 48 49 // Helper for resolving (SkTypeface, SkGlyphID) tuples to a composition root. 50 // Used post-shaping, to substitute composition glyphs in the rendering tree. 51 class GlyphCompMapper final : public SkRefCnt { 52 public: GlyphCompMapper(std::vector<std::unique_ptr<CustomFont>> && fonts)53 explicit GlyphCompMapper(std::vector<std::unique_ptr<CustomFont>>&& fonts) 54 : fFonts(std::move(fonts)) {} 55 56 ~GlyphCompMapper() override = default; 57 58 sk_sp<sksg::RenderNode> getGlyphComp(const SkTypeface*, SkGlyphID) const; 59 60 private: 61 const std::vector<std::unique_ptr<CustomFont>> fFonts; 62 }; 63 typeface()64 const sk_sp<SkTypeface>& typeface() const { return fTypeface; } 65 glyphCompCount()66 int glyphCompCount() const { return fGlyphComps.count(); } 67 68 private: 69 CustomFont(GlyphCompMap&&, sk_sp<SkTypeface> tf); 70 71 const GlyphCompMap fGlyphComps; 72 const sk_sp<SkTypeface> fTypeface; 73 }; 74 75 } // namespace skottie::internal 76 77 #endif // SkottieFont_DEFINED 78