Completely rewrite nearly everything having to do with fonts.

master
James T. Martin 2020-07-18 00:24:03 -07:00
parent 07715b614b
commit 538dfea78c
Signed by: james
GPG Key ID: 4B7F3DA9351E577C
5 changed files with 171 additions and 132 deletions

View File

@ -7,14 +7,17 @@ pub mod tty;
use crate::graphics::color::{COLOR_BLACK, COLOR_WHITE}; use crate::graphics::color::{COLOR_BLACK, COLOR_WHITE};
use crate::graphics::display::gop::GopDisplay; use crate::graphics::display::gop::GopDisplay;
use crate::graphics::font::font; use crate::graphics::font::psf::PSF;
use crate::graphics::terminal::display::DisplayTerminal; use crate::graphics::terminal::display::DisplayTerminal;
use crate::graphics::tty::Tty; use crate::graphics::tty::Tty;
use crate::graphics::tty::terminal::TerminalTty; use crate::graphics::tty::terminal::TerminalTty;
pub fn do_graphics(st: &uefi::prelude::SystemTable<uefi::prelude::Boot>) { pub fn do_graphics(st: &uefi::prelude::SystemTable<uefi::prelude::Boot>) {
let font_data = core::include_bytes!("graphics/font/cozette.psf");
let font = PSF::parse(font_data);
let mut display = GopDisplay::init(st.boot_services()); let mut display = GopDisplay::init(st.boot_services());
let mut terminal = DisplayTerminal::new(&mut display, font(), COLOR_BLACK, COLOR_WHITE); let mut terminal = DisplayTerminal::new(&mut display, &font, COLOR_BLACK, COLOR_WHITE);
let mut tty = TerminalTty::new(&mut terminal); let mut tty = TerminalTty::new(&mut terminal);
for _ in 0..30 { for _ in 0..30 {

View File

@ -1,5 +1,5 @@
use crate::graphics::color::RGB; use crate::graphics::color::RGB;
use crate::graphics::font::psf::PSFGlyph; use crate::graphics::font::Glyph;
pub mod gop; pub mod gop;
@ -8,6 +8,7 @@ pub trait Display {
fn width(&self) -> usize { self.resolution().0 } fn width(&self) -> usize { self.resolution().0 }
fn height(&self) -> usize { self.resolution().1 } fn height(&self) -> usize { self.resolution().1 }
// HACK: This interface sucks.
unsafe fn set_pixel(&mut self, color: RGB, x: usize, y: usize); unsafe fn set_pixel(&mut self, color: RGB, x: usize, y: usize);
fn set_pixel_ignore_oob(&mut self, color: RGB, x: usize, y: usize) { fn set_pixel_ignore_oob(&mut self, color: RGB, x: usize, y: usize) {
if x > self.width() || y > self.height() { if x > self.width() || y > self.height() {
@ -21,34 +22,30 @@ pub trait Display {
fn clear(&mut self, color: RGB); fn clear(&mut self, color: RGB);
unsafe fn draw_glyph(&mut self, color: RGB, x: usize, y: usize, glyph: PSFGlyph) { unsafe fn draw_glyph(&mut self, bounding_box: (usize, usize), x: usize, y: usize, color: RGB, glyph: &dyn Glyph) {
// Glyphs may actually be larger than their nominal bounding box. // We only assume that space was left for pixels within the bounding box,
// In fact, the Cozette font is like this: the heart symbol is 7 pixels wide, // and that pixels outside the bounding box may be out-of-bounds.
// despite nominally being a 6x13 font. // We use `set_pixel` for the in-bounds pixels and `set_pixel_ignore_oob`
// However, despite not being an intended use of the format, that extra pixel // for the out-of-bounds pixels.
// can still be stored in the padding bits of the glyph (and is!).
// Therefore, we just continue writing those extra bits if they are present. // HACK: I should be able to figure out whether a row or column will be out-of-bounds statically, and:
// Note that there is no similar trick for the height, // 1. If it is going to be out-of-bounds and is inside the bounding box, panic, and
// because the height doesn't have padding. // 2. if it is outside of the bounding box, don't bother trying to draw that row.
for glyph_x in 0..glyph.width() {
for glyph_y in 0..glyph.height() { for glyph_x in 0..glyph.width().min(bounding_box.0) {
for glyph_y in 0..glyph.height().min(bounding_box.1) {
if glyph.get(glyph_x, glyph_y) { if glyph.get(glyph_x, glyph_y) {
self.set_pixel(color, x + glyph_x as usize, y + glyph_y as usize); self.set_pixel(color, x + glyph_x, y + glyph_y);
} }
} }
} }
// Sometimes, a font may actually have pixels outside its bounding box! for glyph_x in glyph.width().min(bounding_box.0)..glyph.width() {
// For example, in Cozette, a 6x13 font, ♡ is actually 7 pixels wide. for glyph_y in 0..glyph.height().min(bounding_box.1) {
// This data is still stored in the padding bits of the glyph.
// Note that there is no similar trick for height because height doesn't have padding.
// Futhermore, this only works on fonts whose width is not a multiple of eight.
for glyph_x in glyph.width()..num_integer::div_ceil(glyph.width(), 8) * 8 {
for glyph_y in 0..glyph.height() {
if glyph.get(glyph_x, glyph_y) { if glyph.get(glyph_x, glyph_y) {
// These pixels *nominally* aren't supposed to be there, // These pixels *nominally* aren't supposed to be there,
// so we only force the pixels inside the bounding box. // so we only force the pixels inside the bounding box.
self.set_pixel_ignore_oob(color, x + glyph_x as usize, y + glyph_y as usize); self.set_pixel_ignore_oob(color, x + glyph_x, y + glyph_y);
} }
} }
} }

View File

@ -1,64 +1,31 @@
use alloc::sync::Arc;
use alloc::vec::Vec;
use psf::*;
pub mod psf; pub mod psf;
static mut FONT: Option<Arc<PSF>> = None; // Note that currently the Font and Glyph traits are fairly specialized to PSF.
// They will certainly have to be modified to support other types of fonts,
// but they *work* for now, and that's what's important/
pub fn font() -> Arc<PSF> { pub trait Font {
unsafe { // Once Rust supports existential types, this needs to be an existential type.
FONT.clone().unwrap_or_else(|| { type Glyph: Glyph;
let font = Arc::new(parse_font());
FONT = Some(font.clone()); /// The size, in pixels, of the bounding box of each glyph in this font.
font fn bounding_box(&self) -> (usize, usize);
})
} fn lookup<'a>(&'a self, ch: char) -> Option<&'a Self::Glyph>;
} }
fn parse_font() -> PSF { pub trait Glyph {
use core::convert::TryInto; /// The width, in pixels, of this specific glyph.
let font = core::include_bytes!("font/cozette.psf"); /// This may be a different size than the font's bounding box.
let length = u32::from_le_bytes(font[16..20].try_into().unwrap()); fn width(&self) -> usize;
let charsize = u32::from_le_bytes(font[20..24].try_into().unwrap());
let height = u32::from_le_bytes(font[24..28].try_into().unwrap());
let width = u32::from_le_bytes(font[28..32].try_into().unwrap());
let glyphs_size = (length * charsize) as usize; /// The height, in pixels, of this specific glyph.
let mut glyphs = Vec::with_capacity(glyphs_size); /// This may be a different size than the font's bounding box.
glyphs.extend_from_slice(&font[32..glyphs_size + 32]); fn height(&self) -> usize;
let mut unicode_map = Vec::new(); // TODO: Support glyph offsets relative to the font bounding box.
let unicode_info = &font[glyphs_size + 32..];
let mut glyph = 0;
let mut i = 0;
while i < unicode_info.len() {
let mut nc = unicode_info[i];
while nc != 0xFE && nc != 0xFF { /// Check whether an individual pixel of this glyph is set.
let ch_bytes = nc.leading_ones().max(1) as usize; /// This function will panic if `x` and `y` are outside the width and height of this glyph.
let st = core::str::from_utf8(&unicode_info[i..i + ch_bytes as usize]).expect("Invalid character"); fn get(&self, x: usize, y: usize) -> bool;
let ch = st.chars().next().unwrap();
unicode_map.push(UnicodeMap { c: ch, i: glyph });
i += ch_bytes;
nc = unicode_info[i];
}
// Ignore multi-codepoint spellings of characters (for now).
while nc != 0xFF {
i += 1;
nc = unicode_info[i];
}
i += 1;
glyph += 1;
}
PSF {
width: width,
height: height,
length: length,
charsize: charsize,
glyphs: glyphs,
unicode: unicode_map,
}
} }

View File

@ -1,38 +1,102 @@
use alloc::vec::Vec; use alloc::vec::Vec;
use crate::graphics::font::{Font, Glyph};
pub struct UnicodeMap {
pub c: char,
pub i: usize,
}
pub struct PSF { pub struct PSF {
pub width: u32, width: usize,
pub height: u32, height: usize,
pub length: u32, glyphs: Vec<PSFGlyph>,
pub charsize: u32, // TODO: Replace this with a proper associative structure.
pub glyphs: Vec<u8>, unicode: Vec<UnicodeMap>,
pub unicode: Vec<UnicodeMap>,
} }
pub struct PSFGlyph<'a> { /// Associates a unicode character and a glyph.
width: u32, struct UnicodeMap {
height: u32, c: char,
bitmap: &'a [u8], // The index of the glyph.
i: usize,
}
pub struct PSFGlyph {
bitmap: Vec<u8>,
line_size: usize,
width: usize,
height: usize,
} }
impl PSF { impl PSF {
pub fn resolution(&self) -> (u32, u32) { pub fn parse(font: &[u8]) -> PSF {
(self.width, self.height) use core::convert::TryInto;
}
// The number of glyphs in this font.
pub fn width(&self) -> u32 { let length = u32::from_le_bytes(font[16..20].try_into().unwrap()) as usize;
self.width // The size in bytes of a single glyph.
} let charsize = u32::from_le_bytes(font[20..24].try_into().unwrap()) as usize;
// The height in pixels of this font's bounding box.
pub fn height(&self) -> u32 { let height = u32::from_le_bytes(font[24..28].try_into().unwrap()) as usize;
self.height // The width in pixels of this font's bounding box.
let width = u32::from_le_bytes(font[28..32].try_into().unwrap()) as usize;
// The size in bytes of a single row of pixels in a glyph.
let line_size = num_integer::div_ceil(width, 8);
let glyphs_offset = 32; // the size of the header
let glyphs_size = length * charsize;
let unicode_offset = glyphs_offset + glyphs_size;
let mut glyphs = Vec::with_capacity(length);
for i in 0..length {
let mut bitmap = Vec::with_capacity(charsize);
let bitmap_begin = glyphs_offset + charsize * i;
let bitmap_end = bitmap_begin + charsize;
bitmap.extend_from_slice(&font[bitmap_begin..bitmap_end]);
glyphs.push(PSFGlyph {
bitmap: bitmap,
line_size: line_size,
// Glyphs may overflow the font's nominal resolution in the padding bytes of the line!
// This trick only works for the width because there is no vertical padding.
// TODO: Pre-compute widths and bounding box offsets of individual glyphs.
width: line_size * 8,
height: height,
});
}
// HACK: This unicode map parser is still a mess.
let mut unicode_map = Vec::new();
let unicode_info = &font[unicode_offset..];
let mut glyph = 0;
let mut i = 0;
while i < unicode_info.len() {
let mut nc = unicode_info[i];
while nc != 0xFE && nc != 0xFF {
let ch_bytes = nc.leading_ones().max(1) as usize;
let st = core::str::from_utf8(&unicode_info[i..i + ch_bytes]).expect("Invalid character");
let ch = st.chars().next().unwrap();
unicode_map.push(UnicodeMap { c: ch, i: glyph });
i += ch_bytes;
nc = unicode_info[i];
}
// TODO: Support multi-codepoint spellings of characters.
while nc != 0xFF {
i += 1;
nc = unicode_info[i];
}
i += 1;
glyph += 1;
}
PSF {
width: width,
height: height,
glyphs: glyphs,
unicode: unicode_map,
}
} }
/// The index of the glyph associated with a particular unicde character.
fn index_of(&self, c: char) -> Option<usize> { fn index_of(&self, c: char) -> Option<usize> {
for entry in &self.unicode { for entry in &self.unicode {
if entry.c == c { if entry.c == c {
@ -41,31 +105,33 @@ impl PSF {
} }
None None
} }
}
fn get_bitmap<'a>(&'a self, index: usize) -> &'a [u8] { impl Font for PSF {
let byte_index = self.charsize as usize * index; type Glyph = PSFGlyph;
&self.glyphs[byte_index..byte_index + self.charsize as usize]
fn bounding_box(&self) -> (usize, usize) {
(self.width, self.height)
} }
pub fn lookup<'a>(&'a self, c: char) -> Option<PSFGlyph<'a>> { fn lookup<'a>(&'a self, c: char) -> Option<&'a PSFGlyph> {
self.index_of(c).map(|i| PSFGlyph { self.index_of(c).map(|i| &self.glyphs[i])
width: self.width,
height: self.height,
bitmap: self.get_bitmap(i)
})
} }
} }
impl PSFGlyph<'_> { impl Glyph for PSFGlyph {
pub fn width(&self) -> u32 { self.width } fn width(&self) -> usize { self.width }
fn height(&self) -> usize { self.height }
pub fn height(&self) -> u32 { self.height } fn get(&self, x: usize, y: usize) -> bool {
if x > self.width || y > self.height {
use crate::graphics::tty::Tty;
crate::panic!("Glyph pixel index out of bounds.");
}
pub fn get(&self, x: u32, y: u32) -> bool {
let line_size = num_integer::div_ceil(self.width, 8);
let (line_byte_index, bit_index) = num_integer::div_rem(x, 8); let (line_byte_index, bit_index) = num_integer::div_rem(x, 8);
let mask = 0b10000000 >> bit_index; let mask = 0b10000000 >> bit_index;
let byte = self.bitmap[(y * line_size + line_byte_index) as usize]; let byte = self.bitmap[(y * self.line_size + line_byte_index) as usize];
byte & mask > 0 byte & mask > 0
} }
} }

View File

@ -1,36 +1,38 @@
use alloc::sync::Arc; use crate::graphics::color::{Color, RGB};
use crate::graphics::color::RGB;
use crate::graphics::display::Display; use crate::graphics::display::Display;
use crate::graphics::font::psf::PSF; use crate::graphics::font::{Font, Glyph};
use crate::graphics::terminal::Terminal; use crate::graphics::terminal::Terminal;
use crate::graphics::terminal::frame::TerminalFrame; use crate::graphics::terminal::frame::TerminalFrame;
pub struct DisplayTerminal<'display> { pub struct DisplayTerminal<'d, 'f, G: Glyph> {
display: &'display mut dyn Display, display: &'d mut (dyn Display + 'd),
font: &'f (dyn Font<Glyph = G> + 'f),
frame: TerminalFrame, frame: TerminalFrame,
font: Arc<PSF>,
bg: RGB, bg: RGB,
fg: RGB, fg: RGB,
} }
impl DisplayTerminal<'_> { impl<G: Glyph> DisplayTerminal<'_, '_, G> {
pub fn new<'display>(display: &'display mut dyn Display, font: Arc<PSF>, bg: RGB, fg: RGB) -> DisplayTerminal<'display> { pub fn new<'d, 'f>
(display: &'d mut (dyn Display + 'd), font: &'f (dyn Font<Glyph = G> + 'f),
bg: impl Color, fg: impl Color)
-> DisplayTerminal<'d, 'f, G> {
let (dp_width, dp_height) = display.resolution(); let (dp_width, dp_height) = display.resolution();
let (ft_width, ft_height) = font.resolution(); let (ft_width, ft_height) = font.bounding_box();
let ch_width = dp_width / ft_width as usize; let ch_width = dp_width / ft_width as usize;
let ch_height = dp_height / ft_height as usize; let ch_height = dp_height / ft_height as usize;
DisplayTerminal { DisplayTerminal {
display: display, display: display,
frame: TerminalFrame::new((ch_width, ch_height)),
font: font, font: font,
bg: bg, frame: TerminalFrame::new((ch_width, ch_height)),
fg: fg, bg: bg.into_rgb(),
fg: fg.into_rgb(),
} }
} }
} }
impl Terminal for DisplayTerminal<'_> { impl<G: Glyph> Terminal for DisplayTerminal<'_, '_, G> {
fn get_frame<'a>(&'a self) -> &'a TerminalFrame { fn get_frame<'a>(&'a self) -> &'a TerminalFrame {
&self.frame &self.frame
} }
@ -46,9 +48,13 @@ impl Terminal for DisplayTerminal<'_> {
let c = self.frame.get(x, y); let c = self.frame.get(x, y);
if c == '\u{0}' { continue; } if c == '\u{0}' { continue; }
// FIXME: This code shouldn't throw errors.
// instead, it should display some kind of missing character.
let glyph = self.font.lookup(c).expect("Character missing from font."); 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 { unsafe {
self.display.draw_glyph(self.fg, self.font.width() as usize * x, self.font.height() as usize * y, glyph); self.display.draw_glyph(self.font.bounding_box(), px_x, px_y, self.fg, glyph);
} }
} }
} }