未验证 提交 1263d28a 编写于 作者: G Gary Qian 提交者: GitHub

Fix caret being at left edge when newline pressed on centered text (#7875)

上级 f45572e9
......@@ -1302,6 +1302,11 @@ std::vector<Paragraph::TextBox> Paragraph::GetRectsForRange(
if (line.end != line.end_including_newline && line.end >= start &&
line.end_including_newline <= end) {
SkScalar x = line_widths_[line_number];
// Move empty box to center if center aligned and is an empty line.
if (x == 0 && !isinf(width_) &&
paragraph_style_.effective_align() == TextAlign::center) {
x = width_ / 2;
}
SkScalar top = (line_number > 0) ? line_heights_[line_number - 1] : 0;
SkScalar bottom = line_heights_[line_number];
line_metrics[line_number].boxes.emplace_back(
......
......@@ -1780,6 +1780,83 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraph)) {
ASSERT_TRUE(Snapshot());
}
TEST_F(ParagraphTest,
DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraphNewlineCentered)) {
const char* text = "01234\n";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
icu_text.getBuffer() + icu_text.length());
txt::ParagraphStyle paragraph_style;
paragraph_style.max_lines = 10;
paragraph_style.text_align = TextAlign::center;
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.font_size = 50;
text_style.letter_spacing = 0;
text_style.font_weight = FontWeight::w500;
text_style.word_spacing = 0;
text_style.color = SK_ColorBLACK;
text_style.height = 1;
builder.PushStyle(text_style);
builder.AddText(u16_text);
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(550);
paragraph->Paint(GetCanvas(), 0, 0);
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
// Tests for GetRectsForRange()
Paragraph::RectHeightStyle rect_height_style =
Paragraph::RectHeightStyle::kMax;
Paragraph::RectWidthStyle rect_width_style =
Paragraph::RectWidthStyle::kTight;
paint.setColor(SK_ColorRED);
std::vector<txt::Paragraph::TextBox> boxes =
paragraph->GetRectsForRange(0, 0, rect_height_style, rect_width_style);
for (size_t i = 0; i < boxes.size(); ++i) {
GetCanvas()->drawRect(boxes[i].rect, paint);
}
EXPECT_EQ(boxes.size(), 0ull);
boxes =
paragraph->GetRectsForRange(0, 1, rect_height_style, rect_width_style);
for (size_t i = 0; i < boxes.size(); ++i) {
GetCanvas()->drawRect(boxes[i].rect, paint);
}
EXPECT_EQ(boxes.size(), 1ull);
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 203.95508);
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 232.37305);
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
paint.setColor(SK_ColorGREEN);
boxes =
paragraph->GetRectsForRange(6, 7, rect_height_style, rect_width_style);
for (size_t i = 0; i < boxes.size(); ++i) {
GetCanvas()->drawRect(boxes[i].rect, paint);
}
EXPECT_EQ(boxes.size(), 1ull);
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 275);
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 275);
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(),
75); // TODO(garyq): This value can be improved... Should be
// taller, but we need a good way to obtain a height
// without any glyphs on the line.
ASSERT_TRUE(Snapshot());
}
TEST_F(ParagraphTest,
DISABLE_ON_WINDOWS(GetRectsForRangeCenterMultiLineParagraph)) {
const char* text = "01234   \n0123  "; // includes ideographic
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册