@@ -258,8 +258,7 @@ bool Paragraph::ComputeLineBreaks() {
258258 size_t block_size = block_end - block_start;
259259
260260 if (block_size == 0 ) {
261- line_ranges_.emplace_back (block_start, block_end, block_end,
262- block_end + 1 , true );
261+ line_ranges_.emplace_back (block_start, block_end, block_end + 1 , true );
263262 line_widths_.push_back (0 );
264263 continue ;
265264 }
@@ -312,14 +311,7 @@ bool Paragraph::ComputeLineBreaks() {
312311 bool hard_break = i == breaks_count - 1 ;
313312 size_t line_end_including_newline =
314313 (hard_break && line_end < text_.size ()) ? line_end + 1 : line_end;
315- size_t line_end_excluding_whitespace = line_end;
316- while (
317- line_end_excluding_whitespace > 0 &&
318- minikin::isLineEndSpace (text_[line_end_excluding_whitespace - 1 ])) {
319- line_end_excluding_whitespace--;
320- }
321314 line_ranges_.emplace_back (line_start, line_end,
322- line_end_excluding_whitespace,
323315 line_end_including_newline, hard_break);
324316 line_widths_.push_back (breaker_.getWidths ()[i]);
325317 }
@@ -470,20 +462,13 @@ void Paragraph::Layout(double width, bool force) {
470462 }
471463 }
472464
473- // Exclude trailing whitespace from right-justified lines so the last
474- // visible character in the line will be flush with the right margin.
475- size_t line_end_index =
476- (paragraph_style_.effective_align () == TextAlign::right)
477- ? line_range.end_excluding_whitespace
478- : line_range.end ;
479-
480465 // Find the runs comprising this line.
481466 std::vector<BidiRun> line_runs;
482467 for (const BidiRun& bidi_run : bidi_runs) {
483- if (bidi_run.start () < line_end_index &&
468+ if (bidi_run.start () < line_range. end &&
484469 bidi_run.end () > line_range.start ) {
485470 line_runs.emplace_back (std::max (bidi_run.start (), line_range.start ),
486- std::min (bidi_run.end (), line_end_index ),
471+ std::min (bidi_run.end (), line_range. end ),
487472 bidi_run.direction (), bidi_run.style ());
488473 }
489474 }
@@ -702,7 +687,7 @@ void Paragraph::Layout(double width, bool force) {
702687 }
703688
704689 // Adjust the glyph positions based on the alignment of the line.
705- double line_x_offset = GetLineXOffset (run_x_offset);
690+ double line_x_offset = GetLineXOffset (line_number, run_x_offset);
706691 if (line_x_offset) {
707692 for (CodeUnitRun& code_unit_run : line_code_unit_runs) {
708693 for (GlyphPosition& position : code_unit_run.positions ) {
@@ -795,16 +780,25 @@ void Paragraph::Layout(double width, bool force) {
795780 });
796781}
797782
798- double Paragraph::GetLineXOffset (double line_total_advance) {
799- if (isinf (width_))
783+ double Paragraph::GetLineXOffset (size_t line_number,
784+ double line_total_advance) {
785+ if (line_number >= line_widths_.size () || isinf (width_))
800786 return 0 ;
801787
802- TextAlign align = paragraph_style_.effective_align ();
803-
804- if (align == TextAlign::right) {
805- return width_ - line_total_advance;
806- } else if (align == TextAlign::center) {
807- return (width_ - line_total_advance) / 2 ;
788+ TextAlign align = paragraph_style_.text_align ;
789+ TextDirection direction = paragraph_style_.text_direction ;
790+
791+ if (align == TextAlign::right ||
792+ (align == TextAlign::start && direction == TextDirection::rtl) ||
793+ (align == TextAlign::end && direction == TextDirection::ltr)) {
794+ // If this line has a soft break, then use the line width calculated by the
795+ // line breaker because that width excludes the soft break's whitespace.
796+ double text_width = line_ranges_[line_number].hard_break
797+ ? line_total_advance
798+ : line_widths_[line_number];
799+ return width_ - text_width;
800+ } else if (paragraph_style_.text_align == TextAlign::center) {
801+ return (width_ - line_widths_[line_number]) / 2 ;
808802 } else {
809803 return 0 ;
810804 }
0 commit comments