feat: check in
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"editor.inlayHints.enabled": "offUnlessPressed"
|
"editor.inlayHints.enabled": "off",
|
||||||
|
"editor.formatOnSave": true,
|
||||||
}
|
}
|
||||||
25
dot-matrix/Cargo.lock
generated
25
dot-matrix/Cargo.lock
generated
@@ -100,6 +100,7 @@ dependencies = [
|
|||||||
"glib 0.17.10",
|
"glib 0.17.10",
|
||||||
"gtk4",
|
"gtk4",
|
||||||
"rand",
|
"rand",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -549,6 +550,12 @@ dependencies = [
|
|||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.172"
|
version = "0.2.172"
|
||||||
@@ -734,6 +741,12 @@ dependencies = [
|
|||||||
"semver",
|
"semver",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "semver"
|
name = "semver"
|
||||||
version = "1.0.26"
|
version = "1.0.26"
|
||||||
@@ -760,6 +773,18 @@ dependencies = [
|
|||||||
"syn 2.0.101",
|
"syn 2.0.101",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.140"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_spanned"
|
name = "serde_spanned"
|
||||||
version = "0.6.8"
|
version = "0.6.8"
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ gtk = { version = "0.7", package = "gtk4" }
|
|||||||
glib = "0.17"
|
glib = "0.17"
|
||||||
cairo-rs = "0.17"
|
cairo-rs = "0.17"
|
||||||
rand = "0.9.1"
|
rand = "0.9.1"
|
||||||
|
serde_json = "1.0.140"
|
||||||
|
|||||||
@@ -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 struct Font {
|
||||||
pub name: String,
|
// pub name: String,
|
||||||
pub size: f64,
|
// pub font_json: String,
|
||||||
pub font_json: String,
|
pub font_parsed: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the embedded font JSON by string key
|
/// Return the embedded font JSON by string key
|
||||||
pub fn get_font_json(name: &str) -> Option<&'static str> {
|
pub fn get_font_json(name: &str) -> Option<&'static str> {
|
||||||
match name {
|
match name {
|
||||||
"5x7" => Some(FONT_5x7_JSON),
|
"5x7" => Some(FONT_5X7_JSON),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Font {
|
impl Font {
|
||||||
pub fn new(name: &str, size: f64) -> Self {
|
pub fn new(name: &str) -> Self {
|
||||||
Self {
|
let font_json = get_font_json(name)
|
||||||
font_json: get_font_json(name)
|
.unwrap_or_else(|| panic!("Unknown font: {}", name))
|
||||||
.unwrap_or_else(|| panic!("Unknown font: {}", name))
|
.to_string();
|
||||||
.to_string(),
|
|
||||||
name: name.to_string(),
|
let font_parsed: Value = serde_json::from_str(&font_json).unwrap();
|
||||||
size,
|
|
||||||
}
|
return Self {
|
||||||
|
// name: name.to_string(),
|
||||||
|
// font_json,
|
||||||
|
font_parsed,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn description(&self) -> String {
|
pub fn get_char_bitmap(&self, index: u64) -> Option<Vec<Vec<u8>>> {
|
||||||
format!("{} {}", self.name, self.size)
|
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;
|
mod font;
|
||||||
|
|
||||||
|
use crate::font::Font; // this brings in the struct Font
|
||||||
use gtk::cairo::Context;
|
use gtk::cairo::Context;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Application, ApplicationWindow, DrawingArea};
|
use gtk::{Application, ApplicationWindow, DrawingArea};
|
||||||
use rand::prelude::*;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let app = Application::builder()
|
let app = Application::builder()
|
||||||
@@ -24,19 +24,18 @@ fn build_ui(app: >k::Application) {
|
|||||||
|
|
||||||
let drawing_area = DrawingArea::new();
|
let drawing_area = DrawingArea::new();
|
||||||
|
|
||||||
let mut rng = rand::rng();
|
let rows = 100 as u8;
|
||||||
|
let cols = 100 as u8;
|
||||||
let rows = 100 as u32;
|
|
||||||
let cols = 100 as u32;
|
|
||||||
|
|
||||||
// Generate the matrix using iterators
|
// Generate the matrix using iterators
|
||||||
let mut dot_matrix: Vec<Vec<u32>> = (0..rows)
|
let mut dot_matrix: Vec<Vec<u8>> = (0..rows).map(|_| (0..cols).map(|_| 0).collect()).collect();
|
||||||
.map(|_| (0..cols).map(|_| rng.random_range(0..=10)).collect())
|
let insert_matrix: Vec<Vec<u8>> = (0..7).map(|_| (0..5).map(|_| 1).collect()).collect();
|
||||||
.collect();
|
let font_5x7: Font = Font::new("5x7");
|
||||||
|
let char_bitmap = font_5x7.get_char_bitmap(1).unwrap();
|
||||||
let insert_matrix: Vec<Vec<u32>> = (0..7).map(|_| (0..5).map(|_| 1).collect()).collect();
|
|
||||||
|
|
||||||
insert_matrix_at(&mut dot_matrix, &insert_matrix, 0, 0);
|
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| {
|
drawing_area.set_draw_func(move |_area, cr, width, height| {
|
||||||
draw_dot_matrix(cr, width, height, &dot_matrix);
|
draw_dot_matrix(cr, width, height, &dot_matrix);
|
||||||
@@ -46,7 +45,7 @@ fn build_ui(app: >k::Application) {
|
|||||||
window.show();
|
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 rows = matrix.len();
|
||||||
// let _cols = if rows > 0 { matrix[0].len() } else { 0 };
|
// 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);
|
cr.set_source_rgb(1.0, 1.0, 0.0);
|
||||||
|
|
||||||
// Calculate spacing
|
// Calculate spacing
|
||||||
let spacing_x = 8.0;
|
let spacing_x = 25.0;
|
||||||
let spacing_y = 8.0;
|
let spacing_y = 25.0;
|
||||||
let radius = 2.0;
|
let radius = 10.0;
|
||||||
|
|
||||||
for (row_index, row) in matrix.iter().enumerate() {
|
for (row_index, row) in matrix.iter().enumerate() {
|
||||||
for (col_index, &val) in row.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(
|
fn insert_matrix_at(
|
||||||
target: &mut Vec<Vec<u32>>,
|
target: &mut Vec<Vec<u8>>,
|
||||||
insert: &Vec<Vec<u32>>,
|
insert: &Vec<Vec<u8>>,
|
||||||
row_offset: usize,
|
row_offset: usize,
|
||||||
col_offset: usize,
|
col_offset: usize,
|
||||||
) {
|
) {
|
||||||
|
|||||||
Reference in New Issue
Block a user