diff --git a/frameworks/common/text.cpp b/frameworks/common/text.cpp index 376553154bdd3fb1db42bf1b28f07a63988e35ef..31c9d9ac1a23ef7560e723b4fe18927b664afec2 100755 --- a/frameworks/common/text.cpp +++ b/frameworks/common/text.cpp @@ -160,7 +160,7 @@ void Text::ReMeasureTextSize(const Rect& textRect, const Style& style) UIFont::GetInstance()->SetCurrentFontId(fontId_, fontSize_); int16_t maxWidth = (expandWidth_ ? COORD_MAX : textRect.GetWidth()); if (maxWidth > 0) { - textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineSpace_, maxWidth); + textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineHeight_, maxWidth); FontHeader head; if (UIFont::GetInstance()->GetCurrentFontHeader(head) != 0) { return; @@ -210,12 +210,15 @@ void Text::Draw(BufferInfo& gfxDstBuffer, { Point offset = {offsetX, 0}; int16_t lineMaxWidth = expandWidth_ ? textSize_.x : coords.GetWidth(); - int16_t lineHeight = UIFont::GetInstance()->GetHeight() + style.lineSpace_; + int16_t lineHeight = style.lineHeight_; + if (lineHeight == 0) { + lineHeight = UIFont::GetInstance()->GetHeight(); + } uint16_t lineBegin = 0; uint32_t maxLineBytes = 0; uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes); Point pos; - pos.y = TextPositionY(coords, (lineCount * lineHeight - style.lineSpace_)); + pos.y = TextPositionY(coords, (lineCount * lineHeight)); OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.textOpa_); for (int i = 0; i < lineCount; i++) { if (pos.y > mask.GetBottom()) { @@ -335,12 +338,13 @@ uint16_t Text::GetEllipsisIndex(const Rect& textRect, const Style& style) Point p; p.x = textRect.GetWidth() - letterWidth * TEXT_ELLIPSIS_DOT_NUM; p.y = textRect.GetHeight(); - int16_t height = fontEngine->GetHeight() + style.lineSpace_; + int16_t height = style.lineHeight_; + if (height == 0) { + height = fontEngine->GetHeight(); + } if (height) { p.y -= p.y % height; } - - p.y -= style.lineSpace_; return GetLetterIndexByPosition(textRect, style, p); } @@ -351,7 +355,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style } uint32_t lineStart = 0; uint32_t nextLineStart = 0; - uint16_t letterHeight = UIFont::GetInstance()->GetHeight(); + uint16_t lineHeight = style.lineHeight_; + if (lineHeight == 0) { + lineHeight = UIFont::GetInstance()->GetHeight(); + } int16_t y = 0; uint32_t textLen = static_cast(strlen(text_)); int16_t width = 0; @@ -361,10 +368,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style if (nextLineStart == 0) { break; } - if (pos.y <= y + letterHeight) { + if (pos.y <= y + lineHeight) { break; } - y += letterHeight + style.lineSpace_; + y += lineHeight; lineStart = nextLineStart; } /* Calculate the x coordinate */ diff --git a/frameworks/common/typed_text.cpp b/frameworks/common/typed_text.cpp index bc9d48ecde104e4c61a36bab8c364431eb12a2d3..b185ad77cedb437f6577da85d36cc6144888086a 100755 --- a/frameworks/common/typed_text.cpp +++ b/frameworks/common/typed_text.cpp @@ -22,7 +22,7 @@ namespace OHOS { #ifndef _FONT_TOOL -Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t lineSpace, int16_t maxWidth) +Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t lineHeight, int16_t maxWidth) { Point size{0, 0}; @@ -34,6 +34,9 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line uint32_t lineBegin = 0; uint32_t newLineBegin = 0; uint16_t letterHeight = UIFont::GetInstance()->GetHeight(); + if (lineHeight == 0) { + lineHeight = letterHeight; + } while (text[lineBegin] != '\0') { int16_t lineWidth = maxWidth; @@ -41,19 +44,20 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line if (newLineBegin == lineBegin) { break; } - size.y += letterHeight + lineSpace; + size.y += lineHeight; size.x = MATH_MAX(lineWidth, size.x); lineBegin = newLineBegin; } if ((lineBegin != 0) && ((text[lineBegin - 1] == '\n') || (text[lineBegin - 1] == '\r'))) { - size.y += letterHeight + lineSpace; + size.y += lineHeight; } if (size.y == 0) { - size.y = letterHeight; - } else { - size.y -= lineSpace; + size.y = lineHeight; + } + if (lineHeight < letterHeight) { + size.y += letterHeight - lineHeight; } return size; } diff --git a/frameworks/common/typed_text.h b/frameworks/common/typed_text.h index 36e51faca11bdd6033aa9ea516bf5b17ab77074a..c57ecd69b0a5ac90d504a0f1dc50e1b3bf25ba64 100755 --- a/frameworks/common/typed_text.h +++ b/frameworks/common/typed_text.h @@ -40,7 +40,7 @@ public: static Point GetTextSize(const char* text, int16_t letterSpace, - int16_t lineSpace, + int16_t lineHeight, int16_t maxWidth); static uint32_t GetNextLine(const char* text, diff --git a/frameworks/components/ui_dialog.cpp b/frameworks/components/ui_dialog.cpp index 261325e26bab1c87819db3ee174a53265d73e4dd..060c4b0f25d3f2523fb4ee274bda6239928d55a7 100755 --- a/frameworks/components/ui_dialog.cpp +++ b/frameworks/components/ui_dialog.cpp @@ -478,17 +478,17 @@ uint16_t UIDialog::MeasureButtonWidth() if (button1_ != nullptr) { const char* text1 = button1_->GetText(); buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text1, - button1_->GetStyleConst().letterSpace_, button1_->GetStyleConst().lineSpace_, widthMax_).x); + button1_->GetStyleConst().letterSpace_, button1_->GetStyleConst().lineHeight_, widthMax_).x); } if (button2_ != nullptr) { const char* text2 = button2_->GetText(); buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text2, - button2_->GetStyleConst().letterSpace_, button2_->GetStyleConst().lineSpace_, widthMax_).x); + button2_->GetStyleConst().letterSpace_, button2_->GetStyleConst().lineHeight_, widthMax_).x); } if (button3_ != nullptr) { const char* text3 = button3_->GetText(); buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text3, - button3_->GetStyleConst().letterSpace_, button3_->GetStyleConst().lineSpace_, widthMax_).x); + button3_->GetStyleConst().letterSpace_, button3_->GetStyleConst().lineHeight_, widthMax_).x); } return (buttonTextWidth + BUTTON_HEIGHT) > buttonMaxWidth ? buttonMaxWidth : (buttonTextWidth + BUTTON_HEIGHT); } diff --git a/test/uitest/test_font/ui_test_font.cpp b/test/uitest/test_font/ui_test_font.cpp index 6ee50852aa4367e34bcbee24decb2ee941b2ae66..bfbb22e8ec5f715d42105660853c1a1c8a3ddf68 100755 --- a/test/uitest/test_font/ui_test_font.cpp +++ b/test/uitest/test_font/ui_test_font.cpp @@ -33,7 +33,7 @@ const uint16_t LABEL_WIDTH = 400; const uint16_t LABEL_HEIGHT = 50; const uint16_t FONT_SIZE = 30; const char* SOURCE_HAN_SANS_SC_REGULAR = "SourceHanSansSC-Regular.otf"; -const char* ROBOTO_CONDENSED_REGULAR = "RobotoCondensed-Regulat.ttf"; +const char* ROBOTO_CONDENSED_REGULAR = "RobotoCondensed-Regular.ttf"; } // namespace void UITestFont::SetUp() @@ -79,6 +79,10 @@ const UIView* UITestFont::GetTestView() UIKitFontTestDispaly006(); UIKitFontTestDispaly007(); UIKitFontTestDispaly008(); + UIKitFontTestBaseline001(); + UIKitFontTestBaseline002(); + UIKitFontTestLineHeight001(); + UIKitFontTestLineHeight002(); #if ENABLE_MULTI_FONT UIKitFontMultiLanguage001(); UIKitFontMultiLanguage002(); @@ -251,6 +255,142 @@ void UITestFont::UIKitFontTestDispaly008() positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double } +void UITestFont::UIKitFontTestBaseline001() +{ + if (container_ == nullptr) { + return; + } + InnerTestTitle("Font baseline alignment"); + UILabel* label = new UILabel(); + label->SetPosition(positionX_, positionY_); + label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR); + label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE); +#else + label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4); +#endif + label->SetText("hello, uikit"); + + UILabel* label2 = new UILabel(); + label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half + label2->Resize(LABEL_WIDTH, LABEL_HEIGHT); +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR); + label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE); +#else + label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4); +#endif + label2->SetText("hello, uikit"); + + container_->Add(label); + container_->Add(label2); + positionY_ += LABEL_HEIGHT + GAP; +} + +void UITestFont::UIKitFontTestBaseline002() +{ + if (container_ == nullptr) { + return; + } + InnerTestTitle(" Font baseline alignment"); + UILabel* label = new UILabel(); + label->SetPosition(positionX_, positionY_); + label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR); + label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE); +#else + label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4); +#endif + label->SetText("hello, uikit"); + label->SetStyle(STYLE_LINE_HEIGHT, 30); // 30 : line height + + UILabel* label2 = new UILabel(); + label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half + label2->Resize(LABEL_WIDTH, LABEL_HEIGHT); +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR); + label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE); +#else + label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4); +#endif + label2->SetText("hello, uikit"); + label2->SetStyle(STYLE_LINE_HEIGHT, 30); // 30 : line height + + container_->Add(label); + container_->Add(label2); + positionY_ += LABEL_HEIGHT + GAP; +} + +void UITestFont::UIKitFontTestLineHeight001() +{ + if (container_ == nullptr) { + return; + } + InnerTestTitle(" Font lineheight alignment"); + UILabel* label = new UILabel(); + label->SetPosition(positionX_, positionY_); + label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR); + label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE); +#else + label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4); +#endif + label->SetText("hello,\n uikit"); + + UILabel* label2 = new UILabel(); + label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half + label2->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR); + label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE); +#else + label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4); +#endif + label2->SetText("hello,\n uikit"); + + container_->Add(label); + container_->Add(label2); + positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double +} + +void UITestFont::UIKitFontTestLineHeight002() +{ + if (container_ == nullptr) { + return; + } + InnerTestTitle(" Font lineheight alignment"); + UILabel* label = new UILabel(); + label->SetPosition(positionX_, positionY_); + label->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(SOURCE_HAN_SANS_SC_REGULAR); + label->SetFont(SOURCE_HAN_SANS_SC_REGULAR, FONT_SIZE); +#else + label->SetFontId(F_SOURCEHANSANSSC_REGULAR_30_4); +#endif + label->SetText("hello,\n uikit"); + label->SetStyle(STYLE_LINE_HEIGHT, 40); // 40 : line height + + UILabel* label2 = new UILabel(); + label2->SetPosition(positionX_ + (LABEL_WIDTH / 2), positionY_); // 2 : half + label2->Resize(LABEL_WIDTH / 2, LABEL_HEIGHT * 2); // 2 : half +#if ENABLE_VECTOR_FONT + UIFont::GetInstance()->RegisterFontInfo(ROBOTO_CONDENSED_REGULAR); + label2->SetFont(ROBOTO_CONDENSED_REGULAR, FONT_SIZE); +#else + label2->SetFontId(F_ROBOTOCONDENSED_REGULAR_30_4); +#endif + label2->SetText("hello,\n uikit"); + label2->SetStyle(STYLE_LINE_HEIGHT, 40); // 40 : line height + + container_->Add(label); + container_->Add(label2); + positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double +} + #if ENABLE_MULTI_FONT void UITestFont::UIKitFontMultiLanguage001() { diff --git a/test/uitest/test_font/ui_test_font.h b/test/uitest/test_font/ui_test_font.h index 63bac32ce4826992a3622651aa537e28b8dcd533..f5afba75e8ac1ce1ef767ee949beedafea3fa4f8 100755 --- a/test/uitest/test_font/ui_test_font.h +++ b/test/uitest/test_font/ui_test_font.h @@ -68,6 +68,27 @@ public: * @brief Test multiline line text color display */ void UIKitFontTestDispaly008(); + + /** + * @brief Test font baseline alignment + */ + void UIKitFontTestBaseline001(); + + /** + * @brief Test font baseline alignment + */ + void UIKitFontTestBaseline002(); + + /** + * @brief Test font lineheight alignment + */ + void UIKitFontTestLineHeight001(); + + /** + * @brief Test font lineheight alignment + */ + void UIKitFontTestLineHeight002(); + #if ENABLE_MULTI_FONT /** * @brief Test multilingual display