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 UI_FONT_VECTOR_H
17 #define UI_FONT_VECTOR_H
18 #include <memory>
19 #include "font/base_font.h"
20 #include "graphic_config.h"
21 #include "ft2build.h"
22 #include "freetype/freetype.h"
23 #include "freetype/tttables.h"
24 #include "font/glyphs_cache.h"
25 #include "font/ui_font_cache.h"
26 namespace OHOS {
27 class UIFontVector : public BaseFont {
28 public:
29     UIFontVector();
30 
31     ~UIFontVector();
32     UIFontVector(const UIFontVector&) = delete;
33     UIFontVector& operator=(const UIFontVector&) noexcept = delete;
34     bool IsVectorFont() const override;
35     int8_t SetFontPath(const char* path, FontType type) override;
36     uint16_t GetHeight(uint16_t fontId, uint8_t fontSize) override;
37     uint16_t GetFontId(const char* ttfName, uint8_t fontSize = 0) const override;
38     int16_t GetWidth(uint32_t unicode, uint16_t fontId, uint8_t fontSize) override;
39     uint8_t* GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) override;
40     int8_t GetFontHeader(FontHeader& fontHeader, uint16_t fontId, uint8_t fontSize) override;
41     int8_t GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) override;
42     uint8_t GetFontWeight(uint16_t fontId) override;
43     uint16_t
44         GetShapingFontId(char* text, uint8_t& ttfId, uint32_t& script, uint16_t fontId, uint8_t size) const override;
45     uint8_t RegisterFontInfo(const char* ttfName, uint8_t shaping = 0) override;
46     uint8_t RegisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override;
47     uint8_t UnregisterFontInfo(const char* ttfName) override;
48     uint8_t UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override;
49     uint8_t RegisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count) override;
50     uint8_t UnregisterTtcFontInfo(const char* ttcName, const TtfInfo* ttfInfo, uint8_t count) override;
51     const UITextLanguageFontParam* GetFontInfo(uint16_t fontId) const override;
52     int32_t OpenVectorFont(uint8_t ttfId) override;
53     bool IsColorEmojiFont(FT_Face &face);
54     uint16_t GetOffsetPosY(const char* text, uint16_t lineLength,
55                            bool& isEmojiLarge, uint16_t fontId, uint8_t fontSize) override;
56     uint16_t GetLineMaxHeight(const char* text, uint16_t lineLength, uint16_t fontId, uint8_t fontSize,
57                               uint16_t& letterIndex, SpannableString* spannableString)  override;
58     bool GetTtfInfo(uint8_t ttfId, uint8_t* ttfBuffer, uint32_t ttfBufferSize, TtfHeader& ttfHeader) override;
59     void SetPsramMemory(uintptr_t psramAddr, uint32_t psramLen) override;
60     int8_t SetCurrentLangId(uint8_t langId) override;
61     bool IsEmojiFont(uint16_t fontId) override;
62 
63 private:
64     static constexpr uint8_t FONT_ID_MAX = 0xFF;
65     static constexpr uint8_t FONT_TTC_MAX = 0x20;
66     static constexpr uint8_t FONT_INVALID_TTF_ID = 0xFF;
67     static constexpr uint8_t TTF_NAME_LEN_MAX = 128;
68     UITextLanguageFontParam fontInfo_[FONT_ID_MAX] = {{}};
69     std::string ttfDir_;
70     FT_Library ftLibrary_;
71     FT_Face ftFaces_[FONT_ID_MAX] = {0};
72     uint8_t fontSize_[FONT_ID_MAX] = {0};
73     uint8_t currentFontInfoNum_ = 0;
74     bool freeTypeInited_;
75     struct FaceInfo {
76         FT_Face face;
77         uint16_t key;
78     };
79     struct TtcInfo {
80         const char* ttcName;
81         FT_Stream stream;
82     };
83     TtcInfo ttcInfos_[FONT_TTC_MAX] = {};
84     void SetFace(FaceInfo& faceInfo, uint32_t unicode);
85     void SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textStyle);
86     uint16_t GetFontId(uint32_t unicode) const;
87     uint16_t GetKey(uint16_t fontId, uint8_t size);
88     int8_t LoadGlyphIntoFace(uint16_t& fontId, uint8_t fontSize, uint32_t unicode, GlyphNode& glyphNode);
89 #if defined(ENABLE_TEXT_STYLE) && ENABLE_TEXT_STYLE
90     int8_t LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode, FT_Face face, TextStyle textStyle);
91 #endif
92     void SaveGlyphNode(uint32_t unicode, uint16_t fontKey, Metric* metric);
93     uint8_t IsGlyphFont(uint32_t unicode);
94 #if defined(ENABLE_TEXT_STYLE) && ENABLE_TEXT_STYLE
95     void SetItaly(FT_GlyphSlot slot);
96     void SetBold(uint16_t fontId);
97 #endif
98     int8_t GetFaceInfo(uint16_t fontId, uint8_t fontSize, FaceInfo& faceInfo);
99     uint16_t GetMaxSubLineHeight(uint16_t textNum, uint16_t loopNum, uint16_t maxHeight, uint16_t emojiNum);
100     bool GetTtfInfoFromTtf(uint8_t* ttfBuffer,
101                            uint32_t ttfBufferSize,
102                            TtfHeader& ttfHeader,
103                            UITextLanguageFontParam fontInfo);
104     bool GetTtfInfoFromTtc(uint8_t* ttfBuffer,
105                            uint32_t ttfBufferSize,
106                            TtfHeader& ttfHeader,
107                            UITextLanguageFontParam fontInfo);
108     void ClearFontGlyph(FT_Face face);
109 };
110 } // namespace OHOS
111 #endif
112