macros/
macros.rs

1use si_img::{anymap, preset, render};
2
3use std::{collections::HashMap, fs, io::Write};
4
5use si_img::{SiFont, SiImage, SiPreset, TextOptions};
6
7fn main() {
8    // Create it with macro
9    preset! {
10        my_preset(img, font: SiFont, title: String, tagline: String) {
11            println!("{}", title);
12            let mut new = img;
13            render!(new: title; 480.0, 254.0; "font" &font, "scale" 64.0, "opts" &TextOptions::default(), "color" None);
14            render!(new: tagline; 480.0, 320.0; "font" &font, "scale" 48.0, "opts" &TextOptions::default(), "color" Some(String::from("#FFFFFF")));
15            new
16        }
17    };
18
19    // Use it
20    // Create the image
21    let mut img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
22    // Create the font
23    let font = SiFont::from_network(
24        "https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
25    );
26    img.load_preset(
27        my_preset,
28        anymap! {
29            font: font,
30            title: "Hello, World!".to_string(),
31            tagline: "Cool!".to_string()
32        },
33    );
34    let mut file = fs::OpenOptions::new()
35        .create(true) // To create a new file
36        .write(true)
37        // either use the ? operator or unwrap since it returns a Result
38        .open("out.png")
39        .unwrap();
40    file.write_all(&img.to_bytes()).unwrap();
41}