changed font file to AsRef<Path>, trial code for calling cowgen

This commit is contained in:
emilis 2022-11-02 02:10:46 +00:00
parent 2b6a3f24ad
commit d4946a29d1
3 changed files with 51 additions and 19 deletions

View File

@ -1,14 +1,14 @@
use ril::prelude::*; use ril::prelude::*;
use std::fs::File; use std::{fs::File, path::Path};
pub struct Template<T: AsRef<str>> { pub struct Template<T: AsRef<str>, V: AsRef<Path>> {
elements: Vec<Element<T>>, elements: Vec<Element<T, V>>,
dimensions: (u32, u32), dimensions: (u32, u32),
base: Image, base: Image,
} }
impl<T: AsRef<str>> Template<T> { impl<T: AsRef<str>, V: AsRef<Path>> Template<T, V> {
pub fn new(base: Vec<u8>, elements: Vec<Element<T>>) -> Result<Self, ril::Error> { pub fn new(base: Vec<u8>, elements: Vec<Element<T, V>>) -> Result<Self, CowError> {
let base: Image = Image::from_bytes_inferred(base)?; let base: Image = Image::from_bytes_inferred(base)?;
Ok(Self { Ok(Self {
dimensions: base.dimensions(), dimensions: base.dimensions(),
@ -41,14 +41,14 @@ impl<T: AsRef<str>> Template<T> {
} }
} }
pub struct Element<T: AsRef<str>> { pub struct Element<T: AsRef<str>, V: AsRef<Path>> {
position: (i32, i32), position: (i32, i32),
dimensions: (u32, u32), dimensions: (u32, u32),
media: Media<T>, media: Media<T, V>,
} }
impl<T: AsRef<str>> Element<T> { impl<T: AsRef<str>, V: AsRef<Path>> Element<T, V> {
pub fn new(media: Media<T>, position: (i32, i32), dimensions: (u32, u32)) -> Self { pub fn new(media: Media<T, V>, position: (i32, i32), dimensions: (u32, u32)) -> Self {
Self { Self {
position, position,
dimensions, dimensions,
@ -57,7 +57,7 @@ impl<T: AsRef<str>> Element<T> {
} }
} }
impl<T: AsRef<str>> Into<Image> for Element<T> { impl<T: AsRef<str>, V: AsRef<Path>> Into<Image> for Element<T, V> {
fn into(self) -> Image { fn into(self) -> Image {
match self.media { match self.media {
Media::Image(image) => image.resized( Media::Image(image) => image.resized(
@ -72,7 +72,7 @@ impl<T: AsRef<str>> Into<Image> for Element<T> {
) )
.with( .with(
&TextSegment::new( &TextSegment::new(
&Font::from_reader(text.font, 12.0).unwrap(), &Font::from_reader(File::open(text.font).unwrap(), 12.0).unwrap(),
text.text, text.text,
Dynamic::Rgb(Rgb::new(text.fill.0, text.fill.1, text.fill.2)), Dynamic::Rgb(Rgb::new(text.fill.0, text.fill.1, text.fill.2)),
) )
@ -82,20 +82,20 @@ impl<T: AsRef<str>> Into<Image> for Element<T> {
} }
} }
pub enum Media<T: AsRef<str>> { pub enum Media<T: AsRef<str>, V: AsRef<Path>> {
Text(Text<T>), Text(Text<T, V>),
Image(Image), Image(Image),
} }
pub struct Text<T: AsRef<str>> { pub struct Text<T: AsRef<str>, V: AsRef<Path>> {
text: T, text: T,
font: File, font: V,
size: f32, size: f32,
fill: (u8, u8, u8), fill: (u8, u8, u8),
} }
impl<T: AsRef<str>> Text<T> { impl<T: AsRef<str>, V: AsRef<Path>> Text<T, V> {
pub fn new(text: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self { pub fn new(text: T, font: V, size: f32, fill: (u8, u8, u8)) -> Self {
Self { Self {
text, text,
font, font,
@ -105,6 +105,17 @@ impl<T: AsRef<str>> Text<T> {
} }
} }
#[derive(Clone, Debug)]
pub enum CowError {
Other(String),
}
impl From<ril::Error> for CowError {
fn from(r: ril::Error) -> Self {
CowError::Other(r.to_string())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

BIN
cowmic/src/cow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -1,3 +1,24 @@
fn main() { use std::{fs::File, io::Write};
println!("Hello, world!");
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(())
} }