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 ClearFontFileCache();
36     void Dump();
37     void GetFontDescSharedPtrByFullName(const std::string& fullName,
38         const int32_t& systemFontType, FontDescSharedPtr& result);
39     void GetSystemFontFullNamesByType(const int32_t& systemFontType, std::unordered_set<std::string>& fontList);
40 
41 private:
42     void FontDescriptorScatter(FontDescSharedPtr desc);
43     bool ParserInstallFontsPathList(std::vector<std::string>& fontPathList);
44     bool ProcessSystemFontType(const int32_t& systemFontType, int32_t& fontType);
45     bool ParseInstallFontDescSharedPtrByName(const std::string& fullName, FontDescSharedPtr& result);
46     std::unordered_set<std::string> GetInstallFontList();
47     std::unordered_set<std::string> GetStylishFontList();
48     std::unordered_set<std::string> GetGenericFontList();
49     int32_t WeightAlignment(int32_t weight);
50 
51 private:
52     TextEngine::FontParser parser_;
53 
54     struct FontDescriptorEqual {
operatorFontDescriptorEqual55         bool operator()(const FontDescSharedPtr& lhs, const FontDescSharedPtr& rhs) const
56         {
57             if (lhs->fontFamily == rhs->fontFamily) {
58                 return lhs->fullName < rhs->fullName;
59             }
60             return lhs->fontFamily < rhs->fontFamily;
61         }
62     };
63     std::set<FontDescSharedPtr, FontDescriptorEqual> allFontDescriptor_;
64     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontFamilyMap_;
65     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fullNameMap_;
66     std::unordered_map<std::string, std::set<FontDescSharedPtr>> postScriptNameMap_;
67     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontSubfamilyNameMap_;
68     std::set<FontDescSharedPtr> boldCache_;
69     std::set<FontDescSharedPtr> italicCache_;
70     std::set<FontDescSharedPtr> monoSpaceCache_;
71     std::set<FontDescSharedPtr> symbolicCache_;
72     std::unordered_map<std::string, std::set<FontDescSharedPtr>> stylishFullNameMap_;
73 };
74 } // namespace OHOS::Rosen
75 
76 #endif // OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H