From 3f5ae2f4a39d0acbe5e5c44d3267d31781403573 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 20 Jun 2017 16:22:44 -0700 Subject: [PATCH] Add support for underline, overline, and strikethrough. Change-Id: Id98ecb8409be69d9de44dc931ed6143163469c27 --- engine/src/flutter/src/paint_record.cc | 15 +++- engine/src/flutter/src/paint_record.h | 16 +++- engine/src/flutter/src/paragraph.cc | 85 ++++++++++++++++--- engine/src/flutter/src/paragraph.h | 15 +++- .../flutter/tests/txt/paragraph_unittests.cc | 9 +- 5 files changed, 116 insertions(+), 24 deletions(-) diff --git a/engine/src/flutter/src/paint_record.cc b/engine/src/flutter/src/paint_record.cc index 226b803d42..8740ee8ea4 100644 --- a/engine/src/flutter/src/paint_record.cc +++ b/engine/src/flutter/src/paint_record.cc @@ -15,26 +15,33 @@ */ #include "lib/txt/src/paint_record.h" +#include "lib/ftl/logging.h" namespace txt { -PaintRecord::PaintRecord() = default; - PaintRecord::~PaintRecord() = default; -PaintRecord::PaintRecord(SkColor color, SkPoint offset, sk_sp text) - : color_(color), offset_(offset), text_(std::move(text)) {} +PaintRecord::PaintRecord(SkColor color, + SkPoint offset, + sk_sp text, + SkPaint::FontMetrics metrics) + : color_(color), + offset_(offset), + text_(std::move(text)), + metrics_(metrics) {} PaintRecord::PaintRecord(PaintRecord&& other) { color_ = other.color_; offset_ = other.offset_; text_ = std::move(other.text_); + metrics_ = other.metrics_; } PaintRecord& PaintRecord::operator=(PaintRecord&& other) { color_ = other.color_; offset_ = other.offset_; text_ = std::move(other.text_); + metrics_ = other.metrics_; return *this; } diff --git a/engine/src/flutter/src/paint_record.h b/engine/src/flutter/src/paint_record.h index 04d6f300d6..f758b68729 100644 --- a/engine/src/flutter/src/paint_record.h +++ b/engine/src/flutter/src/paint_record.h @@ -17,20 +17,23 @@ #ifndef LIB_TXT_SRC_PAINT_RECORD_H_ #define LIB_TXT_SRC_PAINT_RECORD_H_ +#include "lib/ftl/logging.h" #include "lib/ftl/macros.h" +#include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkTextBlob.h" namespace txt { class PaintRecord { public: - PaintRecord(); + PaintRecord() = delete; ~PaintRecord(); - PaintRecord(SkColor color, SkPoint offset, sk_sp text); - - PaintRecord(const PaintRecord& other) = delete; + PaintRecord(SkColor color, + SkPoint offset, + sk_sp text, + SkPaint::FontMetrics metrics); PaintRecord(PaintRecord&& other); @@ -42,10 +45,15 @@ class PaintRecord { SkTextBlob* text() const { return text_.get(); } + const SkPaint::FontMetrics& metrics() const { return metrics_; } + private: SkColor color_; SkPoint offset_; sk_sp text_; + SkPaint::FontMetrics metrics_; + + FTL_DISALLOW_COPY_AND_ASSIGN(PaintRecord); }; } // namespace txt diff --git a/engine/src/flutter/src/paragraph.cc b/engine/src/flutter/src/paragraph.cc index 117e20c05a..220e7c260a 100644 --- a/engine/src/flutter/src/paragraph.cc +++ b/engine/src/flutter/src/paragraph.cc @@ -18,16 +18,15 @@ #include #include +#include #include #include #include -#include "minikin/LineBreaker.h" - #include "lib/ftl/logging.h" - #include "lib/txt/src/font_collection.h" #include "lib/txt/src/font_skia.h" +#include "minikin/LineBreaker.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkTextBlob.h" @@ -42,6 +41,7 @@ const sk_sp& GetTypefaceForGlyph(const minikin::Layout& layout, return font->GetSkTypeface(); } +// Return the number of glyphs until the typeface changes. size_t GetBlobLength(const minikin::Layout& layout, size_t blob_start) { const size_t glyph_count = layout.nGlyphs(); const sk_sp& typeface = GetTypefaceForGlyph(layout, blob_start); @@ -52,8 +52,8 @@ size_t GetBlobLength(const minikin::Layout& layout, size_t blob_start) { return glyph_count - blob_start; } -int GetWeight(const TextStyle& style) { - switch (style.font_weight) { +int GetWeight(const FontWeight weight) { + switch (weight) { case FontWeight::w100: return 1; case FontWeight::w200: @@ -75,6 +75,10 @@ int GetWeight(const TextStyle& style) { } } +int GetWeight(const TextStyle& style) { + return GetWeight(style.font_weight); +} + bool GetItalic(const TextStyle& style) { switch (style.font_style) { case FontStyle::normal: @@ -157,6 +161,8 @@ void Paragraph::Layout(double width, size_t break_index = 0; double letter_spacing_offset = 0.0f; double word_spacing_offset = 0.0f; + double max_line_spacing = 0.0f; + for (size_t run_index = 0; run_index < runs_.size(); ++run_index) { auto run = runs_.GetRun(run_index); auto collection = @@ -170,6 +176,7 @@ void Paragraph::Layout(double width, size_t layout_start = run.start; + // Layout until the end of the run or too many lines. while (layout_start < run.end && lines_ < paragraph_style_.max_lines) { const size_t next_break = (break_index > breaks_count - 1) ? std::numeric_limits::max() @@ -182,6 +189,7 @@ void Paragraph::Layout(double width, const size_t glyph_count = layout.nGlyphs(); size_t blob_start = 0; + // Each word/blob. while (blob_start < glyph_count) { const size_t blob_length = GetBlobLength(layout, blob_start); // TODO(abarth): Precompute when we can use allocRunPosH. @@ -189,6 +197,9 @@ void Paragraph::Layout(double width, auto buffer = builder.allocRunPos(paint, blob_length); + letter_spacing_offset += run.style.letter_spacing; + + // Each Glyph/Letter. for (size_t blob_index = 0; blob_index < blob_length; ++blob_index) { const size_t glyph_index = blob_start + blob_index; buffer.glyphs[blob_index] = layout.getGlyphId(glyph_index); @@ -201,8 +212,8 @@ void Paragraph::Layout(double width, blob_start += blob_length; // Subtract letter offset to avoid big gap at end of run. This my be - // removed depending on the specificatins for letter spacing. - letter_spacing_offset -= run.style.letter_spacing; + // removed depending on the specifications for letter spacing. + // letter_spacing_offset -= run.style.letter_spacing; word_spacing_offset += run.style.word_spacing; @@ -213,19 +224,29 @@ void Paragraph::Layout(double width, // Subtract word offset to avoid big gap at end of run. This my be // removed depending on the specificatins for word spacing. word_spacing_offset -= run.style.word_spacing; - // TODO(abarth): We could keep the same SkTextBlobBuilder as long as the // color stayed the same. - records_.push_back( - PaintRecord(run.style.color, SkPoint::Make(x, y_), builder.make())); + // TODO(garyq): Ensure that the typeface does not change throughout a + // run. + SkPaint::FontMetrics metrics; + paint.getFontMetrics(&metrics); + records_.push_back(PaintRecord{run.style.color, SkPoint::Make(x, y_), + builder.make(), metrics}); + decorations_.push_back( + std::make_tuple(run.style.decoration, run.style.decoration_color, + run.style.decoration_style, run.style.font_weight)); + if (max_line_spacing < -metrics.fAscent * run.style.height) + max_line_spacing = -metrics.fAscent * run.style.height; if (layout_end == next_break) { + y_ += max_line_spacing; + + max_line_spacing = 0.0f; x = 0.0f; letter_spacing_offset = 0.0f; word_spacing_offset = 0.0f; // TODO(abarth): Use the line height, which is something like the max // font_size for runs in this line times the paragraph's line height. - y_ += run.style.font_size * run.style.height; break_index += 1; lines_++; } else { @@ -269,11 +290,49 @@ void Paragraph::SetParagraphStyle(const ParagraphStyle& style) { } void Paragraph::Paint(SkCanvas* canvas, double x, double y) { - SkPaint paint; + int i = 0; for (const auto& record : records_) { + SkPaint paint; paint.setColor(record.color()); const SkPoint& offset = record.offset(); canvas->drawTextBlob(record.text(), x + offset.x(), y + offset.y(), paint); + PaintDecorations(canvas, x + offset.x(), y + offset.y(), decorations_[i], + record.metrics(), record.text()); + i++; + } +} + +void Paragraph::PaintDecorations( + SkCanvas* canvas, + double x, + double y, + std::tuple + decoration, + SkPaint::FontMetrics metrics, + SkTextBlob* blob) { + if (std::get<0>(decoration) != TextDecoration::kNone) { + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + paint.setColor(std::get<1>(decoration)); + paint.setAntiAlias(true); + + double width = blob->bounds().fRight + blob->bounds().fLeft; + + if (std::get<0>(decoration) & 0x1) { + paint.setStrokeWidth(metrics.fUnderlineThickness); + canvas->drawLine(x, y + metrics.fUnderlineThickness, x + width, + y + metrics.fUnderlineThickness, paint); + } + if (std::get<0>(decoration) & 0x2) { + paint.setStrokeWidth(metrics.fUnderlineThickness); + canvas->drawLine(x, y + metrics.fAscent, x + width, y + metrics.fAscent, + paint); + } + if (std::get<0>(decoration) & 0x4) { + paint.setStrokeWidth(metrics.fUnderlineThickness); + canvas->drawLine(x, y - metrics.fCapHeight / 2.5, x + width, + y - metrics.fCapHeight / 2.5, paint); + } } } @@ -282,7 +341,7 @@ int Paragraph::GetLineCount() const { } bool Paragraph::DidExceedMaxLines() const { - if (GetLineCount() > paragraph_style_.max_lines) + if (lines_ > paragraph_style_.max_lines) return true; return false; } diff --git a/engine/src/flutter/src/paragraph.h b/engine/src/flutter/src/paragraph.h index 4ef74e3fca..5a1e861afd 100644 --- a/engine/src/flutter/src/paragraph.h +++ b/engine/src/flutter/src/paragraph.h @@ -17,6 +17,7 @@ #ifndef LIB_TXT_SRC_PARAGRAPH_H_ #define LIB_TXT_SRC_PARAGRAPH_H_ +#include #include #include "lib/ftl/macros.h" @@ -74,12 +75,15 @@ class Paragraph { StyledRuns runs_; minikin::LineBreaker breaker_; std::vector records_; + std::vector< + std::tuple> + decorations_; ParagraphStyle paragraph_style_; SkScalar y_ = 0.0f; // Height of the paragraph after Layout(). double width_ = 0.0f; int lines_ = 1; double max_intrinsic_width_ = 0.0f; - double min_intrinsic_width_ = 0; + double min_intrinsic_width_ = 0.0f; void SetText(std::vector text, StyledRuns runs); @@ -87,6 +91,15 @@ class Paragraph { void AddRunsToLineBreaker(const std::string& rootdir = ""); + void PaintDecorations( + SkCanvas* canvas, + double x, + double y, + std::tuple + decoration, + SkPaint::FontMetrics metrics, + SkTextBlob* blob); + FTL_DISALLOW_COPY_AND_ASSIGN(Paragraph); }; diff --git a/engine/src/flutter/tests/txt/paragraph_unittests.cc b/engine/src/flutter/tests/txt/paragraph_unittests.cc index 86988e8a2d..7f01eb45c7 100644 --- a/engine/src/flutter/tests/txt/paragraph_unittests.cc +++ b/engine/src/flutter/tests/txt/paragraph_unittests.cc @@ -96,7 +96,7 @@ TEST_F(RenderTest, RainbowParagraph) { auto icu_text1 = icu::UnicodeString::fromUTF8(text1); std::u16string u16_text1(icu_text1.getBuffer(), icu_text1.getBuffer() + icu_text1.length()); - const char* text2 = "Big Green Default"; + const char* text2 = "big Greeen Default"; auto icu_text2 = icu::UnicodeString::fromUTF8(text2); std::u16string u16_text2(icu_text2.getBuffer(), icu_text2.getBuffer() + icu_text2.length()); @@ -116,6 +116,7 @@ TEST_F(RenderTest, RainbowParagraph) { icu_text5.getBuffer() + icu_text5.length()); txt::ParagraphStyle paragraph_style; + paragraph_style.max_lines = 2; txt::ParagraphBuilder builder(paragraph_style); txt::TextStyle text_style1; @@ -132,6 +133,8 @@ TEST_F(RenderTest, RainbowParagraph) { text_style2.font_weight = txt::FontWeight::w600; text_style2.fake_bold = true; text_style2.color = SK_ColorGREEN; + text_style2.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4); + text_style2.decoration_color = SK_ColorBLACK; builder.PushStyle(text_style2); builder.AddText(u16_text2); @@ -165,6 +168,7 @@ TEST_F(RenderTest, RainbowParagraph) { for (size_t i = 0; i < u16_text1.length(); i++) { ASSERT_EQ(paragraph->text_[i], u16_text1[i]); } + ASSERT_TRUE(Snapshot()); ASSERT_EQ(paragraph->runs_.runs_.size(), 4ull); ASSERT_EQ(paragraph->runs_.styles_.size(), 4ull); ASSERT_TRUE(paragraph->runs_.styles_[0].equals(text_style1)); @@ -175,7 +179,6 @@ TEST_F(RenderTest, RainbowParagraph) { ASSERT_EQ(paragraph->records_[1].color(), text_style2.color); ASSERT_EQ(paragraph->records_[2].color(), text_style3.color); ASSERT_EQ(paragraph->records_[3].color(), text_style4.color); - ASSERT_TRUE(Snapshot()); } // Currently, this should render nothing without a supplied TextStyle. @@ -290,6 +293,8 @@ TEST_F(RenderTest, LinebreakParagraph) { text_style.letter_spacing = 0; text_style.color = SK_ColorBLACK; text_style.height = 1.15; + text_style.decoration = txt::TextDecoration(0x1); + text_style.decoration_color = SK_ColorBLACK; builder.PushStyle(text_style); builder.AddText(u16_text);