Skip to content

Commit 57fffca

Browse files
committed
Skia: Fix no-wrap still wrapping text sometimes
Fixes #7080
1 parent a66aa2d commit 57fffca

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

internal/renderers/skia/textlayout.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,20 @@ pub fn create_layout(
159159
builder.add_text(text);
160160
}
161161

162+
let no_wrap = wrap == items::TextWrap::NoWrap || overflow == items::TextOverflow::Elide;
163+
162164
let mut paragraph = builder.build();
163-
paragraph.layout(max_width.map_or(f32::MAX, |physical_width| physical_width.get()));
165+
paragraph.layout(
166+
max_width.filter(|_| !no_wrap).map_or(f32::MAX, |physical_width| physical_width.get()),
167+
);
168+
169+
// Layouting out with f32::max when wrapping is disabled, causes an overflow and breaks alignment compensation. Lay out again just wide enough
170+
// to find the largest unwrapped line.
171+
if no_wrap
172+
&& matches!(h_align, TextHorizontalAlignment::Right | TextHorizontalAlignment::Center)
173+
{
174+
paragraph.layout(paragraph.longest_line() + 1.);
175+
}
164176

165177
let layout_height = PhysicalLength::new(paragraph.height());
166178

@@ -170,7 +182,25 @@ pub fn create_layout(
170182
i_slint_core::items::TextVerticalAlignment::Bottom => max_height - layout_height,
171183
};
172184

173-
(paragraph, PhysicalPoint::from_lengths(Default::default(), layout_top_y))
185+
let layout_top_x = if no_wrap {
186+
// With no wrapping, the alignment is done against the layout width that's larger than the available width. Compensate for that
187+
// by shifting rendering.
188+
match h_align {
189+
TextHorizontalAlignment::Left => PhysicalLength::zero(),
190+
TextHorizontalAlignment::Center => {
191+
let available_width = max_width.unwrap_or(PhysicalLength::new(f32::MAX));
192+
(PhysicalLength::new(-paragraph.max_width()) + available_width) / 2.
193+
}
194+
TextHorizontalAlignment::Right => {
195+
let available_width = max_width.unwrap_or(PhysicalLength::new(f32::MAX));
196+
PhysicalLength::new(-paragraph.max_width()) + available_width
197+
}
198+
}
199+
} else {
200+
PhysicalLength::zero()
201+
};
202+
203+
(paragraph, PhysicalPoint::from_lengths(layout_top_x, layout_top_y))
174204
}
175205

176206
pub fn font_metrics(

0 commit comments

Comments
 (0)