2020-10-26 19:01:56 -07:00
|
|
|
use pc_screen_font;
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
// 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/
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
pub trait Font {
|
|
|
|
// Once Rust supports existential types, this needs to be an existential type.
|
|
|
|
type Glyph: Glyph;
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
/// The size, in pixels, of the bounding box of each glyph in this font.
|
|
|
|
fn bounding_box(&self) -> (usize, usize);
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
fn lookup<'a>(&'a self, ch: char) -> Option<&'a Self::Glyph>;
|
|
|
|
}
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
pub trait Glyph {
|
|
|
|
/// The width, in pixels, of this specific glyph.
|
|
|
|
/// This may be a different size than the font's bounding box.
|
|
|
|
fn width(&self) -> usize;
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
/// The height, in pixels, of this specific glyph.
|
|
|
|
/// This may be a different size than the font's bounding box.
|
|
|
|
fn height(&self) -> usize;
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
// TODO: Support glyph offsets relative to the font bounding box.
|
2020-07-16 01:17:45 -07:00
|
|
|
|
2020-07-18 00:24:03 -07:00
|
|
|
/// Check whether an individual pixel of this glyph is set.
|
|
|
|
/// This function will panic if `x` and `y` are outside the width and height of this glyph.
|
|
|
|
fn get(&self, x: usize, y: usize) -> bool;
|
2020-07-16 01:17:45 -07:00
|
|
|
}
|
2020-10-26 19:01:56 -07:00
|
|
|
|
|
|
|
impl Font for pc_screen_font::Font {
|
|
|
|
type Glyph = pc_screen_font::Glyph;
|
|
|
|
fn bounding_box(&self) -> (usize, usize) { self.bounding_box() }
|
|
|
|
fn lookup<'a>(&'a self, ch: char) -> Option<&'a Self::Glyph> { self.lookup(ch) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Glyph for pc_screen_font::Glyph {
|
|
|
|
fn width(&self) -> usize { self.width() }
|
|
|
|
fn height(&self) -> usize { self.height() }
|
|
|
|
fn get(&self, x: usize, y: usize) -> bool { self.get(x, y).unwrap() }
|
|
|
|
}
|