changed font file to AsRef<Path>, trial code for calling cowgen
This commit is contained in:
parent
2b6a3f24ad
commit
d4946a29d1
|
@ -1,14 +1,14 @@
|
|||
use ril::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::{fs::File, path::Path};
|
||||
|
||||
pub struct Template<T: AsRef<str>> {
|
||||
elements: Vec<Element<T>>,
|
||||
pub struct Template<T: AsRef<str>, V: AsRef<Path>> {
|
||||
elements: Vec<Element<T, V>>,
|
||||
dimensions: (u32, u32),
|
||||
base: Image,
|
||||
}
|
||||
|
||||
impl<T: AsRef<str>> Template<T> {
|
||||
pub fn new(base: Vec<u8>, elements: Vec<Element<T>>) -> Result<Self, ril::Error> {
|
||||
impl<T: AsRef<str>, V: AsRef<Path>> Template<T, V> {
|
||||
pub fn new(base: Vec<u8>, elements: Vec<Element<T, V>>) -> Result<Self, CowError> {
|
||||
let base: Image = Image::from_bytes_inferred(base)?;
|
||||
Ok(Self {
|
||||
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),
|
||||
dimensions: (u32, u32),
|
||||
media: Media<T>,
|
||||
media: Media<T, V>,
|
||||
}
|
||||
|
||||
impl<T: AsRef<str>> Element<T> {
|
||||
pub fn new(media: Media<T>, position: (i32, i32), dimensions: (u32, u32)) -> Self {
|
||||
impl<T: AsRef<str>, V: AsRef<Path>> Element<T, V> {
|
||||
pub fn new(media: Media<T, V>, position: (i32, i32), dimensions: (u32, u32)) -> Self {
|
||||
Self {
|
||||
position,
|
||||
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 {
|
||||
match self.media {
|
||||
Media::Image(image) => image.resized(
|
||||
|
@ -72,7 +72,7 @@ impl<T: AsRef<str>> Into<Image> for Element<T> {
|
|||
)
|
||||
.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<T: AsRef<str>> Into<Image> for Element<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum Media<T: AsRef<str>> {
|
||||
Text(Text<T>),
|
||||
pub enum Media<T: AsRef<str>, V: AsRef<Path>> {
|
||||
Text(Text<T, V>),
|
||||
Image(Image),
|
||||
}
|
||||
|
||||
pub struct Text<T: AsRef<str>> {
|
||||
pub struct Text<T: AsRef<str>, V: AsRef<Path>> {
|
||||
text: T,
|
||||
font: File,
|
||||
font: V,
|
||||
size: f32,
|
||||
fill: (u8, u8, u8),
|
||||
}
|
||||
|
||||
impl<T: AsRef<str>> Text<T> {
|
||||
pub fn new(text: T, font: File, size: f32, fill: (u8, u8, u8)) -> Self {
|
||||
impl<T: AsRef<str>, V: AsRef<Path>> Text<T, V> {
|
||||
pub fn new(text: T, font: V, size: f32, fill: (u8, u8, u8)) -> Self {
|
||||
Self {
|
||||
text,
|
||||
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)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue