Skip to content

Commit 0efc7b0

Browse files
committed
fix: don't panic if system font not found
1 parent c849fed commit 0efc7b0

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

crates/bevy_text/src/font.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bevy_reflect::TypePath;
1919
pub enum Font {
2020
/// Content of a font file as bytes
2121
Data(Arc<Vec<u8>>),
22-
/// Reference a system font by its name
22+
/// References a font installed on the system by family, weight, stretch, and style.
2323
System {
2424
/// A list of font families that satisfy this font requirement
2525
families: Vec<Family>,
@@ -38,7 +38,7 @@ pub enum Font {
3838
},
3939
}
4040

41-
/// A [font family](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#propdef-font-family).
41+
/// A font family specifier, either by name or generic category.
4242
///
4343
/// See [`cosmic_text::Family`] for details.
4444
#[derive(Clone, Debug, PartialEq, Eq, Hash)]

crates/bevy_text/src/pipeline.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ impl TextPipeline {
152152
font_system,
153153
&mut self.map_handle_to_font_id,
154154
fonts,
155-
);
155+
)
156+
.ok_or(TextError::NoSuchFont)?;
156157

157158
// Save spans that aren't zero-sized.
158159
if scale_factor <= 0.0 || text_font.font_size <= 0.0 {
@@ -508,11 +509,12 @@ pub fn load_font_to_fontdb(
508509
font_system: &mut cosmic_text::FontSystem,
509510
map_handle_to_font_id: &mut HashMap<AssetId<Font>, (cosmic_text::fontdb::ID, Arc<str>)>,
510511
fonts: &Assets<Font>,
511-
) -> FontFaceInfo {
512+
) -> Option<FontFaceInfo> {
512513
let font_handle = text_font.font.clone();
513-
let (face_id, family_name) = map_handle_to_font_id
514-
.entry(font_handle.id())
515-
.or_insert_with(|| {
514+
515+
let (face_id, family_name) = match map_handle_to_font_id.get_mut(&font_handle.id()) {
516+
Some((face_id, family_name)) => (face_id, family_name),
517+
None => {
516518
let font = fonts.get(font_handle.id()).expect(
517519
"Tried getting a font that was not available, probably due to not being loaded yet",
518520
);
@@ -545,26 +547,27 @@ pub fn load_font_to_fontdb(
545547
style: *style,
546548
};
547549

548-
//todo: unwrapping here is fallible
549-
font_system.db().query(&query).unwrap()
550+
font_system.db().query(&query)?
550551
}
551552
};
552553

553554
let face = font_system.db().face(face_id).unwrap();
554555
let family_name = Arc::from(face.families[0].0.as_str());
555556

557+
map_handle_to_font_id.insert(font_handle.id(), (face_id, family_name));
558+
let (face_id, family_name) = map_handle_to_font_id.get_mut(&font_handle.id()).unwrap();
556559
(face_id, family_name)
560+
}
561+
};
557562

558-
//let other_id = font_system.db().query(query)
559-
});
560563
let face = font_system.db().face(*face_id).unwrap();
561564

562-
FontFaceInfo {
565+
Some(FontFaceInfo {
563566
stretch: face.stretch,
564567
style: face.style,
565568
weight: face.weight,
566569
family_name: family_name.clone(),
567-
}
570+
})
568571
}
569572

570573
/// Translates [`TextFont`] to [`Attrs`].

0 commit comments

Comments
 (0)