From 95bda714a0a15d1609966517e71e0e6a2ff45098 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 27 Jul 2017 13:01:21 -0700 Subject: [PATCH] Support handling '\n' characters. Change-Id: I9d8c25df97e600ee3679f0bf31b0a4bb8df9eb6d --- .../src/flutter/libs/minikin/LineBreaker.cpp | 1 + engine/src/flutter/src/paragraph.cc | 15 ++++- engine/src/flutter/src/paragraph.h | 1 + engine/src/flutter/src/paragraph_builder.cc | 16 ++++++ engine/src/flutter/src/paragraph_builder.h | 4 ++ engine/src/flutter/src/styled_runs.cc | 38 +++++++++++++ engine/src/flutter/src/styled_runs.h | 4 ++ .../flutter/tests/txt/paragraph_unittests.cc | 56 +++++++++++++++++++ 8 files changed, 132 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/libs/minikin/LineBreaker.cpp b/engine/src/flutter/libs/minikin/LineBreaker.cpp index 661a8fc30e..2a29989ff9 100644 --- a/engine/src/flutter/libs/minikin/LineBreaker.cpp +++ b/engine/src/flutter/libs/minikin/LineBreaker.cpp @@ -156,6 +156,7 @@ float LineBreaker::addStyleRun(MinikinPaint* paint, const std::shared_ptr text(text_); + SkPaint paint; paint.setAntiAlias(true); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); @@ -293,6 +297,11 @@ void Paragraph::Layout(double width, bool force) { }; for (size_t run_index = 0; run_index < runs_.size(); ++run_index) { auto run = runs_.GetRun(run_index); + bool is_newline = text_[run.start] == '\n' && run.end - run.start == 1; + // Replace '\n' with a null character so that a 'missing glyph' box is not + // drawn. + if (is_newline) + text[run.start] = '\0'; GetFontAndMinikinPaint(run.style, &font, &minikin_paint); GetPaint(run.style, &paint); @@ -311,7 +320,7 @@ void Paragraph::Layout(double width, bool force) { // However, this is not significant for reasonably sized paragraphs. It is // currently recommended to break up very long paragraphs (10k+ // characters) to ensure speedy layout. - layout.doLayout(text_.data() + layout_start, 0, layout_end - layout_start, + layout.doLayout(text.data() + layout_start, 0, layout_end - layout_start, layout_end - layout_start, bidiFlags, font, minikin_paint, font_collection_->GetMinikinFontCollectionForFamily( run.style.font_family)); @@ -419,7 +428,7 @@ void Paragraph::Layout(double width, bool force) { if (max_descent < temp_line_spacing) max_descent = temp_line_spacing; - if (layout_end == next_break) { + if (layout_end == next_break || is_newline) { y += max_line_spacing + prev_max_descent; line_heights_.push_back( (line_heights_.empty() ? 0 : line_heights_.back()) + @@ -769,10 +778,10 @@ void Paragraph::PaintWavyDecoration(SkCanvas* canvas, std::vector Paragraph::GetRectsForRange(size_t start, size_t end) const { - FTL_DCHECK(end >= start && end >= 0 && start >= 0); std::vector rects; end = fmax(start, end); start = fmin(start, end); + FTL_DCHECK(end >= start && end >= 0 && start >= 0); if (end == start) end = start + 1; end = fmin(end, text_.size()); diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index e6bde8dd57..21bb722716 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -153,6 +153,7 @@ class Paragraph { FRIEND_TEST(RenderTest, SpacingParagraph); FRIEND_TEST(RenderTest, LongWordParagraph); FRIEND_TEST(RenderTest, KernParagraph); + FRIEND_TEST(RenderTest, NewlineParagraph); // Starting data to layout. std::vector text_; diff --git a/engine/src/flutter/src/paragraph_builder.cc b/engine/src/flutter/src/paragraph_builder.cc index 31d18ad95f..0cb1170965 100644 --- a/engine/src/flutter/src/paragraph_builder.cc +++ b/engine/src/flutter/src/paragraph_builder.cc @@ -15,6 +15,8 @@ */ #include "lib/ftl/logging.h" +#include + #include "lib/txt/src/paragraph_builder.h" #include "lib/txt/src/paragraph_style.h" #include "third_party/icu/source/common/unicode/unistr.h" @@ -78,6 +80,17 @@ void ParagraphBuilder::AddText(const char* text) { AddText(u16_text); } +void ParagraphBuilder::SplitNewlineRuns() { + std::list newline_positions; + for (size_t i = 0; i < text_.size(); ++i) { + if (text_[i] == '\n') { + newline_positions.push_back(i); + } + } + if (newline_positions.size() > 0) + runs_.SplitNewlineRuns(newline_positions); +} + std::unique_ptr ParagraphBuilder::Build() { if (font_collection_ == nullptr) { // Will be deprecated when full compatibility with Flutter Engine is @@ -86,6 +99,9 @@ std::unique_ptr ParagraphBuilder::Build() { } runs_.EndRunIfNeeded(text_.size()); + + SplitNewlineRuns(); + std::unique_ptr paragraph = std::make_unique(); paragraph->SetText(std::move(text_), std::move(runs_)); paragraph->SetParagraphStyle(paragraph_style_); diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h index 7055894e8c..df756672a7 100644 --- a/engine/src/flutter/src/paragraph_builder.h +++ b/engine/src/flutter/src/paragraph_builder.h @@ -83,6 +83,10 @@ class ParagraphBuilder { ParagraphStyle paragraph_style_; FontCollection* font_collection_ = nullptr; + // Break any newline '\n' characters into their own runs. This allows + // Paragraph::Layout to cleanly discover and handle newlines. + void SplitNewlineRuns(); + FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphBuilder); }; diff --git a/engine/src/flutter/src/styled_runs.cc b/engine/src/flutter/src/styled_runs.cc index 3f5f79da62..7e64460c18 100644 --- a/engine/src/flutter/src/styled_runs.cc +++ b/engine/src/flutter/src/styled_runs.cc @@ -16,6 +16,8 @@ #include "lib/txt/src/styled_runs.h" +#include "lib/ftl/logging.h" + namespace txt { StyledRuns::StyledRuns() = default; @@ -65,4 +67,40 @@ StyledRuns::Run StyledRuns::GetRun(size_t index) const { return Run{styles_[run.style_index], run.start, run.end}; } +void StyledRuns::SplitNewlineRuns(std::list newline_positions) { + std::vector result; + for (size_t i = 0; i < runs_.size(); ++i) { + if (runs_[i].end <= newline_positions.front() || + newline_positions.empty()) { + result.push_back(runs_[i]); + } else { + size_t start = runs_[i].start; + size_t end = runs_[i].end; + while (end > newline_positions.front() && !newline_positions.empty() && + start < end) { + IndexedRun temp_run; + temp_run.style_index = runs_[i].style_index; + temp_run.start = start; + temp_run.end = newline_positions.front(); + newline_positions.pop_front(); + result.push_back(temp_run); + + temp_run.start = temp_run.end; + temp_run.end = temp_run.end + 1; + result.push_back(temp_run); + + start = temp_run.end; + } + if (start < end) { + IndexedRun temp_run; + temp_run.style_index = runs_[i].style_index; + temp_run.start = start; + temp_run.end = end; + result.push_back(temp_run); + } + } + } + runs_ = result; +} + } // namespace txt diff --git a/engine/src/flutter/src/styled_runs.h b/engine/src/flutter/src/styled_runs.h index 3fd52ad78c..24ac305700 100644 --- a/engine/src/flutter/src/styled_runs.h +++ b/engine/src/flutter/src/styled_runs.h @@ -17,6 +17,7 @@ #ifndef LIB_TXT_SRC_STYLED_RUNS_H_ #define LIB_TXT_SRC_STYLED_RUNS_H_ +#include #include #include "lib/txt/src/text_style.h" @@ -56,6 +57,9 @@ class StyledRuns { Run GetRun(size_t index) const; + // Break any newline '\n' characters into their own runs. + void SplitNewlineRuns(std::list newline_positions); + private: FRIEND_TEST(RenderTest, SimpleParagraph); FRIEND_TEST(RenderTest, SimpleRedParagraph); diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 98762eace3..974ac595cf 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -1335,4 +1335,60 @@ TEST_F(RenderTest, KernParagraph) { ASSERT_TRUE(Snapshot()); } +TEST_F(RenderTest, NewlineParagraph) { + txt::ParagraphStyle paragraph_style; + paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality; + auto font_collection = FontCollection::GetFontCollection(txt::GetFontDir()); + txt::ParagraphBuilder builder(paragraph_style, &font_collection); + + txt::TextStyle text_style; + text_style.font_family = "Roboto"; + text_style.font_size = 60; + text_style.letter_spacing = 0; + text_style.word_spacing = 0; + text_style.color = SK_ColorBLACK; + text_style.height = 1; + builder.PushStyle(text_style); + builder.AddText( + "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 " + "test1 test2 test3 test4"); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(GetTestCanvasWidth() - 300); + + paragraph->Paint(GetCanvas(), 0, 0); + + ASSERT_TRUE(Snapshot()); + + ASSERT_EQ(paragraph->runs_.size(), 9ull); + ASSERT_EQ(paragraph->runs_.GetRun(1).end - paragraph->runs_.GetRun(1).start, + 1ull); + ASSERT_EQ(paragraph->runs_.GetRun(3).end - paragraph->runs_.GetRun(3).start, + 1ull); + ASSERT_EQ(paragraph->runs_.GetRun(5).end - paragraph->runs_.GetRun(5).start, + 1ull); + ASSERT_EQ(paragraph->runs_.GetRun(7).end - paragraph->runs_.GetRun(7).start, + 1ull); + + ASSERT_EQ(paragraph->records_.size(), 10ull); + EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 129); + EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(), + paragraph->records_[0].offset().y()); + EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 593); + EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(), + paragraph->records_[4].offset().y()); + EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[6].offset().x(), 129); + EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().x(), 0); + EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().y(), 336.9140625); + EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().y(), 407.2265625); + +} + } // namespace txt