Skip to content

Commit 157575e

Browse files
Replace unwrap() calls with proper error handling in font_altas (bevyengine#21790)
# Objective - Replace unwrap() calls with proper error handling in `font_atlas.rs`'s `add_glyph()` and `add_glyph_to_atlas()` to prevent panics and provide clear information. ## Solution - Add new variants to the TextError enum to provide more specific error messages.
1 parent dc22ec3 commit 157575e

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

crates/bevy_sprite/src/text2d.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ pub fn update_text2d_layout(
253253
// queue for further processing
254254
queue.insert(entity);
255255
}
256-
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
256+
Err(
257+
e @ (TextError::FailedToAddGlyph(_)
258+
| TextError::FailedToGetGlyphImage(_)
259+
| TextError::MissingAtlasLayout
260+
| TextError::MissingAtlasTexture
261+
| TextError::InconsistentAtlasState),
262+
) => {
257263
panic!("Fatal error when processing text: {e}.");
258264
}
259265
Ok(()) => {

crates/bevy_text/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ pub enum TextError {
1414
/// Failed to get scaled glyph image for cache key
1515
#[error("failed to get scaled glyph image for cache key: {0:?}")]
1616
FailedToGetGlyphImage(CacheKey),
17+
/// Missing texture atlas layout for the font
18+
#[error("missing texture atlas layout for the font")]
19+
MissingAtlasLayout,
20+
/// Missing texture for the font atlas
21+
#[error("missing texture for the font atlas")]
22+
MissingAtlasTexture,
23+
/// Failed to find glyph in atlas after it was added
24+
#[error("failed to find glyph in atlas after it was added")]
25+
InconsistentAtlasState,
1726
}

crates/bevy_text/src/font_atlas.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ impl FontAtlas {
8787
texture: &Image,
8888
offset: IVec2,
8989
) -> Result<(), TextError> {
90-
let atlas_layout = atlas_layouts.get_mut(&self.texture_atlas).unwrap();
91-
let atlas_texture = textures.get_mut(&self.texture).unwrap();
90+
let atlas_layout = atlas_layouts
91+
.get_mut(&self.texture_atlas)
92+
.ok_or(TextError::MissingAtlasLayout)?;
93+
let atlas_texture = textures
94+
.get_mut(&self.texture)
95+
.ok_or(TextError::MissingAtlasTexture)?;
9296

9397
if let Ok(glyph_index) =
9498
self.dynamic_texture_atlas_builder
@@ -154,23 +158,27 @@ pub fn add_glyph_to_atlas(
154158
.max(glyph_texture.width());
155159
// Pick the higher of 512 or the smallest power of 2 greater than glyph_max_size
156160
let containing = (1u32 << (32 - glyph_max_size.leading_zeros())).max(512);
157-
font_atlases.push(FontAtlas::new(
161+
162+
let mut new_atlas = FontAtlas::new(
158163
textures,
159164
texture_atlases,
160165
UVec2::splat(containing),
161166
font_smoothing,
162-
));
167+
);
163168

164-
font_atlases.last_mut().unwrap().add_glyph(
169+
new_atlas.add_glyph(
165170
textures,
166171
texture_atlases,
167172
physical_glyph.cache_key,
168173
&glyph_texture,
169174
offset,
170175
)?;
176+
177+
font_atlases.push(new_atlas);
171178
}
172179

173-
Ok(get_glyph_atlas_info(font_atlases, physical_glyph.cache_key).unwrap())
180+
get_glyph_atlas_info(font_atlases, physical_glyph.cache_key)
181+
.ok_or(TextError::InconsistentAtlasState)
174182
}
175183

176184
/// Get the texture of the glyph as a rendered image, and its offset

crates/bevy_ui/src/widget/text.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,13 @@ fn create_text_measure<'a>(
260260
// Try again next frame
261261
text_flags.needs_measure_fn = true;
262262
}
263-
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
263+
Err(
264+
e @ (TextError::FailedToAddGlyph(_)
265+
| TextError::FailedToGetGlyphImage(_)
266+
| TextError::MissingAtlasLayout
267+
| TextError::MissingAtlasTexture
268+
| TextError::InconsistentAtlasState),
269+
) => {
264270
panic!("Fatal error when processing text: {e}.");
265271
}
266272
};
@@ -372,7 +378,13 @@ fn queue_text(
372378
// There was an error processing the text layout, try again next frame
373379
text_flags.needs_recompute = true;
374380
}
375-
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
381+
Err(
382+
e @ (TextError::FailedToAddGlyph(_)
383+
| TextError::FailedToGetGlyphImage(_)
384+
| TextError::MissingAtlasLayout
385+
| TextError::MissingAtlasTexture
386+
| TextError::InconsistentAtlasState),
387+
) => {
376388
panic!("Fatal error when processing text: {e}.");
377389
}
378390
Ok(()) => {

0 commit comments

Comments
 (0)