From b934a2fd83d63fc115c01f959e888c7bc1aa87e4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 21 Mar 2023 23:18:56 +0100 Subject: Embed standard fonts in binary --- cli/Cargo.toml | 10 ++++++++++ cli/src/main.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'cli') diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 97e2fa02..b91e15a5 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -27,3 +27,13 @@ pico-args = "0.4" same-file = "1" siphasher = "0.3" walkdir = "2" + +[features] +default = ["embed-fonts"] + +# Embeds Typst's default fonts for +# - text (Linux Libertine), +# - math (New Computer Modern Math), and +# - code (Deja Vu Sans Mono) +# into the binary. +embed-fonts = [] diff --git a/cli/src/main.rs b/cli/src/main.rs index 40d1a780..3f41ac78 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -404,6 +404,9 @@ impl SystemWorld { let mut searcher = FontSearcher::new(); searcher.search_system(); + #[cfg(feature = "embed-fonts")] + searcher.add_embedded(); + Self { root, library: Prehashed::new(typst_library::build()), @@ -617,6 +620,32 @@ impl FontSearcher { Self { book: FontBook::new(), fonts: vec![] } } + /// Add fonts that are embedded in the binary. + #[cfg(feature = "embed-fonts")] + fn add_embedded(&mut self) { + let mut add = |bytes: &[u8]| { + let buffer = Buffer::from(bytes); + for (i, font) in Font::iter(buffer).enumerate() { + self.book.push(font.info().clone()); + self.fonts.push(FontSlot { + path: PathBuf::new(), + index: i as u32, + font: OnceCell::from(Some(font)), + }); + } + }; + + // Embed default fonts. + add(include_bytes!("../../assets/fonts/LinLibertine_R.ttf")); + add(include_bytes!("../../assets/fonts/LinLibertine_RB.ttf")); + add(include_bytes!("../../assets/fonts/LinLibertine_RBI.ttf")); + add(include_bytes!("../../assets/fonts/LinLibertine_RI.ttf")); + add(include_bytes!("../../assets/fonts/NewCMMath-Book.otf")); + add(include_bytes!("../../assets/fonts/NewCMMath-Regular.otf")); + add(include_bytes!("../../assets/fonts/DejaVuSansMono.ttf")); + add(include_bytes!("../../assets/fonts/DejaVuSansMono-Bold.ttf")); + } + /// Search for fonts in the linux system font directories. #[cfg(all(unix, not(target_os = "macos")))] fn search_system(&mut self) { -- cgit v1.2.3