diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 549de3af7b..dff8b5f449 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -180,10 +180,36 @@ void Paragraph::Layout(double width, std::vector 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()); diff --git a/engine/src/flutter/src/text_style.cc b/engine/src/flutter/src/text_style.cc index bacb9a55fe..692176c687 100644 --- a/engine/src/flutter/src/text_style.cc +++ b/engine/src/flutter/src/text_style.cc @@ -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) diff --git a/engine/src/flutter/src/text_style.h b/engine/src/flutter/src/text_style.h index 23c2043b0a..7692c7a58c 100644 --- a/engine/src/flutter/src/text_style.h +++ b/engine/src/flutter/src/text_style.h @@ -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 diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index e18cc23a78..d93ea32e3e 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -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()); }