• 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_change_observer.h"
25 #include "core/common/font_loader.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components_ng/base/frame_node.h"
28 #include "core/pipeline/pipeline_base.h"
29 
30 namespace OHOS::Ace {
31 
32 struct FontInfo {
33     std::string path;
34     std::string postScriptName;
35     std::string fullName;
36     std::string family;
37     std::string subfamily;
38     uint32_t weight = 0;
39     uint32_t width = 0;
40     bool italic = false;
41     bool monoSpace = false;
42     bool symbolic = false;
43 };
44 
45 typedef struct AdjustInfo {
46     int origValue = 0;
47     int newValue = 0;
48 } AdjustInfo;
49 using AdjustSet = std::vector<AdjustInfo>;
50 
51 typedef struct AliasInfo {
52     std::string familyName;
53     int weight = 0;
54 } AliasInfo;
55 using AliasSet = std::vector<AliasInfo>;
56 
57 typedef struct FallbackInfo {
58     std::string familyName;
59     std::string font;
60 } FallbackInfo;
61 using FallbackInfoSet = std::vector<FallbackInfo>;
62 
63 typedef struct FallbackGroup {
64     std::string groupName;
65     FallbackInfoSet fallbackInfoSet;
66 } FallbackGroup;
67 using FallbackGroupSet = std::vector<FallbackGroup>;
68 
69 typedef struct FontGenericInfo {
70     std::string familyName;
71     AliasSet aliasSet;
72     AdjustSet adjustSet;
73 } FontGenericInfo;
74 using GenericSet = std::vector<FontGenericInfo>;
75 
76 typedef struct FontConfigJsonInfo {
77     std::vector<std::string> fontDirSet;
78     GenericSet genericSet;
79     FallbackGroupSet fallbackGroupSet;
80 } FontConfigJsonInfo;
81 
82 using ExternalLoadFontPair = std::pair<std::string, std::function<void()>>;
83 
84 class FontManager : public virtual AceType {
85     DECLARE_ACE_TYPE(FontManager, AceType);
86 
87 public:
88     FontManager() = default;
89     ~FontManager() override = default;
90 
91     virtual void VaryFontCollectionWithFontWeightScale() = 0;
92 
93     virtual void LoadSystemFont() = 0;
94 
95     static RefPtr<FontManager> Create();
96 
97     void RegisterFont(const std::string& familyName, const std::string& familySrc, const RefPtr<PipelineBase>& context,
98         const std::string& bundleName = "", const std::string& moduleName = "");
99     void GetSystemFontList(std::vector<std::string>& fontList);
100     bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo);
101     bool RegisterCallback(
102         const WeakPtr<RenderNode>& node, const std::string& familyName, const std::function<void()>& callback);
103     void UnRegisterCallback(const WeakPtr<RenderNode>& node);
104     const std::vector<std::string>& GetFontNames() const;
105     void AddFontNode(const WeakPtr<RenderNode>& node);
106     void RemoveFontNode(const WeakPtr<RenderNode>& node);
107     void SetFontFamily(const char* familyName, const std::vector<std::string>& familySrc);
108     void RebuildFontNode();
109     void RebuildFontNodeNG();
110     void UpdateFontWeightScale();
111     void AddVariationNode(const WeakPtr<RenderNode>& node);
112     void RemoveVariationNode(const WeakPtr<RenderNode>& node);
113     void NotifyVariationNodes();
114     void GetUIFontConfig(FontConfigJsonInfo& info);
115 
116     bool RegisterCallbackNG(
117         const WeakPtr<NG::UINode>& node, const std::string& familyName, const std::function<void()>& callback);
118     void UnRegisterCallbackNG(const WeakPtr<NG::UINode>& node);
119     void AddFontNodeNG(const WeakPtr<NG::UINode>& node);
120     void RemoveFontNodeNG(const WeakPtr<NG::UINode>& node);
121     void AddVariationNodeNG(const WeakPtr<NG::UINode>& node);
122     void RemoveVariationNodeNG(const WeakPtr<NG::UINode>& node);
123     bool IsDefaultFontChanged();
124     bool IsUseAppCustomFont() const;
125     void SetAppCustomFont(const std::string& familyName);
126     const std::string& GetAppCustomFont() const;
127     void AddFontObserver(WeakPtr<FontChangeObserver> node);
128     void RemoveFontChangeObserver(WeakPtr<FontChangeObserver> node);
129     std::vector<std::string> GetFontNames();
130 
131     void AddHybridRenderNode(const WeakPtr<NG::UINode>& node);
132     void RemoveHybridRenderNode(const WeakPtr<NG::UINode>& node);
133     void UpdateHybridRenderNodes();
134 
135     using StartAbilityOnInstallAppInStoreHandler = std::function<void(const std::string& appName)>;
SetStartAbilityOnInstallAppInStoreHandler(StartAbilityOnInstallAppInStoreHandler && listener)136     void SetStartAbilityOnInstallAppInStoreHandler(StartAbilityOnInstallAppInStoreHandler&& listener)
137     {
138         startAbilityOnInstallAppInStoreHandler_ = std::move(listener);
139     }
140 
141     using StartAbilityOnJumpBrowserHandler = std::function<void(const std::string& appName)>;
SetStartAbilityOnJumpBrowserHandler(StartAbilityOnJumpBrowserHandler && listener)142     void SetStartAbilityOnJumpBrowserHandler(StartAbilityOnJumpBrowserHandler&& listener)
143     {
144         startAbilityOnJumpBrowserHandler_ = std::move(listener);
145     }
146 
147     using OpenLinkOnMapSearchHandler = std::function<void(const std::string& address)>;
SetOpenLinkOnMapSearchHandler(OpenLinkOnMapSearchHandler && listener)148     void SetOpenLinkOnMapSearchHandler(OpenLinkOnMapSearchHandler&& listener)
149     {
150         startOpenLinkOnMapSearchHandler_ = std::move(listener);
151     }
152 
153     using StartAbilityOnCanlendarHandler = std::function<void(const std::map<std::string, std::string>& params)>;
SetStartAbilityOnCalendar(StartAbilityOnCanlendarHandler && listener)154     void SetStartAbilityOnCalendar(StartAbilityOnCanlendarHandler&& listener)
155     {
156         startAbilityOnCalendarHandler_ = std::move(listener);
157     }
158 
159     void StartAbilityOnJumpBrowser(const std::string& address) const;
160     void StartAbilityOnInstallAppInStore(const std::string& appName) const;
161     void StartAbilityOnCalendar(const std::map<std::string, std::string>& params) const;
162     void OpenLinkOnMapSearch(const std::string& address);
163 
164     void OnPreviewMenuOptionClick(TextDataDetectType type, const std::string& content);
165 
166 protected:
167     static float fontWeightScale_;
168     static bool isDefaultFontChanged_;
169     static std::string appCustomFont_;
170 
171 private:
172     void FontNodeChangeStyleNG();
173     void RegisterLoadFontCallbacks();
174     void OnLoadFontChanged(const WeakPtr<PipelineBase>& context, const std::string& fontName);
175 
176     std::list<RefPtr<FontLoader>> fontLoaders_;
177     std::vector<std::string> fontNames_;
178     std::set<WeakPtr<RenderNode>> fontNodes_;
179     std::set<WeakPtr<NG::UINode>> fontNodesNG_;
180     // Render nodes need to layout when wght scale is changed.
181     std::set<WeakPtr<RenderNode>> variationNodes_;
182     std::set<WeakPtr<NG::UINode>> variationNodesNG_;
183     std::set<WeakPtr<FontChangeObserver>> observers_;
184     std::map<WeakPtr<NG::UINode>, ExternalLoadFontPair> externalLoadCallbacks_;
185     bool hasRegisterLoadFontCallback_ = false;
186 
187     StartAbilityOnInstallAppInStoreHandler startAbilityOnInstallAppInStoreHandler_;
188     StartAbilityOnJumpBrowserHandler startAbilityOnJumpBrowserHandler_;
189     OpenLinkOnMapSearchHandler startOpenLinkOnMapSearchHandler_;
190     StartAbilityOnCanlendarHandler startAbilityOnCalendarHandler_;
191 
192     std::mutex hybridRenderNodesMutex_;
193     std::set<WeakPtr<NG::UINode>> hybridRenderNodes_;
194 };
195 
196 } // namespace OHOS::Ace
197 
198 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FONT_MANAGER_H
199