Skip to main content

figlet_rs/
toilet.rs

1use crate::shared::{
2    load_font_file, parse_font_bytes, parse_font_content, render, FIGcharacter, FIGure, FontData,
3    HeaderLine,
4};
5use std::collections::HashMap;
6
7/// Toilet font, which supports loading `.tlf` files, including zip-packaged fonts.
8#[derive(Debug, Clone)]
9pub struct Toilet {
10    pub header_line: HeaderLine,
11    pub comments: String,
12    pub fonts: HashMap<u32, FIGcharacter>,
13}
14
15impl Toilet {
16    /// generate Toilet font from string literal
17    pub fn from_content(contents: &str) -> Result<Toilet, String> {
18        Ok(parse_font_content(contents)?.into())
19    }
20
21    /// generate Toilet font from specified file
22    pub fn from_file(fontname: &str) -> Result<Toilet, String> {
23        Ok(load_font_file(fontname)?.into())
24    }
25
26    fn from_bytes(bytes: &[u8]) -> Result<Toilet, String> {
27        Ok(parse_font_bytes(bytes)?.into())
28    }
29
30    /// the smblock Toilet font bundled with the crate
31    pub fn smblock() -> Result<Toilet, String> {
32        Toilet::from_bytes(include_bytes!("../resources/smblock.tlf"))
33    }
34
35    /// the mono12 Toilet font bundled with the crate
36    pub fn mono12() -> Result<Toilet, String> {
37        Toilet::from_bytes(include_bytes!("../resources/mono12.tlf"))
38    }
39
40    /// the future Toilet font bundled with the crate
41    pub fn future() -> Result<Toilet, String> {
42        Toilet::from_bytes(include_bytes!("../resources/future.tlf"))
43    }
44
45    /// the wideterm Toilet font bundled with the crate
46    pub fn wideterm() -> Result<Toilet, String> {
47        Toilet::from_bytes(include_bytes!("../resources/wideterm.tlf"))
48    }
49
50    /// the mono9 Toilet font bundled with the crate
51    pub fn mono9() -> Result<Toilet, String> {
52        Toilet::from_bytes(include_bytes!("../resources/mono9.tlf"))
53    }
54
55    /// convert string literal to FIGure
56    pub fn convert(&self, message: &str) -> Option<FIGure<'_>> {
57        render(&self.header_line, &self.fonts, message)
58    }
59}
60
61impl From<FontData> for Toilet {
62    fn from(data: FontData) -> Self {
63        Self {
64            header_line: data.header_line,
65            comments: data.comments,
66            fonts: data.fonts,
67        }
68    }
69}