1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use image::imageops::FilterType;
use image::{DynamicImage, ImageFormat};
use std::fs::File;
use std::path::Path;

pub const APPLE_TOUCH_ICON_57: Preset<'static> =
    Preset::new("apple_touch_icon-57.png", Format::Png, 57, 57);
pub const APPLE_TOUCH_ICON_60: Preset<'static> =
    Preset::new("apple_touch_icon-60.png", Format::Png, 60, 60);
pub const APPLE_TOUCH_ICON_72: Preset<'static> =
    Preset::new("apple_touch_icon-72.png", Format::Png, 70, 70);
pub const APPLE_TOUCH_ICON_76: Preset<'static> =
    Preset::new("apple_touch_icon-76.png", Format::Png, 76, 76);
pub const APPLE_TOUCH_ICON_114: Preset<'static> =
    Preset::new("apple_touch_icon-114.png", Format::Png, 114, 114);
pub const APPLE_TOUCH_ICON_120: Preset<'static> =
    Preset::new("apple_touch_icon-120.png", Format::Png, 120, 120);
pub const APPLE_TOUCH_ICON_144: Preset<'static> =
    Preset::new("apple_touch_icon-144.png", Format::Png, 144, 144);
pub const APPLE_TOUCH_ICON_152: Preset<'static> =
    Preset::new("apple_touch_icon-152.png", Format::Png, 152, 152);

pub const FAVICON: Preset<'static> = Preset::new("favicon.ico", Format::Ico, 64, 64);
pub const FAVICON_16: Preset<'static> = Preset::new("favicon-16.png", Format::Png, 16, 16);
pub const FAVICON_32: Preset<'static> = Preset::new("favicon-32.png", Format::Png, 32, 32);
pub const FAVICON_96: Preset<'static> = Preset::new("favicon-96.png", Format::Png, 96, 96);
pub const FAVICON_128: Preset<'static> = Preset::new("favicon-128.png", Format::Png, 128, 128);
pub const FAVICON_196: Preset<'static> = Preset::new("favicon-32.png", Format::Png, 196, 196);

pub const MS_TILE_70: Preset<'static> = Preset::new("mstile-70.png", Format::Png, 70, 70);
pub const MS_TILE_144: Preset<'static> = Preset::new("mstile-144.png", Format::Png, 144, 144);
pub const MS_TILE_150: Preset<'static> = Preset::new("mstile-150.png", Format::Png, 150, 150);
pub const MS_TILE_310X150: Preset<'static> =
    Preset::new("mstile-310x150.png", Format::Png, 310, 150);
pub const MS_TILE_310: Preset<'static> = Preset::new("mstile-310.png", Format::Png, 310, 310);

pub enum Format {
    Ico,
    Png,
}

pub struct Preset<'a> {
    pub(crate) name: &'a str,
    pub(crate) format: Format,
    pub(crate) height: u32,
    pub(crate) width: u32,
}

impl<'a> Preset<'a> {
    pub const fn new(name: &'a str, format: Format, height: u32, width: u32) -> Self {
        Self {
            name,
            format,
            height,
            width,
        }
    }
}

pub struct Favicon<'a, P: AsRef<Path>> {
    pub(crate) image: DynamicImage,
    pub(crate) out_dir: P,
    pub(crate) presets: Vec<Preset<'a>>,
}

impl<'a, P: AsRef<Path>> Favicon<'a, P> {
    pub fn new(file: P, out_dir: P, presets: Vec<Preset<'a>>) -> Self
    where
        P: AsRef<Path>,
    {
        let image = image::open(file).unwrap();

        Self {
            image,
            out_dir,
            presets,
        }
    }

    pub fn empty(file: P, out_dir: P) -> Self {
        let image = image::open(file).unwrap();

        Self {
            image,
            out_dir,
            presets: Vec::default(),
        }
    }

    pub fn scale_down(&self, preset: Preset<'a>) {
        let scaled = self
            .image
            .resize(preset.width, preset.height, FilterType::Nearest);
        let mut output = File::create(preset.name).unwrap();

        scaled.write_to(&mut output, ImageFormat::Png).unwrap();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let favicon = Favicon::empty("fixtures/dice.png", "output.png");

        favicon.scale_down(APPLE_TOUCH_ICON_57);
    }
}