2022-11-02 00:51:06 +00:00
|
|
|
use ril::prelude::*;
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
struct Template<T: AsRef<str>> {
|
|
|
|
elements: Vec<Element<T>>,
|
|
|
|
dimensions: (u32, u32),
|
|
|
|
base: Image,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<str>> Template<T> {
|
|
|
|
pub fn new(elements: Vec<Element<T>>) -> Self {
|
|
|
|
Self {
|
|
|
|
elements,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn produce(self, image: File) -> Result<File, ril::Error> {
|
|
|
|
for element in self.elements {
|
|
|
|
|
|
|
|
image.draw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Element<T: AsRef<str>> {
|
|
|
|
position: (i32, i32),
|
|
|
|
dimensions: (u32, u32),
|
|
|
|
media: Media<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<str>> Element<T> {
|
|
|
|
pub fn new(media: Media<T>, position: (i32, i32), dimensions: (u32, u32)) -> Self {
|
|
|
|
Self {
|
|
|
|
position,
|
|
|
|
dimensions,
|
|
|
|
media,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<str>> Into<Image> for Element<T> {
|
|
|
|
pub fn into(self) -> Image {
|
|
|
|
match self.media {
|
|
|
|
Media::Image(image) => {
|
|
|
|
image.resized(self.dimensions.0, self.dimensions.1, ResizeAlgorithm::Bicubic)
|
|
|
|
},
|
|
|
|
Media::Text(text) => {
|
|
|
|
Image::new(self.dimensions.0, self.dimensions.1, Rgba::transparent())
|
|
|
|
.with(TextSegment::new(text.font, text.text, Rgb::new(text.fill.0, text.fill.1, text.fill.2))
|
|
|
|
.with_size(text.size))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Media<T: AsRef<str>> {
|
|
|
|
Text(Text<T>),
|
|
|
|
Image(Image),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Text<T: AsRef<str>> {
|
|
|
|
text: T,
|
|
|
|
font: File,
|
|
|
|
size: f32,
|
|
|
|
fill: (u8, u8, u8),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<str>> Text<T> {
|
|
|
|
pub fn new(contents: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self {
|
|
|
|
Self {
|
|
|
|
contents,
|
|
|
|
font,
|
|
|
|
size,
|
|
|
|
fill,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-01 21:31:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
let result = 2 + 2;
|
|
|
|
assert_eq!(result, 4);
|
|
|
|
}
|
|
|
|
}
|