diff options
author | 2019-11-03 04:39:11 +0100 | |
---|---|---|
committer | 2019-11-03 04:39:11 +0100 | |
commit | 2c6bfdbc8c2262c3550fa16d4472e29d73077956 (patch) | |
tree | 4428dea15c7ec8f9baa385927ce96264470d1915 /wgpu | |
parent | ef056d84890e745010675e70f734f882f89356c2 (diff) | |
download | iced-2c6bfdbc8c2262c3550fa16d4472e29d73077956.tar.gz iced-2c6bfdbc8c2262c3550fa16d4472e29d73077956.tar.bz2 iced-2c6bfdbc8c2262c3550fa16d4472e29d73077956.zip |
Implement debug view and load system fonts
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/Cargo.toml | 1 | ||||
-rw-r--r-- | wgpu/src/font.rs | 38 | ||||
-rw-r--r-- | wgpu/src/lib.rs | 1 | ||||
-rw-r--r-- | wgpu/src/renderer.rs | 64 |
4 files changed, 92 insertions, 12 deletions
diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 04fae248..2c212286 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -14,4 +14,5 @@ wgpu_glyph = { version = "0.4", git = "https://github.com/hecrj/wgpu_glyph", rev raw-window-handle = "0.3" image = "0.22" glam = "0.8" +font-kit = "0.4" log = "0.4" diff --git a/wgpu/src/font.rs b/wgpu/src/font.rs new file mode 100644 index 00000000..e2838268 --- /dev/null +++ b/wgpu/src/font.rs @@ -0,0 +1,38 @@ +pub use font_kit::family_name::FamilyName as Family; + +pub struct Source { + raw: font_kit::sources::fontconfig::FontconfigSource, +} + +impl Source { + pub fn new() -> Self { + Source { + raw: font_kit::sources::fontconfig::FontconfigSource::new(), + } + } + + pub fn load(&self, families: &[Family]) -> Vec<u8> { + let font = self + .raw + .select_best_match( + families, + &font_kit::properties::Properties::default(), + ) + .expect("Find font"); + + match font { + font_kit::handle::Handle::Path { path, .. } => { + use std::io::Read; + + let mut buf = Vec::new(); + let mut reader = std::fs::File::open(path).expect("Read font"); + let _ = reader.read_to_end(&mut buf); + + buf + } + font_kit::handle::Handle::Memory { bytes, .. } => { + bytes.as_ref().clone() + } + } + } +} diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 01dc4c20..f504897d 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,3 +1,4 @@ +mod font; mod image; mod primitive; mod quad; diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 1988dfae..770430ad 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,6 +1,6 @@ -use crate::{quad, Image, Primitive, Quad, Transformation}; +use crate::{font, quad, Image, Primitive, Quad, Transformation}; use iced_native::{ - renderer::Debugger, renderer::Windowed, Background, Color, Layout, Metrics, + renderer::Debugger, renderer::Windowed, Background, Color, Layout, MouseCursor, Point, Rectangle, Widget, }; @@ -62,13 +62,16 @@ impl Renderer { limits: Limits { max_bind_groups: 2 }, }); - // TODO: Think about font loading strategy - // Loading system fonts with fallback may be a good idea - let font: &[u8] = - include_bytes!("../../examples/resources/Roboto-Regular.ttf"); + // 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_font_bytes(font) - .build(&mut device, TextureFormat::Bgra8UnormSrgb); + let glyph_brush = GlyphBrushBuilder::using_fonts_bytes(vec![ + sans_serif_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); @@ -83,9 +86,10 @@ impl Renderer { } } - fn draw( + fn draw<T: AsRef<str>>( &mut self, (primitive, mouse_cursor): &(Primitive, MouseCursor), + overlay: &[T], target: &mut Target, ) -> MouseCursor { log::debug!("Drawing"); @@ -127,6 +131,7 @@ impl Renderer { )); self.draw_primitive(primitive, &mut layers); + self.draw_overlay(overlay, &mut layers); for layer in layers { self.flush(transformation, &layer, &mut encoder, &frame.view); @@ -260,6 +265,41 @@ impl Renderer { } } + fn draw_overlay<'a, T: AsRef<str>>( + &mut self, + lines: &'a [T], + layers: &mut Vec<Layer<'a>>, + ) { + let first = layers.first().unwrap(); + let mut overlay = Layer::new(first.bounds, 0); + + let font_id = + wgpu_glyph::FontId(self.glyph_brush.borrow().fonts().len() - 1); + let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 }; + + for (i, line) in lines.iter().enumerate() { + overlay.text.push(Section { + text: line.as_ref(), + screen_position: (11.0, 11.0 + 25.0 * i as f32), + color: [0.9, 0.9, 0.9, 1.0], + scale, + font_id, + ..Section::default() + }); + + overlay.text.push(Section { + text: line.as_ref(), + screen_position: (10.0, 10.0 + 25.0 * i as f32), + color: [0.0, 0.0, 0.0, 1.0], + scale, + font_id, + ..Section::default() + }); + } + + layers.push(overlay); + } + fn flush( &mut self, transformation: Transformation, @@ -328,13 +368,13 @@ impl Windowed for Renderer { Self::new() } - fn draw( + fn draw<T: AsRef<str>>( &mut self, output: &Self::Output, - metrics: Option<Metrics>, + overlay: &[T], target: &mut Target, ) -> MouseCursor { - self.draw(output, target) + self.draw(output, overlay, target) } } |