1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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_SPTEXT_FONT_COLLECTION_H
17 #define ROSEN_MODULES_SPTEXT_FONT_COLLECTION_H
18 
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23 #include <mutex>
24 
25 #include "modules/skparagraph/include/FontCollection.h"
26 #include "txt/asset_font_manager.h"
27 #include "txt/text_style.h"
28 #include "utils.h"
29 
30 namespace txt {
31 class FontCollection : public std::enable_shared_from_this<FontCollection> {
32 public:
33     FontCollection();
34 
35     ~FontCollection();
36 
37     size_t GetFontManagersCount() const;
38 
39     void SetupDefaultFontManager();
40     void SetDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager);
41     void SetAssetFontManager(std::shared_ptr<RSFontMgr> fontManager);
42     void SetDynamicFontManager(std::shared_ptr<RSFontMgr> fontManager);
43     void SetTestFontManager(std::shared_ptr<RSFontMgr> fontManager);
44 
45     void DisableFontFallback();
46 
47     void ClearFontFamilyCache();
48 
49     sk_sp<skia::textlayout::FontCollection> CreateSktFontCollection();
50 
51 private:
52     std::shared_ptr<RSFontMgr> defaultFontManager_;
53     std::shared_ptr<RSFontMgr> assetFontManager_;
54     std::shared_ptr<RSFontMgr> dynamicFontManager_;
55     std::shared_ptr<RSFontMgr> testFontManager_;
56     bool enableFontFallback_;
57 
58     sk_sp<skia::textlayout::FontCollection> sktFontCollection_;
59     mutable std::mutex collectionMutex_;
60 
61     std::vector<std::shared_ptr<RSFontMgr>> GetFontManagerOrder() const;
62     DISALLOW_COPY_AND_ASSIGN(FontCollection);
63 };
64 } // namespace txt
65 
66 #endif // ROSEN_MODULES_SPTEXT_FONT_COLLECTION_H
67