Add custom font search path support for typst cli (#270)

This commit is contained in:
7sDream 2023-03-25 22:04:56 +08:00 committed by GitHub
parent 25e8520a4e
commit a4a915cc5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,6 +40,7 @@ struct CompileCommand {
output: PathBuf, output: PathBuf,
root: Option<PathBuf>, root: Option<PathBuf>,
watch: bool, watch: bool,
font_paths: Vec<PathBuf>,
} }
const HELP: &'static str = "\ const HELP: &'static str = "\
@ -57,25 +58,28 @@ OPTIONS:
-h, --help Print this help -h, --help Print this help
-V, --version Print the CLI's version -V, --version Print the CLI's version
-w, --watch Watch the inputs and recompile on changes -w, --watch Watch the inputs and recompile on changes
--font-path <dir> Add additional directories to search for fonts
--root <dir> Configure the root for absolute paths --root <dir> Configure the root for absolute paths
SUBCOMMANDS: SUBCOMMANDS:
--fonts List all discovered system fonts --fonts List all discovered fonts in system and custom font paths
"; ";
/// List discovered system fonts. /// List discovered system fonts.
struct FontsCommand { struct FontsCommand {
font_paths: Vec<PathBuf>,
variants: bool, variants: bool,
} }
const HELP_FONTS: &'static str = "\ const HELP_FONTS: &'static str = "\
typst --fonts lists all discovered system fonts typst --fonts lists all discovered fonts in system and custom font paths
USAGE: USAGE:
typst --fonts [OPTIONS] typst --fonts [OPTIONS]
OPTIONS: OPTIONS:
-h, --help Print this help -h, --help Print this help
--font-path <dir> Add additional directories to search for fonts
--variants Also list style variants of each font family --variants Also list style variants of each font family
"; ";
@ -100,13 +104,14 @@ fn parse_args() -> StrResult<Command> {
} }
let help = args.contains(["-h", "--help"]); let help = args.contains(["-h", "--help"]);
let font_paths = args.values_from_str("--font-path").unwrap();
let command = if args.contains("--fonts") { let command = if args.contains("--fonts") {
if help { if help {
print_help(HELP_FONTS); print_help(HELP_FONTS);
} }
Command::Fonts(FontsCommand { variants: args.contains("--variants") }) Command::Fonts(FontsCommand { font_paths, variants: args.contains("--variants") })
} else { } else {
if help { if help {
print_help(HELP); print_help(HELP);
@ -115,7 +120,7 @@ fn parse_args() -> StrResult<Command> {
let root = args.opt_value_from_str("--root").map_err(|_| "missing root path")?; let root = args.opt_value_from_str("--root").map_err(|_| "missing root path")?;
let watch = args.contains(["-w", "--watch"]); let watch = args.contains(["-w", "--watch"]);
let (input, output) = parse_input_output(&mut args, "pdf")?; let (input, output) = parse_input_output(&mut args, "pdf")?;
Command::Compile(CompileCommand { input, output, watch, root }) Command::Compile(CompileCommand { input, output, watch, root, font_paths })
}; };
// Don't allow excess arguments. // Don't allow excess arguments.
@ -191,7 +196,7 @@ fn compile(command: CompileCommand) -> StrResult<()> {
}; };
// Create the world that serves sources, fonts and files. // Create the world that serves sources, fonts and files.
let mut world = SystemWorld::new(root); let mut world = SystemWorld::new(root, &command.font_paths);
// Perform initial compilation. // Perform initial compilation.
compile_once(&mut world, &command)?; compile_once(&mut world, &command)?;
@ -360,6 +365,9 @@ fn print_diagnostics(
fn fonts(command: FontsCommand) -> StrResult<()> { fn fonts(command: FontsCommand) -> StrResult<()> {
let mut searcher = FontSearcher::new(); let mut searcher = FontSearcher::new();
searcher.search_system(); searcher.search_system();
for path in &command.font_paths {
searcher.search_dir(path)
}
for (name, infos) in searcher.book.families() { for (name, infos) in searcher.book.families() {
println!("{name}"); println!("{name}");
if command.variants { if command.variants {
@ -400,13 +408,17 @@ struct PathSlot {
} }
impl SystemWorld { impl SystemWorld {
fn new(root: PathBuf) -> Self { fn new(root: PathBuf, font_paths: &[PathBuf]) -> Self {
let mut searcher = FontSearcher::new(); let mut searcher = FontSearcher::new();
searcher.search_system(); searcher.search_system();
#[cfg(feature = "embed-fonts")] #[cfg(feature = "embed-fonts")]
searcher.add_embedded(); searcher.add_embedded();
for path in font_paths {
searcher.search_dir(path)
}
Self { Self {
root, root,
library: Prehashed::new(typst_library::build()), library: Prehashed::new(typst_library::build()),