diff options
| author | Wenzhuo Liu <mgt@oi-wiki.org> | 2023-08-08 18:54:13 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-08 12:54:13 +0200 |
| commit | 61e4ad6bbafaa97b965a206ad06af65a0805be7e (patch) | |
| tree | 54f0d17b4f36fb58eee483d7913e8eae84dee525 /crates/typst-cli | |
| parent | f56060c1b3fd029f1744322025b89253c5cf5457 (diff) | |
Add SVG export (#1729)
Diffstat (limited to 'crates/typst-cli')
| -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(()) |
