diff --git a/Cargo.lock b/Cargo.lock index 9e03e14..d4c553a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ahash" version = "0.7.6" @@ -16,6 +18,8 @@ name = "asciiGenerator" version = "0.1.0" dependencies = [ "fontdue", + "serde", + "serde_json", ] [[package]] @@ -54,6 +58,12 @@ dependencies = [ "ahash", ] +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + [[package]] name = "libc" version = "0.2.108" @@ -66,12 +76,84 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +[[package]] +name = "proc-macro2" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" + +[[package]] +name = "serde" +version = "1.0.131" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ad69dfbd3e45369132cc64e6748c2d65cdfb001a2b1c232d128b4ad60561c1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.131" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b710a83c4e0dff6a3d511946b95274ad9ca9e5d3ae497b63fda866ac955358d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "ttf-parser" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ae2f58a822f08abdaf668897e96a5656fe72f5a9ce66422423e8849384872e6" +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + [[package]] name = "version_check" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index 4e53078..07cfe42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,14 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -fontdue = "0.6.2" \ No newline at end of file +fontdue = "0.6.2" + +# The core APIs, including the Serialize and Deserialize traits. Always +# required when using Serde. The "derive" feature is only required when +# using #[derive(Serialize, Deserialize)] to make Serde work with structs +# and enums defined in your crate. +serde = { version = "1.0", features = ["derive"] } + +# Each data format lives in its own crate; the sample code below uses JSON +# but you may be using a different one. +serde_json = "1.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2ba6514..4436db3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use fontdue; use std::collections::HashMap; use std::ops::Add; use std::fs::File; +use serde_json::to_string; fn main() { @@ -12,21 +13,47 @@ fn main() { //let mut currentPath = env::current_dir().expect("what").as_path(); let data = fs::read("./font/Lucida Console.ttf").expect("it didnt work"); let font = fontdue::Font::from_bytes(data, fontdue::FontSettings::default()).expect("yessir"); - let mut output: HashMap>> = HashMap::new(); //todo Second value should be bitmap type??? + let mut font_data: HashMap>> = HashMap::new(); //todo Second value should be bitmap type??? let mut char_index:u32 = 0; //NOTE this starts at 0 const RASTER_FACTOR: f32 = 15.0; while char_index < 10000 { let char = std::char::from_u32(char_index).expect("error converting u32 to char"); - char_index += 1; let (metrics, mut unsorted_raster) = font.rasterize(char, RASTER_FACTOR); if (unsorted_raster.len() > 0) { - + let sorted = rasterize(unsorted_raster, RASTER_FACTOR as usize); + font_data.insert(char, sorted); //(chunks, remainder) = unsorted_raster.as_chunks(RASTER_FACTOR as usize); //output.insert(char, chunks.clone()); } + char_index += 1; } + + print_json(&mut font_data); + //INFO the data in fontdata is a grid as an array with each value being a number from 1 to 100, not a float + //TODO load in image and convert to greyscale + //TODO create image string + //TODO loop through chunks of image adding a new line at the end of each line + //TODO compare each chunk to the font_data to find its most representative character and add to the image string + +} + +fn print_json(output: &mut HashMap>>) { + let json = serde_json::to_string(&output).unwrap(); + + print!("{}", json); +} + + +fn rasterize(mut unsorted: Vec, chunk_size: usize) -> Vec> { + let mut result: Vec> = Vec::new(); + while unsorted.len() > chunk_size { + let mut temp = unsorted.clone(); + unsorted = temp.split_off(chunk_size); + result.push(temp.clone()); + } + return result; } //returns remainder, chunk