1 /*
2  * Copyright (c) 2020-2022 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 BASE_FONT
17 #define BASE_FONT
18 #include "common/text.h"
19 #include "font/ui_font_header.h"
20 #include "font/ui_font_builder.h"
21 #include "graphic_config.h"
22 
23 namespace OHOS {
24 class BaseFont : public HeapBase {
25 public:
26     enum FontType : uint8_t {
27         DYNAMIC_FONT = 0,
28         STATIC_FONT = 1,
29         VECTOR_FONT = 2,
30     };
31 
BaseFont()32     BaseFont() : ramAddr_(0), ramLen_(0) {}
~BaseFont()33     virtual ~BaseFont() {}
34 
35     /**
36      * @brief Indicates whether the current font library is a vector font library.
37      * @return uint8_t: 0 BitmapFont  1 VectorFont
38      */
39     virtual bool IsVectorFont() const = 0;
40 
41     /**
42      * @brief Get height for specific font
43      *
44      * @return uint16_t
45      */
46     virtual uint16_t GetHeight(uint16_t fontId, uint8_t fontSize) = 0;
47 
48     /**
49      * @brief Get font id
50      *
51      * @param ttfName
52      * @param size   0: invalid size
53      * @return uint8_t
54      */
55     virtual uint16_t GetFontId(const char* ttfName, uint8_t fontSize) const = 0;
56 
57     /**
58      * @brief Get width
59      *
60      * @param unicode
61      * @return int16_t
62      */
63     virtual int16_t GetWidth(uint32_t unicode, uint16_t fontId, uint8_t fontSize) = 0;
64 
OpenVectorFont(uint8_t ttfId)65     virtual int32_t OpenVectorFont(uint8_t ttfId)
66     {
67         return -1;
68     }
69 
GetTtfInfo(uint8_t ttfId,uint8_t * ttfBuffer,uint32_t bufferSize,TtfHeader & TtfHeader)70     virtual bool GetTtfInfo(uint8_t ttfId, uint8_t* ttfBuffer, uint32_t bufferSize, TtfHeader& TtfHeader)
71     {
72         return -1;
73     }
74 
75     /**
76      * @brief Get bitmap for specific unicode
77      *
78      * @param unicode
79      * @return uint8_t*
80      */
81     virtual uint8_t* GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) = 0;
82 
83     /**
84      * @brief Get font header
85      *
86      * @param fontHeader
87      * @return int8_t
88      */
89     virtual int8_t GetFontHeader(FontHeader& fontHeader, uint16_t fontId, uint8_t fontSize) = 0;
90 
91     /**
92      * @brief Get the glyph node
93      *
94      * @param unicode
95      * @param glyphNode
96      * @param isGlyph
97      * @return int8_t
98      */
99     virtual int8_t GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) = 0;
100     virtual uint8_t GetFontWeight(uint16_t fontId) = 0;
101 
SetCurrentLangId(uint8_t langId)102     virtual int8_t SetCurrentLangId(uint8_t langId)
103     {
104         return 0;
105     }
106 
GetCurrentLangId()107     virtual uint8_t GetCurrentLangId() const
108     {
109         return UIFontBuilder::GetInstance()->GetTotalLangId();
110     }
111 
112     int8_t GetDefaultParamByLangId(uint8_t langId, LangTextParam** pParam) const;
113 
114     /**
115      * @brief Get the Font Shaping Property
116      * @param text [in] the content
117      * @param ttfId [out] the ttf id
118      * @param fontId [in] the font id
119      * @param size [in] the font size
120      * @return uint8_t: needShaping property
121      */
GetShapingFontId(char * text,uint8_t & ttfId,uint32_t & script,uint16_t fontId,uint8_t size)122     virtual uint16_t GetShapingFontId(char* text, uint8_t& ttfId, uint32_t& script, uint16_t fontId, uint8_t size) const
123     {
124         return 0;
125     }
126 
127     /**
128      * @brief Set the Font Path
129      *
130      * @param path
131      * @param type
132      * @return int8_t
133      */
134     virtual int8_t SetFontPath(const char* path, FontType type) = 0;
135 
GetFontVersion(FontType type,const char * path,char * version,uint8_t len)136     virtual int8_t GetFontVersion(FontType type, const char* path, char* version, uint8_t len)
137     {
138         return INVALID_RET_VALUE;
139     }
140 
141     /**
142      * @brief Get the text in utf-8 format
143      *
144      * @param textId
145      * @param utf8Addr
146      * @param utf8Len
147      * @return int8_t
148      */
GetTextUtf8(uint16_t textId,uint8_t ** utf8Addr,uint16_t & utf8Len)149     virtual int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const
150     {
151         return 0;
152     }
153 
154     /**
155      * @brief Get the ttfId
156      * @param fontId [in] the font id
157      * @param size [in] the font size
158      * @return uint8_t: ttfId property
159      */
GetFontTtfId(uint16_t fontId,uint8_t size)160     virtual uint8_t GetFontTtfId(uint16_t fontId, uint8_t size) const
161     {
162         return 0;
163     }
164 
GetFontInfo(uint16_t fontId)165     virtual const UITextLanguageFontParam* GetFontInfo(uint16_t fontId) const
166     {
167         return nullptr;
168     }
169 
RegisterFontInfo(const char * ttfName,uint8_t shaping)170     virtual uint8_t RegisterFontInfo(const char* ttfName, uint8_t shaping)
171     {
172         return 0;
173     }
174 
RegisterFontInfo(const UITextLanguageFontParam * fontsTable,uint8_t num)175     virtual uint8_t RegisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num)
176     {
177         return 0;
178     }
179 
RegisterTtcFontInfo(const char * ttcName,const TtfInfo * ttfInfo,uint8_t count)180     virtual uint8_t RegisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count)
181     {
182         return 0;
183     }
184 
UnregisterTtcFontInfo(const char * ttcName,const TtfInfo * ttfInfo,uint8_t count)185     virtual uint8_t UnregisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count)
186     {
187         return 0;
188     }
189 
UnregisterFontInfo(const char * ttfName)190     virtual uint8_t UnregisterFontInfo(const char* ttfName)
191     {
192         return 0;
193     }
194 
UnregisterFontInfo(const UITextLanguageFontParam * fontsTable,uint8_t num)195     virtual uint8_t UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num)
196     {
197         return 0;
198     }
199 
GetWildCardStaticStr(uint16_t textId,UITextWildcardStaticType type,uint8_t ** strAddr,uint16_t & strLen)200     virtual int8_t GetWildCardStaticStr(uint16_t textId,
201                                         UITextWildcardStaticType type,
202                                         uint8_t** strAddr,
203                                         uint16_t& strLen) const
204     {
205         return 0;
206     }
207 
GetCodePoints(uint16_t textId,uint32_t ** codePoints,uint16_t & codePointsNum)208     virtual int8_t GetCodePoints(uint16_t textId, uint32_t** codePoints, uint16_t& codePointsNum) const
209     {
210         return 0;
211     }
212 
GetTextParam(uint16_t textId,UITextLanguageTextParam & param)213     virtual int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const
214     {
215         return 0;
216     }
217 
SetFontFileOffset(uint32_t offset)218     virtual void SetFontFileOffset(uint32_t offset) {}
219 
220     void SetRamAddr(uintptr_t ramAddr);
221     uintptr_t GetRamAddr();
222     uint32_t GetRamLen();
223     void SetRamLen(uint32_t ramLen);
224     virtual void SetPsramMemory(uintptr_t psramAddr, uint32_t psramLen);
225 
226     virtual uint16_t GetOffsetPosY(const char* text, uint16_t lineLength, bool& isEmoijLarge,
227                                    uint16_t fontId, uint8_t fontSize) = 0;
228     virtual uint16_t GetLineMaxHeight(const char* text, uint16_t lineLength, uint16_t fontId, uint8_t fontSize,
229                                       uint16_t& letterIndex, SpannableString* spannableString) = 0;
230     virtual bool IsEmojiFont(uint16_t fontId) = 0;
231 
232 private:
233     uintptr_t ramAddr_;
234     uint32_t ramLen_;
235 };
236 } // namespace OHOS
237 #endif /* UI_BASE_FONT_H */
238