Improved work on Font Handling, still not finished and also added a few testcases
parent
156eb43eda
commit
e59926e0c7
|
|
@ -1,5 +1,108 @@
|
||||||
|
//width must be multiple of 8
|
||||||
|
//
|
||||||
|
//chars are build in the bitmap like this example of a width 16, height 2 font:
|
||||||
|
//12
|
||||||
|
//34
|
||||||
|
// first char is the first ascii letter you want
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct Font<'a> {
|
||||||
|
width: u8,
|
||||||
|
height: u8,
|
||||||
|
first_char: u8,
|
||||||
|
last_char: u8,
|
||||||
|
bitmap: &'a [u8],
|
||||||
|
widthmap: &'a [u8]
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn to_bitmap(input: char) -> [u8; 8] {
|
impl<'a> Font<'a> {
|
||||||
|
/// Panics if either Bitmap or Widthmap of the Font are to small for the amount and size of chars
|
||||||
|
pub fn new(width: u8, height: u8, first_char: u8, last_char: u8, bitmap: &'a [u8], widthmap: &'a [u8]) -> Font<'a> {
|
||||||
|
//Assertion so it shouldn't be able to panic later
|
||||||
|
let length_of_char = width as usize / 8 * height as usize;
|
||||||
|
let amount_of_chars = last_char as usize - first_char as usize + 1;
|
||||||
|
assert!(bitmap.len() >= amount_of_chars * length_of_char);
|
||||||
|
assert!(widthmap.len() >= amount_of_chars);
|
||||||
|
|
||||||
|
Font {width, height, first_char, last_char, bitmap, widthmap }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_length_of_char(&self) -> usize {
|
||||||
|
self.width as usize / 8 * self.height as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_char_pos(&self, input: char) -> usize {
|
||||||
|
(input as usize - self.first_char as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Can panic, when end_pos > bitmap.len, should be caught in Font::new already
|
||||||
|
pub(crate) fn get_char(&'a self, input: char) -> &'a [u8] {
|
||||||
|
let start_pos = self.get_char_pos(input) * self.get_length_of_char();
|
||||||
|
let end_pos = start_pos + self.get_length_of_char();
|
||||||
|
|
||||||
|
&self.bitmap[start_pos .. end_pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Can panic, when get_char_pos > widthmap.len(), should be caught in Font::new already
|
||||||
|
pub(crate) fn get_char_width(&self, input: char) -> u8 {
|
||||||
|
self.widthmap[self.get_char_pos(input)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fonts_test() {
|
||||||
|
let bitmap = [
|
||||||
|
0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, // '!'
|
||||||
|
0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, // '"'
|
||||||
|
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00, // '#'
|
||||||
|
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00]; // '$'
|
||||||
|
|
||||||
|
let widthmap = [8,8,8,8];
|
||||||
|
|
||||||
|
let font = Font::new(8, 8, '!' as u8, '$' as u8, &bitmap, &widthmap);
|
||||||
|
|
||||||
|
let hashtag = [0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00, 0x00];
|
||||||
|
|
||||||
|
assert_eq!(font.get_char('#'), hashtag);
|
||||||
|
|
||||||
|
assert_eq!(font.get_char('$')[7], 0x00);
|
||||||
|
|
||||||
|
assert_eq!(font.get_char_width('#'), widthmap[2]);
|
||||||
|
assert_eq!(font.get_char_width('$'), widthmap[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bitmap_8x8_test() {
|
||||||
|
let and = [0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00, 0x00];
|
||||||
|
let zero = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
|
let first_value = [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
|
let last_value = [0x00, 0x41, 0x36, 0x08, 0x00, 0x00, 0x00, 0x00];
|
||||||
|
|
||||||
|
assert_eq!(bitmap_8x8('&'), and);
|
||||||
|
|
||||||
|
assert_eq!(bitmap_8x8('ß'), zero);
|
||||||
|
assert_eq!(bitmap_8x8('°'), zero);
|
||||||
|
|
||||||
|
assert_eq!(bitmap_8x8('!'), first_value);
|
||||||
|
assert_eq!(bitmap_8x8('}'), last_value);
|
||||||
|
|
||||||
|
assert_eq!(bitmap_8x8('0')[1], 0x3E);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub(crate) fn bitmap_8x8(input: char) -> [u8; 8] {
|
||||||
// Populate the array with the data from the character array at the right index
|
// Populate the array with the data from the character array at the right index
|
||||||
match input {
|
match input {
|
||||||
'!' => [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
|
'!' => [0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
|
||||||
|
|
@ -100,10 +203,10 @@
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
pub(crate) const VCR_OSD_MONO_Bitmap = [
|
pub(crate) const VCR_OSD_MONO_Bitmap = [
|
||||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
|
af afa 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
|
||||||
0xFF, 0xFF, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F,
|
0xFF, 0xFF, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F,
|
||||||
0xFC, 0x3F, 0x30, 0x0C, 0x30, 0x0C, 0x00, 0x0F, 0x00, 0x00, 0xF0, 0x0F,
|
0xFC, 0x3F, 0x30, 0x0C, 0x30, 0x0C, 0x00, 0x0F, 0x00, 0x00, 0xF0, 0x0F,
|
||||||
0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF, 0xF0,
|
aaa 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF, 0xF0,
|
||||||
0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0, 0xF0,
|
0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0, 0xF0,
|
||||||
0x0F, 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF,
|
0x0F, 0x0F, 0x00, 0xF0, 0xF0, 0x0F, 0x0F, 0xF0, 0xF0, 0xFF, 0x0F, 0xFF,
|
||||||
0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0,
|
0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xF0, 0xFF, 0x0F, 0x0F, 0xF0,
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ impl Color {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct Graphics {
|
pub struct Graphics {
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
|
|
@ -184,8 +184,10 @@ impl Graphics {
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: implement!
|
///TODO: implement!
|
||||||
|
/// TODO: use Fonts
|
||||||
pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
|
let _a = font.get_char(input);
|
||||||
for &elem in font::bitmap_8x8(input).iter() {
|
for &elem in font::bitmap_8x8(input).iter() {
|
||||||
self.draw_byte(buffer, x0, y0 + counter * self.width, elem, color);
|
self.draw_byte(buffer, x0, y0 + counter * self.width, elem, color);
|
||||||
counter += 1;
|
counter += 1;
|
||||||
|
|
@ -202,7 +204,7 @@ impl Graphics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: implement!
|
///TODO: test!
|
||||||
pub fn draw_char_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) {
|
pub fn draw_char_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for &elem in font::bitmap_8x8(input).iter() {
|
for &elem in font::bitmap_8x8(input).iter() {
|
||||||
|
|
@ -213,7 +215,7 @@ impl Graphics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: implement!
|
///TODO: test!
|
||||||
/// no autobreak line yet
|
/// no autobreak line yet
|
||||||
pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) {
|
pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue