• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FONT_MGR_H
17 #define FONT_MGR_H
18 
19 #include <memory>
20 #include <cstdint>
21 
22 #include "impl_interface/font_mgr_impl.h"
23 #include "text/font_style.h"
24 #include "text/font_style_set.h"
25 #include "text/typeface.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class DRAWING_API FontMgr {
31 public:
32     explicit FontMgr(std::shared_ptr<FontMgrImpl> fontMgrImpl) noexcept;
33     virtual ~FontMgr() = default;
34 
35     /**
36      * @brief   Create a default fontMgr.
37      * @return  A shared pointer to default fontMgr.
38      */
39     static std::shared_ptr<FontMgr> CreateDefaultFontMgr();
40 
41 #ifndef USE_TEXGINE
42     /**
43      * @brief   Create a dynamic fontMgr.
44      * @return  A shared pointer to dynamic fontMgr.
45      */
46     static std::shared_ptr<FontMgr> CreateDynamicFontMgr();
47 
48     /**
49      * @brief             Load dynamic font typeface.
50      * @param familyName  Font family name.
51      * @param data        Font data.
52      * @param dataLength  The size of font data.
53      * @return  A pointer to typeface.
54      */
55     Typeface* LoadDynamicFont(const std::string& familyName, const uint8_t* data, size_t dataLength);
56 
57     /**
58      * @brief             Load theme font typeface.
59      * @param familyName  Font family name.
60      * @param themeName   Theme name.
61      * @param data        Font data.
62      * @param dataLength  The size of font data.
63      * @return  A pointer to typeface.
64      */
65     Typeface* LoadThemeFont(const std::string& familyName, const std::string& themeName,
66         const uint8_t* data, size_t dataLength);
67 
68     /*
69      * @brief             Load theme font typeface.
70      * @param themeName   Theme name.
71      * @param typeface    Typeface
72     */
73     void LoadThemeFont(const std::string& themeName, std::shared_ptr<Drawing::Typeface> typeface);
74 #endif
75 
76     /**
77      * @brief             Use the system fallback to find a typeface for the given character.
78      * @param familyName  A const char array of familyName.
79      * @param fontStyle   FontStyle.
80      * @param bcp47       Is a combination of ISO 639, 15924, and 3166-1 codes.
81      * @param bcp47Count  Length of bcp47.
82      * @param character   The given character.
83      * @return            If find, return typeface. else, return nullptr.
84      */
85     Typeface* MatchFamilyStyleCharacter(const char familyName[], const FontStyle& fontStyle,
86                                         const char* bcp47[], int bcp47Count,
87                                         int32_t character) const;
88 
89     /**
90      * @brief             Find a fontStyleSet for the given familyName.
91      * @param familyName  A const char array of familyName.
92      * @return            If find, return fontStyleSet. else, return nullptr.
93      */
94     FontStyleSet* MatchFamily(const char familyName[]) const;
95 
96     template<typename T>
GetImpl()97     T* GetImpl() const
98     {
99         if (fontMgrImpl_) {
100             return fontMgrImpl_->DowncastingTo<T>();
101         }
102         return nullptr;
103     }
104 
105     /**
106      * @brief             Find the corresponding font based on the style and font name
107      * @param familyName  The name of the font you want to apply
108      * @param fontStyle   The font style you want to achieve
109      * @return            Returns the corresponding font
110      */
111     Typeface* MatchFamilyStyle(const char familyName[], const FontStyle& fontStyle) const;
112 
113     int CountFamilies() const;
114 
115     void GetFamilyName(int index, std::string& str) const;
116 
117     FontStyleSet* CreateStyleSet(int index) const;
118 
119 private:
120     std::shared_ptr<FontMgrImpl> fontMgrImpl_;
121 };
122 } // namespace Drawing
123 } // namespace Rosen
124 } // namespace OHOS
125 #endif