Custom width tracking for accurate and consistent underlines and decorations.

Change-Id: I8ea703321cefc5cc402bf6d4247cd45eb3e085b2
This commit is contained in:
Gary Qian
2017-07-26 16:17:10 -07:00
parent 476860da54
commit 8a5babb292
4 changed files with 30 additions and 38 deletions

View File

@@ -38,7 +38,7 @@ static void BM_PaintRecordInit(benchmark::State& state) {
auto text_blob = builder.make();
while (state.KeepRunning()) {
PaintRecord PaintRecord(style, text_blob, SkPaint::FontMetrics(), 0);
PaintRecord PaintRecord(style, text_blob, SkPaint::FontMetrics(), 0, 0);
}
}
BENCHMARK(BM_PaintRecordInit);

View File

@@ -25,18 +25,25 @@ PaintRecord::PaintRecord(TextStyle style,
SkPoint offset,
sk_sp<SkTextBlob> text,
SkPaint::FontMetrics metrics,
int line)
int line,
double run_width)
: style_(style),
offset_(offset),
text_(std::move(text)),
metrics_(metrics),
line_(line) {}
line_(line),
run_width_(run_width) {}
PaintRecord::PaintRecord(TextStyle style,
sk_sp<SkTextBlob> text,
SkPaint::FontMetrics metrics,
int line)
: style_(style), text_(std::move(text)), metrics_(metrics), line_(line) {}
int line,
double run_width)
: style_(style),
text_(std::move(text)),
metrics_(metrics),
line_(line),
run_width_(run_width) {}
PaintRecord::PaintRecord(PaintRecord&& other) {
style_ = other.style_;
@@ -44,7 +51,7 @@ PaintRecord::PaintRecord(PaintRecord&& other) {
text_ = std::move(other.text_);
metrics_ = other.metrics_;
line_ = other.line_;
width_modifier_ = other.width_modifier_;
run_width_ = other.run_width_;
}
PaintRecord& PaintRecord::operator=(PaintRecord&& other) {
@@ -53,7 +60,7 @@ PaintRecord& PaintRecord::operator=(PaintRecord&& other) {
text_ = std::move(other.text_);
metrics_ = other.metrics_;
line_ = other.line_;
width_modifier_ = other.width_modifier_;
run_width_ = other.run_width_;
return *this;
}
@@ -61,8 +68,4 @@ void PaintRecord::SetOffset(SkPoint pt) {
offset_ = pt;
}
void PaintRecord::SetWidthModifier(double val) {
width_modifier_ = val;
}
} // namespace txt

View File

@@ -38,12 +38,14 @@ class PaintRecord {
SkPoint offset,
sk_sp<SkTextBlob> text,
SkPaint::FontMetrics metrics,
int line);
int line,
double run_width);
PaintRecord(TextStyle style,
sk_sp<SkTextBlob> text,
SkPaint::FontMetrics metrics,
int line);
int line,
double run_width);
PaintRecord(PaintRecord&& other);
@@ -72,8 +74,7 @@ class PaintRecord {
// FontMetrics stores the measurements of the font used.
SkPaint::FontMetrics metrics_;
size_t line_;
// Change in width when using a non-left text alignment.
double width_modifier_ = 0.0f;
double run_width_ = 0.0f;
FTL_DISALLOW_COPY_AND_ASSIGN(PaintRecord);
};

View File

@@ -247,9 +247,6 @@ void Paragraph::Layout(double width, bool force) {
records_[record_index].offset().x() + width_ -
breaker_.getWidths()[records_[record_index].line()],
records_[record_index].offset().y()));
records_[record_index].SetWidthModifier(
records_[record_index].GetWidthModifier() + width_ -
breaker_.getWidths()[records_[record_index].line()]);
break;
}
case TextAlign::center: {
@@ -259,18 +256,9 @@ void Paragraph::Layout(double width, bool force) {
breaker_.getWidths()[records_[record_index].line()]) /
2,
records_[record_index].offset().y()));
records_[record_index].SetWidthModifier(
records_[record_index].GetWidthModifier() +
(width_ - breaker_.getWidths()[records_[record_index].line()]) /
2);
break;
}
case TextAlign::justify: {
records_[record_index].SetWidthModifier(
records_[record_index].GetWidthModifier() +
records_[record_index].GetWidthModifier() + width_ -
breaker_.getWidths()[records_[record_index].line()]);
break;
}
}
@@ -339,6 +327,7 @@ void Paragraph::Layout(double width, bool force) {
word_count = 0;
double temp_line_spacing = 0;
double current_x_position = previous_run_x_position;
double run_width = 0.0f;
while (blob_start < glyph_count) {
const size_t blob_length = GetBlobLength(layout, blob_start);
buffer_sizes.push_back(blob_length);
@@ -347,9 +336,10 @@ void Paragraph::Layout(double width, bool force) {
// Check if we should remove trailing whitespace of blobs.
size_t trailing_length = 0;
while (paragraph_style_.text_align == TextAlign::justify &&
minikin::isWordSpace(
text_[blob_start + blob_length - trailing_length - 1]) &&
while ((paragraph_style_.text_align == TextAlign::center ||
paragraph_style_.text_align == TextAlign::right) &&
whitespace_set_.count(layout.getGlyphId(
blob_start + blob_length - trailing_length - 1)) > 0 &&
layout_end == next_break) {
++trailing_length;
}
@@ -387,6 +377,7 @@ void Paragraph::Layout(double width, bool force) {
}
prev_char_advance = layout.getCharAdvance(glyph_index);
run_width += layout.getCharAdvance(glyph_index);
}
blob_start += blob_length;
previous_run_x_position += current_x_position + prev_char_advance;
@@ -402,7 +393,7 @@ void Paragraph::Layout(double width, bool force) {
JustifyLine(buffers, buffer_sizes, word_count, justify_spacing);
}
records_.push_back(
PaintRecord{run.style, builder.make(), metrics, lines_});
PaintRecord{run.style, builder.make(), metrics, lines_, run_width});
line_width +=
std::abs(records_[records_.size() - 1].text()->bounds().fRight +
records_[records_.size() - 1].text()->bounds().fLeft);
@@ -480,7 +471,6 @@ void Paragraph::Layout(double width, bool force) {
// Remove decoration extra width if the last line.
size_t i = records_.size() - 1;
while (records_[i].line() == lines_ - 1) {
records_[i].SetWidthModifier(0);
--i;
}
}
@@ -636,15 +626,13 @@ void Paragraph::PaintDecorations(SkCanvas* canvas,
std::vector<WaveCoordinates> wave_coords;
double width = 0;
if (record_index == records_.size() - 1 ||
record.line() < records_[record_index + 1].line()) {
width = line_widths_[record.line()] - x;
if (paragraph_style_.text_align == TextAlign::justify &&
record.line() != lines_ - 1) {
width = width_;
} else {
width = record.text()->bounds().fRight + record.text()->bounds().fLeft;
width = record.GetRunWidth();
}
width += record.GetWidthModifier();
paint.setStrokeWidth(
(SkToBool(metrics.fFlags & SkPaint::FontMetrics::FontMetricsFlags::
kUnderlineThicknessIsValid_Flag))