Commit 49236cbd authored by tbarzic's avatar tbarzic Committed by Commit bot

Make StyledLabel wrap long words

This fixes StyleLabel layout in languages that don't have word breaks
(e.g. Japanese).

BUG=479859

Review URL: https://codereview.chromium.org/1140083002

Cr-Commit-Position: refs/heads/master@{#330807}
parent 6e909fed
......@@ -281,12 +281,10 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
text_font_list,
chunk_bounds.width(),
chunk_bounds.height(),
gfx::IGNORE_LONG_WORDS,
gfx::WRAP_LONG_WORDS,
&substrings);
DCHECK(!substrings.empty());
base::string16 chunk = substrings[0];
if (chunk.empty()) {
if (substrings.empty() || substrings[0].empty()) {
// Nothing fits on this line. Start a new line.
// If x is 0, first line may have leading whitespace that doesn't fit in a
// single line, so try trimming those. Otherwise there is no room for
......@@ -305,6 +303,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
continue;
}
base::string16 chunk = substrings[0];
scoped_ptr<Label> label;
if (position >= range.start()) {
const RangeStyleInfo& style_info = current_range->style_info;
......@@ -318,7 +318,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
continue;
}
chunk = chunk.substr(0, std::min(chunk.size(), range.end() - position));
if (chunk.size() > range.end() - position)
chunk = chunk.substr(0, range.end() - position);
label = CreateLabelRange(chunk, font_list_, style_info, this);
......@@ -354,6 +355,16 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
x += view_size.width() - focus_border_insets.width();
used_width = std::max(used_width, x);
// If |gfx::ElideRectangleText| returned more than one substring, that
// means the whole text did not fit into remaining line width, with text
// after |susbtring[0]| spilling into next line. If whole |substring[0]|
// was added to the current line (this may not be the case if part of the
// substring has different style), proceed to the next line.
if (substrings.size() > 1 && chunk.size() == substrings[0].size()) {
x = 0;
++line;
}
remaining_string = remaining_string.substr(chunk.size());
}
......
......@@ -124,6 +124,32 @@ TEST_F(StyledLabelTest, BasicWrapping) {
EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom());
}
TEST_F(StyledLabelTest, WrapLongWords) {
const std::string text("ThisIsTextAsASingleWord");
InitStyledLabel(text);
Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3)));
gfx::Size label_preferred_size = label.GetPreferredSize();
EXPECT_EQ(label_preferred_size.height() * 2,
StyledLabelContentHeightForWidth(label_preferred_size.width()));
styled()->SetBounds(
0, 0, styled()->GetInsets().width() + label_preferred_size.width(),
styled()->GetInsets().height() + 2 * label_preferred_size.height());
styled()->Layout();
ASSERT_EQ(2, styled()->child_count());
ASSERT_EQ(gfx::Point(), styled()->bounds().origin());
EXPECT_EQ(gfx::Point(), styled()->child_at(0)->bounds().origin());
EXPECT_EQ(gfx::Point(0, styled()->height() / 2),
styled()->child_at(1)->bounds().origin());
EXPECT_FALSE(static_cast<Label*>(styled()->child_at(0))->text().empty());
EXPECT_FALSE(static_cast<Label*>(styled()->child_at(1))->text().empty());
EXPECT_EQ(ASCIIToUTF16(text),
static_cast<Label*>(styled()->child_at(0))->text() +
static_cast<Label*>(styled()->child_at(1))->text());
}
TEST_F(StyledLabelTest, CreateLinks) {
const std::string text("This is a test block of text.");
InitStyledLabel(text);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment