include default font in binary

This commit is contained in:
Vinzenz Schroeter 2024-05-18 17:20:30 +02:00
parent a2bd0978e7
commit b84c18f5c7
2 changed files with 20 additions and 9 deletions

View file

@ -1,18 +1,20 @@
use std::sync::Arc;
use font_kit::canvas::{Canvas, Format, RasterizationOptions}; use font_kit::canvas::{Canvas, Format, RasterizationOptions};
use font_kit::font::Font;
use font_kit::hinting::HintingOptions; use font_kit::hinting::HintingOptions;
use pathfinder_geometry::transform2d::Transform2F; use pathfinder_geometry::transform2d::Transform2F;
use pathfinder_geometry::vector::{vec2f, vec2i}; use pathfinder_geometry::vector::{vec2f, vec2i};
use servicepoint2::{PixelGrid, TILE_SIZE}; use servicepoint2::{PixelGrid, TILE_SIZE};
const DEFAULT_FONT_FILE: &[u8] = include_bytes!("../Web437_IBM_BIOS.woff");
pub struct BitmapFont { pub struct BitmapFont {
bitmaps: [PixelGrid; u8::MAX as usize], bitmaps: [PixelGrid; u8::MAX as usize],
} }
impl BitmapFont { impl BitmapFont {
pub fn load_file(file: &str) -> BitmapFont { pub fn load(font: Font) -> BitmapFont {
let font = font_kit::font::Font::from_path(file, 0)
.expect("could not load font");
let mut bitmaps = core::array::from_fn(|_| { let mut bitmaps = core::array::from_fn(|_| {
PixelGrid::new(TILE_SIZE as usize, TILE_SIZE as usize) PixelGrid::new(TILE_SIZE as usize, TILE_SIZE as usize)
}); });
@ -57,3 +59,11 @@ impl BitmapFont {
&self.bitmaps[char_code as usize] &self.bitmaps[char_code as usize]
} }
} }
impl Default for BitmapFont {
fn default() -> Self {
let font = Font::from_bytes(Arc::new(DEFAULT_FONT_FILE.to_vec()), 0)
.expect("could not load included font");
Self::load(font)
}
}

View file

@ -6,7 +6,7 @@ use std::sync::{mpsc, RwLock};
use std::time::Duration; use std::time::Duration;
use clap::Parser; use clap::Parser;
use log::{info, warn}; use log::{info, LevelFilter, warn};
use servicepoint2::{ use servicepoint2::{
ByteGrid, Command, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid, TILE_HEIGHT, ByteGrid, Command, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid, TILE_HEIGHT,
TILE_WIDTH, TILE_WIDTH,
@ -36,7 +36,10 @@ struct Cli {
} }
fn main() { fn main() {
env_logger::init(); env_logger::builder()
.filter_level(LevelFilter::Info)
.parse_default_env()
.init();
let mut cli = Cli::parse(); let mut cli = Cli::parse();
if !(cli.red || cli.blue || cli.green) { if !(cli.red || cli.blue || cli.green) {
@ -49,8 +52,6 @@ fn main() {
.set_nonblocking(true) .set_nonblocking(true)
.expect("could not enter non blocking mode"); .expect("could not enter non blocking mode");
let font = BitmapFont::load_file("Web437_IBM_BIOS.woff");
let display = RwLock::new(PixelGrid::new( let display = RwLock::new(PixelGrid::new(
PIXEL_WIDTH as usize, PIXEL_WIDTH as usize,
PIXEL_HEIGHT as usize, PIXEL_HEIGHT as usize,
@ -60,7 +61,7 @@ fn main() {
luma.fill(u8::MAX); luma.fill(u8::MAX);
let luma = RwLock::new(luma); let luma = RwLock::new(luma);
run(&display, &luma, socket, font, &cli); run(&display, &luma, socket, BitmapFont::default(), &cli);
} }
fn run( fn run(