Improve default font loading
This commit is contained in:
parent
40e9a2f6ae
commit
0157121038
2 changed files with 18 additions and 17 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub use font_kit::error::SelectionError as LoadError;
|
||||||
pub use font_kit::family_name::FamilyName as Family;
|
pub use font_kit::family_name::FamilyName as Family;
|
||||||
|
|
||||||
pub struct Source {
|
pub struct Source {
|
||||||
|
|
@ -11,14 +12,11 @@ impl Source {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load(&self, families: &[Family]) -> Vec<u8> {
|
pub fn load(&self, families: &[Family]) -> Result<Vec<u8>, LoadError> {
|
||||||
let font = self
|
let font = self.raw.select_best_match(
|
||||||
.raw
|
families,
|
||||||
.select_best_match(
|
&font_kit::properties::Properties::default(),
|
||||||
families,
|
)?;
|
||||||
&font_kit::properties::Properties::default(),
|
|
||||||
)
|
|
||||||
.expect("Find font");
|
|
||||||
|
|
||||||
match font {
|
match font {
|
||||||
font_kit::handle::Handle::Path { path, .. } => {
|
font_kit::handle::Handle::Path { path, .. } => {
|
||||||
|
|
@ -28,10 +26,10 @@ impl Source {
|
||||||
let mut reader = std::fs::File::open(path).expect("Read font");
|
let mut reader = std::fs::File::open(path).expect("Read font");
|
||||||
let _ = reader.read_to_end(&mut buf);
|
let _ = reader.read_to_end(&mut buf);
|
||||||
|
|
||||||
buf
|
Ok(buf)
|
||||||
}
|
}
|
||||||
font_kit::handle::Handle::Memory { bytes, .. } => {
|
font_kit::handle::Handle::Memory { bytes, .. } => {
|
||||||
bytes.as_ref().clone()
|
Ok(bytes.as_ref().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,14 +64,17 @@ impl Renderer {
|
||||||
|
|
||||||
// TODO: Font customization
|
// TODO: Font customization
|
||||||
let font_source = font::Source::new();
|
let font_source = font::Source::new();
|
||||||
let sans_serif_font = font_source.load(&[font::Family::SansSerif]);
|
let default_font = font_source
|
||||||
let mono_font = font_source.load(&[font::Family::Monospace]);
|
.load(&[font::Family::SansSerif, font::Family::Serif])
|
||||||
|
.expect("Find sans-serif or serif font");
|
||||||
|
|
||||||
let glyph_brush = GlyphBrushBuilder::using_fonts_bytes(vec![
|
let mono_font = font_source
|
||||||
sans_serif_font,
|
.load(&[font::Family::Monospace])
|
||||||
mono_font,
|
.expect("Find monospace font");
|
||||||
])
|
|
||||||
.build(&mut device, TextureFormat::Bgra8UnormSrgb);
|
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 quad_pipeline = quad::Pipeline::new(&mut device);
|
||||||
let image_pipeline = crate::image::Pipeline::new(&mut device);
|
let image_pipeline = crate::image::Pipeline::new(&mut device);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue