diff --git a/README.md b/README.md index 10ba1c9a4..1a824601b 100644 --- a/README.md +++ b/README.md @@ -113,11 +113,6 @@ typst file.typ typst path/to/source.typ path/to/output.pdf ``` -To properly use Typst, you should also install the fonts "Linux Libertine" and -"New Computer Modern Math". They are available as part of this repository in -`assets/fonts` or you can install them through the package manager of your -choice. - You can also watch source files and automatically recompile on changes. This is faster than compiling from scratch each time because Typst has incremental compilation. diff --git a/assets/fonts/NewCMMath-Book.otf b/assets/fonts/NewCMMath-Book.otf new file mode 100644 index 000000000..eeca1dde9 Binary files /dev/null and b/assets/fonts/NewCMMath-Book.otf differ diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 97e2fa02b..b91e15a5b 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 40d1a7802..3f41ac782 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) { diff --git a/src/util/buffer.rs b/src/util/buffer.rs index da12b6cb3..23fb9802d 100644 --- a/src/util/buffer.rs +++ b/src/util/buffer.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::fmt::{self, Debug, Formatter}; use std::ops::Deref; use std::sync::Arc; @@ -6,9 +7,14 @@ use comemo::Prehashed; /// A shared buffer that is cheap to clone and hash. #[derive(Clone, Hash, Eq, PartialEq)] -pub struct Buffer(Arc>>); +pub struct Buffer(Arc>>); impl Buffer { + /// Create a buffer from a static byte slice. + pub fn from_static(slice: &'static [u8]) -> Self { + Self(Arc::new(Prehashed::new(Cow::Borrowed(slice)))) + } + /// Return a view into the buffer. pub fn as_slice(&self) -> &[u8] { self @@ -22,13 +28,13 @@ impl Buffer { impl From<&[u8]> for Buffer { fn from(slice: &[u8]) -> Self { - Self(Arc::new(Prehashed::new(slice.to_vec()))) + Self(Arc::new(Prehashed::new(slice.to_vec().into()))) } } impl From> for Buffer { fn from(vec: Vec) -> Self { - Self(Arc::new(Prehashed::new(vec))) + Self(Arc::new(Prehashed::new(vec.into()))) } } diff --git a/tests/ref/compiler/content-field.png b/tests/ref/compiler/content-field.png index 8ad5566af..d2f696b42 100644 Binary files a/tests/ref/compiler/content-field.png and b/tests/ref/compiler/content-field.png differ diff --git a/tests/ref/layout/enum-numbering.png b/tests/ref/layout/enum-numbering.png index 4804d5733..7c39da4f9 100644 Binary files a/tests/ref/layout/enum-numbering.png and b/tests/ref/layout/enum-numbering.png differ diff --git a/tests/ref/math/accent.png b/tests/ref/math/accent.png index bea94cab3..a87a684d0 100644 Binary files a/tests/ref/math/accent.png and b/tests/ref/math/accent.png differ diff --git a/tests/ref/math/attach.png b/tests/ref/math/attach.png index aeab9af8b..956199ca2 100644 Binary files a/tests/ref/math/attach.png and b/tests/ref/math/attach.png differ diff --git a/tests/ref/math/cases.png b/tests/ref/math/cases.png index 87c358da6..c9eca24c2 100644 Binary files a/tests/ref/math/cases.png and b/tests/ref/math/cases.png differ diff --git a/tests/ref/math/content.png b/tests/ref/math/content.png index 92fe9860f..433c8ddcf 100644 Binary files a/tests/ref/math/content.png and b/tests/ref/math/content.png differ diff --git a/tests/ref/math/delimited.png b/tests/ref/math/delimited.png index 34b61a706..0670337fe 100644 Binary files a/tests/ref/math/delimited.png and b/tests/ref/math/delimited.png differ diff --git a/tests/ref/math/frac.png b/tests/ref/math/frac.png index d0ac9c1a5..fc8789b36 100644 Binary files a/tests/ref/math/frac.png and b/tests/ref/math/frac.png differ diff --git a/tests/ref/math/matrix.png b/tests/ref/math/matrix.png index fa6e53f31..d97d6ec13 100644 Binary files a/tests/ref/math/matrix.png and b/tests/ref/math/matrix.png differ diff --git a/tests/ref/math/multiline.png b/tests/ref/math/multiline.png index 1433ba301..94ef11964 100644 Binary files a/tests/ref/math/multiline.png and b/tests/ref/math/multiline.png differ diff --git a/tests/ref/math/numbering.png b/tests/ref/math/numbering.png index 3b9db84f1..a06e2b0c8 100644 Binary files a/tests/ref/math/numbering.png and b/tests/ref/math/numbering.png differ diff --git a/tests/ref/math/op.png b/tests/ref/math/op.png index ac93559c6..b4878438c 100644 Binary files a/tests/ref/math/op.png and b/tests/ref/math/op.png differ diff --git a/tests/ref/math/root.png b/tests/ref/math/root.png index 267249b05..ccd284a61 100644 Binary files a/tests/ref/math/root.png and b/tests/ref/math/root.png differ diff --git a/tests/ref/math/spacing.png b/tests/ref/math/spacing.png index 38d210260..921996b01 100644 Binary files a/tests/ref/math/spacing.png and b/tests/ref/math/spacing.png differ diff --git a/tests/ref/math/style.png b/tests/ref/math/style.png index 78cfe9b1d..46d726628 100644 Binary files a/tests/ref/math/style.png and b/tests/ref/math/style.png differ diff --git a/tests/ref/math/syntax.png b/tests/ref/math/syntax.png index 442e8bfec..f223ba5ae 100644 Binary files a/tests/ref/math/syntax.png and b/tests/ref/math/syntax.png differ diff --git a/tests/ref/math/underover.png b/tests/ref/math/underover.png index a585f59dd..d090057d6 100644 Binary files a/tests/ref/math/underover.png and b/tests/ref/math/underover.png differ diff --git a/tests/ref/math/vec.png b/tests/ref/math/vec.png index 20db15122..a8343d9a8 100644 Binary files a/tests/ref/math/vec.png and b/tests/ref/math/vec.png differ diff --git a/tests/ref/meta/state.png b/tests/ref/meta/state.png index 86d9da4a9..ca77fdc7e 100644 Binary files a/tests/ref/meta/state.png and b/tests/ref/meta/state.png differ