diff --git a/cowgen/src/lib.rs b/cowgen/src/lib.rs index 1d000f6..a0d0237 100644 --- a/cowgen/src/lib.rs +++ b/cowgen/src/lib.rs @@ -1,14 +1,14 @@ use ril::prelude::*; -use std::fs::File; +use std::{fs::File, path::Path}; -pub struct Template> { - elements: Vec>, +pub struct Template, V: AsRef> { + elements: Vec>, dimensions: (u32, u32), base: Image, } -impl> Template { - pub fn new(base: Vec, elements: Vec>) -> Result { +impl, V: AsRef> Template { + pub fn new(base: Vec, elements: Vec>) -> Result { let base: Image = Image::from_bytes_inferred(base)?; Ok(Self { dimensions: base.dimensions(), @@ -41,14 +41,14 @@ impl> Template { } } -pub struct Element> { +pub struct Element, V: AsRef> { position: (i32, i32), dimensions: (u32, u32), - media: Media, + media: Media, } -impl> Element { - pub fn new(media: Media, position: (i32, i32), dimensions: (u32, u32)) -> Self { +impl, V: AsRef> Element { + pub fn new(media: Media, position: (i32, i32), dimensions: (u32, u32)) -> Self { Self { position, dimensions, @@ -57,7 +57,7 @@ impl> Element { } } -impl> Into for Element { +impl, V: AsRef> Into for Element { fn into(self) -> Image { match self.media { Media::Image(image) => image.resized( @@ -72,7 +72,7 @@ impl> Into for Element { ) .with( &TextSegment::new( - &Font::from_reader(text.font, 12.0).unwrap(), + &Font::from_reader(File::open(text.font).unwrap(), 12.0).unwrap(), text.text, Dynamic::Rgb(Rgb::new(text.fill.0, text.fill.1, text.fill.2)), ) @@ -82,20 +82,20 @@ impl> Into for Element { } } -pub enum Media> { - Text(Text), +pub enum Media, V: AsRef> { + Text(Text), Image(Image), } -pub struct Text> { +pub struct Text, V: AsRef> { text: T, - font: File, + font: V, size: f32, fill: (u8, u8, u8), } -impl> Text { - pub fn new(text: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self { +impl, V: AsRef> Text { + pub fn new(text: T, font: V, size: f32, fill: (u8, u8, u8)) -> Self { Self { text, font, @@ -105,6 +105,17 @@ impl> Text { } } +#[derive(Clone, Debug)] +pub enum CowError { + Other(String), +} + +impl From for CowError { + fn from(r: ril::Error) -> Self { + CowError::Other(r.to_string()) + } +} + #[cfg(test)] mod tests { #[test] diff --git a/cowmic/src/cow.png b/cowmic/src/cow.png new file mode 100644 index 0000000..acf90ad Binary files /dev/null and b/cowmic/src/cow.png differ diff --git a/cowmic/src/main.rs b/cowmic/src/main.rs index e7a11a9..5e13593 100644 --- a/cowmic/src/main.rs +++ b/cowmic/src/main.rs @@ -1,3 +1,24 @@ -fn main() { - println!("Hello, world!"); +use std::{fs::File, io::Write}; + +use cowgen::{CowError, Element, Text}; + +fn main() -> Result<(), CowError> { + let out = cowgen::Template::new( + include_bytes!("cow.png").to_vec(), + vec![Element::new( + cowgen::Media::Text(Text::new( + "Hello", + "/usr/share/fonts/TTF/OpenSans-ExtraBold.ttf", + 12.0, + (0, 0, 0), + )), + (0, 0), + (64, 64), + )], + )? + .produce(); + let mut f = File::create("out.png").unwrap(); + f.write_all(&out).unwrap(); + f.flush().unwrap(); + Ok(()) }