mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Search for fonts in the project directory
This commit is contained in:
parent
c6f8ad35f4
commit
14048937b8
25
src/main.rs
25
src/main.rs
@ -10,7 +10,10 @@ use same_file::is_same_file;
|
|||||||
use termcolor::{ColorChoice, StandardStream, WriteColor};
|
use termcolor::{ColorChoice, StandardStream, WriteColor};
|
||||||
|
|
||||||
use typst::diag::Error;
|
use typst::diag::Error;
|
||||||
|
use typst::export;
|
||||||
|
use typst::loading::FsLoader;
|
||||||
use typst::source::SourceStore;
|
use typst::source::SourceStore;
|
||||||
|
use typst::Context;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if let Err(error) = try_main() {
|
if let Err(error) = try_main() {
|
||||||
@ -27,14 +30,24 @@ fn try_main() -> anyhow::Result<()> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Create a loader for fonts and files.
|
// Create a loader for fonts and files.
|
||||||
let loader = typst::loading::FsLoader::new()
|
let mut loader = FsLoader::new();
|
||||||
.with_path("fonts")
|
|
||||||
.with_system()
|
// Search for fonts in the project directory.
|
||||||
.wrap();
|
if let Some(dir) = args.input.parent() {
|
||||||
|
if dir.as_os_str().is_empty() {
|
||||||
|
// Just a filename, so directory is current directory.
|
||||||
|
loader.search_path(".");
|
||||||
|
} else {
|
||||||
|
loader.search_path(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search system fonts only now to give local fonts priority.
|
||||||
|
loader.search_system();
|
||||||
|
|
||||||
// Create the context which holds loaded source files, fonts, images and
|
// Create the context which holds loaded source files, fonts, images and
|
||||||
// cached artifacts.
|
// cached artifacts.
|
||||||
let mut ctx = typst::Context::new(loader);
|
let mut ctx = Context::new(loader.wrap());
|
||||||
|
|
||||||
// Ensure that the source file is not overwritten.
|
// Ensure that the source file is not overwritten.
|
||||||
if is_same_file(&args.input, &args.output).unwrap_or(false) {
|
if is_same_file(&args.input, &args.output).unwrap_or(false) {
|
||||||
@ -48,7 +61,7 @@ fn try_main() -> anyhow::Result<()> {
|
|||||||
match ctx.typeset(id) {
|
match ctx.typeset(id) {
|
||||||
// Export the PDF.
|
// Export the PDF.
|
||||||
Ok(document) => {
|
Ok(document) => {
|
||||||
let buffer = typst::export::pdf(&ctx, &document);
|
let buffer = export::pdf(&ctx, &document);
|
||||||
fs::write(&args.output, buffer).context("failed to write PDF file")?;
|
fs::write(&args.output, buffer).context("failed to write PDF file")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user