diff options
Diffstat (limited to 'crates/typst-cli/src/compile.rs')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index ca088a76..331a2b1f 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -95,10 +95,20 @@ pub fn compile_once( Ok(()) } +enum ExportImageFormat { + Png, + Svg, +} + /// Export into the target format. fn export(document: &Document, command: &CompileCommand) -> StrResult<()> { match command.output().extension() { - Some(ext) if ext.eq_ignore_ascii_case("png") => export_png(document, command), + Some(ext) if ext.eq_ignore_ascii_case("png") => { + export_image(document, command, ExportImageFormat::Png) + } + Some(ext) if ext.eq_ignore_ascii_case("svg") => { + export_image(document, command, ExportImageFormat::Svg) + } _ => export_pdf(document, command), } } @@ -112,7 +122,11 @@ fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> { } /// Export to one or multiple PNGs. -fn export_png(document: &Document, command: &CompileCommand) -> StrResult<()> { +fn export_image( + document: &Document, + command: &CompileCommand, + fmt: ExportImageFormat, +) -> StrResult<()> { // Determine whether we have a `{n}` numbering. let output = command.output(); let string = output.to_str().unwrap_or_default(); @@ -128,14 +142,23 @@ fn export_png(document: &Document, command: &CompileCommand) -> StrResult<()> { let mut storage; for (i, frame) in document.pages.iter().enumerate() { - let pixmap = typst::export::render(frame, command.ppi / 72.0, Color::WHITE); let path = if numbered { storage = string.replace("{n}", &format!("{:0width$}", i + 1)); Path::new(&storage) } else { output.as_path() }; - pixmap.save_png(path).map_err(|_| "failed to write PNG file")?; + match fmt { + ExportImageFormat::Png => { + let pixmap = + typst::export::render(frame, command.ppi / 72.0, Color::WHITE); + pixmap.save_png(path).map_err(|_| "failed to write PNG file")?; + } + ExportImageFormat::Svg => { + let svg = typst::export::svg_frame(frame); + fs::write(path, svg).map_err(|_| "failed to write SVG file")?; + } + } } Ok(()) |
