diff options
author | 2019-11-05 03:57:13 +0100 | |
---|---|---|
committer | 2019-11-05 03:57:13 +0100 | |
commit | 0157121038987feb6c2ea3066a21ce25e689888e (patch) | |
tree | 9545408530437eb780ac73c600f7d40968803786 /wgpu | |
parent | 40e9a2f6ae8a9d8576531c79fb57bc53fe5a0acf (diff) | |
download | iced-0157121038987feb6c2ea3066a21ce25e689888e.tar.gz iced-0157121038987feb6c2ea3066a21ce25e689888e.tar.bz2 iced-0157121038987feb6c2ea3066a21ce25e689888e.zip |
Improve default font loading
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/font.rs | 18 | ||||
-rw-r--r-- | wgpu/src/renderer.rs | 19 |
2 files changed, 19 insertions, 18 deletions
diff --git a/wgpu/src/font.rs b/wgpu/src/font.rs index 9bba9b22..31df5bf4 100644 --- a/wgpu/src/font.rs +++ b/wgpu/src/font.rs @@ -1,3 +1,4 @@ +pub use font_kit::error::SelectionError as LoadError; pub use font_kit::family_name::FamilyName as Family; pub struct Source { @@ -11,14 +12,11 @@ impl Source { } } - pub fn load(&self, families: &[Family]) -> Vec<u8> { - let font = self - .raw - .select_best_match( - families, - &font_kit::properties::Properties::default(), - ) - .expect("Find font"); + pub fn load(&self, families: &[Family]) -> Result<Vec<u8>, LoadError> { + let font = self.raw.select_best_match( + families, + &font_kit::properties::Properties::default(), + )?; match font { font_kit::handle::Handle::Path { path, .. } => { @@ -28,10 +26,10 @@ impl Source { let mut reader = std::fs::File::open(path).expect("Read font"); let _ = reader.read_to_end(&mut buf); - buf + Ok(buf) } font_kit::handle::Handle::Memory { bytes, .. } => { - bytes.as_ref().clone() + Ok(bytes.as_ref().clone()) } } } diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index c167f2bf..060f07a3 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -64,14 +64,17 @@ impl Renderer { // TODO: Font customization let font_source = font::Source::new(); - let sans_serif_font = font_source.load(&[font::Family::SansSerif]); - let mono_font = font_source.load(&[font::Family::Monospace]); - - let glyph_brush = GlyphBrushBuilder::using_fonts_bytes(vec![ - sans_serif_font, - mono_font, - ]) - .build(&mut device, TextureFormat::Bgra8UnormSrgb); + let default_font = font_source + .load(&[font::Family::SansSerif, font::Family::Serif]) + .expect("Find sans-serif or serif font"); + + let mono_font = font_source + .load(&[font::Family::Monospace]) + .expect("Find monospace font"); + + let glyph_brush = + GlyphBrushBuilder::using_fonts_bytes(vec![default_font, mono_font]) + .build(&mut device, TextureFormat::Bgra8UnormSrgb); let quad_pipeline = quad::Pipeline::new(&mut device); let image_pipeline = crate::image::Pipeline::new(&mut device); |