Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/examples/svg_pages.rs

2.1 KiB, 1 run

created by r1870400018:73201, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Compiles a document through the evaluator to one SVG per page, the pages the live view receives
2//! (`compileProjectVector`), and writes them as `OUT_DIR/page-NNN.svg`. The command line writes only the
3//! PDF; this is the native way to see the browser's pages, for the SVG size and pixel gates.
4//!
5//! `cargo run --release --example svg_pages -- SOURCE.typ OUT_DIR [--root DIR] [--font-path DIR]...`
6
7use oxedyne_fe2o3_austenite::{
8 compile,
9 emit::sinks::VectorSink,
10 flow::text::FontStore,
11};
12
13use oxedyne_fe2o3_core::prelude::*;
14
15use std::path::PathBuf;
16
17fn main() -> Outcome<()> {
18 let mut pos: Vec<String> = Vec::new();
19 let mut root: Option<String> = None;
20 let mut fonts = FontStore::default();
21 let mut args = std::env::args().skip(1);
22 while let Some(a) = args.next() {
23 match a.as_str() {
24 "--root" => root = Some(res!(args.next().ok_or_else(|| err!(
25 "--root needs a directory argument."; Input, Missing)))),
26 "--font-path" => fonts.add_dir(PathBuf::from(res!(args.next().ok_or_else(|| err!(
27 "--font-path needs a directory argument."; Input, Missing))))),
28 _ => pos.push(a),
29 }
30 }
31 if pos.len() != 2 {
32 return Err(err!("Usage: svg_pages SOURCE.typ OUT_DIR [--root DIR] [--font-path DIR]..."; Input, Invalid));
33 }
34 let main = PathBuf::from(&pos[0]);
35 let out = PathBuf::from(&pos[1]);
36 let root = match root {
37 Some(r) => PathBuf::from(r),
38 None => main.parent().map(|p| p.to_path_buf()).unwrap_or_else(|| PathBuf::from(".")),
39 };
40 compile::supply_typst_package_cache();
41 let mut session = compile::Session::new(fonts);
42 let mut sink = VectorSink::default();
43 let done = res!(session.compile(&main, &root, &mut sink, None, None, true));
44 if let Err(e) = &done.laid {
45 return Err(err!("{} did not lay out: {}", main.display(), e.plain(); Input, Invalid));
46 }
47 let pages = res!(sink.into_pages().ok_or_else(|| err!(
48 "The fixpoint ended without a finished pass for {}.", main.display(); Bug)));
49 res!(std::fs::create_dir_all(&out));
50 for (i, svg) in pages.iter().enumerate() {
51 res!(std::fs::write(out.join(format!("page-{:03}.svg", i + 1)), svg));
52 }
53 println!("{}: {} page(s) to {}", main.display(), pages.len(), out.display());
54 Ok(())
55}