• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
21 #include <set>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "font_config.h"
27 #include "font_parser.h"
28 
29 namespace OHOS::Rosen {
30 using FontDescSharedPtr = std::shared_ptr<TextEngine::FontParser::FontDescriptor>;
31 class FontDescriptorCache {
32 public:
33     FontDescriptorCache();
34     ~FontDescriptorCache();
35     void ParserSystemFonts();
36     void ParserStylishFonts();
37     void MatchFromFontDescriptor(FontDescSharedPtr desc, std::set<FontDescSharedPtr>& result);
38     void ClearFontFileCache();
39     void Dump() const;
40     void GetFontDescSharedPtrByFullName(const std::string& fullName,
41         int32_t systemFontType, FontDescSharedPtr& result);
42     void GetSystemFontFullNamesByType(int32_t systemFontType, std::unordered_set<std::string>& fontList);
43     void CacheDynamicTypeface(std::shared_ptr<Drawing::Typeface> typeface, const std::string &familyName);
44     void DeleteDynamicTypefaceFromCache(const std::string &familyName);
45 
46 private:
47     void FontDescriptorScatter(FontDescSharedPtr desc);
48     bool ParserInstallFontsPathList(std::vector<std::string>& fontPathList);
49     static bool ParserInstallFontsPathList(TextEngine::FullNameToPath& fontPathList);
50     static bool ProcessSystemFontType(int32_t systemFontType, int32_t& fontType);
51     bool ParseInstallFontDescSharedPtrByName(const std::string& fullName, FontDescSharedPtr& result) const;
52     std::unordered_set<std::string> GetInstallFontList();
53     std::unordered_set<std::string> GetStylishFontList();
54     std::unordered_set<std::string> GetGenericFontList();
55     std::unordered_set<std::string> GetDynamicFontList();
56     bool HandleMapIntersection(std::set<FontDescSharedPtr>& finishRet, const std::string& name,
57         std::unordered_map<std::string, std::set<FontDescSharedPtr>>& map);
58     bool FilterBoldCache(int weight, std::set<FontDescSharedPtr>& finishRet);
59     bool FilterWidthCache(int width, std::set<FontDescSharedPtr>& finishRet);
60     bool FilterItalicCache(int italic, std::set<FontDescSharedPtr>& finishRet);
61     bool FilterMonoSpaceCache(bool monoSpace, std::set<FontDescSharedPtr>& finishRet);
62     bool FilterSymbolicCache(bool symbolic, std::set<FontDescSharedPtr>& finishRet);
63     bool IsDefault(FontDescSharedPtr desc);
64     static int32_t WeightAlignment(int32_t weight);
65     static bool GetFontTypeFromParams(const std::string& fullName,
66         int32_t systemFontType, int32_t& fontType);
67     void ParserFontsByFontType(int32_t fontType);
68 
69 private:
70     TextEngine::FontParser parser_;
71 
72     struct FontDescriptorEqual {
operatorFontDescriptorEqual73         bool operator()(const FontDescSharedPtr& lhs, const FontDescSharedPtr& rhs) const
74         {
75             if (lhs->fontFamily == rhs->fontFamily) {
76                 return lhs->fullName < rhs->fullName;
77             }
78             return lhs->fontFamily < rhs->fontFamily;
79         }
80     };
81     std::set<FontDescSharedPtr, FontDescriptorEqual> allFontDescriptor_;
82     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontFamilyMap_;
83     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fullNameMap_;
84     std::unordered_map<std::string, std::set<FontDescSharedPtr>> postScriptNameMap_;
85     std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontSubfamilyNameMap_;
86     // cache dynamic ttf, key is familyName
87     std::unordered_map<std::string, FontDescSharedPtr> dynamicFullNameMap_;
88     std::set<FontDescSharedPtr> boldCache_;
89     std::set<FontDescSharedPtr> italicCache_;
90     std::set<FontDescSharedPtr> monoSpaceCache_;
91     std::set<FontDescSharedPtr> symbolicCache_;
92     std::unordered_map<std::string, std::set<FontDescSharedPtr>> stylishFullNameMap_;
93     std::mutex mutex_;
94 };
95 } // namespace OHOS::Rosen
96 
97 #endif // OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H