make splitting functionality dynamic with json-ized data structure
This commit is contained in:
parent
9b14bced87
commit
3f48f819d5
8 changed files with 125 additions and 11 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,3 +1,3 @@
|
||||||
/target
|
/target
|
||||||
/font
|
/font
|
||||||
/res
|
font.sff
|
||||||
|
|
|
||||||
26
Cargo.lock
generated
26
Cargo.lock
generated
|
|
@ -386,6 +386,12 @@ dependencies = [
|
||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jobserver"
|
name = "jobserver"
|
||||||
version = "0.1.33"
|
version = "0.1.33"
|
||||||
|
|
@ -407,6 +413,8 @@ name = "karlos-helper"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"image",
|
"image",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -760,6 +768,12 @@ version = "1.0.21"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
|
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.219"
|
version = "1.0.219"
|
||||||
|
|
@ -780,6 +794,18 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.141"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_spanned"
|
name = "serde_spanned"
|
||||||
version = "0.6.9"
|
version = "0.6.9"
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,5 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
image = "0.25.6"
|
image = "0.25.6"
|
||||||
|
serde = { version="1.0.219", features=["derive"] }
|
||||||
|
serde_json = "1.0.141"
|
||||||
|
|
|
||||||
BIN
example/ascii.png
Normal file
BIN
example/ascii.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
BIN
example/german.png
Normal file
BIN
example/german.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
26
example/index.json
Normal file
26
example/index.json
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"char_width": 8,
|
||||||
|
"char_height": 18,
|
||||||
|
"splitters": [
|
||||||
|
{
|
||||||
|
"Table": {
|
||||||
|
"path": "example/ascii.png",
|
||||||
|
"nrows": 6,
|
||||||
|
"ncols": 16,
|
||||||
|
"start_number": 32
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Individual": {
|
||||||
|
"path": "example/german.png",
|
||||||
|
"numbers": [228, 196, 246, 214, 252, 220, 223]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Single": {
|
||||||
|
"path": "example/replacement.png",
|
||||||
|
"number": 65533
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
BIN
example/replacement.png
Normal file
BIN
example/replacement.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 384 B |
80
src/main.rs
80
src/main.rs
|
|
@ -41,7 +41,7 @@ fn load_font_dir<P: AsRef<Path>>(dir_with_images: P) -> HashMap<u32, BWImage> {
|
||||||
fn image_split_table<P, Q>(
|
fn image_split_table<P, Q>(
|
||||||
src_image: P,
|
src_image: P,
|
||||||
dst_dir: Q,
|
dst_dir: Q,
|
||||||
start_number: usize,
|
start_number: u32,
|
||||||
nrows: usize,
|
nrows: usize,
|
||||||
ncols: usize,
|
ncols: usize,
|
||||||
width_per_elem: usize,
|
width_per_elem: usize,
|
||||||
|
|
@ -76,7 +76,7 @@ fn image_split_table<P, Q>(
|
||||||
fn image_split_individual<P, Q>(
|
fn image_split_individual<P, Q>(
|
||||||
src_image: P,
|
src_image: P,
|
||||||
dst_dir: Q,
|
dst_dir: Q,
|
||||||
numbers: &[usize],
|
numbers: &[u32],
|
||||||
width_per_elem: usize,
|
width_per_elem: usize,
|
||||||
height_per_elem: usize,
|
height_per_elem: usize,
|
||||||
) where
|
) where
|
||||||
|
|
@ -126,15 +126,75 @@ fn sorted_numbers_into_ranges(sorted_numbers: &[u32]) -> Vec<TableRange> {
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
enum Splitter {
|
||||||
|
Table {
|
||||||
|
path: String,
|
||||||
|
nrows: usize,
|
||||||
|
ncols: usize,
|
||||||
|
start_number: u32,
|
||||||
|
},
|
||||||
|
Individual {
|
||||||
|
path: String,
|
||||||
|
numbers: Vec<u32>,
|
||||||
|
},
|
||||||
|
Single {
|
||||||
|
path: String,
|
||||||
|
number: u32,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
struct Index {
|
||||||
|
char_width: usize,
|
||||||
|
char_height: usize,
|
||||||
|
splitters: Vec<Splitter>,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
image_split_table("res/ascii.png", "font", 32, 6, 16, 8, 18);
|
std::fs::remove_dir_all("font").unwrap();
|
||||||
image_split_individual(
|
std::fs::create_dir("font").unwrap();
|
||||||
"res/german.png",
|
let args: Vec<String> = std::env::args().collect();
|
||||||
"font",
|
let index: Index = serde_json::from_str(&std::fs::read_to_string(&args[1]).unwrap()).unwrap();
|
||||||
&[0x00e4, 0x00c4, 0x00f6, 0x00d6, 0x00fc, 0x00dc, 0x00df][..],
|
for splitter in index.splitters {
|
||||||
8,
|
match splitter {
|
||||||
18,
|
Splitter::Table {
|
||||||
);
|
path,
|
||||||
|
nrows,
|
||||||
|
ncols,
|
||||||
|
start_number,
|
||||||
|
} => {
|
||||||
|
image_split_table(
|
||||||
|
path,
|
||||||
|
"font",
|
||||||
|
start_number,
|
||||||
|
nrows,
|
||||||
|
ncols,
|
||||||
|
index.char_width,
|
||||||
|
index.char_height,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Splitter::Individual { path, numbers } => {
|
||||||
|
image_split_individual(
|
||||||
|
path,
|
||||||
|
"font",
|
||||||
|
&numbers[..],
|
||||||
|
index.char_width,
|
||||||
|
index.char_height,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Splitter::Single { path, number } => {
|
||||||
|
image_split_individual(
|
||||||
|
path,
|
||||||
|
"font",
|
||||||
|
&[number][..],
|
||||||
|
index.char_width,
|
||||||
|
index.char_height,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let map = load_font_dir("font");
|
let map = load_font_dir("font");
|
||||||
let mut sorted_keys: Vec<u32> = map.keys().copied().collect();
|
let mut sorted_keys: Vec<u32> = map.keys().copied().collect();
|
||||||
sorted_keys.sort();
|
sorted_keys.sort();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue