feat: check in
This commit is contained in:
@@ -1,31 +1,59 @@
|
||||
const FONT_5x7_JSON: &str = include_str!("../../assets/fonts/5x7.json");
|
||||
use serde_json::Value;
|
||||
|
||||
const FONT_5X7_JSON: &str = include_str!("../../assets/fonts/5x7.json");
|
||||
|
||||
pub struct Font {
|
||||
pub name: String,
|
||||
pub size: f64,
|
||||
pub font_json: String,
|
||||
// pub name: String,
|
||||
// pub font_json: String,
|
||||
pub font_parsed: Value,
|
||||
}
|
||||
|
||||
/// Return the embedded font JSON by string key
|
||||
pub fn get_font_json(name: &str) -> Option<&'static str> {
|
||||
match name {
|
||||
"5x7" => Some(FONT_5x7_JSON),
|
||||
"5x7" => Some(FONT_5X7_JSON),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl Font {
|
||||
pub fn new(name: &str, size: f64) -> Self {
|
||||
Self {
|
||||
font_json: get_font_json(name)
|
||||
.unwrap_or_else(|| panic!("Unknown font: {}", name))
|
||||
.to_string(),
|
||||
name: name.to_string(),
|
||||
size,
|
||||
}
|
||||
pub fn new(name: &str) -> Self {
|
||||
let font_json = get_font_json(name)
|
||||
.unwrap_or_else(|| panic!("Unknown font: {}", name))
|
||||
.to_string();
|
||||
|
||||
let font_parsed: Value = serde_json::from_str(&font_json).unwrap();
|
||||
|
||||
return Self {
|
||||
// name: name.to_string(),
|
||||
// font_json,
|
||||
font_parsed,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn description(&self) -> String {
|
||||
format!("{} {}", self.name, self.size)
|
||||
pub fn get_char_bitmap(&self, index: u64) -> Option<Vec<Vec<u8>>> {
|
||||
let bytes = self
|
||||
.font_parsed
|
||||
.get("data")?
|
||||
.as_array()?
|
||||
.iter()
|
||||
.find(|entry| entry.get("n").map_or(false, |n| n == index))
|
||||
.and_then(|character| {
|
||||
character.get("b")?.as_array().map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|val| val.as_u64())
|
||||
.map(|n| n as u8)
|
||||
.collect::<Vec<u8>>()
|
||||
})
|
||||
})?;
|
||||
|
||||
let bits = bytes
|
||||
.iter()
|
||||
.flat_map(|&byte| (0..8).rev().map(move |i| (byte >> i) & 1))
|
||||
.collect::<Vec<u8>>();
|
||||
|
||||
let char_matrix = bits.chunks(5).map(|chunk| chunk.to_vec()).collect();
|
||||
|
||||
Some(char_matrix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
pub mod font;
|
||||
mod font;
|
||||
|
||||
pub use font::Font;
|
||||
@@ -1,9 +1,9 @@
|
||||
mod font;
|
||||
|
||||
use crate::font::Font; // this brings in the struct Font
|
||||
use gtk::cairo::Context;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Application, ApplicationWindow, DrawingArea};
|
||||
use rand::prelude::*;
|
||||
|
||||
fn main() {
|
||||
let app = Application::builder()
|
||||
@@ -24,19 +24,18 @@ fn build_ui(app: >k::Application) {
|
||||
|
||||
let drawing_area = DrawingArea::new();
|
||||
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let rows = 100 as u32;
|
||||
let cols = 100 as u32;
|
||||
let rows = 100 as u8;
|
||||
let cols = 100 as u8;
|
||||
|
||||
// Generate the matrix using iterators
|
||||
let mut dot_matrix: Vec<Vec<u32>> = (0..rows)
|
||||
.map(|_| (0..cols).map(|_| rng.random_range(0..=10)).collect())
|
||||
.collect();
|
||||
|
||||
let insert_matrix: Vec<Vec<u32>> = (0..7).map(|_| (0..5).map(|_| 1).collect()).collect();
|
||||
let mut dot_matrix: Vec<Vec<u8>> = (0..rows).map(|_| (0..cols).map(|_| 0).collect()).collect();
|
||||
let insert_matrix: Vec<Vec<u8>> = (0..7).map(|_| (0..5).map(|_| 1).collect()).collect();
|
||||
let font_5x7: Font = Font::new("5x7");
|
||||
let char_bitmap = font_5x7.get_char_bitmap(1).unwrap();
|
||||
|
||||
insert_matrix_at(&mut dot_matrix, &insert_matrix, 0, 0);
|
||||
insert_matrix_at(&mut dot_matrix, &insert_matrix, 0, 6);
|
||||
insert_matrix_at(&mut dot_matrix, &char_bitmap, 0, 12);
|
||||
|
||||
drawing_area.set_draw_func(move |_area, cr, width, height| {
|
||||
draw_dot_matrix(cr, width, height, &dot_matrix);
|
||||
@@ -46,7 +45,7 @@ fn build_ui(app: >k::Application) {
|
||||
window.show();
|
||||
}
|
||||
|
||||
fn draw_dot_matrix(cr: &Context, width: i32, height: i32, matrix: &Vec<Vec<u32>>) {
|
||||
fn draw_dot_matrix(cr: &Context, width: i32, height: i32, matrix: &Vec<Vec<u8>>) {
|
||||
// let rows = matrix.len();
|
||||
// let _cols = if rows > 0 { matrix[0].len() } else { 0 };
|
||||
|
||||
@@ -59,9 +58,9 @@ fn draw_dot_matrix(cr: &Context, width: i32, height: i32, matrix: &Vec<Vec<u32>>
|
||||
cr.set_source_rgb(1.0, 1.0, 0.0);
|
||||
|
||||
// Calculate spacing
|
||||
let spacing_x = 8.0;
|
||||
let spacing_y = 8.0;
|
||||
let radius = 2.0;
|
||||
let spacing_x = 25.0;
|
||||
let spacing_y = 25.0;
|
||||
let radius = 10.0;
|
||||
|
||||
for (row_index, row) in matrix.iter().enumerate() {
|
||||
for (col_index, &val) in row.iter().enumerate() {
|
||||
@@ -76,8 +75,8 @@ fn draw_dot_matrix(cr: &Context, width: i32, height: i32, matrix: &Vec<Vec<u32>>
|
||||
}
|
||||
|
||||
fn insert_matrix_at(
|
||||
target: &mut Vec<Vec<u32>>,
|
||||
insert: &Vec<Vec<u32>>,
|
||||
target: &mut Vec<Vec<u8>>,
|
||||
insert: &Vec<Vec<u8>>,
|
||||
row_offset: usize,
|
||||
col_offset: usize,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user