diff --git a/engine/src/flutter/third_party/txt/src/txt/paint_record.cc b/engine/src/flutter/third_party/txt/src/txt/paint_record.cc index 200c48665a..0ee43f8478 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paint_record.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paint_record.cc @@ -26,24 +26,28 @@ PaintRecord::PaintRecord(TextStyle style, sk_sp text, SkFontMetrics metrics, size_t line, - double run_width) + double run_width, + bool is_ghost) : style_(style), offset_(offset), text_(std::move(text)), metrics_(metrics), line_(line), - run_width_(run_width) {} + run_width_(run_width), + is_ghost_(is_ghost) {} PaintRecord::PaintRecord(TextStyle style, sk_sp text, SkFontMetrics metrics, size_t line, - double run_width) + double run_width, + bool is_ghost) : style_(style), text_(std::move(text)), metrics_(metrics), line_(line), - run_width_(run_width) {} + run_width_(run_width), + is_ghost_(is_ghost) {} PaintRecord::PaintRecord(PaintRecord&& other) { style_ = other.style_; @@ -51,7 +55,7 @@ PaintRecord::PaintRecord(PaintRecord&& other) { text_ = std::move(other.text_); metrics_ = other.metrics_; line_ = other.line_; - run_width_ = other.run_width_; + run_width_ = other.run_width_, is_ghost_ = other.is_ghost_; } PaintRecord& PaintRecord::operator=(PaintRecord&& other) { @@ -61,6 +65,7 @@ PaintRecord& PaintRecord::operator=(PaintRecord&& other) { metrics_ = other.metrics_; line_ = other.line_; run_width_ = other.run_width_; + is_ghost_ = other.is_ghost_; return *this; } diff --git a/engine/src/flutter/third_party/txt/src/txt/paint_record.h b/engine/src/flutter/third_party/txt/src/txt/paint_record.h index 13f2b2311d..c07ab6827c 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paint_record.h +++ b/engine/src/flutter/third_party/txt/src/txt/paint_record.h @@ -39,13 +39,15 @@ class PaintRecord { sk_sp text, SkFontMetrics metrics, size_t line, - double run_width); + double run_width, + bool is_ghost); PaintRecord(TextStyle style, sk_sp text, SkFontMetrics metrics, size_t line, - double run_width); + double run_width, + bool is_ghost); PaintRecord(PaintRecord&& other); @@ -65,6 +67,8 @@ class PaintRecord { double GetRunWidth() const { return run_width_; } + bool isGhost() const { return is_ghost_; } + private: TextStyle style_; // offset_ is the overall offset of the origin of the SkTextBlob. @@ -75,6 +79,9 @@ class PaintRecord { SkFontMetrics metrics_; size_t line_; double run_width_ = 0.0f; + // 'Ghost' runs represent trailing whitespace. 'Ghost' runs should not have + // decorations painted on them and do not impact layout of visible glyphs. + bool is_ghost_ = false; FML_DISALLOW_COPY_AND_ASSIGN(PaintRecord); }; diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc index 65efccc8a1..df40bd1cc2 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -552,6 +552,22 @@ void Paragraph::Layout(double width, bool force) { std::min(bidi_run.end(), line_end_index), bidi_run.direction(), bidi_run.style()); } + // A "ghost" run is a run that does not impact the layout, breaking, + // alignment, width, etc but is still "visible" though getRectsForRange. + // For example, trailing whitespace on centered text can be scrolled + // through with the caret but will not wrap the line. + // + // Here, we add an additional run for the whitespace, but dont + // let it impact metrics. After layout of the whitespace run, we do not + // add its width into the x-offset adjustment, effectively nullifying its + // impact on the layout. + if (line_range.end_excluding_whitespace < line_range.end && + bidi_run.start() <= line_range.end && + bidi_run.end() > line_end_index) { + line_runs.emplace_back(std::max(bidi_run.start(), line_end_index), + std::min(bidi_run.end(), line_range.end), + bidi_run.direction(), bidi_run.style(), true); + } } bool line_runs_all_rtl = line_runs.size() && @@ -766,7 +782,7 @@ void Paragraph::Layout(double width, bool force) { font.getMetrics(&metrics); paint_records.emplace_back(run.style(), SkPoint::Make(run_x_offset, 0), builder.make(), metrics, line_number, - layout.getAdvance()); + layout.getAdvance(), run.is_ghost()); line_glyph_positions.insert(line_glyph_positions.end(), glyph_positions.begin(), @@ -789,7 +805,12 @@ void Paragraph::Layout(double width, bool force) { max_right_ = std::max(max_right_, glyph_positions.back().x_pos.end); } // for each in glyph_blobs - run_x_offset += layout.getAdvance(); + // Do not increase x offset for trailing ghost runs as it should not + // impact the layout of visible glyphs. We do keep the record though so + // GetRectsForRange() can find metrics for trailing spaces. + if (!run.is_ghost()) { + run_x_offset += layout.getAdvance(); + } } // for each in line_runs // Adjust the glyph positions based on the alignment of the line. @@ -811,23 +832,6 @@ void Paragraph::Layout(double width, bool force) { code_unit_runs_.insert(code_unit_runs_.end(), line_code_unit_runs.begin(), line_code_unit_runs.end()); - // Add extra empty metrics for skipped whitespace at line end. This allows - // GetRectsForRange to properly draw empty rects at the ends of lines with - // truncated whitespace. - if (line_end_index < line_range.end && !line_code_unit_runs.empty()) { - std::vector empty_glyph_positions; - double end_x = line_code_unit_runs.back().positions.back().x_pos.end; - for (size_t index = line_end_index; index < line_range.end; ++index) { - empty_glyph_positions.emplace_back(end_x, 0, index, 1); - } - code_unit_runs_.emplace_back( - std::move(empty_glyph_positions), - Range(line_end_index, line_range.end), - Range(end_x, end_x), line_code_unit_runs.back().line_number, - line_code_unit_runs.back().font_metrics, - line_code_unit_runs.back().direction); - } - // Calculate the amount to advance in the y direction. This is done by // computing the maximum ascent and descent with respect to the strut. double max_ascent = strut.ascent + strut.half_leading; @@ -1020,6 +1024,9 @@ void Paragraph::PaintDecorations(SkCanvas* canvas, if (record.style().decoration == TextDecoration::kNone) return; + if (record.isGhost()) + return; + const SkFontMetrics& metrics = record.metrics(); SkPaint paint; paint.setStyle(SkPaint::kStroke_Style); diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.h b/engine/src/flutter/third_party/txt/src/txt/paragraph.h index 2866eeaf80..0518bac033 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.h @@ -272,19 +272,31 @@ class Paragraph { class BidiRun { public: + // Constructs a BidiRun with is_ghost defaulted to false. BidiRun(size_t s, size_t e, TextDirection d, const TextStyle& st) - : start_(s), end_(e), direction_(d), style_(&st) {} + : start_(s), end_(e), direction_(d), style_(&st), is_ghost_(false) {} + + // Constructs a BidiRun with a custom is_ghost flag. + BidiRun(size_t s, + size_t e, + TextDirection d, + const TextStyle& st, + bool is_ghost) + : start_(s), end_(e), direction_(d), style_(&st), is_ghost_(is_ghost) {} size_t start() const { return start_; } size_t end() const { return end_; } TextDirection direction() const { return direction_; } const TextStyle& style() const { return *style_; } bool is_rtl() const { return direction_ == TextDirection::rtl; } + // Tracks if the run represents trailing whitespace. + bool is_ghost() const { return is_ghost_; } private: size_t start_, end_; TextDirection direction_; const TextStyle* style_; + bool is_ghost_; }; struct GlyphPosition { diff --git a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc index 5e37ae1d33..0c2f9df3dd 100644 --- a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc @@ -419,7 +419,8 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(RightAlignParagraph)) { ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull); ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull); ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style)); - ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines); + // Two records for each due to 'ghost' trailing whitespace run. + ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines * 2); double expected_y = 24; ASSERT_TRUE(paragraph->records_[0].style().equals(text_style)); @@ -431,15 +432,6 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(RightAlignParagraph)) { paragraph->breaker_.getWidths()[paragraph->records_[0].line()], 2.0); - ASSERT_TRUE(paragraph->records_[1].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y); - expected_y += 30; - ASSERT_NEAR( - paragraph->records_[1].offset().x(), - paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[1].line()], - 2.0); - ASSERT_TRUE(paragraph->records_[2].style().equals(text_style)); ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y); expected_y += 30; @@ -449,21 +441,30 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(RightAlignParagraph)) { paragraph->breaker_.getWidths()[paragraph->records_[2].line()], 2.0); - ASSERT_TRUE(paragraph->records_[3].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y); - expected_y += 30 * 10; + ASSERT_TRUE(paragraph->records_[4].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[4].offset().y(), expected_y); + expected_y += 30; ASSERT_NEAR( - paragraph->records_[3].offset().x(), + paragraph->records_[4].offset().x(), paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[3].line()], + paragraph->breaker_.getWidths()[paragraph->records_[4].line()], 2.0); - ASSERT_TRUE(paragraph->records_[13].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y); + ASSERT_TRUE(paragraph->records_[6].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[6].offset().y(), expected_y); + expected_y += 30 * 10; ASSERT_NEAR( - paragraph->records_[13].offset().x(), + paragraph->records_[6].offset().x(), paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[13].line()], + paragraph->breaker_.getWidths()[paragraph->records_[6].line()], + 2.0); + + ASSERT_TRUE(paragraph->records_[26].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[26].offset().y(), expected_y); + ASSERT_NEAR( + paragraph->records_[26].offset().x(), + paragraph->width_ - + paragraph->breaker_.getWidths()[paragraph->records_[26].line()], 2.0); ASSERT_EQ(paragraph_style.text_align, @@ -528,7 +529,8 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(CenterAlignParagraph)) { ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull); ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull); ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style)); - ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines); + // Two records for each due to 'ghost' trailing whitespace run. + ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines * 2); double expected_y = 24; ASSERT_TRUE(paragraph->records_[0].style().equals(text_style)); @@ -540,15 +542,6 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(CenterAlignParagraph)) { 2, 2.0); - ASSERT_TRUE(paragraph->records_[1].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y); - expected_y += 30; - ASSERT_NEAR(paragraph->records_[1].offset().x(), - (paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[1].line()]) / - 2, - 2.0); - ASSERT_TRUE(paragraph->records_[2].style().equals(text_style)); ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y); expected_y += 30; @@ -558,21 +551,30 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(CenterAlignParagraph)) { 2, 2.0); - ASSERT_TRUE(paragraph->records_[3].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y); - expected_y += 30 * 10; - ASSERT_NEAR(paragraph->records_[3].offset().x(), + ASSERT_TRUE(paragraph->records_[4].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[4].offset().y(), expected_y); + expected_y += 30; + ASSERT_NEAR(paragraph->records_[4].offset().x(), (paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[3].line()]) / + paragraph->breaker_.getWidths()[paragraph->records_[4].line()]) / 2, 2.0); - ASSERT_TRUE(paragraph->records_[13].style().equals(text_style)); - ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y); + ASSERT_TRUE(paragraph->records_[6].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[6].offset().y(), expected_y); + expected_y += 30 * 10; + ASSERT_NEAR(paragraph->records_[6].offset().x(), + (paragraph->width_ - + paragraph->breaker_.getWidths()[paragraph->records_[6].line()]) / + 2, + 2.0); + + ASSERT_TRUE(paragraph->records_[26].style().equals(text_style)); + ASSERT_DOUBLE_EQ(paragraph->records_[26].offset().y(), expected_y); ASSERT_NEAR( - paragraph->records_[13].offset().x(), + paragraph->records_[26].offset().x(), (paragraph->width_ - - paragraph->breaker_.getWidths()[paragraph->records_[13].line()]) / + paragraph->breaker_.getWidths()[paragraph->records_[26].line()]) / 2, 2.0); @@ -1760,7 +1762,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeCenterParagraph)) { EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 346.04492); EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); - EXPECT_FLOAT_EQ(boxes[0].rect.right(), 346.04492); + EXPECT_FLOAT_EQ(boxes[0].rect.right(), 358.49414); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59); paint.setColor(SK_ColorRED); @@ -1868,7 +1870,7 @@ TEST_F(ParagraphTest, EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 346.04492); EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625); - EXPECT_FLOAT_EQ(boxes[0].rect.right(), 346.04492); + EXPECT_FLOAT_EQ(boxes[0].rect.right(), 358.49414); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59); paint.setColor(SK_ColorBLACK); @@ -1892,7 +1894,7 @@ TEST_F(ParagraphTest, EXPECT_EQ(boxes.size(), 1ull); EXPECT_FLOAT_EQ(boxes[0].rect.left(), 331.83594); EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59.40625); - EXPECT_FLOAT_EQ(boxes[0].rect.right(), 331.83594); + EXPECT_FLOAT_EQ(boxes[0].rect.right(), 419.18359); EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 118); paint.setColor(SK_ColorRED); @@ -2413,7 +2415,7 @@ TEST_F(ParagraphTest, Ellipsize) { // Check that the ellipsizer limited the text to one line and did not wrap // to a second line. - ASSERT_EQ(paragraph->records_.size(), 1ull); + ASSERT_EQ(paragraph->records_.size(), 2ull); } // Test for shifting when identical runs of text are built as multiple runs.