Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1263d28

Browse files
authored
Fix caret being at left edge when newline pressed on centered text (#7875)
1 parent f45572e commit 1263d28

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

third_party/txt/src/txt/paragraph.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,11 @@ std::vector<Paragraph::TextBox> Paragraph::GetRectsForRange(
13021302
if (line.end != line.end_including_newline && line.end >= start &&
13031303
line.end_including_newline <= end) {
13041304
SkScalar x = line_widths_[line_number];
1305+
// Move empty box to center if center aligned and is an empty line.
1306+
if (x == 0 && !isinf(width_) &&
1307+
paragraph_style_.effective_align() == TextAlign::center) {
1308+
x = width_ / 2;
1309+
}
13051310
SkScalar top = (line_number > 0) ? line_heights_[line_number - 1] : 0;
13061311
SkScalar bottom = line_heights_[line_number];
13071312
line_metrics[line_number].boxes.emplace_back(

third_party/txt/tests/paragraph_unittests.cc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,83 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraph)) {
17801780
ASSERT_TRUE(Snapshot());
17811781
}
17821782

1783+
TEST_F(ParagraphTest,
1784+
DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraphNewlineCentered)) {
1785+
const char* text = "01234\n";
1786+
auto icu_text = icu::UnicodeString::fromUTF8(text);
1787+
std::u16string u16_text(icu_text.getBuffer(),
1788+
icu_text.getBuffer() + icu_text.length());
1789+
1790+
txt::ParagraphStyle paragraph_style;
1791+
paragraph_style.max_lines = 10;
1792+
paragraph_style.text_align = TextAlign::center;
1793+
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1794+
1795+
txt::TextStyle text_style;
1796+
text_style.font_families = std::vector<std::string>(1, "Roboto");
1797+
text_style.font_size = 50;
1798+
text_style.letter_spacing = 0;
1799+
text_style.font_weight = FontWeight::w500;
1800+
text_style.word_spacing = 0;
1801+
text_style.color = SK_ColorBLACK;
1802+
text_style.height = 1;
1803+
builder.PushStyle(text_style);
1804+
1805+
builder.AddText(u16_text);
1806+
1807+
builder.Pop();
1808+
1809+
auto paragraph = builder.Build();
1810+
paragraph->Layout(550);
1811+
1812+
paragraph->Paint(GetCanvas(), 0, 0);
1813+
1814+
SkPaint paint;
1815+
paint.setStyle(SkPaint::kStroke_Style);
1816+
paint.setAntiAlias(true);
1817+
paint.setStrokeWidth(1);
1818+
1819+
// Tests for GetRectsForRange()
1820+
Paragraph::RectHeightStyle rect_height_style =
1821+
Paragraph::RectHeightStyle::kMax;
1822+
Paragraph::RectWidthStyle rect_width_style =
1823+
Paragraph::RectWidthStyle::kTight;
1824+
paint.setColor(SK_ColorRED);
1825+
std::vector<txt::Paragraph::TextBox> boxes =
1826+
paragraph->GetRectsForRange(0, 0, rect_height_style, rect_width_style);
1827+
for (size_t i = 0; i < boxes.size(); ++i) {
1828+
GetCanvas()->drawRect(boxes[i].rect, paint);
1829+
}
1830+
EXPECT_EQ(boxes.size(), 0ull);
1831+
1832+
boxes =
1833+
paragraph->GetRectsForRange(0, 1, rect_height_style, rect_width_style);
1834+
for (size_t i = 0; i < boxes.size(); ++i) {
1835+
GetCanvas()->drawRect(boxes[i].rect, paint);
1836+
}
1837+
EXPECT_EQ(boxes.size(), 1ull);
1838+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 203.95508);
1839+
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1840+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 232.37305);
1841+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1842+
1843+
paint.setColor(SK_ColorGREEN);
1844+
boxes =
1845+
paragraph->GetRectsForRange(6, 7, rect_height_style, rect_width_style);
1846+
for (size_t i = 0; i < boxes.size(); ++i) {
1847+
GetCanvas()->drawRect(boxes[i].rect, paint);
1848+
}
1849+
EXPECT_EQ(boxes.size(), 1ull);
1850+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 275);
1851+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 275);
1852+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(),
1853+
75); // TODO(garyq): This value can be improved... Should be
1854+
// taller, but we need a good way to obtain a height
1855+
// without any glyphs on the line.
1856+
1857+
ASSERT_TRUE(Snapshot());
1858+
}
1859+
17831860
TEST_F(ParagraphTest,
17841861
DISABLE_ON_WINDOWS(GetRectsForRangeCenterMultiLineParagraph)) {
17851862
const char* text = "01234   \n0123  "; // includes ideographic

0 commit comments

Comments
 (0)