Implement debug view and load system fonts
This commit is contained in:
parent
ef056d8489
commit
2c6bfdbc8c
17 changed files with 418 additions and 241 deletions
|
|
@ -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"
|
||||
|
|
|
|||
38
wgpu/src/font.rs
Normal file
38
wgpu/src/font.rs
Normal file
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
mod font;
|
||||
mod image;
|
||||
mod primitive;
|
||||
mod quad;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue