- added a new libary libm containing procedural macros for the kernel.
these should be used to include external files and resources in the kernel binary
at compile time.
- libm currently supports loading psf-1 formatted fonts
- added two fonts that are included in the binary at compile time
- refactored libk to make the crate structure more organised and maintainable in future.
new structure:
- drivers (hardware interaction)
- resources (consts and statics included either manually or via macros)
- std (standard functions for higher level interaction with the os, for example creating windows)
- added geometry.rs
- provides the Vec2<T> struct for use with dimensions, coordinates etc.
- added window.rs
- provides the Window struct for rendering the state of an application to the screen
- added application.rs
- provides the Application trait for custom programs to implement in order to run
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "libm"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
darling = "0.20.10"
|
||||
proc-macro2 = "1.0.93"
|
||||
quote = "1.0.38"
|
||||
syn = "2.0.98"
|
||||
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
@@ -0,0 +1,69 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{LitStr, parse_macro_input};
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn include_font(item: TokenStream) -> TokenStream {
|
||||
let filename = parse_macro_input!(item as LitStr);
|
||||
let file_path = filename.value().to_string();
|
||||
|
||||
println!("Loading font: [{}]", file_path);
|
||||
|
||||
let font_data = match Font::new(load_file(file_path)) {
|
||||
Ok(font) => font.0,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
|
||||
quote!(
|
||||
[
|
||||
#(
|
||||
[#(#font_data),*]
|
||||
),*
|
||||
]
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
struct Font([[u8; 16]; 512]);
|
||||
|
||||
impl Font {
|
||||
const MAGIC: u16 = 0x3604;
|
||||
|
||||
pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result<Font, &'static str> {
|
||||
let magic: u16 = (data[0] as u16) << 8 | data[1] as u16;
|
||||
let mode = data[2];
|
||||
let size = data[3];
|
||||
|
||||
if magic != Self::MAGIC {
|
||||
return Err("Magic value is invalid!");
|
||||
}
|
||||
|
||||
let has_512_glyphs = (mode & 0x01) != 0;
|
||||
let mut glyphs = [[0; 16]; 512];
|
||||
let glyph_count = if has_512_glyphs { 512 } else { 256 };
|
||||
|
||||
for i in 0..(glyph_count as usize) {
|
||||
let mut buff: [u8; 16] = [0; 16];
|
||||
for j in 0..(size as usize) {
|
||||
buff[j] = data[4 + i * (size as usize) + j];
|
||||
}
|
||||
glyphs[i] = buff;
|
||||
}
|
||||
|
||||
Ok(Font(glyphs))
|
||||
}
|
||||
}
|
||||
|
||||
fn load_file(filename: String) -> [u8; (32 + 2) * 512 + 4] {
|
||||
let mut buf = [0; (32 + 2) * 512 + 4];
|
||||
let mut f = File::open(filename).unwrap();
|
||||
f.seek(SeekFrom::Start(0)).unwrap();
|
||||
f.read(&mut buf).unwrap();
|
||||
|
||||
return buf;
|
||||
}
|
||||
Reference in New Issue
Block a user