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