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 ROSEN_TEXT_ADAPTER_TXT_FONT_COLLECTION_H 17 #define ROSEN_TEXT_ADAPTER_TXT_FONT_COLLECTION_H 18 19 #include <shared_mutex> 20 #include <string> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include "rosen_text/font_collection.h" 25 #include "rosen_text/symbol_constants.h" 26 #include "txt/asset_font_manager.h" 27 #include "txt/font_collection.h" 28 29 #include "text/font_mgr.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 namespace AdapterTxt { 34 class TypefaceWithAlias { 35 public: 36 TypefaceWithAlias(const std::string& alias, const std::shared_ptr<Drawing::Typeface>& typeface); 37 uint32_t GetHash() const; 38 const std::string& GetAlias() const; 39 const std::shared_ptr<Drawing::Typeface>& GetTypeface() const; 40 bool operator==(const TypefaceWithAlias& other) const; 41 42 struct Hasher { 43 uint32_t operator()(const TypefaceWithAlias& ta) const; 44 }; 45 46 private: 47 std::string alias_; 48 std::shared_ptr<Drawing::Typeface> typeface_; 49 mutable uint32_t hash_ { 0 }; 50 }; 51 52 enum class RegisterError { 53 SUCCESS, 54 ALREADY_EXIST, 55 INVALID_INPUT, 56 REGISTER_FAILED, 57 }; 58 59 class FontCollection : public ::OHOS::Rosen::FontCollection { 60 public: 61 explicit FontCollection(std::shared_ptr<txt::FontCollection> fontCollection = nullptr); 62 ~FontCollection(); 63 std::shared_ptr<txt::FontCollection> Get(); 64 65 void DisableFallback() override; 66 void DisableSystemFont() override; 67 std::shared_ptr<Drawing::Typeface> LoadFont( 68 const std::string& familyName, const uint8_t* data, size_t datalen) override; 69 std::shared_ptr<Drawing::Typeface> LoadThemeFont( 70 const std::string& familyName, const uint8_t* data, size_t datalen) override; 71 std::vector<std::shared_ptr<Drawing::Typeface>> LoadThemeFont( 72 const std::string& familyName, const std::vector<std::pair<const uint8_t*, size_t>>& data) override; 73 void ClearThemeFont() override; 74 std::shared_ptr<Drawing::FontMgr> GetFontMgr() override; 75 LoadSymbolErrorCode LoadSymbolFont(const std::string& familyName, const uint8_t* data, size_t datalen) override; 76 LoadSymbolErrorCode LoadSymbolJson(const std::string& familyName, const uint8_t* data, size_t datalen) override; 77 void ClearCaches() override; 78 bool UnloadFont(const std::string& familyName) override; 79 80 private: 81 class FontCallbackGuard { 82 public: 83 FontCallbackGuard(const FontCollection* fc, const std::string& familyName, const FontCallback& begin, 84 const FontCallback& end); 85 ~FontCallbackGuard(); 86 87 private: 88 const FontCollection* fc_; 89 const std::string& familyName_; 90 const FontCallback& begin_; 91 const FontCallback& end_; 92 }; 93 RegisterError RegisterTypeface(const TypefaceWithAlias& ta); 94 95 std::shared_ptr<txt::FontCollection> fontCollection_ = nullptr; 96 std::shared_ptr<Drawing::FontMgr> dfmanager_ = nullptr; 97 std::unordered_set<TypefaceWithAlias, TypefaceWithAlias::Hasher> typefaceSet_; 98 std::unordered_map<uint32_t, std::string> familyNames_; 99 std::shared_mutex mutex_; 100 }; 101 } // namespace AdapterTxt 102 } // namespace Rosen 103 } // namespace OHOS 104 105 #endif // ROSEN_TEXT_ADAPTER_TXT_FONT_COLLECTION_H 106