Support handling '\n' characters.

Change-Id: I9d8c25df97e600ee3679f0bf31b0a4bb8df9eb6d
This commit is contained in:
Gary Qian
2017-07-27 13:01:21 -07:00
parent 8a5babb292
commit 95bda714a0
8 changed files with 132 additions and 3 deletions

View File

@@ -156,6 +156,7 @@ float LineBreaker::addStyleRun(MinikinPaint* paint, const std::shared_ptr<FontCo
} else {
if (isWordSpace(c)) mSpaceCount += 1;
mWidth += mCharWidths[i];
if (c == '\n') mWidth += INT_MAX;
if (!isLineEndSpace(c)) {
postBreak = mWidth;
postSpaceCount = mSpaceCount;

View File

@@ -190,6 +190,10 @@ void Paragraph::Layout(double width, bool force) {
size_t breaks_count = breaker_.computeBreaks();
const int* breaks = breaker_.getBreaks();
// Create a copy of text_ to use locally so that any changes made to the
// vector (such as removing newline characters) is not permanent.
std::vector<uint16_t> 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<SkRect> Paragraph::GetRectsForRange(size_t start,
size_t end) const {
FTL_DCHECK(end >= start && end >= 0 && start >= 0);
std::vector<SkRect> 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());

View File

@@ -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<uint16_t> text_;

View File

@@ -15,6 +15,8 @@
*/
#include "lib/ftl/logging.h"
#include <list>
#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<size_t> 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<Paragraph> ParagraphBuilder::Build() {
if (font_collection_ == nullptr) {
// Will be deprecated when full compatibility with Flutter Engine is
@@ -86,6 +99,9 @@ std::unique_ptr<Paragraph> ParagraphBuilder::Build() {
}
runs_.EndRunIfNeeded(text_.size());
SplitNewlineRuns();
std::unique_ptr<Paragraph> paragraph = std::make_unique<Paragraph>();
paragraph->SetText(std::move(text_), std::move(runs_));
paragraph->SetParagraphStyle(paragraph_style_);

View File

@@ -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);
};

View File

@@ -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<size_t> newline_positions) {
std::vector<IndexedRun> 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

View File

@@ -17,6 +17,7 @@
#ifndef LIB_TXT_SRC_STYLED_RUNS_H_
#define LIB_TXT_SRC_STYLED_RUNS_H_
#include <list>
#include <vector>
#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<size_t> newline_positions);
private:
FRIEND_TEST(RenderTest, SimpleParagraph);
FRIEND_TEST(RenderTest, SimpleRedParagraph);

View File

@@ -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