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_EXPORT_TEXGINE_FONT_PROVIDERS_H
17 #define ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_FONT_PROVIDERS_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "texgine/ifont_provider.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace TextEngine {
30 class FontCollection;
31 
32 /*
33  * @brief FontProviders providers an interface for developers to determine the order in
34  *        which fonts are used.
35  *        Texgine is provided with the internal function of generate font collection by
36  *        using font families.
37  *        The order in which fonts are used in generating depends on the order in which
38  *        the collection is appended with FontProvider.
39  */
40 class FontProviders {
41 public:
42     FontProviders() = default;
43     /*
44      * @brief Allocates an empty FontProviders.
45      */
46     static std::shared_ptr<FontProviders> Create() noexcept(true);
47 
48     /*
49      * @brief Allocates a FontProviders that have only one FontProvider which is SystemFontProvider.
50      *        The first FontProvider in the allocated FontProviders is SystemFontProvider,
51      *        add additional FontProvider can be appended if necessary.
52      */
53     static std::shared_ptr<FontProviders> SystemFontOnly() noexcept(true);
54 
55     /*
56      * @brief Append FontProvider into FontProviders.
57      * @param provider  FontProvider that's going to be appended to the FontProviders
58      *                  FontProvider cannot be appended repeatedly
59      */
60     void AppendFontProvider(const std::shared_ptr<IFontProvider>& provider) noexcept(true);
61 
62     /*
63      * @brief Disable find and use fallback font.
64      */
65     void DisableFallback();
66 
67     /*
68      * @brief Generate font collection based on parameter font families and FontProvider that
69      *        has been appended.
70      * @param families  A family vector for selecting fonts from FontProviders
71      * @return          FontCollection from selected fonts
72      */
73     std::shared_ptr<FontCollection> GenerateFontCollection(
74         const std::vector<std::string>& families) const noexcept(true);
75 
Clear()76     void Clear()
77     {
78         std::lock_guard<std::mutex> lock(mutex_);
79         fontStyleSetCache_.clear();
80     }
81 
82 private:
83     friend void ReportMemoryUsage(const std::string& member, const FontProviders& that, const bool needThis);
84 
85     FontProviders(const FontProviders&) = delete;
86     FontProviders(FontProviders&&) = delete;
87     FontProviders& operator=(const FontProviders&) = delete;
88     FontProviders& operator=(FontProviders&&) = delete;
89 
90     bool enableFallback_ = true;
91     std::vector<std::shared_ptr<IFontProvider>> providers_ = {};
92 
93     mutable std::map<std::string, std::shared_ptr<VariantFontStyleSet>> fontStyleSetCache_ = {};
94     mutable std::mutex mutex_;
95 };
96 } // namespace TextEngine
97 } // namespace Rosen
98 } // namespace OHOS
99 
100 #endif // ROSEN_MODULES_TEXGINE_EXPORT_TEXGINE_FONT_PROVIDERS_H
101