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 int32_t systemFontType, FontDescSharedPtr& result); 40 void GetSystemFontFullNamesByType(int32_t systemFontType, std::unordered_set<std::string>& fontList); 41 void CacheDynamicTypeface(std::shared_ptr<Drawing::Typeface> typeface, const std::string &familyName); 42 void DeleteDynamicTypefaceFromCache(const std::string &familyName); 43 44 private: 45 void FontDescriptorScatter(FontDescSharedPtr desc); 46 bool ParserInstallFontsPathList(std::vector<std::string>& fontPathList); 47 bool ProcessSystemFontType(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 std::unordered_set<std::string> GetDynamicFontList(); 53 bool HandleMapIntersection(std::set<FontDescSharedPtr>& finishRet, const std::string& name, 54 std::unordered_map<std::string, std::set<FontDescSharedPtr>>& map); 55 bool FilterBoldCache(int weight, std::set<FontDescSharedPtr>& finishRet); 56 bool FilterWidthCache(int width, std::set<FontDescSharedPtr>& finishRet); 57 bool FilterItalicCache(int italic, std::set<FontDescSharedPtr>& finishRet); 58 bool FilterMonoSpaceCache(bool monoSpace, std::set<FontDescSharedPtr>& finishRet); 59 bool FilterSymbolicCache(bool symbolic, std::set<FontDescSharedPtr>& finishRet); 60 bool IsDefault(FontDescSharedPtr desc); 61 int32_t WeightAlignment(int32_t weight); 62 bool GetFontTypeFromParams(const std::string& fullName, 63 int32_t systemFontType, int32_t& fontType); 64 void ParserFontsByFontType(int32_t fontType); 65 66 private: 67 TextEngine::FontParser parser_; 68 69 struct FontDescriptorEqual { operatorFontDescriptorEqual70 bool operator()(const FontDescSharedPtr& lhs, const FontDescSharedPtr& rhs) const 71 { 72 if (lhs->fontFamily == rhs->fontFamily) { 73 return lhs->fullName < rhs->fullName; 74 } 75 return lhs->fontFamily < rhs->fontFamily; 76 } 77 }; 78 std::set<FontDescSharedPtr, FontDescriptorEqual> allFontDescriptor_; 79 std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontFamilyMap_; 80 std::unordered_map<std::string, std::set<FontDescSharedPtr>> fullNameMap_; 81 std::unordered_map<std::string, std::set<FontDescSharedPtr>> postScriptNameMap_; 82 std::unordered_map<std::string, std::set<FontDescSharedPtr>> fontSubfamilyNameMap_; 83 // cache dynamic ttf, key is familyName 84 std::unordered_map<std::string, FontDescSharedPtr> dynamicFullNameMap_; 85 std::set<FontDescSharedPtr> boldCache_; 86 std::set<FontDescSharedPtr> italicCache_; 87 std::set<FontDescSharedPtr> monoSpaceCache_; 88 std::set<FontDescSharedPtr> symbolicCache_; 89 std::unordered_map<std::string, std::set<FontDescSharedPtr>> stylishFullNameMap_; 90 }; 91 } // namespace OHOS::Rosen 92 93 #endif // OHOS_ROSEN_FONT_DESCRIPTOR_CACHE_H