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,
root: Option<PathBuf>,
watch: bool,
font_paths: Vec<PathBuf>,
}
const HELP: &'static str = "\
@ -54,29 +55,32 @@ ARGS:
[output.pdf] Path to output PDF file
OPTIONS:
-h, --help Print this help
-V, --version Print the CLI's version
-w, --watch Watch the inputs and recompile on changes
--root <dir> Configure the root for absolute paths
-h, --help Print this help
-V, --version Print the CLI's version
-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
SUBCOMMANDS:
--fonts List all discovered system fonts
--fonts List all discovered fonts in system and custom font paths
";
/// List discovered system fonts.
struct FontsCommand {
font_paths: Vec<PathBuf>,
variants: bool,
}
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:
typst --fonts [OPTIONS]
OPTIONS:
-h, --help Print this help
--variants Also list style variants of each font family
-h, --help Print this help
--font-path <dir> Add additional directories to search for fonts
--variants Also list style variants of each font family
";
/// Entry point.
@ -100,13 +104,14 @@ fn parse_args() -> StrResult<Command> {
}
let help = args.contains(["-h", "--help"]);
let font_paths = args.values_from_str("--font-path").unwrap();
let command = if args.contains("--fonts") {
if help {
print_help(HELP_FONTS);
}
Command::Fonts(FontsCommand { variants: args.contains("--variants") })
Command::Fonts(FontsCommand { font_paths, variants: args.contains("--variants") })
} else {
if 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 watch = args.contains(["-w", "--watch"]);
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.
@ -191,7 +196,7 @@ fn compile(command: CompileCommand) -> StrResult<()> {
};
// 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.
compile_once(&mut world, &command)?;
@ -360,6 +365,9 @@ fn print_diagnostics(
fn fonts(command: FontsCommand) -> StrResult<()> {
let mut searcher = FontSearcher::new();
searcher.search_system();
for path in &command.font_paths {
searcher.search_dir(path)
}
for (name, infos) in searcher.book.families() {
println!("{name}");
if command.variants {
@ -400,13 +408,17 @@ struct PathSlot {
}
impl SystemWorld {
fn new(root: PathBuf) -> Self {
fn new(root: PathBuf, font_paths: &[PathBuf]) -> Self {
let mut searcher = FontSearcher::new();
searcher.search_system();
#[cfg(feature = "embed-fonts")]
searcher.add_embedded();
for path in font_paths {
searcher.search_dir(path)
}
Self {
root,
library: Prehashed::new(typst_library::build()),