@@ -15,28 +15,26 @@ pub(crate) fn align<B: Brush>(
15
15
) {
16
16
// Whether the text base direction is right-to-left.
17
17
let is_rtl = layout. base_level & 1 == 1 ;
18
- let alignment_width = alignment_width. unwrap_or_else ( || {
19
- let max_line_length = layout
20
- . lines
21
- . iter ( )
22
- . map ( |line| line. metrics . advance )
23
- . max_by ( f32:: total_cmp)
24
- . unwrap_or ( 0.0 ) ;
25
- max_line_length. min ( max_line_length)
26
- } ) ;
18
+ let alignment_width = alignment_width. unwrap_or ( layout. width ) ;
27
19
28
20
// Apply alignment to line items
29
21
for line in & mut layout. lines {
30
22
// TODO: remove this field
31
23
line. alignment = alignment;
32
24
25
+ if is_rtl {
26
+ // In RTL text, trailing whitespace is on the left. As we hang that whitespace, offset
27
+ // the line to the left.
28
+ line. metrics . offset = -line. metrics . trailing_whitespace ;
29
+ }
30
+
33
31
// Compute free space.
34
32
let free_space = alignment_width - line. metrics . advance + line. metrics . trailing_whitespace ;
35
33
36
34
if !align_when_overflowing && free_space <= 0.0 {
37
35
if is_rtl {
38
36
// In RTL text, right-align on overflow.
39
- line. metrics . offset = free_space;
37
+ line. metrics . offset + = free_space;
40
38
}
41
39
continue ;
42
40
}
@@ -46,10 +44,10 @@ pub(crate) fn align<B: Brush>(
46
44
// Do nothing
47
45
}
48
46
( Alignment :: Right , _) | ( Alignment :: Start , true ) | ( Alignment :: End , false ) => {
49
- line. metrics . offset = free_space;
47
+ line. metrics . offset + = free_space;
50
48
}
51
49
( Alignment :: Middle , _) => {
52
- line. metrics . offset = free_space * 0.5 ;
50
+ line. metrics . offset + = free_space * 0.5 ;
53
51
}
54
52
( Alignment :: Justified , _) => {
55
53
// Justified alignment doesn't have any effect if free_space is negative or zero
@@ -62,7 +60,7 @@ pub(crate) fn align<B: Brush>(
62
60
// case, start-align, i.e., left-align for LTR text and right-align for RTL text.
63
61
if line. break_reason == BreakReason :: None || line. num_spaces == 0 {
64
62
if is_rtl {
65
- line. metrics . offset = free_space;
63
+ line. metrics . offset + = free_space;
66
64
}
67
65
continue ;
68
66
}
@@ -100,12 +98,6 @@ pub(crate) fn align<B: Brush>(
100
98
} ) ;
101
99
}
102
100
}
103
-
104
- if is_rtl {
105
- // In RTL text, trailing whitespace is on the left. As we hang that whitespace, offset
106
- // the line to the left.
107
- line. metrics . offset -= line. metrics . trailing_whitespace ;
108
- }
109
101
}
110
102
}
111
103
0 commit comments