1 /* 2 * Copyright (c) 2023 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 ROSEN_MODULES_TEXGINE_SRC_FONT_COLLECTION_H 17 #define ROSEN_MODULES_TEXGINE_SRC_FONT_COLLECTION_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "font_styles.h" 24 #include "texgine_font_manager.h" 25 #include "typeface.h" 26 #include "variant_font_style_set.h" 27 28 namespace OHOS { 29 namespace Rosen { 30 namespace TextEngine { 31 class FontCollection { 32 public: 33 FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> &&fontStyleSets); 34 35 std::shared_ptr<Typeface> GetTypefaceForChar(const uint32_t &ch, FontStyles &style, 36 const std::string &script, const std::string &locale) const; 37 38 std::shared_ptr<Typeface> GetTypefaceForFontStyles(const FontStyles &style, const std::string &script, 39 const std::string &locale) const; 40 41 std::shared_ptr<Typeface> FindThemeTypeface(const FontStyles &style) const; 42 43 std::shared_ptr<Typeface> FindFallBackTypeface(const uint32_t &ch, const FontStyles &style, 44 const std::string &script, const std::string &locale) const; 45 46 void DisableFallback(); 47 int DetectionScript(const std::string script) const; 48 int DetectChinesePointUnicode(uint32_t ch) const; 49 private: 50 void SortTypeface(FontStyles &style) const; 51 void FillDefaultItalicSupportFile(); 52 void FillDefaultChinesePointUnicode(); 53 private: 54 bool enableFallback_ = true; 55 std::vector<std::shared_ptr<VariantFontStyleSet>> fontStyleSets_; 56 57 struct TypefaceCacheKey { 58 std::shared_ptr<VariantFontStyleSet> fss = {}; 59 FontStyles fs = {}; 60 61 bool operator <(const struct TypefaceCacheKey &rhs) const 62 { 63 return fss != rhs.fss ? fss < rhs.fss : fs < rhs.fs; 64 } 65 }; 66 static inline std::map<struct TypefaceCacheKey, std::shared_ptr<Typeface>> typefaceCache_; 67 68 struct FallbackCacheKey { 69 std::string script = ""; 70 std::string locale = ""; 71 FontStyles fs = {}; 72 73 bool operator <(const struct FallbackCacheKey &rhs) const 74 { 75 return script < rhs.script || locale < rhs.locale || fs < rhs.fs; 76 } 77 }; 78 std::map<std::string, int> supportScript_; 79 std::map<uint32_t, int> chinesePointUnicode_; 80 static inline std::map<struct FallbackCacheKey, std::shared_ptr<Typeface>> fallbackCache_; 81 }; 82 } // namespace TextEngine 83 } // namespace Rosen 84 } // namespace OHOS 85 86 #endif // ROSEN_MODULES_TEXGINE_SRC_FONT_COLLECTION_H 87