From e2c999263835c76e39e2cfc9a1a680e1ec178eed Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 25 Jul 2017 17:30:56 -0700 Subject: [PATCH] API comments and reorder vars. Change-Id: I9f73047bc5ad453b05083f019623f553d13bbba4 --- engine/src/flutter/src/font_collection.cc | 10 ++-- engine/src/flutter/src/font_collection.h | 16 ++++--- engine/src/flutter/src/paint_record.h | 12 +++-- engine/src/flutter/src/paragraph.cc | 2 + engine/src/flutter/src/paragraph.h | 53 ++++++++++++++++++++-- engine/src/flutter/src/paragraph_builder.h | 4 ++ 6 files changed, 78 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index 38825b6217..ae59ddf639 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -31,14 +31,15 @@ #include "third_party/skia/include/ports/SkFontMgr_directory.h" namespace txt { - -// Will be deprecated when full compatibility with Flutter Engine is complete. +// TODO(garyq): Will be deprecated when full compatibility with Flutter Engine +// is complete. FontCollection& FontCollection::GetFontCollection(std::string dir) { std::vector dirs = {dir}; return GetFontCollection(std::move(dirs)); } -// Will be deprecated when full compatibility with Flutter Engine is complete. +// TODO(garyq): Will be deprecated when full compatibility with Flutter Engine +// is complete. FontCollection& FontCollection::GetFontCollection( std::vector dirs) { static FontCollection* collection = nullptr; @@ -47,7 +48,8 @@ FontCollection& FontCollection::GetFontCollection( return *collection; } -// Will be deprecated when full compatibility with Flutter Engine is complete. +// TODO(garyq): Will be deprecated when full compatibility with Flutter Engine +// is complete. FontCollection& FontCollection::GetDefaultFontCollection() { return GetFontCollection(""); } diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index ac09bc7eca..fb3a3be0bd 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -37,7 +37,8 @@ namespace txt { // FontCollection holds a vector of Skia Font Managers and handles font -// fallback. +// fallback. If no additional font directories are provided, then only the +// default font directory will be available. class FontCollection { public: enum CacheMethod { @@ -45,19 +46,22 @@ class FontCollection { kLRU, // Least Recently Used. kUnlimited, }; - // Will be deprecated when full compatibility with Flutter Engine is complete. + // TODO(garyq): Will be deprecated when full compatibility with Flutter Engine + // is complete. static FontCollection& GetDefaultFontCollection(); - // Will be deprecated when full compatibility with Flutter Engine is complete. + // TODO(garyq): Will be deprecated when full compatibility with Flutter Engine + // is complete.. static FontCollection& GetFontCollection(std::string dir = ""); - // Will be deprecated when full compatibility with Flutter Engine is complete. + // TODO(garyq): 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). + // font is loaded, it is cached and future calls will be very efficient + // (until/if the font is flushed). std::shared_ptr GetMinikinFontCollectionForFamily( const std::string& family); diff --git a/engine/src/flutter/src/paint_record.h b/engine/src/flutter/src/paint_record.h index faf133abe5..e6b2d02874 100644 --- a/engine/src/flutter/src/paint_record.h +++ b/engine/src/flutter/src/paint_record.h @@ -25,6 +25,9 @@ namespace txt { +// PaintRecord holds the layout data after Paragraph::Layout() is called. This +// stores all nessecary offsets, blobs, metrics, and more for Skia to draw the +// text. class PaintRecord { public: PaintRecord() = delete; @@ -50,10 +53,6 @@ class PaintRecord { void SetOffset(SkPoint pt); - void SetWidthModifier(double val); - - double GetWidthModifier() const { return width_modifier_; } - SkTextBlob* text() const { return text_.get(); } const SkPaint::FontMetrics& metrics() const { return metrics_; } @@ -62,10 +61,15 @@ class PaintRecord { size_t line() const { return line_; } + size_t GetRunWidth() const { return run_width_; } + private: TextStyle style_; + // offset_ is the overall offset of the origin of the SkTextBlob. SkPoint offset_; + // SkTextBlob stores the glyphs and coordinates to draw them. sk_sp text_; + // FontMetrics stores the measurements of the font used. SkPaint::FontMetrics metrics_; size_t line_; // Change in width when using a non-left text alignment. diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index f20ad92c2c..daaadaac93 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -213,6 +213,8 @@ void Paragraph::Layout(double width, bool force) { glyph_position_x_.push_back(std::vector()); std::vector glyph_single_line_position_x; glyph_single_line_position_x.push_back(0); + // Track the x of the previous run to maintain accurate xposition when + // multiple SkTextBlobs make up a single line. double previous_run_x_position = 0.0f; SkScalar x = 0.0f; diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index b6d8dd6643..e6bde8dd57 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -37,8 +37,17 @@ namespace txt { using GlyphID = uint32_t; +// Paragraph provides Layout, metrics, and painting capabilites for text. Once a +// Paragraph is constructed with ParagraphBuilder::Build(), an example basic +// workflow can be this: +// +// std::unique_ptr paragraph = paragraph_builder.Build(); +// paragraph->Layout(); +// paragraph->Paint(, , ); class Paragraph { public: + // Constructor. I is highly recommended to construct a paragrph with a + // ParagraphBuilder. Paragraph(); ~Paragraph(); @@ -57,24 +66,38 @@ class Paragraph { // the origin. Only valid after Layout() is called. void Paint(SkCanvas* canvas, double x, double y); + // Getter for paragraph_style_. const ParagraphStyle& GetParagraphStyle() const; + // Returns the number of characters/unicode characters. AKA text_.size() size_t TextSize() const; + // Returns the height of the laid out paragraph. NOTE this is not a tight + // bounding height of the glyphs, as some glyphs do not reach as low as they + // can. 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. + // Returns the width provided in the Layout() method. This is the maximum + // width any line in the laid out paragraph can occupy. We expect that + // GetMaxWidth() >= GetLayoutWidth(). double GetMaxWidth() const; + // Distance from top of paragraph to the Alphabetic baseline of the first + // line. double GetAlphabeticBaseline() const; + // Distance from top of paragraph to the Ideographic baseline of the first + // line. double GetIdeographicBaseline() const; + // Returns the total width covered by the paragraph without linebreaking. double GetMaxIntrinsicWidth() const; + // Currently, calculated similarly to as GetLayoutWidth(), however this is not + // nessecarily 100% correct in all cases. double GetMinIntrinsicWidth() const; // Returns a vector of bounding boxes that enclose all text between start and @@ -98,8 +121,13 @@ class Paragraph { // index offset. SkIPoint GetWordBoundary(size_t offset) const; + // Returns the number of lines the paragraph takes up. If the text exceeds the + // amount width and maxlines provides, Layout() truncates the extra text from + // the layout and this will return the max lines allowed. int GetLineCount() const; + // Checks if the layout extends past the maximum lines and had to be + // truncated. bool DidExceedMaxLines() const; // Sets the needs_layout_ to dirty. When Layout() is called, a new Layout will @@ -126,28 +154,37 @@ class Paragraph { FRIEND_TEST(RenderTest, LongWordParagraph); FRIEND_TEST(RenderTest, KernParagraph); + // Starting data to layout. std::vector text_; StyledRuns runs_; + ParagraphStyle paragraph_style_; + FontCollection* font_collection_; + minikin::LineBreaker breaker_; + + // Stores the result of Layout(). std::vector records_; - std::vector line_widths_; // TODO(garyq): Can we access this info without redundantly storing it here? + std::vector line_widths_; std::vector line_heights_; + // Holds the laid out x positions of each glyph, as well as padding to make + // math on it simpler. std::vector> glyph_position_x_; // Set of glyph IDs that correspond to whitespace. std::set whitespace_set_; - ParagraphStyle paragraph_style_; - FontCollection* font_collection_; - SkScalar height_ = 0.0f; + // The max width of the paragraph as provided in the most recent Layout() + // call. double width_ = 0.0f; + SkScalar height_ = 0.0f; size_t lines_ = 0; double max_intrinsic_width_ = 0; double min_intrinsic_width_ = 0; double alphabetic_baseline_ = FLT_MAX; double ideographic_baseline_ = FLT_MAX; + bool needs_layout_ = true; struct WaveCoordinates { @@ -166,6 +203,9 @@ class Paragraph { void SetFontCollection(FontCollection* font_collection); + // Pass the runs to breaker_. + // NOTE: This is O(N^2) due to minikin breaking being O(N^2) where N = sum of + // all text added using this method. This is insignificant with normal usage. void AddRunsToLineBreaker( std::unordered_map>& collection_map); @@ -175,17 +215,20 @@ class Paragraph { // justifying. void FillWhitespaceSet(size_t start, size_t end, hb_font_t* hb_font); + // Calculates and amends the layout for one line to be justified. void JustifyLine(std::vector& buffers, std::vector& buffer_sizes, int word_count, double& justify_spacing, double multiplier = 1); + // Creates and draws the decorations onto the canvas. void PaintDecorations(SkCanvas* canvas, double x, double y, size_t record_index); + // Calculates wavy decorations and Draws them onto the canvas. void PaintWavyDecoration(SkCanvas* canvas, std::vector wave_coords, SkPaint paint, diff --git a/engine/src/flutter/src/paragraph_builder.h b/engine/src/flutter/src/paragraph_builder.h index 4fbd07b187..7055894e8c 100644 --- a/engine/src/flutter/src/paragraph_builder.h +++ b/engine/src/flutter/src/paragraph_builder.h @@ -56,10 +56,14 @@ class ParagraphBuilder { // builder.AddText(" Back to normal again."); void Pop(); + // Adds text to the builder. Forms the proper runs to use the upper-most style + // on the style_stack_; void AddText(const std::u16string& text); + // Converts to u16string before adding. void AddText(const std::string& text); + // Converts to u16string before adding. void AddText(const char* text); void SetParagraphStyle(const ParagraphStyle& style);