Skip to content

Commit 919919c

Browse files
authored
Fix text measurement algorithm (#8425)
# Objective Followup to #7779 which tweaks the actual text measurement algorithm to be more robust. Before: <img width="822" alt="Screenshot 2023-04-17 at 18 12 05" src="https://user-images.githubusercontent.com/1007307/232566858-3d3f0fd5-f3d4-400a-8371-3c2a3f541e56.png"> After: <img width="810" alt="Screenshot 2023-04-17 at 18 41 40" src="https://user-images.githubusercontent.com/1007307/232566919-4254cbfa-1cc3-4ea7-91ed-8ca1b759bacf.png"> (note extra space taken up in header in before example) ## Solution - Text layout of horizontal text (currently the only kind of text we support) is now based solely on the layout constraints in the horizontal axis. It ignores constraints in the vertical axis and computes vertical size based on wrapping subject to the horizontal axis constraints. - I've also added a paragraph to the `grid` example for testing / demo purposes.
1 parent 7604464 commit 919919c

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

crates/bevy_ui/src/widget/text.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Measure for TextMeasure {
3030
width: Option<f32>,
3131
height: Option<f32>,
3232
available_width: AvailableSpace,
33-
available_height: AvailableSpace,
33+
_available_height: AvailableSpace,
3434
) -> Vec2 {
3535
let x = width.unwrap_or_else(|| match available_width {
3636
AvailableSpace::Definite(x) => x.clamp(
@@ -43,16 +43,10 @@ impl Measure for TextMeasure {
4343

4444
height
4545
.map_or_else(
46-
|| match available_height {
47-
AvailableSpace::Definite(y) => {
48-
let y = y.clamp(
49-
self.info.max_width_content_size.y,
50-
self.info.min_width_content_size.y,
51-
);
52-
self.info.compute_size(Vec2::new(x, y))
53-
}
54-
AvailableSpace::MinContent => Vec2::new(x, self.info.max_width_content_size.y),
55-
AvailableSpace::MaxContent => Vec2::new(x, self.info.min_width_content_size.y),
46+
|| match available_width {
47+
AvailableSpace::Definite(_) => self.info.compute_size(Vec2::new(x, f32::MAX)),
48+
AvailableSpace::MinContent => Vec2::new(x, self.info.min_width_content_size.y),
49+
AvailableSpace::MaxContent => Vec2::new(x, self.info.max_width_content_size.y),
5650
},
5751
|y| Vec2::new(x, y),
5852
)

examples/ui/grid.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
124124
align_items: AlignItems::Start,
125125
// Align content towards the center in the horizontal axis
126126
justify_items: JustifyItems::Center,
127-
// Add 20px padding to the top
128-
padding: UiRect::top(Val::Px(20.)),
127+
// Add 10px padding
128+
padding: UiRect::all(Val::Px(10.)),
129+
// Add an fr track to take up all the available space at the bottom of the column so that the text nodes
130+
// can be top-aligned. Normally you'd use flexbox for this, but this is the CSS Grid example so we're using grid.
131+
grid_template_rows: vec![GridTrack::auto(), GridTrack::auto(), GridTrack::fr(1.0)],
132+
// Add a 10px gap between rows
133+
gap: Size::height(Val::Px(10.)),
129134
..default()
130135
},
131136
background_color: BackgroundColor(Color::BLACK),
@@ -135,11 +140,20 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
135140
builder.spawn(TextBundle::from_section(
136141
"Sidebar",
137142
TextStyle {
138-
font,
143+
font: font.clone(),
139144
font_size: 24.0,
140145
color: Color::WHITE,
141146
},
142147
));
148+
builder.spawn(TextBundle::from_section(
149+
"A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely.",
150+
TextStyle {
151+
font: font.clone(),
152+
font_size: 16.0,
153+
color: Color::WHITE,
154+
},
155+
));
156+
builder.spawn(NodeBundle::default());
143157
});
144158

145159
// Footer / status bar

0 commit comments

Comments
 (0)