1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FONT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FONT_MANAGER_H
18 
19 #include <list>
20 #include <set>
21 #include <vector>
22 
23 #include "base/memory/ace_type.h"
24 #include "core/common/font_change_observer.h"
25 #include "core/common/font_loader.h"
26 #include "core/pipeline/pipeline_base.h"
27 
28 namespace OHOS::Ace {
29 
30 struct FontInfo {
31     std::string path;
32     std::string postScriptName;
33     std::string fullName;
34     std::string family;
35     std::string subfamily;
36     uint32_t weight = 0;
37     uint32_t width = 0;
38     bool italic = false;
39     bool monoSpace = false;
40     bool symbolic = false;
41 };
42 
43 typedef struct AdjustInfo {
44     int origValue = 0;
45     int newValue = 0;
46 } AdjustInfo;
47 using AdjustSet = std::vector<AdjustInfo>;
48 
49 typedef struct AliasInfo {
50     std::string familyName;
51     int weight = 0;
52 } AliasInfo;
53 using AliasSet = std::vector<AliasInfo>;
54 
55 typedef struct FallbackInfo {
56     std::string familyName;
57     std::string font;
58 } FallbackInfo;
59 using FallbackInfoSet = std::vector<FallbackInfo>;
60 
61 typedef struct FallbackGroup {
62     std::string groupName;
63     FallbackInfoSet fallbackInfoSet;
64 } FallbackGroup;
65 using FallbackGroupSet = std::vector<FallbackGroup>;
66 
67 typedef struct FontGenericInfo {
68     std::string familyName;
69     AliasSet aliasSet;
70     AdjustSet adjustSet;
71 } FontGenericInfo;
72 using GenericSet = std::vector<FontGenericInfo>;
73 
74 typedef struct FontConfigJsonInfo {
75     std::vector<std::string> fontDirSet;
76     GenericSet genericSet;
77     FallbackGroupSet fallbackGroupSet;
78 } FontConfigJsonInfo;
79 
80 class FontManager : public virtual AceType {
81     DECLARE_ACE_TYPE(FontManager, AceType);
82 
83 public:
84     FontManager() = default;
85     ~FontManager() override = default;
86 
87     virtual void VaryFontCollectionWithFontWeightScale() = 0;
88 
89     virtual void LoadSystemFont() = 0;
90 
91     static RefPtr<FontManager> Create();
92 
93     void RegisterFont(const std::string& familyName, const std::string& familySrc, const RefPtr<PipelineBase>& context,
94         const std::string& bundleName = "", const std::string& moduleName = "");
95     void GetSystemFontList(std::vector<std::string>& fontList);
96     bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo);
97     bool RegisterCallback(
98         const WeakPtr<RenderNode>& node, const std::string& familyName, const std::function<void()>& callback);
99     void UnRegisterCallback(const WeakPtr<RenderNode>& node);
100     const std::vector<std::string>& GetFontNames() const;
101     void AddFontNode(const WeakPtr<RenderNode>& node);
102     void RemoveFontNode(const WeakPtr<RenderNode>& node);
103     void SetFontFamily(const char* familyName, const char* familySrc);
104     void RebuildFontNode();
105     void RebuildFontNodeNG();
106     void UpdateFontWeightScale();
107     void AddVariationNode(const WeakPtr<RenderNode>& node);
108     void RemoveVariationNode(const WeakPtr<RenderNode>& node);
109     void NotifyVariationNodes();
110     void GetUIFontConfig(FontConfigJsonInfo& info);
111 
112     bool RegisterCallbackNG(
113         const WeakPtr<NG::UINode>& node, const std::string& familyName, const std::function<void()>& callback);
114     void UnRegisterCallbackNG(const WeakPtr<NG::UINode>& node);
115     void AddFontNodeNG(const WeakPtr<NG::UINode>& node);
116     void RemoveFontNodeNG(const WeakPtr<NG::UINode>& node);
117     void AddVariationNodeNG(const WeakPtr<NG::UINode>& node);
118     void RemoveVariationNodeNG(const WeakPtr<NG::UINode>& node);
119     bool IsDefaultFontChanged();
120     bool IsUseAppCustomFont() const;
121     void SetAppCustomFont(const std::string& familyName);
122     const std::string& GetAppCustomFont() const;
123     void AddFontObserver(WeakPtr<FontChangeObserver> node);
124     void RemoveFontChangeObserver(WeakPtr<FontChangeObserver> node);
125 
126 protected:
127     static float fontWeightScale_;
128     static bool isDefaultFontChanged_;
129     static std::string appCustomFont_;
130 
131 private:
132     std::list<RefPtr<FontLoader>> fontLoaders_;
133     std::vector<std::string> fontNames_;
134     std::set<WeakPtr<RenderNode>> fontNodes_;
135     std::set<WeakPtr<NG::UINode>> fontNodesNG_;
136     // Render nodes need to layout when wght scale is changed.
137     std::set<WeakPtr<RenderNode>> variationNodes_;
138     std::set<WeakPtr<NG::UINode>> variationNodesNG_;
139     std::set<WeakPtr<FontChangeObserver>> observers_;
140 };
141 
142 } // namespace OHOS::Ace
143 
144 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FONT_MANAGER_H
145