Rework the height and spacing, add comments.

Change-Id: I5ad517b0e1aa3938da63c4069880768477b788ac
This commit is contained in:
Gary Qian
2017-07-13 10:24:41 -07:00
parent 8c0962d728
commit 18ed4e65c9
7 changed files with 68 additions and 17 deletions

View File

@@ -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<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).
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
const std::string& family);
@@ -70,10 +76,16 @@ class FontCollection {
// Provides a set of all available family names.
std::set<std::string> 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 <cap> 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

View File

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

View File

@@ -299,6 +299,7 @@ void Paragraph::Layout(double width, bool force) {
buffers = std::vector<const SkTextBlobBuilder::RunBuffer*>();
buffer_sizes = std::vector<size_t>();
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) {

View File

@@ -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<SkRect> 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<std::string, std::shared_ptr<minikin::FontCollection>>&
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<const SkTextBlobBuilder::RunBuffer*>& buffers,

View File

@@ -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<Paragraph> Build();
private:

View File

@@ -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 {

View File

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