Handle paragraph alignment and direction in newline rectangles (flutter/engine#17750)
This commit is contained in:
@@ -1649,6 +1649,7 @@ std::vector<Paragraph::TextBox> ParagraphTxt::GetRectsForRange(
|
||||
// Text direction of the first line so we can extend the correct side for
|
||||
// RectWidthStyle::kMax.
|
||||
TextDirection first_line_dir = TextDirection::ltr;
|
||||
std::map<size_t, size_t> newline_x_positions;
|
||||
|
||||
// Lines that are actually in the requested range.
|
||||
size_t max_line = 0;
|
||||
@@ -1660,6 +1661,11 @@ std::vector<Paragraph::TextBox> ParagraphTxt::GetRectsForRange(
|
||||
// Check to see if we are finished.
|
||||
if (run.code_units.start >= end)
|
||||
break;
|
||||
|
||||
// Update new line x position with the ending of last bidi run on the line
|
||||
newline_x_positions[run.line_number] =
|
||||
run.direction == TextDirection::ltr ? run.x_pos.end : run.x_pos.start;
|
||||
|
||||
if (run.code_units.end <= start)
|
||||
continue;
|
||||
|
||||
@@ -1730,11 +1736,12 @@ std::vector<Paragraph::TextBox> ParagraphTxt::GetRectsForRange(
|
||||
if (line_box_metrics.find(line_number) == line_box_metrics.end()) {
|
||||
if (line.end_index != line.end_including_newline &&
|
||||
line.end_index >= 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 x;
|
||||
auto it = newline_x_positions.find(line_number);
|
||||
if (it != newline_x_positions.end()) {
|
||||
x = it->second;
|
||||
} else {
|
||||
x = GetLineXOffset(0, false);
|
||||
}
|
||||
SkScalar top =
|
||||
(line_number > 0) ? line_metrics_[line_number - 1].height : 0;
|
||||
|
||||
@@ -3943,15 +3943,210 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraph)) {
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
TEST_F(ParagraphTest,
|
||||
LINUX_ONLY(GetRectsForRangeCenterParagraphNewlineCentered)) {
|
||||
const char* text = "01234\n";
|
||||
TEST_F(ParagraphTest, LINUX_ONLY(GetRectsForRangeParagraphNewlineLeftAlign)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::ltr;
|
||||
paragraph_style.text_align = TextAlign::left;
|
||||
txt::ParagraphBuilderTxt 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 = BuildParagraph(builder);
|
||||
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(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 28.417969);
|
||||
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(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 0);
|
||||
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.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 77);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 77);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 27);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 27);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
TEST_F(ParagraphTest, LINUX_ONLY(GetRectsForRangeParagraphNewlineRightAlign)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::ltr;
|
||||
paragraph_style.text_align = TextAlign::right;
|
||||
txt::ParagraphBuilderTxt 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 = BuildParagraph(builder);
|
||||
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(), 407.91016);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 436.32812);
|
||||
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(), 550);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 550);
|
||||
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.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 550);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 550);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 483);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 483);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
TEST_F(ParagraphTest,
|
||||
LINUX_ONLY(GetRectsForRangeCenterParagraphNewlineCentered)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::ltr;
|
||||
paragraph_style.text_align = TextAlign::center;
|
||||
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
|
||||
|
||||
@@ -4017,6 +4212,319 @@ TEST_F(ParagraphTest,
|
||||
// taller, but we need a good way to obtain a height
|
||||
// without any glyphs on the line.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 313);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 313);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 255);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 255);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
TEST_F(ParagraphTest,
|
||||
LINUX_ONLY(GetRectsForRangeParagraphNewlineRTLLeftAlign)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::rtl;
|
||||
paragraph_style.text_align = TextAlign::left;
|
||||
txt::ParagraphBuilderTxt 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 = BuildParagraph(builder);
|
||||
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(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 28.417969);
|
||||
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(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 0);
|
||||
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.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 55);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 55);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 0);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
TEST_F(ParagraphTest,
|
||||
LINUX_ONLY(GetRectsForRangeParagraphNewlineRTLRightAlign)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::rtl;
|
||||
paragraph_style.text_align = TextAlign::right;
|
||||
txt::ParagraphBuilderTxt 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 = BuildParagraph(builder);
|
||||
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(), 407.91016);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 436.32812);
|
||||
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(), 550);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 550);
|
||||
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.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 527);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 527);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 456);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 456);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
TEST_F(ParagraphTest,
|
||||
LINUX_ONLY(GetRectsForRangeCenterParagraphNewlineRTLCentered)) {
|
||||
const char* text = "01234\n\nعab\naعلی\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_direction = TextDirection::rtl;
|
||||
paragraph_style.text_align = TextAlign::center;
|
||||
txt::ParagraphBuilderTxt 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 = BuildParagraph(builder);
|
||||
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.
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(10, 11, 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(), 291);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 291);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 134);
|
||||
|
||||
boxes =
|
||||
paragraph->GetRectsForRange(15, 16, 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(), 228);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 228);
|
||||
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 193);
|
||||
|
||||
ASSERT_TRUE(Snapshot());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user