-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,23 @@ | ||
use crate::engine::{FontSearcher, FontSlot}; | ||
use comemo::Prehashed; | ||
use typst::eval::Library; | ||
use typst::font::{Font, FontBook}; | ||
use typst::util::Buffer; | ||
|
||
pub struct TypstEngine { | ||
pub library: Prehashed<Library>, | ||
pub fontbook: Prehashed<FontBook>, | ||
pub fonts: Vec<Font>, | ||
pub fonts: Vec<FontSlot>, | ||
} | ||
|
||
impl TypstEngine { | ||
pub fn new() -> Self { | ||
// https://github.com/typst/typst/blob/085282c138899dd5aaa06bc6ae7bd2f79d75d7e1/cli/src/main.rs#L695 | ||
const EMBEDDED_FONTS: [&[u8]; 10] = [ | ||
include_bytes!("../../assets/fonts/LinLibertine_R.ttf"), | ||
include_bytes!("../../assets/fonts/LinLibertine_RB.ttf"), | ||
include_bytes!("../../assets/fonts/LinLibertine_RBI.ttf"), | ||
include_bytes!("../../assets/fonts/LinLibertine_RI.ttf"), | ||
include_bytes!("../../assets/fonts/NewCMMath-Book.otf"), | ||
include_bytes!("../../assets/fonts/NewCMMath-Regular.otf"), | ||
include_bytes!("../../assets/fonts/DejaVuSansMono.ttf"), | ||
include_bytes!("../../assets/fonts/DejaVuSansMono-Bold.ttf"), | ||
include_bytes!("../../assets/fonts/DejaVuSansMono-Oblique.ttf"), | ||
include_bytes!("../../assets/fonts/DejaVuSansMono-BoldOblique.ttf"), | ||
]; | ||
|
||
let mut fontbook = FontBook::new(); | ||
let mut fonts = vec![]; | ||
|
||
for file in EMBEDDED_FONTS { | ||
for font in Font::iter(Buffer::from_static(file)) { | ||
fontbook.push(font.info().clone()); | ||
fonts.push(font); | ||
} | ||
} | ||
let mut searcher = FontSearcher::new(); | ||
searcher.search(&[]); | ||
|
||
Self { | ||
library: Prehashed::new(typst_library::build()), | ||
fontbook: Prehashed::new(fontbook), | ||
fonts, | ||
fontbook: Prehashed::new(searcher.book), | ||
fonts: searcher.fonts, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use log::{debug, trace}; | ||
use memmap2::Mmap; | ||
use once_cell::sync::OnceCell; | ||
use std::fs::File; | ||
use std::path::{Path, PathBuf}; | ||
use typst::font::{Font, FontBook, FontInfo}; | ||
use typst::util::Buffer; | ||
use walkdir::WalkDir; | ||
|
||
// Taken from typst-cli | ||
|
||
/// Holds details about the location of a font and lazily the font itself. | ||
pub struct FontSlot { | ||
pub path: PathBuf, | ||
pub index: u32, | ||
pub font: OnceCell<Option<Font>>, | ||
} | ||
|
||
pub struct FontSearcher { | ||
pub book: FontBook, | ||
pub fonts: Vec<FontSlot>, | ||
} | ||
|
||
impl FontSearcher { | ||
/// Create a new, empty system searcher. | ||
pub fn new() -> Self { | ||
Self { | ||
book: FontBook::new(), | ||
fonts: vec![], | ||
} | ||
} | ||
|
||
/// Search everything that is available. | ||
pub fn search(&mut self, font_paths: &[PathBuf]) { | ||
self.search_system(); | ||
|
||
#[cfg(feature = "embed-fonts")] | ||
self.search_embedded(); | ||
|
||
for path in font_paths { | ||
self.search_dir(path); | ||
} | ||
|
||
debug!("discovered {} fonts", self.fonts.len()); | ||
} | ||
|
||
/// Add fonts that are embedded in the binary. | ||
#[cfg(feature = "embed-fonts")] | ||
fn search_embedded(&mut self) { | ||
let mut search = |bytes: &'static [u8]| { | ||
let buffer = Buffer::from_static(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. | ||
search(include_bytes!("../../assets/fonts/LinLibertine_R.ttf")); | ||
search(include_bytes!("../../assets/fonts/LinLibertine_RB.ttf")); | ||
search(include_bytes!("../../assets/fonts/LinLibertine_RBI.ttf")); | ||
search(include_bytes!("../../assets/fonts/LinLibertine_RI.ttf")); | ||
search(include_bytes!("../../assets/fonts/NewCMMath-Book.otf")); | ||
search(include_bytes!("../../assets/fonts/NewCMMath-Regular.otf")); | ||
search(include_bytes!("../../assets/fonts/DejaVuSansMono.ttf")); | ||
search(include_bytes!("../../assets/fonts/DejaVuSansMono-Bold.ttf")); | ||
search(include_bytes!( | ||
"../../assets/fonts/DejaVuSansMono-Oblique.ttf" | ||
)); | ||
search(include_bytes!( | ||
"../../assets/fonts/DejaVuSansMono-BoldOblique.ttf" | ||
)); | ||
} | ||
|
||
/// Search for fonts in the linux system font directories. | ||
#[cfg(all(unix, not(target_os = "macos")))] | ||
fn search_system(&mut self) { | ||
self.search_dir("/usr/share/fonts"); | ||
self.search_dir("/usr/local/share/fonts"); | ||
|
||
if let Some(dir) = dirs::font_dir() { | ||
self.search_dir(dir); | ||
} | ||
} | ||
|
||
/// Search for fonts in the macOS system font directories. | ||
#[cfg(target_os = "macos")] | ||
fn search_system(&mut self) { | ||
self.search_dir("/Library/Fonts"); | ||
self.search_dir("/Network/Library/Fonts"); | ||
self.search_dir("/System/Library/Fonts"); | ||
|
||
if let Some(dir) = dirs::font_dir() { | ||
self.search_dir(dir); | ||
} | ||
} | ||
|
||
/// Search for fonts in the Windows system font directories. | ||
#[cfg(windows)] | ||
fn search_system(&mut self) { | ||
let windir = std::env::var("WINDIR").unwrap_or_else(|_| "C:\\Windows".to_string()); | ||
|
||
self.search_dir(Path::new(&windir).join("Fonts")); | ||
|
||
if let Some(roaming) = dirs::config_dir() { | ||
self.search_dir(roaming.join("Microsoft\\Windows\\Fonts")); | ||
} | ||
|
||
if let Some(local) = dirs::cache_dir() { | ||
self.search_dir(local.join("Microsoft\\Windows\\Fonts")); | ||
} | ||
} | ||
|
||
/// Search for all fonts in a directory recursively. | ||
fn search_dir(&mut self, path: impl AsRef<Path>) { | ||
for entry in WalkDir::new(path) | ||
.follow_links(true) | ||
.sort_by(|a, b| a.file_name().cmp(b.file_name())) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
{ | ||
let path = entry.path(); | ||
if matches!( | ||
path.extension().and_then(|s| s.to_str()), | ||
Some("ttf" | "otf" | "TTF" | "OTF" | "ttc" | "otc" | "TTC" | "OTC"), | ||
) { | ||
self.search_file(path); | ||
} | ||
} | ||
} | ||
|
||
/// Index the fonts in the file at the given path. | ||
fn search_file(&mut self, path: impl AsRef<Path>) { | ||
trace!("searching font file {:?}", path.as_ref()); | ||
let path = path.as_ref(); | ||
if let Ok(file) = File::open(path) { | ||
if let Ok(mmap) = unsafe { Mmap::map(&file) } { | ||
for (i, info) in FontInfo::iter(&mmap).enumerate() { | ||
self.book.push(info); | ||
self.fonts.push(FontSlot { | ||
path: path.into(), | ||
index: i as u32, | ||
font: OnceCell::new(), | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod engine; | ||
mod font; | ||
|
||
pub use engine::*; | ||
pub use font::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters