提交 889e6803 编写于 作者: O openharmony_ci 提交者: Gitee

!205 支持多语言字体对齐

Merge pull request !205 from wangtiantian/lineHeight
...@@ -160,7 +160,7 @@ void Text::ReMeasureTextSize(const Rect& textRect, const Style& style) ...@@ -160,7 +160,7 @@ void Text::ReMeasureTextSize(const Rect& textRect, const Style& style)
UIFont::GetInstance()->SetCurrentFontId(fontId_, fontSize_); UIFont::GetInstance()->SetCurrentFontId(fontId_, fontSize_);
int16_t maxWidth = (expandWidth_ ? COORD_MAX : textRect.GetWidth()); int16_t maxWidth = (expandWidth_ ? COORD_MAX : textRect.GetWidth());
if (maxWidth > 0) { if (maxWidth > 0) {
textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineSpace_, maxWidth); textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineHeight_, maxWidth);
FontHeader head; FontHeader head;
if (UIFont::GetInstance()->GetCurrentFontHeader(head) != 0) { if (UIFont::GetInstance()->GetCurrentFontHeader(head) != 0) {
return; return;
...@@ -210,12 +210,15 @@ void Text::Draw(BufferInfo& gfxDstBuffer, ...@@ -210,12 +210,15 @@ void Text::Draw(BufferInfo& gfxDstBuffer,
{ {
Point offset = {offsetX, 0}; Point offset = {offsetX, 0};
int16_t lineMaxWidth = expandWidth_ ? textSize_.x : coords.GetWidth(); 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; uint16_t lineBegin = 0;
uint32_t maxLineBytes = 0; uint32_t maxLineBytes = 0;
uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes); uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes);
Point pos; Point pos;
pos.y = TextPositionY(coords, (lineCount * lineHeight - style.lineSpace_)); pos.y = TextPositionY(coords, (lineCount * lineHeight));
OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.textOpa_); OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.textOpa_);
for (int i = 0; i < lineCount; i++) { for (int i = 0; i < lineCount; i++) {
if (pos.y > mask.GetBottom()) { if (pos.y > mask.GetBottom()) {
...@@ -335,12 +338,13 @@ uint16_t Text::GetEllipsisIndex(const Rect& textRect, const Style& style) ...@@ -335,12 +338,13 @@ uint16_t Text::GetEllipsisIndex(const Rect& textRect, const Style& style)
Point p; Point p;
p.x = textRect.GetWidth() - letterWidth * TEXT_ELLIPSIS_DOT_NUM; p.x = textRect.GetWidth() - letterWidth * TEXT_ELLIPSIS_DOT_NUM;
p.y = textRect.GetHeight(); p.y = textRect.GetHeight();
int16_t height = fontEngine->GetHeight() + style.lineSpace_; int16_t height = style.lineHeight_;
if (height == 0) {
height = fontEngine->GetHeight();
}
if (height) { if (height) {
p.y -= p.y % height; p.y -= p.y % height;
} }
p.y -= style.lineSpace_;
return GetLetterIndexByPosition(textRect, style, p); return GetLetterIndexByPosition(textRect, style, p);
} }
...@@ -351,7 +355,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style ...@@ -351,7 +355,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style
} }
uint32_t lineStart = 0; uint32_t lineStart = 0;
uint32_t nextLineStart = 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; int16_t y = 0;
uint32_t textLen = static_cast<uint32_t>(strlen(text_)); uint32_t textLen = static_cast<uint32_t>(strlen(text_));
int16_t width = 0; int16_t width = 0;
...@@ -361,10 +368,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style ...@@ -361,10 +368,10 @@ uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style
if (nextLineStart == 0) { if (nextLineStart == 0) {
break; break;
} }
if (pos.y <= y + letterHeight) { if (pos.y <= y + lineHeight) {
break; break;
} }
y += letterHeight + style.lineSpace_; y += lineHeight;
lineStart = nextLineStart; lineStart = nextLineStart;
} }
/* Calculate the x coordinate */ /* Calculate the x coordinate */
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
namespace OHOS { namespace OHOS {
#ifndef _FONT_TOOL #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}; Point size{0, 0};
...@@ -34,6 +34,9 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line ...@@ -34,6 +34,9 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line
uint32_t lineBegin = 0; uint32_t lineBegin = 0;
uint32_t newLineBegin = 0; uint32_t newLineBegin = 0;
uint16_t letterHeight = UIFont::GetInstance()->GetHeight(); uint16_t letterHeight = UIFont::GetInstance()->GetHeight();
if (lineHeight == 0) {
lineHeight = letterHeight;
}
while (text[lineBegin] != '\0') { while (text[lineBegin] != '\0') {
int16_t lineWidth = maxWidth; int16_t lineWidth = maxWidth;
...@@ -41,19 +44,20 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line ...@@ -41,19 +44,20 @@ Point TypedText::GetTextSize(const char* text, int16_t letterSpace, int16_t line
if (newLineBegin == lineBegin) { if (newLineBegin == lineBegin) {
break; break;
} }
size.y += letterHeight + lineSpace; size.y += lineHeight;
size.x = MATH_MAX(lineWidth, size.x); size.x = MATH_MAX(lineWidth, size.x);
lineBegin = newLineBegin; lineBegin = newLineBegin;
} }
if ((lineBegin != 0) && ((text[lineBegin - 1] == '\n') || (text[lineBegin - 1] == '\r'))) { if ((lineBegin != 0) && ((text[lineBegin - 1] == '\n') || (text[lineBegin - 1] == '\r'))) {
size.y += letterHeight + lineSpace; size.y += lineHeight;
} }
if (size.y == 0) { if (size.y == 0) {
size.y = letterHeight; size.y = lineHeight;
} else { }
size.y -= lineSpace; if (lineHeight < letterHeight) {
size.y += letterHeight - lineHeight;
} }
return size; return size;
} }
......
...@@ -40,7 +40,7 @@ public: ...@@ -40,7 +40,7 @@ public:
static Point GetTextSize(const char* text, static Point GetTextSize(const char* text,
int16_t letterSpace, int16_t letterSpace,
int16_t lineSpace, int16_t lineHeight,
int16_t maxWidth); int16_t maxWidth);
static uint32_t GetNextLine(const char* text, static uint32_t GetNextLine(const char* text,
......
...@@ -478,17 +478,17 @@ uint16_t UIDialog::MeasureButtonWidth() ...@@ -478,17 +478,17 @@ uint16_t UIDialog::MeasureButtonWidth()
if (button1_ != nullptr) { if (button1_ != nullptr) {
const char* text1 = button1_->GetText(); const char* text1 = button1_->GetText();
buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text1, 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) { if (button2_ != nullptr) {
const char* text2 = button2_->GetText(); const char* text2 = button2_->GetText();
buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text2, 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) { if (button3_ != nullptr) {
const char* text3 = button3_->GetText(); const char* text3 = button3_->GetText();
buttonTextWidth = MATH_MAX(buttonTextWidth, TypedText::GetTextSize(text3, 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); return (buttonTextWidth + BUTTON_HEIGHT) > buttonMaxWidth ? buttonMaxWidth : (buttonTextWidth + BUTTON_HEIGHT);
} }
......
...@@ -33,7 +33,7 @@ const uint16_t LABEL_WIDTH = 400; ...@@ -33,7 +33,7 @@ const uint16_t LABEL_WIDTH = 400;
const uint16_t LABEL_HEIGHT = 50; const uint16_t LABEL_HEIGHT = 50;
const uint16_t FONT_SIZE = 30; const uint16_t FONT_SIZE = 30;
const char* SOURCE_HAN_SANS_SC_REGULAR = "SourceHanSansSC-Regular.otf"; 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 } // namespace
void UITestFont::SetUp() void UITestFont::SetUp()
...@@ -79,6 +79,10 @@ const UIView* UITestFont::GetTestView() ...@@ -79,6 +79,10 @@ const UIView* UITestFont::GetTestView()
UIKitFontTestDispaly006(); UIKitFontTestDispaly006();
UIKitFontTestDispaly007(); UIKitFontTestDispaly007();
UIKitFontTestDispaly008(); UIKitFontTestDispaly008();
UIKitFontTestBaseline001();
UIKitFontTestBaseline002();
UIKitFontTestLineHeight001();
UIKitFontTestLineHeight002();
#if ENABLE_MULTI_FONT #if ENABLE_MULTI_FONT
UIKitFontMultiLanguage001(); UIKitFontMultiLanguage001();
UIKitFontMultiLanguage002(); UIKitFontMultiLanguage002();
...@@ -251,6 +255,142 @@ void UITestFont::UIKitFontTestDispaly008() ...@@ -251,6 +255,142 @@ void UITestFont::UIKitFontTestDispaly008()
positionY_ += LABEL_HEIGHT * 2 + GAP; // 2 : double 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 #if ENABLE_MULTI_FONT
void UITestFont::UIKitFontMultiLanguage001() void UITestFont::UIKitFontMultiLanguage001()
{ {
......
...@@ -68,6 +68,27 @@ public: ...@@ -68,6 +68,27 @@ public:
* @brief Test multiline line text color display * @brief Test multiline line text color display
*/ */
void UIKitFontTestDispaly008(); 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 #if ENABLE_MULTI_FONT
/** /**
* @brief Test multilingual display * @brief Test multilingual display
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册