From ce40702a501c3159db89693bd7da713ae1b4c011 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 29 Apr 2026 21:25:40 +0200 Subject: feat(typst): embed fonts --- build.zig | 2 +- src/data/_base.typ | 14 ++++++++++++++ src/data/test_content_1.typ | 13 +++---------- src/data/test_content_2.typ | 13 +++---------- src/eval/template_math_block.typ | 14 +++++++++----- src/eval/template_math_content.typ | 15 +++++++++------ typst/Cargo.lock | 1 + typst/Cargo.toml | 5 +++++ typst/src/world.rs | 29 ++++++++++------------------- 9 files changed, 55 insertions(+), 51 deletions(-) create mode 100644 src/data/_base.typ diff --git a/build.zig b/build.zig index c85791f..6782207 100644 --- a/build.zig +++ b/build.zig @@ -76,7 +76,7 @@ fn generateSVG(b: *std.Build, step: *std.Build.Step) !void { defer dir.close(); var iter = dir.iterate(); while (try iter.next()) |it| { - if (it.kind == .file and std.mem.endsWith(u8, it.name, ".typ")) { + if (it.kind == .file and std.mem.endsWith(u8, it.name, ".typ") and !std.mem.startsWith(u8, it.name, "_")) { const cmd = b.addSystemCommand(&[_][]const u8{ "typst", "c", "-f", "svg", diff --git a/src/data/_base.typ b/src/data/_base.typ new file mode 100644 index 0000000..fa5b362 --- /dev/null +++ b/src/data/_base.typ @@ -0,0 +1,14 @@ +#let display(body) = context { + show math.equation: set text(font: "New Computer Modern Math") + show math.text: set text(font: "New Computer Modern") + + let margin = 4pt + let m = measure(body) + set page( + fill: none, + margin: margin, + width: m.width + margin*2, + height: m.height + margin*2, + ) + body +} diff --git a/src/data/test_content_1.typ b/src/data/test_content_1.typ index 3adf59a..e2f21a5 100644 --- a/src/data/test_content_1.typ +++ b/src/data/test_content_1.typ @@ -1,12 +1,5 @@ -#set page( - fill: none, - margin: 2pt, -); +#import "_base.typ": * -#let display(body) = context { - let m = measure(body) - set page(width: m.width + page.margin.length*2, height: m.height + page.margin.length*2) - body -} +#show: display.with() -#display()[$x$] +$text("Adwaita Mono") x$ diff --git a/src/data/test_content_2.typ b/src/data/test_content_2.typ index db02163..47666a8 100644 --- a/src/data/test_content_2.typ +++ b/src/data/test_content_2.typ @@ -1,12 +1,5 @@ -#set page( - fill: none, - margin: 2pt, -); +#import "_base.typ": * -#let display(body) = context { - let m = measure(body) - set page(width: m.width + page.margin.length*2, height: m.height + page.margin.length*2) - body -} +#show: display.with() -#display()[$x^2$] +$x^2$ diff --git a/src/eval/template_math_block.typ b/src/eval/template_math_block.typ index 3d5891a..ecadae1 100644 --- a/src/eval/template_math_block.typ +++ b/src/eval/template_math_block.typ @@ -1,11 +1,15 @@ -#set page( - fill: none, - margin: 2pt, -); +#show math.equation: set text(font: "New Computer Modern Math") +#show math.text: set text(font: "New Computer Modern") #let display(body) = context { + let margin = 2pt let m = measure(body) - set page(width: m.width + page.margin.length*2, height: m.height + page.margin.length*2) + set page( + fill: none, + margin: margin, + width: m.width + margin*2, + height: m.height + margin*2, + ) body } diff --git a/src/eval/template_math_content.typ b/src/eval/template_math_content.typ index 03732af..cd31ef6 100644 --- a/src/eval/template_math_content.typ +++ b/src/eval/template_math_content.typ @@ -1,11 +1,14 @@ -#set page( - fill: none, - margin: 2pt, -); - #let display(body) = context { + show math.equation: set text(font: "New Computer Modern Math") + + let margin = 2pt let m = measure(body) - set page(width: m.width + page.margin.length*2, height: m.height + page.margin.length*2) + set page( + fill: none, + margin: margin, + width: m.width + margin*2, + height: m.height + margin*2, + ) body } diff --git a/typst/Cargo.lock b/typst/Cargo.lock index db63b22..3ce45b7 100644 --- a/typst/Cargo.lock +++ b/typst/Cargo.lock @@ -2424,6 +2424,7 @@ dependencies = [ "serde", "serde_json", "tar", + "typst-assets", "typst-library", "typst-syntax", "typst-timing", diff --git a/typst/Cargo.toml b/typst/Cargo.toml index 3cdc8a7..dc7eb97 100644 --- a/typst/Cargo.toml +++ b/typst/Cargo.toml @@ -16,3 +16,8 @@ opt-level = 2 codegen-units = 1 lto = true strip = true + +[features] +default = ["embed-fonts"] + +embed-fonts = ["typst-kit/embed-fonts"] diff --git a/typst/src/world.rs b/typst/src/world.rs index 5bbefee..c2fe7c7 100644 --- a/typst/src/world.rs +++ b/typst/src/world.rs @@ -10,7 +10,7 @@ use typst::foundations::{Bytes, Datetime}; use typst::syntax::{FileId, Source}; use typst::text::{Font, FontBook}; use typst::utils::LazyHash; -use typst_kit::fonts::Fonts; +use typst_kit::fonts::{FontSlot, Fonts}; /// Main interface that determines the environment for Typst. pub struct MinimalWorld { @@ -24,33 +24,24 @@ pub struct MinimalWorld { book: LazyHash, /// Metadata about all known fonts. - fonts: Vec, + fonts: Vec, } impl MinimalWorld { pub fn new(source: impl Into) -> Self { - let (fonts, book) = Self::load_fonts(); + let mut searcher = Fonts::searcher(); + searcher.include_system_fonts(true); + #[cfg(feature = "embed-fonts")] + searcher.include_embedded_fonts(true); + let fonts = searcher.search(); Self { library: LazyHash::new(Library::default()), - book: LazyHash::new(book), - fonts: fonts, + book: LazyHash::new(fonts.book), + fonts: fonts.fonts, source: Source::detached(source), } } - - fn load_fonts() -> (Vec, FontBook) { - let mut searcher = Fonts::searcher(); - searcher.include_system_fonts(true); - - let mut fonts = Vec::new(); - let mut book = FontBook::new(); - for font in searcher.search().fonts { - book.push(font.get().unwrap().info().clone()); - fonts.push(font.get().unwrap()); - } - (fonts, book) - } } impl World for MinimalWorld { @@ -85,7 +76,7 @@ impl World for MinimalWorld { /// Accessing a specified font per index of font book. fn font(&self, id: usize) -> Option { - self.fonts.get(id).cloned() + self.fonts.get(id)?.get() } /// Get the current date. -- cgit v1.2.3