API comments and reorder vars.

Change-Id: I9f73047bc5ad453b05083f019623f553d13bbba4
This commit is contained in:
Gary Qian
2017-07-25 17:30:56 -07:00
parent 1946e649c7
commit e2c9992638
6 changed files with 78 additions and 19 deletions

View File

@@ -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<std::string> 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<std::string> 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("");
}

View File

@@ -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<std::string> 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<minikin::FontCollection> GetMinikinFontCollectionForFamily(
const std::string& family);

View File

@@ -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<SkTextBlob> 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.

View File

@@ -213,6 +213,8 @@ void Paragraph::Layout(double width, bool force) {
glyph_position_x_.push_back(std::vector<double>());
std::vector<double> 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;

View File

@@ -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 = paragraph_builder.Build();
// paragraph->Layout(<somewidthgoeshere>);
// paragraph->Paint(<someSkCanvas>, <xpos>, <ypos>);
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<uint16_t> text_;
StyledRuns runs_;
ParagraphStyle paragraph_style_;
FontCollection* font_collection_;
minikin::LineBreaker breaker_;
// Stores the result of Layout().
std::vector<PaintRecord> records_;
std::vector<double> line_widths_;
// TODO(garyq): Can we access this info without redundantly storing it here?
std::vector<double> line_widths_;
std::vector<double> line_heights_;
// Holds the laid out x positions of each glyph, as well as padding to make
// math on it simpler.
std::vector<std::vector<double>> glyph_position_x_;
// Set of glyph IDs that correspond to whitespace.
std::set<GlyphID> 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<std::string, std::shared_ptr<minikin::FontCollection>>&
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<const SkTextBlobBuilder::RunBuffer*>& buffers,
std::vector<size_t>& 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<WaveCoordinates> wave_coords,
SkPaint paint,

View File

@@ -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);