Embed standard fonts in binary
@ -113,11 +113,6 @@ typst file.typ
|
|||||||
typst path/to/source.typ path/to/output.pdf
|
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
|
You can also watch source files and automatically recompile on changes. This is
|
||||||
faster than compiling from scratch each time because Typst has incremental
|
faster than compiling from scratch each time because Typst has incremental
|
||||||
compilation.
|
compilation.
|
||||||
|
BIN
assets/fonts/NewCMMath-Book.otf
Normal file
@ -27,3 +27,13 @@ pico-args = "0.4"
|
|||||||
same-file = "1"
|
same-file = "1"
|
||||||
siphasher = "0.3"
|
siphasher = "0.3"
|
||||||
walkdir = "2"
|
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 = []
|
||||||
|
@ -404,6 +404,9 @@ impl SystemWorld {
|
|||||||
let mut searcher = FontSearcher::new();
|
let mut searcher = FontSearcher::new();
|
||||||
searcher.search_system();
|
searcher.search_system();
|
||||||
|
|
||||||
|
#[cfg(feature = "embed-fonts")]
|
||||||
|
searcher.add_embedded();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
root,
|
root,
|
||||||
library: Prehashed::new(typst_library::build()),
|
library: Prehashed::new(typst_library::build()),
|
||||||
@ -617,6 +620,32 @@ impl FontSearcher {
|
|||||||
Self { book: FontBook::new(), fonts: vec![] }
|
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.
|
/// Search for fonts in the linux system font directories.
|
||||||
#[cfg(all(unix, not(target_os = "macos")))]
|
#[cfg(all(unix, not(target_os = "macos")))]
|
||||||
fn search_system(&mut self) {
|
fn search_system(&mut self) {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
use std::fmt::{self, Debug, Formatter};
|
use std::fmt::{self, Debug, Formatter};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -6,9 +7,14 @@ use comemo::Prehashed;
|
|||||||
|
|
||||||
/// A shared buffer that is cheap to clone and hash.
|
/// A shared buffer that is cheap to clone and hash.
|
||||||
#[derive(Clone, Hash, Eq, PartialEq)]
|
#[derive(Clone, Hash, Eq, PartialEq)]
|
||||||
pub struct Buffer(Arc<Prehashed<Vec<u8>>>);
|
pub struct Buffer(Arc<Prehashed<Cow<'static, [u8]>>>);
|
||||||
|
|
||||||
impl Buffer {
|
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.
|
/// Return a view into the buffer.
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
self
|
self
|
||||||
@ -22,13 +28,13 @@ impl Buffer {
|
|||||||
|
|
||||||
impl From<&[u8]> for Buffer {
|
impl From<&[u8]> for Buffer {
|
||||||
fn from(slice: &[u8]) -> Self {
|
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 {
|
impl From<Vec<u8>> for Buffer {
|
||||||
fn from(vec: Vec<u8>) -> Self {
|
fn from(vec: Vec<u8>) -> Self {
|
||||||
Self(Arc::new(Prehashed::new(vec)))
|
Self(Arc::new(Prehashed::new(vec.into())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 7.0 KiB After Width: | Height: | Size: 7.2 KiB |
Before Width: | Height: | Size: 7.8 KiB After Width: | Height: | Size: 8.0 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 6.1 KiB |
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.6 KiB |
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |