• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_loader.h"
25 #include "core/pipeline/pipeline_base.h"
26 
27 namespace OHOS::Ace {
28 
29 struct FontInfo {
30     std::string path;
31     std::string postScriptName;
32     std::string fullName;
33     std::string family;
34     std::string subfamily;
35     uint32_t weight = 0;
36     uint32_t width = 0;
37     bool italic = false;
38     bool monoSpace = false;
39     bool symbolic = false;
40 };
41 
42 typedef struct AdjustInfo {
43     int origValue = 0;
44     int newValue = 0;
45 } AdjustInfo;
46 using AdjustSet = std::vector<AdjustInfo>;
47 
48 typedef struct AliasInfo {
49     std::string familyName;
50     int weight = 0;
51 } AliasInfo;
52 using AliasSet = std::vector<AliasInfo>;
53 
54 typedef struct FallbackInfo {
55     std::string familyName;
56     std::string font;
57 } FallbackInfo;
58 using FallbackInfoSet = std::vector<FallbackInfo>;
59 
60 typedef struct FallbackGroup {
61     std::string groupName;
62     FallbackInfoSet fallbackInfoSet;
63 } FallbackGroup;
64 using FallbackGroupSet = std::vector<FallbackGroup>;
65 
66 typedef struct FontGenericInfo {
67     std::string familyName;
68     AliasSet aliasSet;
69     AdjustSet adjustSet;
70 } FontGenericInfo;
71 using GenericSet = std::vector<FontGenericInfo>;
72 
73 typedef struct FontConfigJsonInfo {
74     std::vector<std::string> fontDirSet;
75     GenericSet genericSet;
76     FallbackGroupSet fallbackGroupSet;
77 } FontConfigJsonInfo;
78 
79 class FontManager : public virtual AceType {
80     DECLARE_ACE_TYPE(FontManager, AceType);
81 
82 public:
83     FontManager() = default;
84     ~FontManager() override = default;
85 
86     virtual void VaryFontCollectionWithFontWeightScale() = 0;
87 
88     virtual void LoadSystemFont() = 0;
89 
90     static RefPtr<FontManager> Create();
91 
92     void RegisterFont(const std::string& familyName, const std::string& familySrc, const RefPtr<PipelineBase>& context,
93         const std::string& bundleName = "", const std::string& moduleName = "");
94     void GetSystemFontList(std::vector<std::string>& fontList);
95     bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo);
96     bool RegisterCallback(
97         const WeakPtr<RenderNode>& node, const std::string& familyName, const std::function<void()>& callback);
98     void UnRegisterCallback(const WeakPtr<RenderNode>& node);
99     const std::vector<std::string>& GetFontNames() const;
100     void AddFontNode(const WeakPtr<RenderNode>& node);
101     void RemoveFontNode(const WeakPtr<RenderNode>& node);
102     void SetFontFamily(const char* familyName, const char* familySrc);
103     void RebuildFontNode();
104     void UpdateFontWeightScale();
105     void AddVariationNode(const WeakPtr<RenderNode>& node);
106     void RemoveVariationNode(const WeakPtr<RenderNode>& node);
107     void NotifyVariationNodes();
108     void GetUIFontConfig(FontConfigJsonInfo& info);
109 
110     bool RegisterCallbackNG(
111         const WeakPtr<NG::UINode>& node, const std::string& familyName, const std::function<void()>& callback);
112     void UnRegisterCallbackNG(const WeakPtr<NG::UINode>& node);
113     void AddFontNodeNG(const WeakPtr<NG::UINode>& node);
114     void RemoveFontNodeNG(const WeakPtr<NG::UINode>& node);
115     void AddVariationNodeNG(const WeakPtr<NG::UINode>& node);
116     void RemoveVariationNodeNG(const WeakPtr<NG::UINode>& node);
117     bool IsDefaultFontChanged();
118 
119 protected:
120     static float fontWeightScale_;
121     static bool isDefaultFontChanged_;
122 
123 private:
124     std::list<RefPtr<FontLoader>> fontLoaders_;
125     std::vector<std::string> fontNames_;
126     std::set<WeakPtr<RenderNode>> fontNodes_;
127     std::set<WeakPtr<NG::UINode>> fontNodesNG_;
128     // Render nodes need to layout when wght scale is changed.
129     std::set<WeakPtr<RenderNode>> variationNodes_;
130     std::set<WeakPtr<NG::UINode>> variationNodesNG_;
131 };
132 
133 } // namespace OHOS::Ace
134 
135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FONT_MANAGER_H
136