mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-25 03:53:23 +01:00
AniCraft 0.1.0 (early alpha): milestones M0 and M1. Contributors are credited in AUTHORS.md.
84 lines
3.6 KiB
Rust
84 lines
3.6 KiB
Rust
//! Optional font build input (the Craft family convention, as in PhotoCraft).
|
|
//!
|
|
//! `CRAFT_FONTS_DIR=<craft-fonts checkout> cargo build` embeds the UI typefaces chosen in
|
|
//! ADR 0005: Inter (prose) and JetBrains Mono (data), both SIL OFL 1.1. Font files are never
|
|
//! committed to this repository. Without the variable the app uses egui's bundled defaults, and
|
|
//! everything still works.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// Files we look for, and the generated constant each fills.
|
|
const FONTS: [(&str, &str); 2] = [("Inter-Regular.ttf", "UI_FONT"), ("JetBrainsMono-Regular.ttf", "MONO_FONT")];
|
|
|
|
fn find(dir: &Path, name: &str, depth: u32) -> Option<PathBuf> {
|
|
let entries = std::fs::read_dir(dir).ok()?;
|
|
let mut subdirs = Vec::new();
|
|
for e in entries.flatten() {
|
|
let p = e.path();
|
|
if p.is_dir() {
|
|
subdirs.push(p);
|
|
} else if p.file_name().is_some_and(|n| n.eq_ignore_ascii_case(name)) {
|
|
return Some(p);
|
|
}
|
|
}
|
|
if depth == 0 {
|
|
return None;
|
|
}
|
|
subdirs.iter().find_map(|d| find(d, name, depth - 1))
|
|
}
|
|
|
|
/// Where to look for the fonts when `CRAFT_FONTS_DIR` is not set: a `craft-fonts` checkout next
|
|
/// to this repository (or up to three folders above it), then a sibling Craft app that vendors
|
|
/// the same OFL files (PhotoCraft's `assets/fonts`). This makes ordinary local builds look like
|
|
/// releases without committing font files here.
|
|
fn discover() -> Option<PathBuf> {
|
|
if let Some(d) = std::env::var_os("CRAFT_FONTS_DIR") {
|
|
return Some(PathBuf::from(d));
|
|
}
|
|
let manifest = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR")?);
|
|
let mut dir = manifest.parent().map(Path::to_path_buf);
|
|
for _ in 0..4 {
|
|
let Some(d) = dir else { break };
|
|
for candidate in [d.join("craft-fonts"), d.join("photocraft").join("assets").join("fonts")] {
|
|
if candidate.is_dir() {
|
|
return Some(candidate);
|
|
}
|
|
}
|
|
dir = d.parent().map(Path::to_path_buf);
|
|
}
|
|
None
|
|
}
|
|
|
|
fn main() {
|
|
println!("cargo:rerun-if-env-changed=CRAFT_FONTS_DIR");
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
let dir = discover();
|
|
match &dir {
|
|
Some(d) => println!("cargo:rerun-if-changed={}", d.display()),
|
|
None => println!("cargo:warning=UI fonts: no craft-fonts found (set CRAFT_FONTS_DIR or clone storytold/craft-fonts next to the repo); using the built-in fallback fonts"),
|
|
}
|
|
let mut code = String::from("// Generated by build.rs (CRAFT_FONTS_DIR font input).\n");
|
|
let mut total = 0u64;
|
|
for (file, constant) in FONTS {
|
|
let found = dir.as_deref().and_then(|d| find(d, file, 3));
|
|
match found {
|
|
Some(p) => {
|
|
println!("cargo:rerun-if-changed={}", p.display());
|
|
total += std::fs::metadata(&p).map(|m| m.len()).unwrap_or(0);
|
|
code.push_str(&format!("pub const {constant}: Option<&[u8]> = Some(include_bytes!({:?}));\n", p.display().to_string()));
|
|
}
|
|
None => {
|
|
if dir.is_some() {
|
|
println!("cargo:warning=CRAFT_FONTS_DIR is set but {file} was not found; using the bundled default");
|
|
}
|
|
code.push_str(&format!("pub const {constant}: Option<&[u8]> = None;\n"));
|
|
}
|
|
}
|
|
}
|
|
code.push_str(&format!("pub const CRAFT_FONT_BYTES: u64 = {total};\n"));
|
|
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("fonts.rs");
|
|
if let Err(e) = std::fs::write(&out, code) {
|
|
println!("cargo:warning=could not write {}: {e}", out.display());
|
|
}
|
|
}
|