From 18ed4e65c98621f2af2157bf5509714642206f88 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 13 Jul 2017 10:24:41 -0700 Subject: [PATCH] Rework the height and spacing, add comments. Change-Id: I5ad517b0e1aa3938da63c4069880768477b788ac --- engine/src/flutter/src/font_collection.h | 12 ++++++++++++ engine/src/flutter/src/font_weight.h | 18 +++++++++--------- engine/src/flutter/src/paragraph.cc | 18 ++++++++++-------- engine/src/flutter/src/paragraph.h | 10 ++++++++++ engine/src/flutter/src/paragraph_builder.h | 17 +++++++++++++++++ engine/src/flutter/src/styled_runs.h | 2 ++ .../flutter/tests/txt/paragraph_unittests.cc | 8 ++++++++ 7 files changed, 68 insertions(+), 17 deletions(-) diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index 865af8b380..ac09bc7eca 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -36,6 +36,8 @@ namespace txt { +// FontCollection holds a vector of Skia Font Managers and handles font +// fallback. class FontCollection { public: enum CacheMethod { @@ -52,6 +54,10 @@ class FontCollection { // Will be deprecated when full compatibility with Flutter Engine is complete. static FontCollection& GetFontCollection(std::vector dirs); + // Provides a pointer to the minikin FontCollection for the given font family. + // If the famly is not in any font manager, this will return a nullptr. Once a + // font is loaded, it is cached and future calls will be very efficient (until + // the font is flushed). std::shared_ptr GetMinikinFontCollectionForFamily( const std::string& family); @@ -70,10 +76,16 @@ class FontCollection { // Provides a set of all available family names. std::set GetFamilyNames(); + // Returns true when the supplied font family exists in any of the font + // managers. bool HasFamily(const std::string family) const; + // Removes all fonts that do not fit in the cache capacity from memory. void FlushCache(); + // When in LRU mode, the cache will only hold the most recently used + // fonts. This may be used when the application becomes low on memory or a + // very large number of fonts are used. void SetCacheCapacity(const size_t cap); // Call this to limit memory usage by cached fonts. SetLowMemoryMode() will diff --git a/engine/src/flutter/src/font_weight.h b/engine/src/flutter/src/font_weight.h index cd1811e14d..c3d6745b7a 100644 --- a/engine/src/flutter/src/font_weight.h +++ b/engine/src/flutter/src/font_weight.h @@ -20,15 +20,15 @@ namespace txt { enum class FontWeight { - w100, - w200, - w300, - w400, - w500, - w600, - w700, - w800, - w900, + w100, // Thin + w200, // Extra-Light + w300, // Light + w400, // Normal/Regular + w500, // Medium + w600, // Semi-bold + w700, // Bold + w800, // Extra-Bold + w900, // Black }; } // namespace txt diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index dce596447c..e1506ea127 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -299,6 +299,7 @@ void Paragraph::Layout(double width, bool force) { buffers = std::vector(); buffer_sizes = std::vector(); word_count = 0; + double temp_line_spacing = 0; while (blob_start < glyph_count) { const size_t blob_length = GetBlobLength(layout, blob_start); buffer_sizes.push_back(blob_length); @@ -378,11 +379,11 @@ void Paragraph::Layout(double width, bool force) { // finished. x_queue.push_back(x); - if (max_line_spacing < - (-metrics.fAscent + metrics.fLeading) * run.style.height) { - max_line_spacing = lines_ == 0 ? metrics.fCapHeight * run.style.height - : (-metrics.fAscent + metrics.fLeading) * - run.style.height; + temp_line_spacing = lines_ == 0 ? metrics.fCapHeight * run.style.height + : (-metrics.fAscent + metrics.fLeading) * + run.style.height; + if (max_line_spacing < temp_line_spacing) { + max_line_spacing = temp_line_spacing; // Record the alphabetic_baseline_: if (lines_ == 0) { alphabetic_baseline_ = metrics.fCapHeight * run.style.height; @@ -392,8 +393,9 @@ void Paragraph::Layout(double width, bool force) { run.style.height; } } - if (max_descent < metrics.fDescent * run.style.height) - max_descent = metrics.fDescent * run.style.height; + temp_line_spacing = metrics.fDescent * run.style.height; + if (max_descent < temp_line_spacing) + max_descent = temp_line_spacing; if (layout_end == next_break) { y += max_line_spacing + prev_max_descent; @@ -504,7 +506,7 @@ size_t Paragraph::TextSize() const { } double Paragraph::GetHeight() const { - return height_; + return line_heights_[line_heights_.size() - 2]; } void Paragraph::SetParagraphStyle(const ParagraphStyle& style) { diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 5bfe723ad4..a0785e647a 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -61,12 +61,19 @@ class Paragraph { double GetMinIntrinsicWidth() const; + // Returns a vector of bounding boxes that enclose all text between start and + // end glyph indexes. std::vector GetRectsForRange(size_t start, size_t end) const; + // Returns the index of the glyph that corresponds to the provided coordinate, + // with the top left corner as the origin, and +y direction as down. size_t GetGlyphPositionAtCoordinate(double dx, double dy) const; + // Returns a bounding box that encloses the glyph at the index pos. SkRect GetCoordinatesForGlyphPosition(size_t pos) const; + // Finds the first and last glyphs that define a word containing the glyph at + // index offset. SkIPoint GetWordBoundary(size_t offset) const; int GetLineCount() const; @@ -134,6 +141,9 @@ class Paragraph { std::unordered_map>& collection_map); + // Calculates the GlyphIDs of all whitespace characters present in the text + // between start and end. THis is used to correctly add extra whitespace when + // justifying. void FillWhitespaceSet(size_t start, size_t end, hb_font_t* hb_font); void JustifyLine(std::vector& buffers, diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h index 07d92e69d6..4fbd07b187 100644 --- a/engine/src/flutter/src/paragraph_builder.h +++ b/engine/src/flutter/src/paragraph_builder.h @@ -39,8 +39,21 @@ class ParagraphBuilder { ~ParagraphBuilder(); + // Push a style to the stack. The corresponding text added with AddText will + // use the top-most style. void PushStyle(const TextStyle& style); + // Remove a style from the stack. Useful to apply different styles to chunks + // of text such as bolding. + // Example: + // builder.PushStyle(normal_style); + // builder.AddText("Hello this is normal. "); + // + // builder.PushStyle(bold_style); + // builder.AddText("And this is BOLD. "); + // + // builder.Pop(); + // builder.AddText(" Back to normal again."); void Pop(); void AddText(const std::u16string& text); @@ -51,8 +64,12 @@ class ParagraphBuilder { void SetParagraphStyle(const ParagraphStyle& style); + // It is recommended to initialize the ParagraphBuilder with a font collection + // as default font collection fallback will be deprecated. void SetFontCollection(FontCollection* font_collection); + // Constructs a Paragraph object that can be used to layout and paint the text + // to a SkCanvas. std::unique_ptr Build(); private: diff --git a/engine/src/flutter/src/styled_runs.h b/engine/src/flutter/src/styled_runs.h index dbaa111db3..d88306d704 100644 --- a/engine/src/flutter/src/styled_runs.h +++ b/engine/src/flutter/src/styled_runs.h @@ -24,6 +24,8 @@ namespace txt { +// This holds and handles the start/end positions of discrete chunks of text +// that use different styles (a 'run'). class StyledRuns { public: struct Run { diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 54f64c58ca..0f2b0ac244 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -348,6 +348,8 @@ TEST_F(RenderTest, LeftAlignParagraph) { ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 30), 68ull); ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 60), 134ull); ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(2000, 30), 133ull); + + ASSERT_TRUE(Snapshot()); } TEST_F(RenderTest, RightAlignParagraph) { @@ -397,6 +399,8 @@ TEST_F(RenderTest, RightAlignParagraph) { paragraph->Layout(GetTestCanvasWidth() - 100); paragraph->Paint(GetCanvas(), 0, 0); + + ASSERT_TRUE(Snapshot()); ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); for (size_t i = 0; i < u16_text.length(); i++) { ASSERT_EQ(paragraph->text_[i], u16_text[i]); @@ -499,6 +503,8 @@ TEST_F(RenderTest, CenterAlignParagraph) { paragraph->Layout(GetTestCanvasWidth() - 100); paragraph->Paint(GetCanvas(), 0, 0); + + ASSERT_TRUE(Snapshot()); ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); for (size_t i = 0; i < u16_text.length(); i++) { ASSERT_EQ(paragraph->text_[i], u16_text[i]); @@ -603,6 +609,8 @@ TEST_F(RenderTest, JustifyAlignParagraph) { paragraph->Layout(GetTestCanvasWidth() - 100); paragraph->Paint(GetCanvas(), 0, 0); + + ASSERT_TRUE(Snapshot()); ASSERT_EQ(paragraph->text_.size(), std::string{text}.length()); for (size_t i = 0; i < u16_text.length(); i++) { ASSERT_EQ(paragraph->text_[i], u16_text[i]);