summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-24 17:51:07 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-24 17:51:07 +0100
commit96f72eee6c6b595164c7a0576c407d7a590661db (patch)
treed212f57faf3e919937921884a75bf45efb02f4ae
parent8d3c68a1deb28dce2b80ed61f85141180ce6a951 (diff)
Separate typesetting and compilation
-rw-r--r--cli/src/main.rs28
-rw-r--r--src/lib.rs12
-rw-r--r--src/model/mod.rs2
-rw-r--r--src/model/typeset.rs17
-rw-r--r--tests/src/benches.rs12
-rw-r--r--tests/src/tests.rs2
6 files changed, 49 insertions, 24 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index 3db491bb..2825f74c 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -30,12 +30,12 @@ type CodespanError = codespan_reporting::files::Error;
/// What to do.
enum Command {
- Typeset(TypesetCommand),
+ Compile(CompileCommand),
Fonts(FontsCommand),
}
-/// Typeset a .typ file into a PDF file.
-struct TypesetCommand {
+/// Compile a .typ file into a PDF file.
+struct CompileCommand {
input: PathBuf,
output: PathBuf,
root: Option<PathBuf>,
@@ -110,7 +110,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::Typeset(TypesetCommand { input, output, watch, root })
+ Command::Compile(CompileCommand { input, output, watch, root })
};
// Don't allow excess arguments.
@@ -164,13 +164,13 @@ fn print_error(msg: &str) -> io::Result<()> {
/// Dispatch a command.
fn dispatch(command: Command) -> StrResult<()> {
match command {
- Command::Typeset(command) => typeset(command),
+ Command::Compile(command) => compile(command),
Command::Fonts(command) => fonts(command),
}
}
-/// Execute a typesetting command.
-fn typeset(command: TypesetCommand) -> StrResult<()> {
+/// Execute a compilation command.
+fn compile(command: CompileCommand) -> StrResult<()> {
let root = if let Some(root) = &command.root {
root.clone()
} else if let Some(dir) = command.input.parent() {
@@ -182,8 +182,8 @@ fn typeset(command: TypesetCommand) -> StrResult<()> {
// Create the world that serves sources, fonts and files.
let mut world = SystemWorld::new(root);
- // Typeset.
- typeset_once(&mut world, &command)?;
+ // Perform initial compilation.
+ compile_once(&mut world, &command)?;
if !command.watch {
return Ok(());
@@ -221,20 +221,20 @@ fn typeset(command: TypesetCommand) -> StrResult<()> {
}
if recompile {
- typeset_once(&mut world, &command)?;
+ compile_once(&mut world, &command)?;
}
}
}
-/// Typeset a single time.
-fn typeset_once(world: &mut SystemWorld, command: &TypesetCommand) -> StrResult<()> {
+/// Compile a single time.
+fn compile_once(world: &mut SystemWorld, command: &CompileCommand) -> StrResult<()> {
status(command, Status::Compiling).unwrap();
world.reset();
let main = world.resolve(&command.input).map_err(|err| err.to_string())?;
let source = world.source(main);
- match typst::typeset(world, source) {
+ match typst::compile(world, source) {
// Export the PDF.
Ok(frames) => {
let buffer = typst::export::pdf(&frames);
@@ -254,7 +254,7 @@ fn typeset_once(world: &mut SystemWorld, command: &TypesetCommand) -> StrResult<
}
/// Clear the terminal and render the status message.
-fn status(command: &TypesetCommand, status: Status) -> io::Result<()> {
+fn status(command: &CompileCommand, status: Status) -> io::Result<()> {
if !command.watch {
return Ok(());
}
diff --git a/src/lib.rs b/src/lib.rs
index 75200144..4607cd0c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -49,16 +49,16 @@ use comemo::{Prehashed, Track};
use crate::diag::{FileResult, SourceResult};
use crate::font::{Font, FontBook};
use crate::frame::Frame;
-use crate::model::{Library, Route, StyleChain};
+use crate::model::{Library, Route};
use crate::syntax::{Source, SourceId};
use crate::util::Buffer;
-/// Typeset a source file into a collection of layouted frames.
+/// Compile a source file into a collection of layouted frames.
///
/// Returns either a vector of frames representing individual pages or
/// diagnostics in the form of a vector of error message with file and span
/// information.
-pub fn typeset(
+pub fn compile(
world: &(dyn World + 'static),
source: &Source,
) -> SourceResult<Vec<Frame>> {
@@ -66,10 +66,8 @@ pub fn typeset(
let route = Route::default();
let module = model::eval(world.track(), route.track(), source)?;
- // Layout the module's contents.
- let library = world.library();
- let styles = StyleChain::with_root(&library.styles);
- (library.items.layout)(&module.content, world.track(), styles)
+ // Typeset the module's contents.
+ model::typeset(world.track(), &module.content)
}
/// The environment in which typesetting occurs.
diff --git a/src/model/mod.rs b/src/model/mod.rs
index 93e33d5c..f5c1e6dd 100644
--- a/src/model/mod.rs
+++ b/src/model/mod.rs
@@ -21,6 +21,7 @@ mod func;
mod methods;
mod ops;
mod scope;
+mod typeset;
mod vm;
#[doc(hidden)]
@@ -38,5 +39,6 @@ pub use self::library::*;
pub use self::scope::*;
pub use self::str::*;
pub use self::styles::*;
+pub use self::typeset::*;
pub use self::value::*;
pub use self::vm::*;
diff --git a/src/model/typeset.rs b/src/model/typeset.rs
new file mode 100644
index 00000000..2026bf99
--- /dev/null
+++ b/src/model/typeset.rs
@@ -0,0 +1,17 @@
+use comemo::Tracked;
+
+use super::{Content, StyleChain};
+use crate::diag::SourceResult;
+use crate::frame::Frame;
+use crate::World;
+
+/// Typeset content into a collection of layouted frames.
+///
+/// Returns either a vector of frames representing individual pages or
+/// diagnostics in the form of a vector of error message with file and span
+/// information.
+pub fn typeset(world: Tracked<dyn World>, content: &Content) -> SourceResult<Vec<Frame>> {
+ let library = world.library();
+ let styles = StyleChain::with_root(&library.styles);
+ (library.items.layout)(content, world, styles)
+}
diff --git a/tests/src/benches.rs b/tests/src/benches.rs
index 1e5550c5..754f80a9 100644
--- a/tests/src/benches.rs
+++ b/tests/src/benches.rs
@@ -21,6 +21,7 @@ main!(
bench_edit,
bench_eval,
bench_typeset,
+ bench_compile,
bench_highlight,
bench_render,
);
@@ -81,12 +82,19 @@ fn bench_eval(iai: &mut Iai) {
fn bench_typeset(iai: &mut Iai) {
let world = BenchWorld::new();
- iai.run(|| typst::typeset(&world, &world.source));
+ let route = typst::model::Route::default();
+ let module = typst::model::eval(world.track(), route.track(), &world.source).unwrap();
+ iai.run(|| typst::model::typeset(world.track(), &module.content));
+}
+
+fn bench_compile(iai: &mut Iai) {
+ let world = BenchWorld::new();
+ iai.run(|| typst::compile(&world, &world.source));
}
fn bench_render(iai: &mut Iai) {
let world = BenchWorld::new();
- let frames = typst::typeset(&world, &world.source).unwrap();
+ let frames = typst::compile(&world, &world.source).unwrap();
iai.run(|| typst::export::render(&frames[0], 1.0))
}
diff --git a/tests/src/tests.rs b/tests/src/tests.rs
index 2c0a1e71..f96c651b 100644
--- a/tests/src/tests.rs
+++ b/tests/src/tests.rs
@@ -424,7 +424,7 @@ fn test_part(
println!("Model:\n{:#?}\n", module.content);
}
- let (mut frames, errors) = match typst::typeset(world, source) {
+ let (mut frames, errors) = match typst::compile(world, source) {
Ok(frames) => (frames, vec![]),
Err(errors) => (vec![], *errors),
};