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 #ifndef UI_FONT_H
16 #define UI_FONT_H
17 #include "font/base_font.h"
18 
19 namespace OHOS {
20 class UIFont : public HeapBase {
21 public:
22     UIFont(const UIFont &) = delete;
23     UIFont& operator=(const UIFont &) = delete;
24 
25     /**
26      * @brief judge whether you need shaping for text
27      *
28      * @param text means input text
29      * @param ttfId font index
30      * @param script shaping script
31      * @param fontId font index
32      * @param size font size
33      * @return int8_t Shaping mode
34      */
GetShapingFontId(char * text,uint8_t & ttfId,uint32_t & script,uint16_t fontId,uint8_t size)35     uint16_t GetShapingFontId(char* text, uint8_t& ttfId, uint32_t& script, uint16_t fontId, uint8_t size) const
36     {
37         return instance_->GetShapingFontId(text, ttfId, script, fontId, size);
38     }
39 
40     /**
41      * @brief Get width of the letter
42      *
43      * @param unicode: [in] unicode or glyph index according to isGlyph param
44      * @param fontId: [in] font id
45      * @param fontSize: [in] font size
46      * @param shapingId: [in] font shaping id
47      * @return uint16_t: the letter width
48      */
49     uint16_t GetWidth(uint32_t unicode, uint16_t fontId, uint8_t fontSize, uint8_t shapingId);
50 
51     /**
52      * @brief Get height for specific font
53      *
54      * @param fontId: font id
55      * @param fontSize: font size
56      *
57      * @return uint16_t
58      */
GetHeight(uint16_t fontId,uint8_t fontSize)59     uint16_t GetHeight(uint16_t fontId, uint8_t fontSize)
60     {
61         return instance_->GetHeight(fontId, fontSize);
62     }
63 
64     /**
65      * @brief Get the font weight
66      * @param fontId
67      * @return uint8_t: fontWeight
68      */
GetFontWeight(uint16_t fontId)69     uint8_t GetFontWeight(uint16_t fontId)
70     {
71         return instance_->GetFontWeight(fontId);
72     }
73 
74     /**
75      * @brief Get font header
76      *
77      * @param fontHeader
78      * @return int8_t
79      */
GetFontHeader(FontHeader & fontHeader,uint16_t fontId,uint8_t fontSize)80     int8_t GetFontHeader(FontHeader& fontHeader, uint16_t fontId, uint8_t fontSize)
81     {
82         return instance_->GetFontHeader(fontHeader, fontId, fontSize);
83     }
84 
85     /**
86      * @brief Get font id
87      *
88      * @param name
89      * @param size
90      * @return uint16_t
91      */
92     uint16_t GetFontId(const char* name, uint8_t size = 0)
93     {
94         return instance_->GetFontId(name, size);
95     }
96 
97     /**
98      * @brief Set the cache start address
99      *
100      * @param psramAddr
101      * @param psramLen
102      */
SetPsramMemory(uintptr_t psramAddr,uint32_t psramLen)103     void SetPsramMemory(uintptr_t psramAddr, uint32_t psramLen)
104     {
105         instance_->SetPsramMemory(psramAddr, psramLen);
106     }
107 
108     /**
109      * @brief Get bitmap for specific unicode
110      *
111      * @param unicode
112      * @return uint8_t*
113      */
114     uint8_t* GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize, uint8_t shapingFont);
115 
116     int8_t GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize);
117 
118     /**
119      * @brief Indicates whether the current font library is a vector font library.
120      * @return uint8_t: 0 BitmapFont  1 VectorFont
121      */
IsVectorFont()122     bool IsVectorFont()
123     {
124         return instance_->IsVectorFont();
125     }
126 
127     int8_t SetCurrentLangId(uint8_t langId);
128 
129     uint8_t GetCurrentLangId() const;
130 
GetDefaultParamByLangId(uint8_t langId,LangTextParam ** pParam)131     int8_t GetDefaultParamByLangId(uint8_t langId, LangTextParam** pParam) const
132     {
133         return instance_->GetDefaultParamByLangId(langId, pParam);
134     }
135 
136     int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const;
137 
GetFontTtfId(uint16_t fontId,uint8_t size)138     uint8_t GetFontTtfId(uint16_t fontId, uint8_t size) const
139     {
140         return instance_->GetFontTtfId(fontId, size);
141     }
142 
OpenVectorFont(uint8_t ttfId)143     int32_t OpenVectorFont(uint8_t ttfId)
144     {
145         return instance_->OpenVectorFont(ttfId);
146     }
147 
GetTtfInfo(uint8_t ttfId,uint8_t * ttfBuffer,uint32_t bufferSize,TtfHeader & ttfHeader)148     bool GetTtfInfo(uint8_t ttfId, uint8_t* ttfBuffer, uint32_t bufferSize, TtfHeader& ttfHeader)
149     {
150         return instance_->GetTtfInfo(ttfId, ttfBuffer, bufferSize, ttfHeader);
151     }
152 
GetFontInfo(uint16_t fontId)153     const UITextLanguageFontParam* GetFontInfo(uint16_t fontId) const
154     {
155         return instance_->GetFontInfo(fontId);
156     }
157 
158     int8_t SetFontPath(const char* path, BaseFont::FontType type);
159 
GetFontVersion(BaseFont::FontType type,const char * path,char * version,uint8_t len)160     int8_t GetFontVersion(BaseFont::FontType type, const char* path, char* version, uint8_t len) const
161     {
162         return instance_->GetFontVersion(type, path, version, len);
163     }
164 
165     uint8_t RegisterFontInfo(const char* ttfName, uint8_t shaping = 0)
166     {
167         return instance_->RegisterFontInfo(ttfName, shaping);
168     }
169 
RegisterFontInfo(const UITextLanguageFontParam * fontsTable,uint8_t num)170     uint8_t RegisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num)
171     {
172         return instance_->RegisterFontInfo(fontsTable, num);
173     }
174 
RegisterTtcFontInfo(const char * ttcName,const TtfInfo * ttfInfo,uint8_t count)175     uint8_t RegisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count)
176     {
177         return instance_->RegisterTtcFontInfo(ttcName, ttfInfo, count);
178     }
179 
UnregisterTtcFontInfo(const char * ttcName,const TtfInfo * ttfInfo,uint8_t count)180     uint8_t UnregisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count)
181     {
182         return instance_->UnregisterTtcFontInfo(ttcName, ttfInfo, count);
183     }
184 
UnregisterFontInfo(const char * ttfName)185     uint8_t UnregisterFontInfo(const char* ttfName)
186     {
187         return instance_->UnregisterFontInfo(ttfName);
188     }
189 
UnregisterFontInfo(const UITextLanguageFontParam * fontsTable,uint8_t num)190     uint8_t UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num)
191     {
192         return instance_->UnregisterFontInfo(fontsTable, num);
193     }
194 
195     int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const;
196 
197     int8_t GetWildCardStaticStr(uint16_t textId, UITextWildcardStaticType type,
198         uint8_t** strAddr, uint16_t& strLen) const;
199 
200     int8_t GetCodePoints(uint16_t textId, uint32_t** codePoints, uint16_t& codePointsNum) const;
201 
GetColorType(uint16_t fontId)202     ColorMode GetColorType(uint16_t fontId)
203     {
204         switch (instance_->GetFontWeight(fontId)) {
205             case FONT_WEIGHT_1:
206                 return A1;
207             case FONT_WEIGHT_2:
208                 return A2;
209             case FONT_WEIGHT_4:
210                 return A4;
211             case FONT_WEIGHT_8:
212                 return A8;
213             case FONT_WEIGHT_32:
214                 return ARGB8888;
215             default:
216                 return UNKNOWN;
217         }
218     }
219 
220     static UIFont* GetInstance();
221     void SetFont(BaseFont* font);
222     BaseFont* GetFont();
223 
224     void SetFontFileOffset(uint32_t offset);
225 
226     virtual uint16_t
GetOffsetPosY(const char * text,uint16_t lineLength,bool & isAllEmoji,uint16_t fontId,uint8_t fontSize)227         GetOffsetPosY(const char* text, uint16_t lineLength, bool& isAllEmoji, uint16_t fontId, uint8_t fontSize)
228     {
229         return instance_->GetOffsetPosY(text, lineLength, isAllEmoji, fontId, fontSize);
230     }
231 
232     virtual uint16_t GetLineMaxHeight(const char* text, uint16_t lineLength, uint16_t fontId, uint8_t fontSize,
233                                       uint16_t letterIndex, SpannableString* spannableString);
234 
IsEmojiFont(uint16_t fontid)235     bool IsEmojiFont(uint16_t fontid)
236     {
237         return instance_->IsEmojiFont(fontid);
238     }
239 
240 #if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
241     /**
242      * @brief Set bitmap font, only needed when using both vector font and bitmap font
243      *
244      * @param font bitmap font
245      */
246     void SetBitmapFont(BaseFont* font);
247 
248     /**
249      * @brief Get bitmap font, only needed when using both vector font and bitmap font
250      *
251      * @return UIFont bitmap font instance
252      */
253     static UIFont* GetBitmapInstance();
254 #endif
255 
256 private:
257     UIFont();
258     /**
259      * @brief Destroy the UIFontFactor object
260      *
261      */
262     ~UIFont();
263 
264     BaseFont* instance_;
265     BaseFont* defaultInstance_;
266     static bool setFontAllocFlag_;
267 };
268 } // namespace OHOS
269 #endif
270