Add support for underline, overline, and strikethrough.
Change-Id: Id98ecb8409be69d9de44dc931ed6143163469c27
This commit is contained in:
@@ -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<SkTextBlob> text)
|
||||
: color_(color), offset_(offset), text_(std::move(text)) {}
|
||||
PaintRecord::PaintRecord(SkColor color,
|
||||
SkPoint offset,
|
||||
sk_sp<SkTextBlob> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<SkTextBlob> text);
|
||||
|
||||
PaintRecord(const PaintRecord& other) = delete;
|
||||
PaintRecord(SkColor color,
|
||||
SkPoint offset,
|
||||
sk_sp<SkTextBlob> 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<SkTextBlob> text_;
|
||||
SkPaint::FontMetrics metrics_;
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(PaintRecord);
|
||||
};
|
||||
|
||||
} // namespace txt
|
||||
|
||||
@@ -18,16 +18,15 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <minikin/Layout.h>
|
||||
#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<SkTypeface>& 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<SkTypeface>& 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<size_t>::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<TextDecoration, SkColor, TextDecorationStyle, FontWeight>
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#ifndef LIB_TXT_SRC_PARAGRAPH_H_
|
||||
#define LIB_TXT_SRC_PARAGRAPH_H_
|
||||
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
@@ -74,12 +75,15 @@ class Paragraph {
|
||||
StyledRuns runs_;
|
||||
minikin::LineBreaker breaker_;
|
||||
std::vector<PaintRecord> records_;
|
||||
std::vector<
|
||||
std::tuple<TextDecoration, SkColor, TextDecorationStyle, FontWeight>>
|
||||
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<uint16_t> 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<TextDecoration, SkColor, TextDecorationStyle, FontWeight>
|
||||
decoration,
|
||||
SkPaint::FontMetrics metrics,
|
||||
SkTextBlob* blob);
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(Paragraph);
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user