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