bootproof/src/driver/text_display/graphic.rs

63 lines
2.1 KiB
Rust
Raw Normal View History

use crate::driver::graphic_display::GraphicDisplay;
use crate::driver::text_display::{TextDisplayFrame, TextDisplay};
use crate::graphics::color::{Color, RGB};
use crate::graphics::font::{Font, Glyph};
/// A virtual text display that renders itself onto a graphic display.
pub struct GraphicTextDisplay<'d, 'f, G: Glyph> {
display: &'d mut (dyn GraphicDisplay + 'd),
font: &'f (dyn Font<Glyph = G> + 'f),
frame: TextDisplayFrame,
bg: RGB,
fg: RGB,
}
impl<G: Glyph> GraphicTextDisplay<'_, '_, G> {
pub fn new<'d, 'f>
(display: &'d mut (dyn GraphicDisplay + 'd), font: &'f (dyn Font<Glyph = G> + 'f),
bg: impl Color, fg: impl Color)
-> GraphicTextDisplay<'d, 'f, G> {
2020-07-16 17:04:19 -07:00
let (dp_width, dp_height) = display.resolution();
let (ft_width, ft_height) = font.bounding_box();
2020-07-16 17:04:19 -07:00
let ch_width = dp_width / ft_width as usize;
let ch_height = dp_height / ft_height as usize;
GraphicTextDisplay {
2020-07-16 17:04:19 -07:00
display: display,
font: font,
frame: TextDisplayFrame::new((ch_width, ch_height)),
bg: bg.into_rgb(),
fg: fg.into_rgb(),
}
}
}
impl<G: Glyph> TextDisplay for GraphicTextDisplay<'_, '_, G> {
fn borrow_frame<'a>(&'a self) -> &'a TextDisplayFrame {
2020-07-16 17:04:19 -07:00
&self.frame
}
fn borrow_mut_frame<'a>(&'a mut self) -> &'a mut TextDisplayFrame {
2020-07-16 17:04:19 -07:00
&mut self.frame
}
fn refresh(&mut self) {
2020-07-16 17:04:19 -07:00
self.display.clear(self.bg);
for x in 0..self.frame.width() {
for y in 0..self.frame.height() {
let c = self.frame.get(x, y);
if c == '\u{0}' { continue; }
// FIXME: This code shouldn't throw errors.
// instead, it should display some kind of missing character.
2020-07-16 17:04:19 -07:00
let glyph = self.font.lookup(c).expect("Character missing from font.");
let px_x = x * self.font.bounding_box().0;
let px_y = y * self.font.bounding_box().1;
unsafe {
self.display.draw_glyph(self.font.bounding_box(), px_x, px_y, self.fg, glyph);
}
}
}
}
}