summaryrefslogtreecommitdiffstats
path: root/wgpu/src/text.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-26 15:32:11 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-26 15:32:11 +0100
commita99aa1dd61e15e28d93c0719037b6683f32e310e (patch)
tree861873a4bc344182a1f07e5552fd146b78ef6e9b /wgpu/src/text.rs
parent84874ac5dc2dc787833bcebf37751735f4c4ab42 (diff)
downloadiced-a99aa1dd61e15e28d93c0719037b6683f32e310e.tar.gz
iced-a99aa1dd61e15e28d93c0719037b6683f32e310e.tar.bz2
iced-a99aa1dd61e15e28d93c0719037b6683f32e310e.zip
Fallback to embedded font when system font fails
Diffstat (limited to 'wgpu/src/text.rs')
-rw-r--r--wgpu/src/text.rs36
1 files changed, 25 insertions, 11 deletions
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index bf37abe5..f29440c7 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -11,6 +11,8 @@ pub const BUILTIN_ICONS: iced_native::Font = iced_native::Font::External {
pub const CHECKMARK_ICON: char = '\u{F00C}';
+const FALLBACK_FONT: &[u8] = include_bytes!("../fonts/Lato-Regular.ttf");
+
#[derive(Debug)]
pub struct Pipeline {
draw_brush: RefCell<wgpu_glyph::GlyphBrush<'static, ()>>,
@@ -26,24 +28,36 @@ impl Pipeline {
let default_font = font_source
.load(&[font::Family::SansSerif, font::Family::Serif])
- .expect("Find sans-serif or serif font");
+ .unwrap_or_else(|_| FALLBACK_FONT.to_vec());
let mono_font = font_source
.load(&[font::Family::Monospace])
- .expect("Find monospace font");
+ .unwrap_or_else(|_| FALLBACK_FONT.to_vec());
+
+ let load_glyph_brush = |font: Vec<u8>| {
+ let builder =
+ wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![
+ mono_font.clone(),
+ font.clone(),
+ ])?;
+
+ Ok((
+ builder,
+ glyph_brush::GlyphBrushBuilder::using_font_bytes(font).build(),
+ ))
+ };
+
+ let (brush_builder, measure_brush) = load_glyph_brush(default_font)
+ .unwrap_or_else(|_: wgpu_glyph::rusttype::Error| {
+ log::warn!("System font failed to load. Falling back to embedded font...");
- let draw_brush =
- wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![
- mono_font,
- default_font.clone(),
- ])
+ load_glyph_brush(FALLBACK_FONT.to_vec()).expect("Load fallback font")
+ });
+
+ let draw_brush = brush_builder
.initial_cache_size((2048, 2048))
.build(device, wgpu::TextureFormat::Bgra8UnormSrgb);
- let measure_brush =
- glyph_brush::GlyphBrushBuilder::using_font_bytes(default_font)
- .build();
-
Pipeline {
draw_brush: RefCell::new(draw_brush),
draw_font_map: RefCell::new(HashMap::new()),