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