Min and Max Intrinsic Widths look mostly correct now.

Change-Id: I0201c88bab4a46baf5164def8cccd51f4c5486c1
This commit is contained in:
Gary Qian
2017-07-17 14:40:43 -07:00
parent 1a68f70b47
commit 69b2d8180a
4 changed files with 44 additions and 11 deletions

View File

@@ -401,11 +401,10 @@ void Paragraph::Layout(double width, bool force) {
max_line_spacing = temp_line_spacing; max_line_spacing = temp_line_spacing;
// Record the alphabetic_baseline_: // Record the alphabetic_baseline_:
if (lines_ == 0) { if (lines_ == 0) {
alphabetic_baseline_ = metrics.fCapHeight * run.style.height; alphabetic_baseline_ = -metrics.fAscent * run.style.height;
// TODO(garyq): Properly implement ideographic_baseline_. // TODO(garyq): Properly implement ideographic_baseline_.
ideographic_baseline_ = ideographic_baseline_ =
(metrics.fUnderlinePosition + metrics.fCapHeight) * (metrics.fUnderlinePosition - metrics.fAscent) * run.style.height;
run.style.height;
} }
} }
temp_line_spacing = metrics.fDescent * 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) { buffer_sizes.size() > 0) {
JustifyLine(buffers, buffer_sizes, word_count, justify_spacing, -1); JustifyLine(buffers, buffer_sizes, word_count, justify_spacing, -1);
} }
line_widths_ =
std::vector<double>(breaker_.getWidths(), breaker_.getWidths() + lines_);
CalculateIntrinsicWidths(); CalculateIntrinsicWidths();
} }
@@ -509,12 +510,25 @@ double Paragraph::GetIdeographicBaseline() const {
} }
void Paragraph::CalculateIntrinsicWidths() { void Paragraph::CalculateIntrinsicWidths() {
for (size_t i = 0; i < word_widths_.size(); ++i) { // TODO(garyq): Investigate correctness of the following implementation of max
max_intrinsic_width_ += word_widths_[i]; // 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! // TODO(garyq): Investigate correctness of the following implementation of max
min_intrinsic_width_ = max_intrinsic_width_ / paragraph_style_.max_lines; // 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 { double Paragraph::GetMaxIntrinsicWidth() const {
@@ -534,6 +548,18 @@ double Paragraph::GetHeight() const {
return line_heights_[line_heights_.size() - 2]; 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) { void Paragraph::SetParagraphStyle(const ParagraphStyle& style) {
needs_layout_ = true; needs_layout_ = true;
paragraph_style_ = style; paragraph_style_ = style;

View File

@@ -53,6 +53,12 @@ class Paragraph {
double GetHeight() const; 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 GetAlphabeticBaseline() const;
double GetIdeographicBaseline() const; double GetIdeographicBaseline() const;
@@ -115,8 +121,8 @@ class Paragraph {
SkScalar height_ = 0.0f; SkScalar height_ = 0.0f;
double width_ = 0.0f; double width_ = 0.0f;
size_t lines_ = 0; size_t lines_ = 0;
double max_intrinsic_width_ = -1; double max_intrinsic_width_ = 0;
double min_intrinsic_width_ = -1; double min_intrinsic_width_ = 0;
// TODO(garyq): Instead of using whitespace to delimit "words", use the // TODO(garyq): Instead of using whitespace to delimit "words", use the
// results of minikin breaker. // results of minikin breaker.
std::vector<double> word_widths_; std::vector<double> word_widths_;

View File

@@ -17,6 +17,7 @@
#ifndef LIB_TXT_SRC_PARAGRAPH_STYLE_H_ #ifndef LIB_TXT_SRC_PARAGRAPH_STYLE_H_
#define LIB_TXT_SRC_PARAGRAPH_STYLE_H_ #define LIB_TXT_SRC_PARAGRAPH_STYLE_H_
#include <climits>
#include <string> #include <string>
#include "lib/txt/src/font_style.h" #include "lib/txt/src/font_style.h"
@@ -32,7 +33,7 @@ class ParagraphStyle {
FontStyle font_style = FontStyle::normal; FontStyle font_style = FontStyle::normal;
std::string font_family = ""; std::string font_family = "";
double font_size = 14; double font_size = 14;
size_t max_lines = 1; size_t max_lines = UINT_MAX;
double line_height = 1.0; double line_height = 1.0;
std::string ellipsis = "..."; std::string ellipsis = "...";
// TODO(garyq): Implement right to left. // TODO(garyq): Implement right to left.

View File

@@ -1025,7 +1025,7 @@ TEST_F(RenderTest, GetRectsForRangeParagraph) {
// end of paragraph handling needs to be fixed in a later patch. // end of paragraph handling needs to be fixed in a later patch.
EXPECT_FLOAT_EQ(rects[16].left(), 0); EXPECT_FLOAT_EQ(rects[16].left(), 0);
EXPECT_FLOAT_EQ(rects[16].top(), 234.375); 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); EXPECT_FLOAT_EQ(rects[16].bottom(), 292.96875);
ASSERT_TRUE(Snapshot()); ASSERT_TRUE(Snapshot());