diff --git a/frameworks/common/spannable_string.cpp b/frameworks/common/spannable_string.cpp index 299457f35d22e15a1e52cc591fe6e9597cf14dd2..10f321a83d728a0ef4a317d55c77b50a05abe8e7 100644 --- a/frameworks/common/spannable_string.cpp +++ b/frameworks/common/spannable_string.cpp @@ -17,7 +17,7 @@ #include "gfx_utils/graphic_log.h" #include "securec.h" namespace OHOS { -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING SpannableString::~SpannableString() { if (text_ != nullptr) { diff --git a/frameworks/common/text.cpp b/frameworks/common/text.cpp index fd6a1f5a0bf40211d35b3c1f32566bce9786c567..5bb0539d4aaff005e950d9e752f55712e9a3c8c8 100644 --- a/frameworks/common/text.cpp +++ b/frameworks/common/text.cpp @@ -40,7 +40,7 @@ Text::Text() horizontalAlign_(TEXT_ALIGNMENT_LEFT), verticalAlign_(TEXT_ALIGNMENT_TOP) { -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING textStyles_ = nullptr; #endif SetFont(DEFAULT_VECTOR_FONT_FILENAME, DEFAULT_VECTOR_FONT_SIZE); @@ -52,7 +52,7 @@ Text::~Text() UIFree(text_); text_ = nullptr; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING if (textStyles_ != nullptr) { UIFree(textStyles_); textStyles_ = nullptr; @@ -73,7 +73,7 @@ Text::~Text() } } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void Text::SetSpannableString(const SpannableString* spannableString) { SetText(spannableString->text_); @@ -130,7 +130,7 @@ void Text::SetText(const char* text) text_ = nullptr; return; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING if (textStyles_ != nullptr) { UIFree(textStyles_); textStyles_ = nullptr; @@ -334,7 +334,7 @@ void Text::Draw(BufferInfo& gfxDstBuffer, 0, opa, style, &text_[lineBegin], textLine_[i].lineBytes, lineBegin, fontId_, fontSize_, 0, static_cast(direct_), nullptr, baseLine_, -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING textStyles_, #endif &backgroundColor_, &foregroundColor_, &linebackgroundColor_, sizeSpans_, 0}; diff --git a/frameworks/components/ui_label.cpp b/frameworks/components/ui_label.cpp index a6e5327c6889ad4eeb3c3e5bf621418b4531a4ec..900d6a7a567fb7a0eb127fd817cefaf573b9c554 100644 --- a/frameworks/components/ui_label.cpp +++ b/frameworks/components/ui_label.cpp @@ -183,7 +183,7 @@ void UILabel::SetText(const char* text) } } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void UILabel::SetText(const SpannableString* text) { InitLabelText(); diff --git a/frameworks/draw/draw_label.cpp b/frameworks/draw/draw_label.cpp index cfd95632e8123c4f8d6b41e2fdbe87ca657ca66b..7a1e161e0fd0a8e6a0d13b241dfb4dab3e62a984 100644 --- a/frameworks/draw/draw_label.cpp +++ b/frameworks/draw/draw_label.cpp @@ -57,7 +57,7 @@ uint16_t DrawLabel::DrawTextOneLine(BufferInfo& gfxDstBuffer, const LabelLineInf ColorType foregroundColor = labelLine.style.textColor_; GetForegroundColor(letterIndex, labelLine.foregroundColor, foregroundColor); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle textStyle = TEXT_STYLE_NORMAL; if (labelLine.textStyles) { textStyle = labelLine.textStyles[letterIndex]; @@ -74,7 +74,7 @@ uint16_t DrawLabel::DrawTextOneLine(BufferInfo& gfxDstBuffer, const LabelLineInf fontId, 0, fontSize, -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING textStyle, #endif labelLine.baseLine, @@ -82,7 +82,7 @@ uint16_t DrawLabel::DrawTextOneLine(BufferInfo& gfxDstBuffer, const LabelLineInf labelLine.style.lineSpace_, havebackgroundColor, backgroundColor}; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING glyphNode.textStyle = letterInfo.textStyle; #endif glyphNode.advance = 0; @@ -217,7 +217,7 @@ void DrawLabel::DrawLetterWithRotate(BufferInfo& gfxDstBuffer, UIFont* fontEngine = UIFont::GetInstance(); FontHeader head; GlyphNode node; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING node.textStyle = TEXT_STYLE_NORMAL; #endif if (fontEngine->GetFontHeader(head, fontId, fontSize) != 0) { diff --git a/frameworks/draw/draw_utils.h b/frameworks/draw/draw_utils.h index 77954f36a568d12fcdcfc3a4388fe4e0a4ed974a..0f47487f3fc8633dcd4bcf08006bdf516061ce14 100644 --- a/frameworks/draw/draw_utils.h +++ b/frameworks/draw/draw_utils.h @@ -74,7 +74,7 @@ struct LabelLineInfo { UITextLanguageDirect direct; uint32_t* codePoints; bool baseLine; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle* textStyles; #endif List* backgroundColor; @@ -97,7 +97,7 @@ struct LabelLetterInfo { uint16_t fontId; uint8_t shapingId; uint8_t fontSize; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle textStyle; #endif bool baseLine; diff --git a/frameworks/font/glyphs_cache.cpp b/frameworks/font/glyphs_cache.cpp index a06a6b6cd3e4630497f0104764699de60ea6546a..a98134980a60df5eb5c3630d74dded706904b0c7 100644 --- a/frameworks/font/glyphs_cache.cpp +++ b/frameworks/font/glyphs_cache.cpp @@ -67,7 +67,7 @@ GlyphNode* GlyphsCache::GetNodeFromCache(uint32_t unicode, uint16_t fontId) { GlyphNode* node = nullptr; - uint8_t font = fontId & FONT_HASH_MASK; + uint8_t font = (fontId | unicode) & FONT_HASH_MASK; uint8_t uc = unicode & UNICODE_HASH_MASK; for (uint8_t i = 0; i < NODE_HASH_NR; i++) { GlyphNode* p = &((*nodeCache_)[font][uc][i]); @@ -81,7 +81,7 @@ GlyphNode* GlyphsCache::GetNodeFromCache(uint32_t unicode, uint16_t fontId) GlyphNode* GlyphsCache::GetNodeCacheSpace(uint32_t unicode, uint16_t fontId) { - uint8_t font = fontId & FONT_HASH_MASK; + uint8_t font = (fontId | unicode) & FONT_HASH_MASK; uint8_t uc = unicode & UNICODE_HASH_MASK; uint8_t i = (*cacheStatus_)[font][uc]; GlyphNode* node = &((*nodeCache_)[font][uc][i]); diff --git a/frameworks/font/glyphs_cache.h b/frameworks/font/glyphs_cache.h index 1a04ec3fac8c28288781d693c6c5fc325d805977..5eabfa8125c950e2f99358addbed8065a076bb3e 100644 --- a/frameworks/font/glyphs_cache.h +++ b/frameworks/font/glyphs_cache.h @@ -39,7 +39,11 @@ public: GlyphNode* GetNodeCacheSpace(uint32_t unicode, uint16_t fontId); private: +#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT ==1)) + static constexpr uint8_t FONT_HASH_SHIFT = 1; +#else static constexpr uint8_t FONT_HASH_SHIFT = 3; +#endif static constexpr uint8_t FONT_HASH_NR = 1 << FONT_HASH_SHIFT; static constexpr uint32_t FONT_HASH_MASK = FONT_HASH_NR - 1; static constexpr uint8_t UNICODE_HASH_SHIFT = 6; diff --git a/frameworks/font/ui_font_cache.cpp b/frameworks/font/ui_font_cache.cpp index 646c47da76423af4cb8ff0fd2d09e03f82b2c880..7829e1f844c2614e356b976896e801d6315916c7 100644 --- a/frameworks/font/ui_font_cache.cpp +++ b/frameworks/font/ui_font_cache.cpp @@ -73,7 +73,7 @@ uint8_t* UIFontCache::GetSpace(uint32_t fontId, uint32_t unicode, uint32_t size, bitmap->fontId = fontId; bitmap->unicode = unicode; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING bitmap->textStyle = textStyle; #endif @@ -101,7 +101,7 @@ uint8_t* UIFontCache::GetBitmap(uint32_t fontId, uint32_t unicode, TextStyle tex bitmap = reinterpret_cast(reinterpret_cast(node) - offsetof(struct Bitmap, hashHead)); if ((bitmap->fontId == fontId) && -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING (bitmap->textStyle == textStyle) && #endif (bitmap->unicode == unicode)) { diff --git a/frameworks/font/ui_font_cache.h b/frameworks/font/ui_font_cache.h index ae1fc3ee5ab90dc7ef07f36af3d180244e519604..a20560722426f898ca84b797a5feed2ef682df1d 100644 --- a/frameworks/font/ui_font_cache.h +++ b/frameworks/font/ui_font_cache.h @@ -54,7 +54,7 @@ public: ListHead lruHead; uint32_t fontId; // bitmap font: fontId vector font: fontKey ttfId + fontsize uint32_t unicode; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle textStyle; #endif uint8_t data[]; diff --git a/frameworks/font/ui_font_vector.cpp b/frameworks/font/ui_font_vector.cpp index 2f58c1e3711021a96f9a7fdbe1bdebe1f88dc9c8..514e3c62b6fb74409a71fd51ffc19a9d9e0bd4b7 100644 --- a/frameworks/font/ui_font_vector.cpp +++ b/frameworks/font/ui_font_vector.cpp @@ -580,28 +580,12 @@ int16_t UIFontVector::GetWidth(uint32_t unicode, uint16_t fontId, uint8_t fontSi return INVALID_RET_VALUE; } - if (!freeTypeInited_ || (bitmapCache_ == nullptr)) { - return INVALID_RET_VALUE; - } - - FaceInfo faceInfo = {}; - faceInfo.key = GetKey(fontId, fontSize); - uint8_t* bitmap = bitmapCache_->GetBitmap(faceInfo.key, unicode); - if (bitmap != nullptr) { - return reinterpret_cast(bitmap)->advance; - } - - int8_t ret = GetFaceInfo(fontId, fontSize, faceInfo); - if ((ret != RET_VALUE_OK) || (faceInfo.face == nullptr)) { - return INVALID_RET_VALUE; - } - - int8_t error = LoadGlyphIntoFace(fontId, unicode); - if (error != RET_VALUE_OK) { + GlyphNode node; + int8_t ret = GetGlyphNode(unicode, node, fontId, fontSize); + if (ret != RET_VALUE_OK) { return INVALID_RET_VALUE; } - SetFace(faceInfo, unicode); - return static_cast(faceInfo.face->glyph->advance.x / FONT_PIXEL_IN_POINT); + return node.advance; } int8_t UIFontVector::GetFontHeader(FontHeader& fontHeader, uint16_t fontId, uint8_t fontSize) @@ -621,20 +605,39 @@ int8_t UIFontVector::GetFontHeader(FontHeader& fontHeader, uint16_t fontId, uint return RET_VALUE_OK; } +void UIFontVector::SaveGlyphNode(uint32_t unicode, uint32_t fontKey, Metric *metric) +{ + GlyphNode *node = glyphsCache_.GetNodeCacheSpace(unicode, fontKey); + if (node == nullptr) { + return; + } + node->left = metric->left; + node->top = metric->top; + node->cols = metric->cols; + node->rows = metric->rows; + node->advance = metric->advance; + node->unicode = unicode; + node->fontId = fontKey; +} + int8_t UIFontVector::GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) { - FaceInfo faceInfo; - int8_t ret = GetFaceInfo(fontId, fontSize, faceInfo); - if (ret != RET_VALUE_OK) { - return INVALID_RET_VALUE; + // get glyph from glyph cache + uint32_t fontKey = GetKey(fontId, fontSize); + GlyphNode* node = glyphsCache_.GetNodeFromCache(unicode, fontKey); + if (node != nullptr) { + glyphNode = *node; + return RET_VALUE_OK; } - if (!freeTypeInited_ || (faceInfo.face == nullptr) || (bitmapCache_ == nullptr)) { + + // get glyph from bitmap cache + if (bitmapCache_ == nullptr) { return INVALID_RET_VALUE; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT - uint8_t* bitmap = bitmapCache_->GetBitmap(faceInfo.key, unicode, glyphNode.textStyle); +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING + uint8_t* bitmap = bitmapCache_->GetBitmap(fontKey, unicode, glyphNode.textStyle); #else - uint8_t* bitmap = bitmapCache_->GetBitmap(faceInfo.key, unicode); + uint8_t* bitmap = bitmapCache_->GetBitmap(fontKey, unicode); #endif if (bitmap != nullptr) { Metric* f = reinterpret_cast(bitmap); @@ -644,45 +647,69 @@ int8_t UIFontVector::GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode, uint16 glyphNode.rows = f->rows; glyphNode.advance = f->advance; glyphNode.fontId = fontId; + + SaveGlyphNode(unicode, fontKey, f); return RET_VALUE_OK; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT - int8_t error = LoadGlyphIntoFace(fontId, unicode, faceInfo.face, glyphNode.textStyle); -#else - int8_t error = LoadGlyphIntoFace(fontId, unicode); -#endif + + FaceInfo faceInfo; + int8_t ret = GetFaceInfo(fontId, fontSize, faceInfo); + if (ret != RET_VALUE_OK) { + return INVALID_RET_VALUE; + } + if (faceInfo.face == nullptr) { + return INVALID_RET_VALUE; + } + + int8_t error = LoadGlyphIntoFace(fontId, fontSize, unicode, glyphNode); if (error != RET_VALUE_OK) { return INVALID_RET_VALUE; } - glyphNode.left = faceInfo.face->glyph->bitmap_left; - glyphNode.top = faceInfo.face->glyph->bitmap_top; - glyphNode.cols = faceInfo.face->glyph->bitmap.width; - glyphNode.rows = faceInfo.face->glyph->bitmap.rows; - glyphNode.advance = static_cast(faceInfo.face->glyph->advance.x / FONT_PIXEL_IN_POINT); - glyphNode.fontId = fontId; -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT - SetFace(faceInfo, unicode, glyphNode.textStyle); -#else - SetFace(faceInfo, unicode); -#endif return RET_VALUE_OK; } uint8_t* UIFontVector::GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint16_t fontId, uint8_t fontSize) { - if (GetGlyphNode(unicode, glyphNode, fontId, fontSize) != RET_VALUE_OK) { + if (bitmapCache_ == nullptr) { return nullptr; } + uint32_t fontKey = GetKey(fontId, fontSize); +#if ENABLE_SPANNALBE_STRING + uint8_t* bitmap = bitmapCache_->GetBitmap(fontKey, unicode, glyphNode.textStyle); +#else + uint8_t* bitmap = bitmapCache_->GetBitmap(fontKey, unicode); +#endif + if (bitmap != nullptr) { + Metric* f = reinterpret_cast(bitmap); + glyphNode.left = f->left; + glyphNode.top = f->top; + glyphNode.cols = f->cols; + glyphNode.rows = f->rows; + glyphNode.advance = f->advance; + glyphNode.fontId = fontId; + SaveGlyphNode(unicode, fontKey, f); + return bitmap + sizeof(Metric); + } + FaceInfo faceInfo; int8_t ret = GetFaceInfo(fontId, fontSize, faceInfo); if (ret != RET_VALUE_OK) { return nullptr; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT - uint8_t* bitmap = bitmapCache_->GetBitmap(faceInfo.key, unicode, glyphNode.textStyle); + if (faceInfo.face == nullptr) { + return nullptr; + } + + int8_t error = LoadGlyphIntoFace(fontId, fontSize, unicode, glyphNode); + if (error != RET_VALUE_OK) { + return nullptr; + } + +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING + bitmap = bitmapCache_->GetBitmap(fontKey, unicode, glyphNode.textStyle); #else - uint8_t* bitmap = bitmapCache_->GetBitmap(faceInfo.key, unicode); + bitmap = bitmapCache_->GetBitmap(fontKey, unicode); #endif if (bitmap != nullptr) { return bitmap + sizeof(Metric); @@ -699,7 +726,7 @@ bool UIFontVector::IsEmojiFont(uint16_t fontId) return (fontInfo_[fontId].fontWeight >= 16); // 16: rgb color font } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING void UIFontVector::SetItaly(FT_GlyphSlot slot) { if (slot->format != FT_GLYPH_FORMAT_OUTLINE) { @@ -742,15 +769,14 @@ void UIFontVector::SetBold(uint16_t fontId) } #endif -int8_t UIFontVector::LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode) +int8_t UIFontVector::LoadGlyphIntoFace(uint16_t& fontId, uint8_t fontSize, uint32_t unicode, GlyphNode& glyphNode) { int32_t error; if (IsGlyphFont(unicode) != 0) { if (fontId != GetFontId(unicode)) { return INVALID_RET_VALUE; } - unicode = unicode & (0xFFFFFF); // Whether 0 ~24 bit storage is unicode - error = FT_Load_Glyph(ftFaces_[fontId], unicode, FT_LOAD_RENDER); + error = FT_Load_Glyph(ftFaces_[fontId], unicode & (0xFFFFFF), FT_LOAD_RENDER); } else { if (IsEmojiFont(fontId)) { error = FT_Load_Char(ftFaces_[fontId], unicode, FT_LOAD_COLOR); @@ -761,10 +787,27 @@ int8_t UIFontVector::LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode) if ((error != 0) || (ftFaces_[fontId]->glyph->glyph_index == 0)) { return INVALID_RET_VALUE; } + + FaceInfo faceInfo; + faceInfo.key = GetKey(fontId, fontSize); + faceInfo.face = ftFaces_[fontId]; + + glyphNode.left = faceInfo.face->glyph->bitmap_left; + glyphNode.top = faceInfo.face->glyph->bitmap_top; + glyphNode.cols = faceInfo.face->glyph->bitmap.width; + glyphNode.rows = faceInfo.face->glyph->bitmap.rows; + glyphNode.advance = static_cast(faceInfo.face->glyph->advance.x / FONT_PIXEL_IN_POINT); + glyphNode.fontId = fontId; + +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING + SetFace(faceInfo, unicode, glyphNode.textStyle); +#else + SetFace(faceInfo, unicode); +#endif return RET_VALUE_OK; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING int8_t UIFontVector::LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode, FT_Face face, TextStyle textStyle) { int32_t error; @@ -814,7 +857,7 @@ uint8_t UIFontVector::IsGlyphFont(uint32_t unicode) void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode) { -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING SetFace(faceInfo, unicode, TEXT_STYLE_NORMAL); #else Metric f; @@ -824,17 +867,24 @@ void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode) f.cols = faceInfo.face->glyph->bitmap.width; f.rows = faceInfo.face->glyph->bitmap.rows; + // cache glyph + SaveGlyphNode(unicode, faceInfo.key, &f); + int16_t pixSize = 1; if (faceInfo.face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { pixSize = 0x04; // 4 Byte } uint32_t bitmapSize = faceInfo.face->glyph->bitmap.width * faceInfo.face->glyph->bitmap.rows * pixSize; + // cache bitmap uint8_t* bitmap = bitmapCache_->GetSpace(faceInfo.key, unicode, bitmapSize + sizeof(Metric)); if (bitmap != nullptr) { if (memcpy_s(bitmap, sizeof(Metric), &f, sizeof(Metric)) != EOK) { + bitmapCache_->PutSpace(bitmap); return; } - if (memcpy_s(bitmap + sizeof(Metric), bitmapSize, faceInfo.face->glyph->bitmap.buffer, bitmapSize) != EOK) { + if ((faceInfo.face->glyph->bitmap.buffer != nullptr) && + (memcpy_s(bitmap + sizeof(Metric), bitmapSize, faceInfo.face->glyph->bitmap.buffer, bitmapSize) != EOK)) { + bitmapCache_->PutSpace(bitmap); return; } ClearFontGlyph(faceInfo.face); @@ -842,7 +892,7 @@ void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode) #endif } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNALBE_STRING) && ENABLE_SPANNALBE_STRING void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textStyle) { Metric f; @@ -852,17 +902,23 @@ void UIFontVector::SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textS f.cols = faceInfo.face->glyph->bitmap.width; f.rows = faceInfo.face->glyph->bitmap.rows; + // cache glyph + SaveGlyphNode(unicode, faceInfo.key, &f); + int16_t pixSize = 1; if (faceInfo.face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { pixSize = 0x04; // 4 Byte } uint32_t bitmapSize = faceInfo.face->glyph->bitmap.width * faceInfo.face->glyph->bitmap.rows * pixSize; + // cache bitmap uint8_t* bitmap = bitmapCache_->GetSpace(faceInfo.key, unicode, bitmapSize + sizeof(Metric), textStyle); if (bitmap != nullptr) { if (memcpy_s(bitmap, sizeof(Metric), &f, sizeof(Metric)) != EOK) { + bitmapCache_->PutSpace(bitmap); return; } if (memcpy_s(bitmap + sizeof(Metric), bitmapSize, faceInfo.face->glyph->bitmap.buffer, bitmapSize) != EOK) { + bitmapCache_->PutSpace(bitmap); return; } ClearFontGlyph(faceInfo.face); @@ -1014,26 +1070,31 @@ void UIFontVector::SetPsramMemory(uintptr_t psramAddr, uint32_t psramLen) { BaseFont::SetPsramMemory(psramAddr, psramLen); FontRamAllocator::GetInstance().SetRamAddr(psramAddr, psramLen); - BitmapCacheInit(); + FontCacheInit(); } -void UIFontVector::BitmapCacheInit() +void UIFontVector::FontCacheInit() { - if (bitmapCache_ != nullptr) { + // glyph cache + if (glyphsCache_.CacheInit() != RET_VALUE_OK) { + GRAPHIC_LOGE("UIFontVector::FontCacheInit glyphs cache init failed"); return; } + // bitmap cache + if (bitmapCache_ != nullptr) { + return; + } #if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1)) - uint32_t cacheSize = FONT_BITMAP_CACHE_SIZE; + uint32_t bitmapCacheSize = FONT_BITMAP_CACHE_SIZE; #else - uint32_t cacheSize = GetRamLen(); + uint32_t bitmapCacheSize = GetRamLen() - FontRamAllocator::GetInstance().GetRamUsedLen(); #endif - uint8_t* bitmapCacheAddr = reinterpret_cast(FontRamAllocator::GetInstance().Allocate(cacheSize)); + uint8_t* bitmapCacheAddr = reinterpret_cast(FontRamAllocator::GetInstance().Allocate(bitmapCacheSize)); if (bitmapCacheAddr == nullptr) { - GRAPHIC_LOGE("UIFontVector::BitmapCacheInit allocate failed"); + GRAPHIC_LOGE("UIFontVector::FontCacheInit allocate failed"); return; } - bitmapCache_ = new UIFontCache(bitmapCacheAddr, FONT_BITMAP_CACHE_SIZE); + bitmapCache_ = new UIFontCache(bitmapCacheAddr, bitmapCacheSize); } - } // namespace OHOS diff --git a/interfaces/innerkits/font/ui_font_vector.h b/interfaces/innerkits/font/ui_font_vector.h index a814f14c009d1ff3cef3f5fc6c695b7065d2c4d1..9e14c72870a33f045c25792b5ccb1833940d3bd7 100644 --- a/interfaces/innerkits/font/ui_font_vector.h +++ b/interfaces/innerkits/font/ui_font_vector.h @@ -20,6 +20,7 @@ #include "ft2build.h" #include "freetype/freetype.h" #include "freetype/tttables.h" +#include "font/glyphs_cache.h" #include "font/ui_font_cache.h" #include @@ -72,6 +73,7 @@ private: uint8_t currentFontInfoNum_ = 0; bool freeTypeInited_; UIFontCache* bitmapCache_; + GlyphsCache glyphsCache_; struct FaceInfo { FT_Face face; uint32_t key; @@ -90,17 +92,18 @@ private: }; TtcInfo ttcInfos_[FONT_TTC_MAX] = {}; void SetFace(FaceInfo& faceInfo, uint32_t unicode); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textStyle); #endif uint16_t GetFontId(uint32_t unicode) const; uint32_t GetKey(uint16_t fontId, uint32_t size); - int8_t LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT + int8_t LoadGlyphIntoFace(uint16_t& fontId, uint8_t fontSize, uint32_t unicode, GlyphNode& glyphNode); +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING int8_t LoadGlyphIntoFace(uint16_t& fontId, uint32_t unicode, FT_Face face, TextStyle textStyle); #endif + void SaveGlyphNode(uint32_t unicode, uint32_t fontKey, Metric *metric); uint8_t IsGlyphFont(uint32_t unicode); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void SetItaly(FT_GlyphSlot slot); void SetBold(uint16_t fontId); #endif @@ -114,7 +117,7 @@ private: uint32_t ttfBufferSize, TtfHeader& ttfHeader, UITextLanguageFontParam fontInfo); - void BitmapCacheInit(); + void FontCacheInit(); void ClearFontGlyph(FT_Face face); }; } // namespace OHOS diff --git a/interfaces/kits/common/spannable_string.h b/interfaces/kits/common/spannable_string.h index defa44a4e4f3a48e20414f774fceb71e921df76b..b6b29d786a7d8b14f884f5b253dff4a4ed79b8bd 100644 --- a/interfaces/kits/common/spannable_string.h +++ b/interfaces/kits/common/spannable_string.h @@ -18,7 +18,7 @@ #include "gfx_utils/list.h" #include "font/ui_font_header.h" namespace OHOS { -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING class StyleSpan : public HeapBase { public: StyleSpan() {}; diff --git a/interfaces/kits/common/text.h b/interfaces/kits/common/text.h index 525df7f7ce16234294650479b8c77f8a7d416b7a..541f79ff43c85e674b316b5fcc5c7791b093eecf 100644 --- a/interfaces/kits/common/text.h +++ b/interfaces/kits/common/text.h @@ -43,7 +43,7 @@ #include "gfx_utils/vector.h" #include "engines/gfx/gfx_engine_manager.h" #include "font/ui_font_header.h" -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING #include "common/spannable_string.h" #endif @@ -164,7 +164,7 @@ public: */ virtual void SetText(const char* text); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING /** * @brief Sets the SpannableString for this text. * @@ -515,8 +515,9 @@ protected: SizeSpan* sizeSpans); uint8_t GetSpanFontIdBySize(uint8_t size); void InitSizeSpans(); - +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle* textStyles_; +#endif char* text_; uint16_t fontId_; uint8_t fontSize_; // Only the vector font library has a valid value. diff --git a/interfaces/kits/components/ui_label.h b/interfaces/kits/components/ui_label.h index d3ec8b8d833936364b6385e35fdd01005aa1230b..296372bd848923d97ada3fdcf1ed4b1aac596708 100644 --- a/interfaces/kits/components/ui_label.h +++ b/interfaces/kits/components/ui_label.h @@ -38,7 +38,7 @@ #include "animator/animator.h" #include "common/text.h" #include "components/ui_view.h" -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING #include "common/spannable_string.h" #endif @@ -188,7 +188,7 @@ public: */ void SetText(const char* text); -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING /** * @brief Sets the SpannableString content for this label. * @@ -369,7 +369,7 @@ public: * @return Returns the scroll speed. */ uint16_t GetRollSpeed() const; - + /** * @brief Obtains the width of this text. * diff --git a/interfaces/kits/font/ui_font_header.h b/interfaces/kits/font/ui_font_header.h index bf3eebeddb477f32b98f326bbd4135f5285447f4..bc1db693d02ac72e54ceb74b0927d77a7c09b824 100644 --- a/interfaces/kits/font/ui_font_header.h +++ b/interfaces/kits/font/ui_font_header.h @@ -290,7 +290,7 @@ struct GlyphNode { uint16_t kernSize; // GLYPH_KERN_SIZE_OFFSET uint16_t fontId; // GLYPH_FONT_ID uint16_t dataFlag; // GLYPH_DATA_HEAD_FLAG -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TextStyle textStyle; // GLYPH_TEXT_STYLE #endif }; diff --git a/test/uitest/test_vector_font/ui_test_vector_font.cpp b/test/uitest/test_vector_font/ui_test_vector_font.cpp index a943af22e3cbbd8baafbd6e85667695d59352aba..cef1cada35757ea164932af851ee5072ac950eaa 100644 --- a/test/uitest/test_vector_font/ui_test_vector_font.cpp +++ b/test/uitest/test_vector_font/ui_test_vector_font.cpp @@ -75,7 +75,7 @@ void UITestVectorFont::InnerTestTitle(const char* title) const UIView* UITestVectorFont::GetTestView() { -#if ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING TestDrawTextITALYBOLD(); #endif FontFontEngineVectorTestCHLang001(); @@ -340,7 +340,7 @@ void UITestVectorFont::FontFontEngineMultiLanguageTestROLang001() } } #endif -#if ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void UITestVectorFont::TestDrawTextITALYBOLD() { if (container_ != nullptr) { diff --git a/test/uitest/test_vector_font/ui_test_vector_font.h b/test/uitest/test_vector_font/ui_test_vector_font.h index 80e3caf6f8cf06cb5068aaaea0cc5777621a83ca..2a0e29831b5490837c616b568303050387d3b6d4 100644 --- a/test/uitest/test_vector_font/ui_test_vector_font.h +++ b/test/uitest/test_vector_font/ui_test_vector_font.h @@ -74,7 +74,7 @@ public: void FontFontEngineMultiLanguageTestROLang001(); #endif -#if ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING void TestDrawTextITALYBOLD(); #endif private: diff --git a/test/unittest/common/text_unit_test.cpp b/test/unittest/common/text_unit_test.cpp index 414d92dffd8d3d0a08356e50c9640c6295e370dc..f64561fcbf8603e9b4724337af1fe059af24286c 100644 --- a/test/unittest/common/text_unit_test.cpp +++ b/test/unittest/common/text_unit_test.cpp @@ -185,7 +185,7 @@ HWTEST_F(TextTest, TextSetRelativeSpan_001, TestSize.Level1) text = nullptr; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING HWTEST_F(TextTest, TextSetStyleSpan_001, TestSize.Level1) { SpannableString spannableString("图形子系统测试正常粗体斜体粗斜体"); diff --git a/test/unittest/components/ui_label_unit_test.cpp b/test/unittest/components/ui_label_unit_test.cpp index b7841b3c1d09201b99f531f296d501795b72786d..32ce64b9b782015f4d2d421e260d05b720ad0ccc 100644 --- a/test/unittest/components/ui_label_unit_test.cpp +++ b/test/unittest/components/ui_label_unit_test.cpp @@ -263,7 +263,7 @@ HWTEST_F(UILabelTest, UILabelOnPreDraw_001, TestSize.Level1) invalidatedArea = nullptr; } -#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT +#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING /** * @tc.name: UILabelSetText_002 * @tc.desc: Verify SetText function.