Files
AniCraft/build.rs
T
Tanish Manku 7dcd01041e feat(shell): web build of the editor: browser entry, file picker and downloads, autosave to browser storage (M8)
src/web.rs starts the editor on a page canvas with eframe's WebRunner (WebGL2 via glow) and
provides the browser's services; platform.rs decides when: picked files go to the engine's
in-memory store and the command runs, saves and exports leave as downloads, preferences and
recovery copies live in localStorage, Web Locks tell open tabs from crashed ones, and leaving
with unsaved work asks first. The GPU stage and sound output become build cfgs that are off
on wasm. Video export and plug-in installs are dimmed in the browser with the reason.

scripts/build-web.sh and .ps1 build web/ with the wasm-bindgen CLI pinned to Cargo.lock's
version, from packaging/web/index.html. The web profile (opt-level s, stripped, renderer and
geometry at opt-level 3) gives an 8973 KB module, 3278 KB gzipped, in 41 s.
crates/engine/tests/wasm.rs runs the engine inside WebAssembly in Node (3 tests, 0.6 s).
2026-11-09 22:36:09 -07:00

131 lines
5.7 KiB
Rust

//! Optional font build input (the Craft family convention, as in PhotoCraft), and the app icon
//! embedded in the Windows executable.
//!
//! `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
}
/// Release builds set this (packaging/, ADR 0026): a build that would ship without its icon then
/// fails instead of warning.
fn required(var: &str) -> bool {
println!("cargo:rerun-if-env-changed={var}");
std::env::var(var).is_ok_and(|v| v == "1")
}
/// Stops the build with a message (only for the release requirement above).
fn fail(message: &str) -> ! {
eprintln!("error: {message}");
std::process::exit(1);
}
/// Windows only: embed the app icon into `anicraft.exe` (taskbar, Start menu, Explorer, Alt-Tab).
/// A missing resource compiler is a warning, so a cross-compile still links, unless
/// `ANICRAFT_REQUIRE_WINRES=1` (packaging/windows/package.ps1).
fn windows_icon() {
println!("cargo:rerun-if-changed=assets/app-icon/anicraft.ico");
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
return;
}
let mut res = winresource::WindowsResource::new();
res.set_icon("assets/app-icon/anicraft.ico").set("ProductName", "AniCraft").set("FileDescription", "AniCraft 2D vector animation studio");
if let Err(e) = res.compile() {
if required("ANICRAFT_REQUIRE_WINRES") {
fail(&format!("ANICRAFT_REQUIRE_WINRES=1 but anicraft.exe's icon and version resource could not be embedded: {e}"));
}
println!("cargo:warning=anicraft.exe built without its icon: {e}");
}
}
/// The desktop-only features (ADR 0036) as cfgs: `gpu_stage` and `sound_output` are on when
/// their feature is and the target isn't the web, so `cargo build --target wasm32-unknown-unknown`
/// works with the default features and the web build draws on the CPU, silently.
fn native_features() {
println!("cargo::rustc-check-cfg=cfg(gpu_stage, sound_output)");
let web = std::env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|a| a == "wasm32");
for (feature, cfg) in [("CARGO_FEATURE_GPU_STAGE", "gpu_stage"), ("CARGO_FEATURE_SOUND_OUTPUT", "sound_output")] {
if std::env::var_os(feature).is_some() && !web {
println!("cargo::rustc-cfg={cfg}");
}
}
}
fn main() {
native_features();
windows_icon();
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());
}
}