@@ -258,8 +258,7 @@ bool Paragraph::ComputeLineBreaks() {
258
258
size_t block_size = block_end - block_start;
259
259
260
260
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 );
263
262
line_widths_.push_back (0 );
264
263
continue ;
265
264
}
@@ -312,14 +311,7 @@ bool Paragraph::ComputeLineBreaks() {
312
311
bool hard_break = i == breaks_count - 1 ;
313
312
size_t line_end_including_newline =
314
313
(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
- }
321
314
line_ranges_.emplace_back (line_start, line_end,
322
- line_end_excluding_whitespace,
323
315
line_end_including_newline, hard_break);
324
316
line_widths_.push_back (breaker_.getWidths ()[i]);
325
317
}
@@ -470,20 +462,13 @@ void Paragraph::Layout(double width, bool force) {
470
462
}
471
463
}
472
464
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
-
480
465
// Find the runs comprising this line.
481
466
std::vector<BidiRun> line_runs;
482
467
for (const BidiRun& bidi_run : bidi_runs) {
483
- if (bidi_run.start () < line_end_index &&
468
+ if (bidi_run.start () < line_range. end &&
484
469
bidi_run.end () > line_range.start ) {
485
470
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 ),
487
472
bidi_run.direction (), bidi_run.style ());
488
473
}
489
474
}
@@ -702,7 +687,7 @@ void Paragraph::Layout(double width, bool force) {
702
687
}
703
688
704
689
// 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);
706
691
if (line_x_offset) {
707
692
for (CodeUnitRun& code_unit_run : line_code_unit_runs) {
708
693
for (GlyphPosition& position : code_unit_run.positions ) {
@@ -795,16 +780,25 @@ void Paragraph::Layout(double width, bool force) {
795
780
});
796
781
}
797
782
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_))
800
786
return 0 ;
801
787
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 ;
808
802
} else {
809
803
return 0 ;
810
804
}
0 commit comments