Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

4.0 KB
Raw Blame
//! rustyfiles' own look outside the interface: the wordmark (the logo beside the name, as outlines, from
//! `assets/rustyfiles-wordmark.svg`, made by `tools/make_wordmark.py`) and the brand fonts (Bricolage Grotesque for words,
//! Doto for the big numbers; `assets/fonts`, SIL OFL 1.1). The speed test card is drawn with them, and the About page shows the
//! wordmark at its top. The fonts are read only by the card's SVG renderer, never loaded into the interface.

use std::sync::Arc;

use gpui_kit::RenderImage;

/// The wordmark's SVG: ink `#f2f0ec` for "rusty", ember for "files" and the logo's own colours.
pub const WORDMARK: &str = include_str!("../assets/rustyfiles-wordmark.svg");

/// The ink the wordmark is made with (for a dark background).
pub const WORDMARK_INK: &str = "#f2f0ec";

/// The fonts the card names: "Bricolage Grotesque 96pt" (SemiBold, headlines), "Bricolage Grotesque" (Regular, lines) and
/// "Doto" (Black, the big numbers).
pub static FONTS: &[&[u8]] = &[
    include_bytes!("../assets/fonts/BricolageGrotesque96pt-SemiBold.otf"),
    include_bytes!("../assets/fonts/BricolageGrotesque-Regular.otf"),
    include_bytes!("../assets/fonts/Doto-Black.ttf"),
];

/// The wordmark's width over its height.
pub fn wordmark_aspect() -> f32 {
    view_box(WORDMARK).map(|(w, h)| w / h).unwrap_or(3.57)
}

fn view_box(svg: &str) -> Option<(f32, f32)> {
    let start = svg.find("viewBox=\"")? + 9;
    let end = start + svg[start..].find('"')?;
    let v: Vec<f32> = svg[start..end].split_whitespace().filter_map(|s| s.parse().ok()).collect();
    (v.len() == 4).then(|| (v[2], v[3]))
}

/// The wordmark as an `<svg>` element to put inside another SVG, `height` tall with its top left at (`x`, `y`), "rusty" in `ink`.
pub fn wordmark_nested(x: f32, y: f32, height: f32, ink: &str) -> String {
    let body = WORDMARK.find("<svg").map(|i| &WORDMARK[i..]).unwrap_or(WORDMARK);
    let width = height * wordmark_aspect();
    let body = body.replacen("<svg ", &format!("<svg x=\"{x}\" y=\"{y}\" width=\"{width}\" height=\"{height}\" "), 1);
    body.replace(WORDMARK_INK, ink)
}

/// The wordmark drawn `height_px` pixels tall (device pixels), "rusty" in `ink`, ready for GPUI: BGRA, straight alpha.
pub fn wordmark_image(height_px: u32, ink: &str) -> Option<Arc<RenderImage>> {
    use resvg::{tiny_skia, usvg};
    let svg = WORDMARK.replace(WORDMARK_INK, ink);
    let tree = usvg::Tree::from_data(svg.as_bytes(), &usvg::Options::default()).ok()?;
    let size = tree.size();
    let scale = height_px.max(1) as f32 / size.height();
    let width = (size.width() * scale).ceil() as u32;
    let mut pix = tiny_skia::Pixmap::new(width.max(1), height_px.max(1))?;
    resvg::render(&tree, tiny_skia::Transform::from_scale(scale, scale), &mut pix.as_mut());
    let mut data = pix.take();
    for px in data.chunks_exact_mut(4) {
        // premultiplied RGBA -> straight BGRA
        let a = px[3] as u32;
        if a > 0 && a < 255 {
            for c in &mut px[..3] {
                *c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8;
            }
        }
        px.swap(0, 2);
    }
    let buf = image::RgbaImage::from_raw(width.max(1), height_px.max(1), data)?;
    Some(Arc::new(RenderImage::new(vec![image::Frame::new(buf)])))
}

#[cfg(test)]
mod tests {
    #[test]
    fn the_wordmark_is_whole_and_draws() {
        assert!(super::WORDMARK.contains(super::WORDMARK_INK), "the ink the app swaps is in the file");
        let a = super::wordmark_aspect();
        assert!((3.0..4.5).contains(&a), "{a}");
        let nested = super::wordmark_nested(10.0, 20.0, 50.0, "#000000");
        assert!(nested.starts_with("<svg x=\"10\" y=\"20\"") && !nested.contains(super::WORDMARK_INK));
        let img = super::wordmark_image(40, "#000000").expect("drawn");
        let size = img.size(0);
        assert_eq!(size.height.0, 40);
    }

    #[test]
    fn the_fonts_are_there() {
        for f in super::FONTS {
            assert!(f.len() > 10_000 && (f.starts_with(b"OTTO") || f.starts_with(&[0, 1, 0, 0])));
        }
    }
}