From b5d528f2793cc656919f47ae2793d8b88615dc30 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Wed, 21 Jun 2017 15:22:27 -0700 Subject: [PATCH] Implemented early version of text alignment/justification and baselines. Change-Id: Id36a20bb9c392f09813175677776d9367d8ccfc2 --- engine/src/flutter/src/paint_record.cc | 13 ++-- engine/src/flutter/src/paint_record.h | 9 ++- engine/src/flutter/src/paragraph.cc | 65 +++++++++++++++---- engine/src/flutter/src/paragraph.h | 4 +- .../flutter/tests/txt/paragraph_unittests.cc | 3 + 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/engine/src/flutter/src/paint_record.cc b/engine/src/flutter/src/paint_record.cc index e360e55a43..07322ce05a 100644 --- a/engine/src/flutter/src/paint_record.cc +++ b/engine/src/flutter/src/paint_record.cc @@ -24,22 +24,26 @@ PaintRecord::~PaintRecord() = default; PaintRecord::PaintRecord(TextStyle style, SkPoint offset, sk_sp text, - SkPaint::FontMetrics metrics) + SkPaint::FontMetrics metrics, + int line) : style_(style), offset_(offset), text_(std::move(text)), - metrics_(metrics) {} + metrics_(metrics), + line_(line) {} PaintRecord::PaintRecord(TextStyle style, sk_sp text, - SkPaint::FontMetrics metrics) - : style_(style), text_(std::move(text)), metrics_(metrics) {} + SkPaint::FontMetrics metrics, + int line) + : style_(style), text_(std::move(text)), metrics_(metrics), line_(line) {} PaintRecord::PaintRecord(PaintRecord&& other) { style_ = other.style_; offset_ = other.offset_; text_ = std::move(other.text_); metrics_ = other.metrics_; + line_ = other.line_; } PaintRecord& PaintRecord::operator=(PaintRecord&& other) { @@ -47,6 +51,7 @@ PaintRecord& PaintRecord::operator=(PaintRecord&& other) { offset_ = other.offset_; text_ = std::move(other.text_); metrics_ = other.metrics_; + line_ = other.line_; return *this; } diff --git a/engine/src/flutter/src/paint_record.h b/engine/src/flutter/src/paint_record.h index f9434ed163..cc5b82807a 100644 --- a/engine/src/flutter/src/paint_record.h +++ b/engine/src/flutter/src/paint_record.h @@ -34,11 +34,13 @@ class PaintRecord { PaintRecord(TextStyle style, SkPoint offset, sk_sp text, - SkPaint::FontMetrics metrics); + SkPaint::FontMetrics metrics, + int line); PaintRecord(TextStyle style, sk_sp text, - SkPaint::FontMetrics metrics); + SkPaint::FontMetrics metrics, + int line); PaintRecord(PaintRecord&& other); @@ -54,11 +56,14 @@ class PaintRecord { const TextStyle& style() const { return style_; } + double line() const { return line_; } + private: TextStyle style_; SkPoint offset_; sk_sp text_; SkPaint::FontMetrics metrics_; + int line_; FTL_DISALLOW_COPY_AND_ASSIGN(PaintRecord); }; diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index cb2d7d99e7..474632c1b7 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -158,21 +158,22 @@ void Paragraph::Layout(double width, // Reset member variables so Layout still works when called more than once max_intrinsic_width_ = 0.0f; lines_ = 0; - y_ = y_offset; SkScalar x = x_offset; + SkScalar y = y_offset; size_t break_index = 0; double letter_spacing_offset = 0.0f; double word_spacing_offset = 0.0f; double max_line_spacing = 0.0f; double max_descent = 0.0f; double prev_max_descent = 0.0f; + double line_width = 0.0f; std::vector x_queue; - auto flush = [this, &x_queue]() -> void { + auto flush = [this, &x_queue, &y]() -> void { for (size_t i = 0; i < x_queue.size(); ++i) { records_[records_.size() - (x_queue.size() - i)].SetOffset( - SkPoint::Make(x_queue[i], y_)); + SkPoint::Make(x_queue[i], y)); } x_queue.clear(); }; @@ -240,13 +241,18 @@ void Paragraph::Layout(double width, // Subtract word offset to avoid big gap at end of run. This my be // removed depending on the specificatins for word spacing. word_spacing_offset -= run.style.word_spacing; + // TODO(abarth): We could keep the same SkTextBlobBuilder as long as the // color stayed the same. // TODO(garyq): Ensure that the typeface does not change throughout a // run. SkPaint::FontMetrics metrics; paint.getFontMetrics(&metrics); - records_.push_back(PaintRecord{run.style, builder.make(), metrics}); + records_.push_back( + PaintRecord{run.style, builder.make(), metrics, lines_}); + line_width += + std::abs(records_[records_.size() - 1].text()->bounds().fRight + + records_[records_.size() - 1].text()->bounds().fLeft); // Must adjust each line to the largest text in the line, so cannot // directly push the offset property of PaintRecord until line is @@ -254,15 +260,25 @@ void Paragraph::Layout(double width, x_queue.push_back(x); if (max_line_spacing < - (-metrics.fAscent + metrics.fLeading) * run.style.height) - max_line_spacing = - (-metrics.fAscent + metrics.fLeading) * run.style.height; + (-metrics.fAscent + metrics.fLeading) * run.style.height) { + max_line_spacing = lines_ == 0 ? -metrics.fAscent * run.style.height + : (-metrics.fAscent + metrics.fLeading) * + run.style.height; + // Record the alphabetic_baseline_: + if (lines_ == 0) { + alphabetic_baseline_ = -metrics.fAscent * run.style.height; + // TODO(garyq): Properly implement ideographic_baseline_. + ideographic_baseline_ = + ((metrics.fDescent / 2) - metrics.fAscent) * run.style.height; + } + } if (max_descent < metrics.fDescent * run.style.height) max_descent = metrics.fDescent * run.style.height; if (layout_end == next_break) { - y_ += max_line_spacing + prev_max_descent; + y += max_line_spacing + prev_max_descent; prev_max_descent = max_descent; + line_widths_.push_back(line_width); flush(); max_line_spacing = 0.0f; @@ -270,6 +286,7 @@ void Paragraph::Layout(double width, x = 0.0f; letter_spacing_offset = 0.0f; word_spacing_offset = 0.0f; + line_width = 0.0f; // TODO(abarth): Use the line height, which is something like the max // font_size for runs in this line times the paragraph's line height. break_index += 1; @@ -281,8 +298,12 @@ void Paragraph::Layout(double width, layout_start = layout_end; } } - y_ += max_line_spacing; + y += max_line_spacing; flush(); + if (line_width != 0) + line_widths_.push_back(line_width); + + height_ = y + max_descent; } const ParagraphStyle& Paragraph::GetParagraphStyle() const { @@ -290,13 +311,12 @@ const ParagraphStyle& Paragraph::GetParagraphStyle() const { } double Paragraph::GetAlphabeticBaseline() const { - // TODO(garyq): Implement. - return FLT_MAX; + return alphabetic_baseline_; } double Paragraph::GetIdeographicBaseline() const { // TODO(garyq): Implement. - return FLT_MAX; + return ideographic_baseline_; } double Paragraph::GetMaxIntrinsicWidth() const { @@ -309,7 +329,7 @@ double Paragraph::GetMinIntrinsicWidth() const { } double Paragraph::GetHeight() const { - return y_; + return height_; } void Paragraph::SetParagraphStyle(const ParagraphStyle& style) { @@ -320,7 +340,24 @@ void Paragraph::Paint(SkCanvas* canvas, double x, double y) { for (const auto& record : records_) { SkPaint paint; paint.setColor(record.style().color); - const SkPoint& offset = record.offset(); + 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_ - line_widths_[record.line()], 0); + break; + } + case TextAlign::center: { + offset.offset((width_ - line_widths_[record.line()]) / 2, 0); + break; + } + case TextAlign::justify: { + // TODO(garyq): implement justify. + 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/paragraph.h b/engine/src/flutter/src/paragraph.h index 1b128481fd..a4e8529e51 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -75,14 +75,16 @@ class Paragraph { StyledRuns runs_; minikin::LineBreaker breaker_; std::vector records_; + std::vector line_widths_; ParagraphStyle paragraph_style_; - SkScalar y_ = 0.0f; // TODO(garyq): Height of the paragraph after Layout(). SkScalar height_ = 0.0f; double width_ = 0.0f; int lines_ = 1; double max_intrinsic_width_ = 0.0f; double min_intrinsic_width_ = 0.0f; + double alphabetic_baseline_ = FLT_MAX; + double ideographic_baseline_ = FLT_MAX; void SetText(std::vector text, StyledRuns runs); diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 941d6cd8c9..463d18e5b0 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -18,6 +18,7 @@ #include "lib/txt/src/font_style.h" #include "lib/txt/src/font_weight.h" #include "lib/txt/src/paragraph.h" +#include "lib/txt/src/text_align.h" #include "lib/txt/tests/txt/utils.h" #include "paragraph_builder.h" #include "render_test.h" @@ -117,6 +118,7 @@ TEST_F(RenderTest, RainbowParagraph) { txt::ParagraphStyle paragraph_style; paragraph_style.max_lines = 2; + paragraph_style.text_align = TextAlign::left; txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style1; @@ -285,6 +287,7 @@ TEST_F(RenderTest, LinebreakParagraph) { txt::ParagraphStyle paragraph_style; paragraph_style.max_lines = 14; + paragraph_style.text_align = TextAlign::center; txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style;