1//! Deflates the font clones the target embeds into `bundled.rs`, a table `layout.rs` includes,
2//! leaving out each whose family the target's system ships.
3
4/// Metric-compatible clones, by the family each stands in for.
5const CLONES: [(&str, &[&str]); 4] = [
6 (
7 "Calibri",
8 &[
9 "Carlito-Regular",
10 "Carlito-Bold",
11 "Carlito-Italic",
12 "Carlito-BoldItalic",
13 ],
14 ),
15 ("Arial", &["Arimo", "Arimo-Italic"]),
16 (
17 "Times New Roman",
18 &[
19 "Tinos-Regular",
20 "Tinos-Bold",
21 "Tinos-Italic",
22 "Tinos-BoldItalic",
23 ],
24 ),
25 (
26 "Courier New",
27 &[
28 "Cousine-Regular",
29 "Cousine-Bold",
30 "Cousine-Italic",
31 "Cousine-BoldItalic",
32 ],
33 ),
34];
35
36fn main() {
37 println!("cargo::rerun-if-changed=build.rs");
38 println!("cargo::rerun-if-changed=assets/fonts");
39 let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
40 let browser = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32";
41 let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
42 let mut table = String::from("&[\n");
43 for (family, faces) in CLONES {
44 // The browser fetches them beside the module instead.
45 let shipped = browser
46 || match os.as_str() {
47 "windows" => true,
48 "macos" | "ios" => family != "Calibri",
49 _ => false,
50 };
51 if shipped {
52 continue;
53 }
54 table += &format!(" ({family:?}, &[\n");
55 for face in faces {
56 let font = std::fs::read(format!("assets/fonts/{face}.ttf")).unwrap();
57 let deflated = format!("{face}.ttf.deflate");
58 std::fs::write(
59 out.join(&deflated),
60 miniz_oxide::deflate::compress_to_vec(&font, 10),
61 )
62 .unwrap();
63 // Relative to OUT_DIR, so a target folder reached by another path still finds them.
64 table +=
65 &format!(" include_bytes!(concat!(env!(\"OUT_DIR\"), \"/{deflated}\")),\n");
66 }
67 table += " ]),\n";
68 }
69 table += "]\n";
70 std::fs::write(out.join("bundled.rs"), table).unwrap();
71}