diff --git a/cli/src/main.rs b/cli/src/main.rs index 2e269bdaa..b7505800f 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -40,6 +40,7 @@ struct CompileCommand { output: PathBuf, root: Option, watch: bool, + font_paths: Vec, } 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 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 Add additional directories to search for fonts + --root 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, 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 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 { } 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 { 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()),