tinymist_task/compute/
svg.rs

1//! The computation for svg export.
2
3use std::sync::Arc;
4
5use tinymist_std::error::prelude::*;
6use tinymist_std::typst::TypstPagedDocument;
7use tinymist_world::{CompilerFeat, ExportComputation, WorldComputeGraph};
8use typst::model::Document;
9
10use crate::compute::{parse_length, select_pages};
11use crate::model::ExportSvgTask;
12use crate::{ImageOutput, PageMerge, PagedOutput};
13
14/// The computation for svg export.
15pub struct SvgExport;
16
17impl<F: CompilerFeat> ExportComputation<F, TypstPagedDocument> for SvgExport {
18    type Output = ImageOutput<String>;
19    type Config = ExportSvgTask;
20
21    fn run(
22        _graph: &Arc<WorldComputeGraph<F>>,
23        doc: &Arc<TypstPagedDocument>,
24        config: &ExportSvgTask,
25    ) -> Result<Self::Output> {
26        let svg_options = typst_svg::SvgOptions::default();
27        let exported_pages = select_pages(doc, &config.pages);
28        if let Some(PageMerge { ref gap }) = config.merge {
29            // Typst does not expose svg-merging API.
30            // Therefore, we have to create a dummy document here.
31            let dummy_doc = TypstPagedDocument::new(
32                exported_pages
33                    .into_iter()
34                    .map(|(_, page)| page.clone())
35                    .collect(),
36                doc.info().clone(),
37            );
38            let gap = gap
39                .as_ref()
40                .and_then(|gap| parse_length(gap).ok())
41                .unwrap_or_default();
42            let svg = typst_svg::svg_merged(&dummy_doc, &svg_options, gap);
43            Ok(ImageOutput::Merged(svg))
44        } else {
45            let exported = exported_pages
46                .into_iter()
47                .map(|(i, page)| {
48                    let svg = typst_svg::svg(page, &svg_options);
49                    Ok(PagedOutput {
50                        page: i,
51                        value: svg,
52                    })
53                })
54                .collect::<Result<Vec<_>>>()?;
55            Ok(ImageOutput::Paged(exported))
56        }
57    }
58}
59
60// impl<F: CompilerFeat> WorldComputable<F> for SvgExport {
61//     type Output = Option<String>;
62
63//     fn compute(graph: &Arc<WorldComputeGraph<F>>) -> Result<Self::Output> {
64//         OptionDocumentTask::run_export::<F, Self>(graph)
65//     }
66// }