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, const 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> FindFallBackTypeface(const uint32_t &ch, const FontStyles &style, 42 const std::string &script, const std::string &locale) const; 43 44 void DisableFallback(); 45 46 private: 47 bool enableFallback_ = true; 48 std::vector<std::shared_ptr<VariantFontStyleSet>> fontStyleSets_; 49 50 struct TypefaceCacheKey { 51 std::shared_ptr<VariantFontStyleSet> fss = {}; 52 FontStyles fs = {}; 53 54 bool operator <(const struct TypefaceCacheKey &rhs) const 55 { 56 return fss != rhs.fss ? fss < rhs.fss : fs < rhs.fs; 57 } 58 }; 59 static inline std::map<struct TypefaceCacheKey, std::shared_ptr<Typeface>> typefaceCache_; 60 61 struct FallbackCacheKey { 62 std::string script = ""; 63 std::string locale = ""; 64 FontStyles fs = {}; 65 66 bool operator <(const struct FallbackCacheKey &rhs) const 67 { 68 return script < rhs.script || locale < rhs.locale || fs < rhs.fs; 69 } 70 }; 71 static inline std::map<struct FallbackCacheKey, std::shared_ptr<Typeface>> fallbackCache_; 72 }; 73 } // namespace TextEngine 74 } // namespace Rosen 75 } // namespace OHOS 76 77 #endif // ROSEN_MODULES_TEXGINE_SRC_FONT_COLLECTION_H 78