arcana is a fucking loser this is whyu you dont get bitches
This commit is contained in:
parent
9d70f1598d
commit
2b6a3f24ad
|
@ -1,28 +1,47 @@
|
||||||
use ril::prelude::*;
|
use ril::prelude::*;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
struct Template<T: AsRef<str>> {
|
pub struct Template<T: AsRef<str>> {
|
||||||
elements: Vec<Element<T>>,
|
elements: Vec<Element<T>>,
|
||||||
dimensions: (u32, u32),
|
dimensions: (u32, u32),
|
||||||
base: Image,
|
base: Image,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsRef<str>> Template<T> {
|
impl<T: AsRef<str>> Template<T> {
|
||||||
pub fn new(elements: Vec<Element<T>>) -> Self {
|
pub fn new(base: Vec<u8>, elements: Vec<Element<T>>) -> Result<Self, ril::Error> {
|
||||||
Self {
|
let base: Image = Image::from_bytes_inferred(base)?;
|
||||||
|
Ok(Self {
|
||||||
|
dimensions: base.dimensions(),
|
||||||
elements,
|
elements,
|
||||||
}
|
base,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn produce(self, image: File) -> Result<File, ril::Error> {
|
pub fn produce(mut self) -> Vec<u8> {
|
||||||
|
let size = self.base.dimensions();
|
||||||
for element in self.elements {
|
for element in self.elements {
|
||||||
|
let mut pos = element.position;
|
||||||
|
let mut img: Image = element.into();
|
||||||
|
if pos.0 < 0 {
|
||||||
|
img.crop(-pos.0 as u32, 0, size.0, size.1);
|
||||||
|
pos.0 = 0;
|
||||||
|
}
|
||||||
|
if pos.1 < 0 {
|
||||||
|
img.crop(0, -pos.1 as u32, size.0, size.1);
|
||||||
|
pos.1 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
image.draw
|
self.base
|
||||||
|
.draw(&Paste::new(img).with_position(pos.0 as u32, pos.1 as u32));
|
||||||
}
|
}
|
||||||
|
let mut buf = Vec::<u8>::new();
|
||||||
|
self.base.encode(ImageFormat::Png, &mut buf).unwrap();
|
||||||
|
|
||||||
|
buf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Element<T: AsRef<str>> {
|
pub struct Element<T: AsRef<str>> {
|
||||||
position: (i32, i32),
|
position: (i32, i32),
|
||||||
dimensions: (u32, u32),
|
dimensions: (u32, u32),
|
||||||
media: Media<T>,
|
media: Media<T>,
|
||||||
|
@ -39,16 +58,26 @@ impl<T: AsRef<str>> Element<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsRef<str>> Into<Image> for Element<T> {
|
impl<T: AsRef<str>> Into<Image> for Element<T> {
|
||||||
pub fn into(self) -> Image {
|
fn into(self) -> Image {
|
||||||
match self.media {
|
match self.media {
|
||||||
Media::Image(image) => {
|
Media::Image(image) => image.resized(
|
||||||
image.resized(self.dimensions.0, self.dimensions.1, ResizeAlgorithm::Bicubic)
|
self.dimensions.0,
|
||||||
},
|
self.dimensions.1,
|
||||||
Media::Text(text) => {
|
ResizeAlgorithm::Bicubic,
|
||||||
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))
|
Media::Text(text) => Image::new(
|
||||||
.with_size(text.size))
|
self.dimensions.0,
|
||||||
},
|
self.dimensions.1,
|
||||||
|
Dynamic::Rgba(Rgba::transparent()),
|
||||||
|
)
|
||||||
|
.with(
|
||||||
|
&TextSegment::new(
|
||||||
|
&Font::from_reader(text.font, 12.0).unwrap(),
|
||||||
|
text.text,
|
||||||
|
Dynamic::Rgb(Rgb::new(text.fill.0, text.fill.1, text.fill.2)),
|
||||||
|
)
|
||||||
|
.with_size(text.size),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +87,7 @@ pub enum Media<T: AsRef<str>> {
|
||||||
Image(Image),
|
Image(Image),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Text<T: AsRef<str>> {
|
pub struct Text<T: AsRef<str>> {
|
||||||
text: T,
|
text: T,
|
||||||
font: File,
|
font: File,
|
||||||
size: f32,
|
size: f32,
|
||||||
|
@ -66,9 +95,9 @@ struct Text<T: AsRef<str>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsRef<str>> Text<T> {
|
impl<T: AsRef<str>> Text<T> {
|
||||||
pub fn new(contents: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self {
|
pub fn new(text: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self {
|
||||||
Self {
|
Self {
|
||||||
contents,
|
text,
|
||||||
font,
|
font,
|
||||||
size,
|
size,
|
||||||
fill,
|
fill,
|
||||||
|
@ -76,8 +105,6 @@ impl<T: AsRef<str>> Text<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue