@@ -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+
200205Paragraph::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 }
0 commit comments