termcinema-engine 0.1.0

🧠 Core typewriter-style terminal animation engine (SVG renderer) for termcinema
Documentation
/// Metadata descriptor for an embeddable font.
///
/// Each embedded font includes a path to the TTF file and
/// a CSS `font-family` name used during SVG rendering.
pub struct EmbeddedFont {
    /// Path to the font file (relative to project root).
    pub path: &'static str,

    /// CSS font-family name to inject via `@font-face`.
    pub css_family: &'static str,
}

/// Built-in embedded font list, ordered by priority.
///
/// Fonts in this list will be embedded into SVG if matched by name.
/// These fonts offer reliable layout and aesthetics across platforms.
pub static EMBEDDED_FONTS: &[EmbeddedFont] = &[
    EmbeddedFont {
        path: "assets/fonts/PxPlus_IBM_VGA_8x16.ttf",
        css_family: "PxPlus IBM VGA8", // Classic VGA pixel font, highly aligned, retro feel.
    },
    EmbeddedFont {
        path: "assets/fonts/JetBrainsMono-Regular.ttf",
        css_family: "JetBrains Mono", // Modern monospace font by JetBrains, great for code output.
    },
    EmbeddedFont {
        path: "assets/fonts/FiraCode-Regular.ttf",
        css_family: "Fira Code", // Ligature-friendly, stylish monospace font.
    },
    EmbeddedFont {
        path: "assets/fonts/SourceCodePro-Regular.ttf",
        css_family: "Source Code Pro", // Clean, well-balanced font from Adobe.
    },
    EmbeddedFont {
        path: "assets/fonts/CascadiaMono-Regular.ttf",
        css_family: "Cascadia Mono", // Default font for Windows Terminal, modern look.
    },
];

/// Resolve a font by its CSS `font-family` name.
///
/// Returns the matching embedded font metadata if the name
/// exactly matches any of the available `css_family` entries.
///
/// Used during style patching and font validation.
pub fn resolve_embedded_font(user_input: &str) -> Option<&'static EmbeddedFont> {
    let trimmed = user_input.trim();
    EMBEDDED_FONTS
        .iter()
        .find(|font| font.css_family == trimmed)
}

/// Returns a list of all embeddable CSS `font-family` names.
///
/// Useful for validation, autocomplete, and UI dropdowns.
pub fn list_embeddable_font_families() -> Vec<&'static str> {
    EMBEDDED_FONTS.iter().map(|f| f.css_family).collect()
}

/// Returns a whitelist of system fonts considered monospace-safe.
///
/// These are not embedded but may be used as fallback options.
/// Each entry includes an optional comment describing platform behavior.
pub fn list_builtin_system_fonts() -> Vec<(&'static str, Option<&'static str>)> {
    vec![
        (
            "Monospace",
            Some("Generic fallback: macOS → Menlo, Windows → Consolas, Linux → DejaVu Sans Mono"),
        ),
        ("Courier New", None),      // Win/macOS
        ("Menlo", None),            // macOS
        ("Consolas", None),         // Windows
        ("Lucida Console", None),   // Windows legacy
        ("DejaVu Sans Mono", None), // Linux
        ("Ubuntu Mono", None),      // Ubuntu
    ]
}

/// Suggest recommended fonts based on the current operating system.
///
/// Returns a prioritized list combining system fonts and known embedded fonts.
/// Can be used to prefill default font-family selections.
pub fn recommended_fonts_for_current_os() -> Vec<&'static str> {
    match std::env::consts::OS {
        "macos" => vec!["Menlo", "Courier New", "JetBrains Mono"],
        "windows" => vec!["Consolas", "Courier New", "JetBrains Mono"],
        "linux" => vec!["DejaVu Sans Mono", "Ubuntu Mono", "JetBrains Mono"],
        _ => vec!["Monospace", "JetBrains Mono"],
    }
}