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