BREP_app 0.2.0

The BREP CAD application: an eframe (egui + wgpu) host that draws the brep-render 3D engine into an egui frame — native + wasm from one codebase.
Documentation
//! Font setup — the app's own icon font over the OS's default monospace font.
//!
//! This REPO ships exactly one font file: `BrepIcons.ttf` (our monoline icon
//! font, generated by `tools/iconfont/`). The BINARY carries five. eframe's
//! `default_fonts` feature is enabled unconditionally in Cargo.toml, and it
//! pulls `epaint_default_fonts`, whose whole contents are four
//! `include_bytes!` constants — Ubuntu-Light, NotoEmoji-Regular, Hack-Regular
//! and emoji-icon-font. `include_bytes!` runs at compile time, so those four
//! are linked into every native binary and wasm module regardless of whether
//! the fallback below ever draws with them. That is a licence obligation, not
//! a detail: two of them are conjoined OFL-1.1 / Ubuntu-font-1.0, so a binary
//! release must carry their notices. See THIRD-PARTY-NOTICES.md.
//!
//! The TEXT a user normally reads comes from none of the five — it is the
//! operating system's default monospace font, located and read at runtime via
//! fontconfig, so the UI reads with the user's own system monospace.
//!
//! `brep_icons` LEADS the fallback chain so it overrides the OS font for the
//! symbol codepoints they share (∠ ≡ ⊥ ◎ … — ~18 of them). Its metrics are
//! cloned from a standard 2048-upm mono face (it was the index-0 metrics driver
//! before too), so leading it leaves the UI row height unchanged.
//!
//! Platform reality: only native can read OS font files. On wasm (browser) —
//! and on native if the OS monospace can't be located — we fall back to egui's
//! bundled default fonts (eframe feature `default_fonts`) for text + emoji,
//! still led by `brep_icons`. That fallback guarantees the family always holds
//! a real text font (egui requires one that supplies a replacement glyph; an
//! icon-only family would panic).
//!
//! Installed at app creation from each shell entry point (`lib.rs` wasm `start`,
//! `main.rs` native `run_native`).

use eframe::egui;

/// Our icon font — the only face this repo SHIPS; the four
/// `epaint_default_fonts` faces arrive compiled-in via eframe. Glyph geometry
/// is edited as SVGs
/// under `tools/iconfont/glyphs/`; regenerate with
/// `cargo run --manifest-path tools/iconfont/Cargo.toml --bin build-font`.
const BREP_ICONS: &[u8] = include_bytes!("../assets/fonts/BrepIcons.ttf");

/// Install fonts into `ctx`. Call once, at app creation.
pub fn install(ctx: &egui::Context) {
    // Base text = OS monospace when we can read it; otherwise egui's bundled
    // defaults (wasm, or a native machine without fontconfig).
    let mut fonts = match os_monospace() {
        Some((bytes, index)) => os_mono_defs(bytes, index),
        None => egui::FontDefinitions::default(),
    };

    // `brep_icons` leads BOTH families so it overrides the text font for the
    // codepoints it defines; text glyphs fall through to the base font.
    fonts.font_data.insert(
        "brep_icons".to_owned(),
        egui::FontData::from_static(BREP_ICONS).into(),
    );
    for family in [egui::FontFamily::Proportional, egui::FontFamily::Monospace] {
        fonts
            .families
            .entry(family)
            .or_default()
            .insert(0, "brep_icons".to_owned());
    }

    ctx.set_fonts(fonts);
}

/// A minimal `FontDefinitions` whose ONLY text font is the OS monospace — no
/// bundled fallbacks. `brep_icons` is prepended by the caller.
fn os_mono_defs(bytes: Vec<u8>, index: u32) -> egui::FontDefinitions {
    let mut fonts = egui::FontDefinitions::empty();
    let mut data = egui::FontData::from_owned(bytes);
    data.index = index; // honor a .ttc collection face index from fontconfig
    fonts.font_data.insert("os_mono".to_owned(), data.into());
    for family in [egui::FontFamily::Proportional, egui::FontFamily::Monospace] {
        fonts.families.insert(family, vec!["os_mono".to_owned()]);
    }
    fonts
}

/// Locate and read the OS default monospace font (path + collection index).
/// Native only, via fontconfig's `fc-match`; returns `None` on any failure so
/// the caller falls back to egui's bundled defaults.
#[cfg(not(target_arch = "wasm32"))]
fn os_monospace() -> Option<(Vec<u8>, u32)> {
    let out = std::process::Command::new("fc-match")
        .args(["--format=%{file}\n%{index}", "monospace"])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&out.stdout);
    let mut lines = text.lines();
    let path = lines.next()?.trim();
    if path.is_empty() {
        return None;
    }
    let index = lines.next().and_then(|l| l.trim().parse().ok()).unwrap_or(0);
    let bytes = std::fs::read(path).ok()?; // read failure => None => default fallback
    Some((bytes, index))
}

/// wasm has no OS font access — always fall back to egui's bundled defaults.
#[cfg(target_arch = "wasm32")]
fn os_monospace() -> Option<(Vec<u8>, u32)> {
    None
}