Skip to content

Commit 7617d28

Browse files
libtxt: post-process glyph positions in order to accurately right-justify text (flutter#5093)
Previously libtxt was applying right justification during line layout based on the line width returned by the Minikin line breaker. However, this width does not include the width of leading or trailing whitespace. So in a right justified line the starting offset of the first glyph would not account for the advance of the leading whitespace, resulting in text being clipped. This PR applies line justification after the glyphs are laid out and the full advance of the text is known. Fixes flutter#16333
1 parent 895c965 commit 7617d28

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

‎third_party/txt/src/txt/paragraph.cc‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ Paragraph::GlyphPosition::GlyphPosition(double x_start,
197197
: code_units(code_unit_index, code_unit_index + code_unit_width),
198198
x_pos(x_start, x_start + x_advance) {}
199199

200+
void Paragraph::GlyphPosition::Shift(double delta) {
201+
x_pos.start += delta;
202+
x_pos.end += delta;
203+
}
204+
200205
Paragraph::GlyphLine::GlyphLine(std::vector<GlyphPosition>&& p, size_t tcu)
201206
: positions(std::move(p)), total_code_units(tcu) {}
202207

@@ -465,7 +470,8 @@ void Paragraph::Layout(double width, bool force) {
465470
}
466471

467472
std::vector<GlyphPosition> line_glyph_positions;
468-
double run_x_offset = GetLineXOffset(line_number);
473+
std::vector<CodeUnitRun> line_code_unit_runs;
474+
double run_x_offset = 0;
469475
double justify_x_offset = 0;
470476
std::vector<PaintRecord> paint_records;
471477

@@ -665,7 +671,7 @@ void Paragraph::Layout(double width, bool force) {
665671
[](const GlyphPosition& a, const GlyphPosition& b) {
666672
return a.code_units.start < b.code_units.start;
667673
});
668-
code_unit_runs_.emplace_back(
674+
line_code_unit_runs.emplace_back(
669675
std::move(code_unit_positions),
670676
Range<size_t>(run.start(), run.end()),
671677
Range<double>(glyph_positions.front().x_pos.start,
@@ -676,6 +682,27 @@ void Paragraph::Layout(double width, bool force) {
676682
run_x_offset += layout.getAdvance();
677683
}
678684

685+
// Adjust the glyph positions based on the alignment of the line.
686+
double line_x_offset = GetLineXOffset(line_number, run_x_offset);
687+
if (line_x_offset) {
688+
for (CodeUnitRun& code_unit_run : line_code_unit_runs) {
689+
for (GlyphPosition& position : code_unit_run.positions) {
690+
position.Shift(line_x_offset);
691+
}
692+
}
693+
for (GlyphPosition& position : line_glyph_positions) {
694+
position.Shift(line_x_offset);
695+
}
696+
}
697+
698+
size_t next_line_start = (line_number < line_ranges_.size() - 1)
699+
? line_ranges_[line_number + 1].start
700+
: text_.size();
701+
glyph_lines_.emplace_back(std::move(line_glyph_positions),
702+
next_line_start - line_range.start);
703+
code_unit_runs_.insert(code_unit_runs_.end(), line_code_unit_runs.begin(),
704+
line_code_unit_runs.end());
705+
679706
double max_line_spacing = 0;
680707
double max_descent = 0;
681708
auto update_line_metrics = [&](const SkPaint::FontMetrics& metrics,
@@ -720,15 +747,9 @@ void Paragraph::Layout(double width, bool force) {
720747

721748
for (PaintRecord& paint_record : paint_records) {
722749
paint_record.SetOffset(
723-
SkPoint::Make(paint_record.offset().x(), y_offset));
750+
SkPoint::Make(paint_record.offset().x() + line_x_offset, y_offset));
724751
records_.emplace_back(std::move(paint_record));
725752
}
726-
727-
size_t next_line_start = (line_number < line_ranges_.size() - 1)
728-
? line_ranges_[line_number + 1].start
729-
: text_.size();
730-
glyph_lines_.emplace_back(std::move(line_glyph_positions),
731-
next_line_start - line_range.start);
732753
}
733754

734755
max_intrinsic_width_ = 0;
@@ -755,8 +776,9 @@ void Paragraph::Layout(double width, bool force) {
755776
});
756777
}
757778

758-
double Paragraph::GetLineXOffset(size_t line) {
759-
if (line >= line_widths_.size() || isinf(width_))
779+
double Paragraph::GetLineXOffset(size_t line_number,
780+
double line_total_advance) {
781+
if (line_number >= line_widths_.size() || isinf(width_))
760782
return 0;
761783

762784
TextAlign align = paragraph_style_.text_align;
@@ -765,9 +787,9 @@ double Paragraph::GetLineXOffset(size_t line) {
765787
if (align == TextAlign::right ||
766788
(align == TextAlign::start && direction == TextDirection::rtl) ||
767789
(align == TextAlign::end && direction == TextDirection::ltr)) {
768-
return width_ - line_widths_[line];
790+
return width_ - line_total_advance;
769791
} else if (paragraph_style_.text_align == TextAlign::center) {
770-
return (width_ - line_widths_[line]) / 2;
792+
return (width_ - line_widths_[line_number]) / 2;
771793
} else {
772794
return 0;
773795
}

‎third_party/txt/src/txt/paragraph.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ class Paragraph {
228228
double x_advance,
229229
size_t code_unit_index,
230230
size_t code_unit_width);
231+
232+
void Shift(double delta);
231233
};
232234

233235
struct GlyphLine {
@@ -298,7 +300,7 @@ class Paragraph {
298300

299301
// Calculate the starting X offset of a line based on the line's width and
300302
// alignment.
301-
double GetLineXOffset(size_t line);
303+
double GetLineXOffset(size_t line_number, double line_total_advance);
302304

303305
// Creates and draws the decorations onto the canvas.
304306
void PaintDecorations(SkCanvas* canvas, const PaintRecord& record);

0 commit comments

Comments
 (0)