Embed standard fonts in binary

This commit is contained in:
Laurenz 2023-03-21 23:18:56 +01:00
parent 27bb5e8d22
commit b934a2fd83
24 changed files with 48 additions and 8 deletions

View File

@ -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.

Binary file not shown.

View File

@ -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 = []

View File

@ -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) {

View File

@ -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<Prehashed<Vec<u8>>>);
pub struct Buffer(Arc<Prehashed<Cow<'static, [u8]>>>);
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<Vec<u8>> for Buffer {
fn from(vec: Vec<u8>) -> Self {
Self(Arc::new(Prehashed::new(vec)))
Self(Arc::new(Prehashed::new(vec.into())))
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB