1 /*
2  * Copyright (c) 2024 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 OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H
17 #define OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H
18 
19 #include <memory>
20 #include <set>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "font_parser.h"
26 
27 namespace OHOS::Rosen {
28 using FontDescSharedPtr = std::shared_ptr<TextEngine::FontParser::FontDescriptor>;
29 class FontDescriptorCache {
30 public:
31     FontDescriptorCache();
32     ~FontDescriptorCache();
33     void ParserSystemFonts();
34     void ParserStylishFonts();
35     void MatchFromFontDescriptor(FontDescSharedPtr desc, std::set<FontDescSharedPtr>& result);
36     void ClearFontFileCache();
37     void Dump();
38     void GetFontDescSharedPtrByFullName(const std::string& fullName,
39         const int32_t& systemFontType, FontDescSharedPtr& result);
40     void GetSystemFontFullNamesByType(const int32_t& systemFontType, std::unordered_set<std::string>& fontList);
41 
42 private:
43     void FontDescriptorScatter(FontDescSharedPtr desc);
44     void ParserInstallFonts();
45     bool ParseInstalledConfigFile(const std::string& fontPath, std::vector<std::string>& fontPathList);
46     bool ProcessInstalledFontPath(const std::string& path);
47     bool ProcessSystemFontType(const int32_t& systemFontType, int32_t& fontType);
48     bool ParseInstallFontDescSharedPtrByName(const std::string& fullName, FontDescSharedPtr& result);
49     std::unordered_set<std::string> GetInstallFontList();
50     std::unordered_set<std::string> GetStylishFontList();
51     std::unordered_set<std::string> GetGenericFontList();
52     bool HandleMapIntersection(std::set<FontDescSharedPtr>& finishRet, const std::string& name,
53         std::unordered_map<std::string, std::set<FontDescSharedPtr>>& map);
54     bool FilterBoldCache(int weight, std::set<FontDescSharedPtr>& finishRet);
55     bool FilterWidthCache(int width, std::set<FontDescSharedPtr>& finishRet);
56     bool FilterItalicCache(int italic, std::set<FontDescSharedPtr>& finishRet);
57     bool FilterMonoSpaceCache(bool monoSpace, std::set<FontDescSharedPtr>& finishRet);
58     bool FilterSymbolicCache(bool symbolic, std::set<FontDescSharedPtr>& finishRet);
59     bool IsDefault(FontDescSharedPtr desc);
60 
61 private:
62     TextEngine::FontParser parser_;
63 
64     struct FontDescriptorEqual {
operator ()OHOS::Rosen::FontDescriptorCache::FontDescriptorEqual65         bool operator()(const FontDescSharedPtr& lhs, const FontDescSharedPtr& rhs) const
66         {
67             if (lhs->fontFamily == rhs->fontFamily) {
68                 return lhs->fullName < rhs->fullName;
69             }
70             return lhs->fontFamily < rhs->fontFamily;
71         }
72     };
73     std::set<FontDescSharedPtr, FontDescriptorEqual> allFontDescriptor_;
74     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontFamilyMap_;
75     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fullNameMap_;
76     std::unordered_map<std::string, std::set<FontDescSharedPtr>> postScriptNameMap_;
77     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontSubfamilyNameMap_;
78     std::set<FontDescSharedPtr> boldCache_;
79     std::set<FontDescSharedPtr> italicCache_;
80     std::set<FontDescSharedPtr> monoSpaceCache_;
81     std::set<FontDescSharedPtr> symbolicCache_;
82     std::unordered_map<std::string, std::vector<std::string>> installPathMap_;
83     std::unordered_map<std::string, std::set<FontDescSharedPtr>> stylishFullNameMap_;
84 };
85 } // namespace OHOS::Rosen
86 
87 #endif // OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H