diff --git a/frameworks/font/ui_font.cpp b/frameworks/font/ui_font.cpp index e49ea644a75af7b8fcf5c1f2f28046e6bf88ec09..0a32e88850efe461ca353f2f533bc7c349e22c9c 100644 --- a/frameworks/font/ui_font.cpp +++ b/frameworks/font/ui_font.cpp @@ -18,7 +18,8 @@ #include "font/ui_font_cache.h" #if ENABLE_VECTOR_FONT #include "font/ui_font_vector.h" -#else +#endif +#if ENABLE_BITMAP_FONT #include "font/ui_font_bitmap.h" #endif #include "graphic_config.h" @@ -35,15 +36,16 @@ UIFont::~UIFont(){} UIFont* UIFont::GetInstance() { static UIFont instance; -#if ENABLE_VECTOR_FONT +#if ENABLE_BITMAP_FONT if (instance.instance_ == nullptr) { - instance.defaultInstance_ = new UIFontVector(); + instance.defaultInstance_ = new UIFontBitmap(); instance.instance_ = instance.defaultInstance_; setFontAllocFlag_ = true; } -#else +#endif +#if ENABLE_VECTOR_FONT if (instance.instance_ == nullptr) { - instance.defaultInstance_ = new UIFontBitmap(); + instance.defaultInstance_ = new UIFontVector(); instance.instance_ = instance.defaultInstance_; setFontAllocFlag_ = true; } @@ -64,6 +66,63 @@ void UIFont::SetFont(BaseFont* font) } } +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) +void UIFont::SetBitampFont(BaseFont* font) +{ + if (font == nullptr) { + return; + } + GetBitmapInstance()->SetFont(font); +} + +UIFont* UIFont::GetBitmapInstance() +{ + static UIFont instance; + if (instance.instance_ == nullptr) { + instance.defaultInstance_ = new UIFontBitmap(); + instance.instance_ = instance.defaultInstance_; + setFontAllocFlag_ = true; + } + return &instance; +} +#endif + +int8_t UIFont::SetCurrentLangId(uint8_t langId) +{ +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + GetBitmapInstance()->SetCurrentLangId(langId); +#else + return instance_->SetCurrentLangId(langId); +#endif +} + +uint8_t UIFont::GetCurrentLangId() const +{ +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + return GetBitmapInstance()->GetCurrentLangId(langId); +#else + return instance_->GetCurrentLangId(); +#endif +} + +int8_t UIFont::GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const +{ +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + return GetBitmapInstance()->GetTextUtf8(textId, utf8Addr, utf8Len); +#else + return instance_->GetTextUtf8(textId, utf8Addr, utf8Len); +#endif +} + +int8_t UIFont::GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const +{ +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + return GetBitmapInstance()->GetTextParam(textId, param); +#else + return instance_->GetTextParam(textId, param); +#endif +} + uint8_t* UIFont::GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint8_t fontId, uint8_t fontSize, uint8_t shapingFont) { diff --git a/frameworks/font/ui_font_vector.cpp b/frameworks/font/ui_font_vector.cpp index 7f34bbfa0a4e0d05a80321a616793848924acec1..35be8cde3fc552e8a2ee15572dde0d1464e305df 100644 --- a/frameworks/font/ui_font_vector.cpp +++ b/frameworks/font/ui_font_vector.cpp @@ -16,6 +16,7 @@ #include "font/ui_font_vector.h" #include #include +#include #include #include "common/typed_text.h" #include "draw/draw_utils.h" @@ -143,6 +144,78 @@ uint8_t UIFontVector::RegisterFontInfo(const UITextLanguageFontParam* fontsTable return count; } +/* Note: when use ttc font file, freetype should export FT_Stream_New/FT_Stream_Free function*/ +uint8_t UIFontVector::RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) +{ + if ((ttcName == nullptr) || !freeTypeInited_) { + return FONT_INVALID_TTF_ID; + } + + if (bitmapCache_ == nullptr) { + bitmapCache_ = new (std::nothrow) UIFontCache(reinterpret_cast(GetRamAddr()), GetRamLen()); + if (bitmapCache_ == nullptr) { + return FONT_INVALID_TTF_ID; + } + } + + int32_t i = 0; + int32_t error = 0; + while (i < FONT_ID_MAX) { + if ((ttcInfos_[i].ttcName != nullptr) && !strncmp(ttcInfos_[i].ttcName, ttcName, TTF_NAME_LEN_MAX)) { + return i; + } else if (ttcInfos_[i].ttcName == nullptr) { + std::string ttcPath = ttfDir_; + ttcPath.append(ttcName); + FT_Open_Args args = {FT_OPEN_PATHNAME, nullptr, 0, const_cast(ttcPath.c_str()), + nullptr, nullptr, 0, nullptr}; + error = FT_Stream_New(ftLibrary_, &args, &ttcInfos_[i].stream); + if (error != 0) { + return FONT_INVALID_TTF_ID; + } + ttcInfos_[i].ttcName = ttcName; + args = {FT_OPEN_STREAM, nullptr, 0, nullptr, ttcInfos_[i].stream, nullptr, 0, nullptr}; + for (uint8_t j = 0; j < count; j++) { + error = FT_Open_Face(ftLibrary_, &args, j, &ftFaces_[i]); + if (error != 0) { + continue; + } + fontInfo_[i].ttfName = ttfInfo[j].ttfName; + fontInfo_[i].shaping = ttfInfo[j].shaping; + fontInfo_[i].ttfId = i; + if (IsColorEmojiFont(ftFaces_[i])) { + SetupColorFont(ftFaces_[i]); + } +#if ENABLE_MULTI_FONT + UIMultiFontManager::GetInstance()->UpdateScript(fontInfo_[j]); +#endif + } + return i; + } + i++; + } + return FONT_INVALID_TTF_ID; +} + +uint8_t UIFontVector::UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) +{ + if (ttcName == nullptr || ttfInfo == nullptr) { + return FONT_INVALID_TTF_ID; + } + + uint8_t i = 0; + while (i < FONT_ID_MAX) { + if ((ttcInfos_[i].ttcName != nullptr) && !strncmp(ttcInfos_[i].ttcName, ttcName, TTF_NAME_LEN_MAX)) { + FT_Stream_Free(ttcInfos_[i].stream, 1); + for (uint8_t j = 0; j < count; j++) { + UnregisterFontInfo(ttfInfo[j].ttfName); + } + return i; + } + i++; + } + return FONT_INVALID_TTF_ID; +} + uint8_t UIFontVector::UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) { if (fontsTable == nullptr) { @@ -526,10 +599,7 @@ int8_t UIFontVector::LoadGlyphIntoFace(uint8_t& fontId, uint32_t unicode, FT_Fac { bool isHaveBitmap = false; int32_t error; - if (IsGlyphFont(unicode) != 0) { - if (fontId != GetFontId(unicode)) { - return INVALID_RET_VALUE; - } + if (fontInfo_[fontId].shaping != 0) { unicode = unicode & (0xFFFFFF); // Whether 0 ~24 bit storage is unicode error = FT_Load_Glyph(face, unicode, FT_LOAD_RENDER); isHaveBitmap = true; diff --git a/interfaces/innerkits/font/ui_font_vector.h b/interfaces/innerkits/font/ui_font_vector.h index 9fffa0d699ca3c6eca22f2936d377bc354c2859e..71fa82082fb9d5fa62bb1fabb684cb1a39be2c37 100644 --- a/interfaces/innerkits/font/ui_font_vector.h +++ b/interfaces/innerkits/font/ui_font_vector.h @@ -46,6 +46,8 @@ public: uint8_t RegisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override; uint8_t UnregisterFontInfo(const char* ttfName) override; uint8_t UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override; + uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) override; + uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) override; const UITextLanguageFontParam* GetFontInfo(uint8_t fontId) const override; int32_t OpenVectorFont(uint8_t ttfId) override; bool IsColorEmojiFont(FT_Face &face); @@ -71,6 +73,10 @@ private: FT_Face face; uint32_t key; }; + struct TtcInfo { + const char* ttcName; + FT_Stream stream; + }; struct Metric { int left; int top; @@ -79,6 +85,7 @@ private: int advance; uint8_t buf[0]; }; + TtcInfo ttcInfos_[FONT_ID_MAX] = {}; void SetFace(FaceInfo& faceInfo, uint32_t unicode) const; #if ENABLE_VECTOR_FONT void SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textStyle) const; diff --git a/interfaces/kits/font/base_font.h b/interfaces/kits/font/base_font.h index 569d68e4631b630b0f5033ca5b0cdaf5173e0e49..b68ce5a349c72ee9ca771c3750f858bc412ae588 100644 --- a/interfaces/kits/font/base_font.h +++ b/interfaces/kits/font/base_font.h @@ -170,6 +170,16 @@ public: return 0; } + virtual uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) + { + return 0; + } + + virtual uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) + { + return 0; + } + virtual uint8_t UnregisterFontInfo(const char* ttfName) { return 0; diff --git a/interfaces/kits/font/ui_font.h b/interfaces/kits/font/ui_font.h index 45593b8294d14008836e22a9740f8a9cb803e29d..548242abde32a50ea036904561315377daac28d9 100644 --- a/interfaces/kits/font/ui_font.h +++ b/interfaces/kits/font/ui_font.h @@ -124,25 +124,16 @@ public: return instance_->IsVectorFont(); } - int8_t SetCurrentLangId(uint8_t langId) - { - return instance_->SetCurrentLangId(langId); - } + int8_t SetCurrentLangId(uint8_t langId); - uint8_t GetCurrentLangId() const - { - return instance_->GetCurrentLangId(); - } + uint8_t GetCurrentLangId() const; int8_t GetDefaultParamByLangId(uint8_t langId, LangTextParam** pParam) const { return instance_->GetDefaultParamByLangId(langId, pParam); } - int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const - { - return instance_->GetTextUtf8(textId, utf8Addr, utf8Len); - } + int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const; uint8_t GetFontTtfId(uint8_t fontId, uint8_t size) const { @@ -179,6 +170,16 @@ public: return instance_->RegisterFontInfo(fontsTable, num); } + uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) + { + return instance_->RegisterTtcFontInfo(ttcName, ttfInfo, count); + } + + uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) + { + return instance_->UnregisterTtcFontInfo(ttcName, ttfInfo, count); + } + uint8_t UnregisterFontInfo(const char* ttfName) { return instance_->UnregisterFontInfo(ttfName); @@ -189,10 +190,7 @@ public: return instance_->UnregisterFontInfo(fontsTable, num); } - int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const - { - return instance_->GetTextParam(textId, param); - } + int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const; int8_t GetWildCardStaticStr(uint16_t textId, UITextWildcardStaticType type, uint8_t** strAddr, uint16_t& strLen) const @@ -226,6 +224,16 @@ public: { return instance_->IsEmojiFont(fontid); } + +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + /** + * @brief Set bitmap font, only needed when using both vector font and bitmap font + * + * @param font bitmap font + */ + void SetBitampFont(BaseFont* font); +#endif + private: UIFont(); /** @@ -234,6 +242,15 @@ private: */ ~UIFont(); +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) + /** + * @brief Get bitmap font, only needed when using both vector font and bitmap font + * + * @return UIFont bitmap font instance + */ + static UIFont* GetBitmapInstance(); +#endif + BaseFont* instance_; BaseFont* defaultInstance_; static bool setFontAllocFlag_; diff --git a/interfaces/kits/font/ui_font_header.h b/interfaces/kits/font/ui_font_header.h index 62c79d777fc419dd41e402036a2b02fab68c214f..37144f86522bd6c0ab3427efdea08345b15a2db4 100644 --- a/interfaces/kits/font/ui_font_header.h +++ b/interfaces/kits/font/ui_font_header.h @@ -389,6 +389,12 @@ struct FileCommonHeader { const char magicWord[5]; // 5:file identifier's length is 5 uint32_t fileLength; }; + +struct TtfInfo { + const char* ttfName; + uint8_t shaping; +}; + #pragma pack() } // namespace OHOS #endif /* UI_FONT_HEADER_H */