From 69b2d8180ac9aba66bd4e1bb58ec16cc5b6efeee Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Mon, 17 Jul 2017 14:40:43 -0700 Subject: [PATCH] Min and Max Intrinsic Widths look mostly correct now. Change-Id: I0201c88bab4a46baf5164def8cccd51f4c5486c1 --- engine/src/flutter/src/paragraph.cc | 40 +++++++++++++++---- engine/src/flutter/src/paragraph.h | 10 ++++- engine/src/flutter/src/paragraph_style.h | 3 +- .../flutter/tests/txt/paragraph_unittests.cc | 2 +- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index a2d2e03a77..ba8f5ac663 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -401,11 +401,10 @@ void Paragraph::Layout(double width, bool force) { max_line_spacing = temp_line_spacing; // Record the alphabetic_baseline_: if (lines_ == 0) { - alphabetic_baseline_ = metrics.fCapHeight * run.style.height; + alphabetic_baseline_ = -metrics.fAscent * run.style.height; // TODO(garyq): Properly implement ideographic_baseline_. ideographic_baseline_ = - (metrics.fUnderlinePosition + metrics.fCapHeight) * - run.style.height; + (metrics.fUnderlinePosition - metrics.fAscent) * run.style.height; } } temp_line_spacing = metrics.fDescent * run.style.height; @@ -455,6 +454,8 @@ void Paragraph::Layout(double width, bool force) { buffer_sizes.size() > 0) { JustifyLine(buffers, buffer_sizes, word_count, justify_spacing, -1); } + line_widths_ = + std::vector(breaker_.getWidths(), breaker_.getWidths() + lines_); CalculateIntrinsicWidths(); } @@ -509,12 +510,25 @@ double Paragraph::GetIdeographicBaseline() const { } void Paragraph::CalculateIntrinsicWidths() { - for (size_t i = 0; i < word_widths_.size(); ++i) { - max_intrinsic_width_ += word_widths_[i]; + // TODO(garyq): Investigate correctness of the following implementation of max + // intrinsic width. This is currently the sum of all the widths of each line + // after layout. + max_intrinsic_width_ = 0; + for (size_t i = 0; i < line_widths_.size(); ++i) { + max_intrinsic_width_ += line_widths_[i]; } - // TODO(garyq): Implement the DP algorithm version instead of this stand in! - min_intrinsic_width_ = max_intrinsic_width_ / paragraph_style_.max_lines; + // TODO(garyq): Investigate correctness of the following implementation of max + // intrinsic width. This is currently the longest line in the text after + // layout. + min_intrinsic_width_ = 0; + for (size_t i = 0; i < line_widths_.size(); ++i) { + min_intrinsic_width_ = std::max(min_intrinsic_width_, line_widths_[i]); + } + + // Ensure that min < max widths. + min_intrinsic_width_ = std::min(max_intrinsic_width_, min_intrinsic_width_); + max_intrinsic_width_ = std::max(max_intrinsic_width_, min_intrinsic_width_); } double Paragraph::GetMaxIntrinsicWidth() const { @@ -534,6 +548,18 @@ double Paragraph::GetHeight() const { return line_heights_[line_heights_.size() - 2]; } +double Paragraph::GetLayoutWidth() const { + double w = 0; + for (size_t i = 0; i < line_widths_.size(); ++i) { + w = std::max(w, line_widths_[i]); + } + return w; +} + +double Paragraph::GetMaxWidth() const { + return width_; +} + void Paragraph::SetParagraphStyle(const ParagraphStyle& style) { needs_layout_ = true; paragraph_style_ = style; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 870e64f431..0e16de48ec 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -53,6 +53,12 @@ class Paragraph { double GetHeight() const; + // Returns the actual max width of the longest line after Layout(). + double GetLayoutWidth() const; + + // Returns the width provided in the Layout() method. + double GetMaxWidth() const; + double GetAlphabeticBaseline() const; double GetIdeographicBaseline() const; @@ -115,8 +121,8 @@ class Paragraph { SkScalar height_ = 0.0f; double width_ = 0.0f; size_t lines_ = 0; - double max_intrinsic_width_ = -1; - double min_intrinsic_width_ = -1; + double max_intrinsic_width_ = 0; + double min_intrinsic_width_ = 0; // TODO(garyq): Instead of using whitespace to delimit "words", use the // results of minikin breaker. std::vector word_widths_; diff --git a/engine/src/flutter/src/paragraph_style.h b/engine/src/flutter/src/paragraph_style.h index 9f30869a15..b6ae31e060 100644 --- a/engine/src/flutter/src/paragraph_style.h +++ b/engine/src/flutter/src/paragraph_style.h @@ -17,6 +17,7 @@ #ifndef LIB_TXT_SRC_PARAGRAPH_STYLE_H_ #define LIB_TXT_SRC_PARAGRAPH_STYLE_H_ +#include #include #include "lib/txt/src/font_style.h" @@ -32,7 +33,7 @@ class ParagraphStyle { FontStyle font_style = FontStyle::normal; std::string font_family = ""; double font_size = 14; - size_t max_lines = 1; + size_t max_lines = UINT_MAX; double line_height = 1.0; std::string ellipsis = "..."; // TODO(garyq): Implement right to left. diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 7ac89151be..719eeb9103 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -1025,7 +1025,7 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) { // end of paragraph handling needs to be fixed in a later patch. EXPECT_FLOAT_EQ(rects[16].left(), 0); EXPECT_FLOAT_EQ(rects[16].top(), 234.375); - EXPECT_FLOAT_EQ(rects[16].right(), 133.875); + EXPECT_FLOAT_EQ(rects[16].right(), 140); EXPECT_FLOAT_EQ(rects[16].bottom(), 292.96875); ASSERT_TRUE(Snapshot());