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 enum FontCheckCode {
31     SUCCESSED                  = 0, /** no error */
32     ERROR_PARSE_CONFIG_FAILED  = 1, /** failed to parse the JSON configuration file */
33     ERROR_TYPE_OTHER           = 2  /** other reasons, such as empty input parameters or other internal reasons */
34 };
35 
36 struct FontByteArray {
37 public:
FontByteArrayFontByteArray38     FontByteArray(std::unique_ptr<uint8_t[]> data, uint32_t dataLen)
39         : strData(std::move(data)), strLen(dataLen) {}
40     std::unique_ptr<uint8_t[]> strData; // A byte array in UTF-16BE encoding
41     uint32_t strLen;
42 };
43 
44 class DRAWING_API FontMgr {
45 public:
46     explicit FontMgr(std::shared_ptr<FontMgrImpl> fontMgrImpl) noexcept;
47     virtual ~FontMgr() = default;
48 
49     /**
50      * @brief   Create a default fontMgr.
51      * @return  A shared pointer to default fontMgr.
52      */
53     static std::shared_ptr<FontMgr> CreateDefaultFontMgr();
54 
55 #ifndef USE_TEXGINE
56     /**
57      * @brief   Create a dynamic fontMgr.
58      * @return  A shared pointer to dynamic fontMgr.
59      */
60     static std::shared_ptr<FontMgr> CreateDynamicFontMgr();
61 
62     /**
63      * @brief             Load dynamic font typeface.
64      * @param familyName  Font family name.
65      * @param data        Font data.
66      * @param dataLength  The size of font data.
67      * @return  A pointer to typeface.
68      */
69     Typeface* LoadDynamicFont(const std::string& familyName, const uint8_t* data, size_t dataLength);
70 
71     /**
72      * @brief             Load theme font typeface.
73      * @param familyName  Font family name.
74      * @param themeName   Theme name.
75      * @param data        Font data.
76      * @param dataLength  The size of font data.
77      * @return  A pointer to typeface.
78      */
79     Typeface* LoadThemeFont(const std::string& familyName, const std::string& themeName,
80         const uint8_t* data, size_t dataLength);
81 
82     /*
83      * @brief             Load theme font typeface.
84      * @param themeName   Theme name.
85      * @param typeface    Typeface
86     */
87     void LoadThemeFont(const std::string& themeName, std::shared_ptr<Drawing::Typeface> typeface);
88 #endif
89 
90     /**
91      * @brief             Use the system fallback to find a typeface for the given character.
92      * @param familyName  A const char array of familyName.
93      * @param fontStyle   FontStyle.
94      * @param bcp47       Is a combination of ISO 639, 15924, and 3166-1 codes.
95      * @param bcp47Count  Length of bcp47.
96      * @param character   The given character.
97      * @return            If find, return typeface. else, return nullptr.
98      */
99     Typeface* MatchFamilyStyleCharacter(const char familyName[], const FontStyle& fontStyle,
100                                         const char* bcp47[], int bcp47Count,
101                                         int32_t character) const;
102 
103     /**
104      * @brief             Find a fontStyleSet for the given familyName.
105      * @param familyName  A const char array of familyName.
106      * @return            If find, return fontStyleSet. else, return nullptr.
107      */
108     FontStyleSet* MatchFamily(const char familyName[]) const;
109 
110     template<typename T>
GetImpl()111     T* GetImpl() const
112     {
113         if (fontMgrImpl_) {
114             return fontMgrImpl_->DowncastingTo<T>();
115         }
116         return nullptr;
117     }
118 
119     /**
120      * @brief             Find the corresponding font based on the style and font name
121      * @param familyName  The name of the font you want to apply
122      * @param fontStyle   The font style you want to achieve
123      * @return            Returns the corresponding font
124      */
125     Typeface* MatchFamilyStyle(const char familyName[], const FontStyle& fontStyle) const;
126 
127     int CountFamilies() const;
128 
129     void GetFamilyName(int index, std::string& str) const;
130 
131     FontStyleSet* CreateStyleSet(int index) const;
132 
133     /**
134      * @brief             Get the fullname of font
135      * @param fontFd      The file descriptor for the font file
136      * @param fullnameVec Read the font fullname list
137      * @return            Returns Whether the fullnameVec was successfully obtained, 0 means success,
138      *                    see FontCheckCode for details
139      */
140     int GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec);
141 
142     /**
143      * @brief             Parse the Installed font configuration file and get the font path list
144      * @param configPath  The path to the configuration file
145      * @param fontPathVec Read a list of font file paths
146      * @return            Returns Whether the configuration file is parsed successfully, 0 means success,
147      *                    see FontCheckCode for details
148      */
149     int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec);
150 private:
151     std::shared_ptr<FontMgrImpl> fontMgrImpl_;
152 };
153 } // namespace Drawing
154 } // namespace Rosen
155 } // namespace OHOS
156 #endif