Reorganize text alignment code location and add unit tests.

Change-Id: I3ccfb03230050c4c0f8ae4b7d0c73bf6f3b685ae
This commit is contained in:
Gary Qian
2017-06-26 09:39:09 -07:00
parent 9935045848
commit adcdcc19a8
4 changed files with 195 additions and 37 deletions

View File

@@ -180,10 +180,36 @@ void Paragraph::Layout(double width,
std::vector<SkScalar> x_queue;
size_t character_index = 0;
auto flush = [this, &x_queue, &y]() -> void {
auto postprocess_line = [this, &x_queue, &y]() -> void {
size_t record_index = 0;
for (size_t i = 0; i < x_queue.size(); ++i) {
records_[records_.size() - (x_queue.size() - i)].SetOffset(
SkPoint::Make(x_queue[i], y));
record_index = records_.size() - (x_queue.size() - i);
records_[record_index].SetOffset(SkPoint::Make(x_queue[i], y));
// TODO(garyq): Fix alignment for paragraphs with multiple styles per
// line.
switch (paragraph_style_.text_align) {
case TextAlign::left:
break;
case TextAlign::right: {
records_[record_index].SetOffset(SkPoint::Make(
records_[record_index].offset().x() + width_ -
breaker_.getWidths()[records_[record_index].line()],
records_[record_index].offset().y()));
break;
}
case TextAlign::center: {
records_[record_index].SetOffset(SkPoint::Make(
records_[record_index].offset().x() +
(width_ -
breaker_.getWidths()[records_[record_index].line()]) /
2,
records_[record_index].offset().y()));
break;
}
case TextAlign::justify: {
break;
}
}
}
x_queue.clear();
};
@@ -304,7 +330,7 @@ void Paragraph::Layout(double width,
y += max_line_spacing + prev_max_descent;
prev_max_descent = max_descent;
line_widths_.push_back(line_width);
flush();
postprocess_line();
max_line_spacing = 0.0f;
max_descent = 0.0f;
@@ -324,7 +350,7 @@ void Paragraph::Layout(double width,
}
}
y += max_line_spacing;
flush();
postprocess_line();
if (line_width != 0)
line_widths_.push_back(line_width);
@@ -406,23 +432,6 @@ void Paragraph::Paint(SkCanvas* canvas, double x, double y) {
SkPaint paint;
paint.setColor(record.style().color);
SkPoint offset = record.offset();
// TODO(garyq): Fix alignment for paragraphs with multiple styles per line.
switch (paragraph_style_.text_align) {
case TextAlign::left:
break;
case TextAlign::right: {
offset.offset(width_ - breaker_.getWidths()[record.line()], 0);
break;
}
case TextAlign::center: {
offset.offset((width_ - breaker_.getWidths()[record.line()]) / 2, 0);
break;
}
case TextAlign::justify: {
// Justify is performed in the Layout().
break;
}
}
canvas->drawTextBlob(record.text(), x + offset.x(), y + offset.y(), paint);
PaintDecorations(canvas, x + offset.x(), y + offset.y(), record.style(),
record.metrics(), record.text());

View File

@@ -21,7 +21,7 @@
namespace txt {
bool TextStyle::equals(TextStyle& other) {
bool TextStyle::equals(const TextStyle& other) const {
if (color != other.color)
return false;
if (font_weight != other.font_weight)

View File

@@ -43,7 +43,7 @@ class TextStyle {
double word_spacing = 0.0;
double height = 1.0;
bool equals(TextStyle& other);
bool equals(const TextStyle& other) const;
};
} // namespace txt

View File

@@ -225,7 +225,6 @@ TEST_F(RenderTest, BoldParagraph) {
txt::TextStyle text_style;
text_style.font_size = 60;
// Letter spacing not yet implemented
text_style.letter_spacing = 10;
text_style.font_weight = txt::FontWeight::w700;
text_style.fake_bold = true;
@@ -285,8 +284,8 @@ TEST_F(RenderTest, LeftAlignParagraph) {
text_style.letter_spacing = 1;
text_style.word_spacing = 5;
text_style.color = SK_ColorBLACK;
text_style.height = 1.15;
text_style.decoration = txt::TextDecoration(0x1);
text_style.height = 1;
text_style.decoration = txt::TextDecoration(0x0);
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
@@ -305,7 +304,36 @@ TEST_F(RenderTest, LeftAlignParagraph) {
ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
ASSERT_TRUE(paragraph->runs_.styles_[0].equals(text_style));
ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
double expected_y = 18.484375;
ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
expected_y += 30.46875 * 10;
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().x(), 0);
ASSERT_EQ(paragraph_style.text_align,
paragraph->GetParagraphStyle().text_align);
ASSERT_TRUE(Snapshot());
}
@@ -342,8 +370,8 @@ TEST_F(RenderTest, RightAlignParagraph) {
text_style.letter_spacing = 1;
text_style.word_spacing = 5;
text_style.color = SK_ColorBLACK;
text_style.height = 1.15;
text_style.decoration = txt::TextDecoration(0x1);
text_style.height = 1;
text_style.decoration = txt::TextDecoration(0x0);
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
@@ -362,7 +390,51 @@ TEST_F(RenderTest, RightAlignParagraph) {
ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
ASSERT_TRUE(paragraph->runs_.styles_[0].equals(text_style));
ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
double expected_y = 18.484375;
ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(
paragraph->records_[0].offset().x(),
paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[0].line()]);
ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(
paragraph->records_[1].offset().x(),
paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[1].line()]);
ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(
paragraph->records_[2].offset().x(),
paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[2].line()]);
ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
expected_y += 30.46875 * 10;
ASSERT_DOUBLE_EQ(
paragraph->records_[3].offset().x(),
paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[3].line()]);
ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
ASSERT_DOUBLE_EQ(
paragraph->records_[13].offset().x(),
paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[13].line()]);
ASSERT_EQ(paragraph_style.text_align,
paragraph->GetParagraphStyle().text_align);
ASSERT_TRUE(Snapshot());
}
@@ -399,8 +471,8 @@ TEST_F(RenderTest, CenterAlignParagraph) {
text_style.letter_spacing = 1;
text_style.word_spacing = 5;
text_style.color = SK_ColorBLACK;
text_style.height = 1.15;
text_style.decoration = txt::TextDecoration(0x1);
text_style.height = 1;
text_style.decoration = txt::TextDecoration(0x0);
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
@@ -419,7 +491,55 @@ TEST_F(RenderTest, CenterAlignParagraph) {
ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
ASSERT_TRUE(paragraph->runs_.styles_[0].equals(text_style));
ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
double expected_y = 18.484375;
ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(
paragraph->records_[0].offset().x(),
(paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[0].line()]) /
2);
ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(
paragraph->records_[1].offset().x(),
(paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[1].line()]) /
2);
ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_EQ(paragraph->records_[2].offset().x(),
(paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[2].line()]) /
2);
ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
expected_y += 30.46875 * 10;
ASSERT_DOUBLE_EQ(
paragraph->records_[3].offset().x(),
(paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[3].line()]) /
2);
ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
ASSERT_DOUBLE_EQ(
paragraph->records_[13].offset().x(),
(paragraph->width_ -
paragraph->breaker_.getWidths()[paragraph->records_[13].line()]) /
2);
ASSERT_EQ(paragraph_style.text_align,
paragraph->GetParagraphStyle().text_align);
ASSERT_TRUE(Snapshot());
}
@@ -456,8 +576,8 @@ TEST_F(RenderTest, JustifyAlignParagraph) {
text_style.letter_spacing = 1;
text_style.word_spacing = 5;
text_style.color = SK_ColorBLACK;
text_style.height = 1.15;
text_style.decoration = txt::TextDecoration(0x1);
text_style.height = 1;
text_style.decoration = txt::TextDecoration(0x0);
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
@@ -476,7 +596,36 @@ TEST_F(RenderTest, JustifyAlignParagraph) {
ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
ASSERT_TRUE(paragraph->runs_.styles_[0].equals(text_style));
ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
double expected_y = 18.484375;
ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
expected_y += 30.46875;
ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
expected_y += 30.46875 * 10;
ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().x(), 0);
ASSERT_EQ(paragraph_style.text_align,
paragraph->GetParagraphStyle().text_align);
ASSERT_TRUE(Snapshot());
}