Extract typst-svg crate

This commit is contained in:
Laurenz 2023-11-08 13:21:25 +01:00
parent ec04c3de2f
commit 80b4ca4c04
10 changed files with 56 additions and 16 deletions

19
Cargo.lock generated
View File

@ -2930,8 +2930,6 @@ dependencies = [
"unscanny", "unscanny",
"usvg", "usvg",
"wasmi", "wasmi",
"xmlparser",
"xmlwriter",
"xmp-writer", "xmp-writer",
] ]
@ -2974,6 +2972,7 @@ dependencies = [
"typst", "typst",
"typst-library", "typst-library",
"typst-render", "typst-render",
"typst-svg",
"ureq", "ureq",
"xz2", "xz2",
"zip", "zip",
@ -3080,6 +3079,21 @@ dependencies = [
"usvg", "usvg",
] ]
[[package]]
name = "typst-svg"
version = "0.9.0"
dependencies = [
"base64",
"comemo",
"ecow",
"flate2",
"tracing",
"ttf-parser",
"typst",
"xmlparser",
"xmlwriter",
]
[[package]] [[package]]
name = "typst-syntax" name = "typst-syntax"
version = "0.9.0" version = "0.9.0"
@ -3111,6 +3125,7 @@ dependencies = [
"typst", "typst",
"typst-library", "typst-library",
"typst-render", "typst-render",
"typst-svg",
"unscanny", "unscanny",
"walkdir", "walkdir",
] ]

View File

@ -20,6 +20,7 @@ typst = { path = "crates/typst" }
typst-library = { path = "crates/typst-library" } typst-library = { path = "crates/typst-library" }
typst-macros = { path = "crates/typst-macros" } typst-macros = { path = "crates/typst-macros" }
typst-render = { path = "crates/typst-render" } typst-render = { path = "crates/typst-render" }
typst-svg = { path = "crates/typst-svg" }
typst-syntax = { path = "crates/typst-syntax" } typst-syntax = { path = "crates/typst-syntax" }
az = "1.2" az = "1.2"
base64 = "0.21.2" base64 = "0.21.2"

View File

@ -23,6 +23,7 @@ doc = false
typst = { workspace = true } typst = { workspace = true }
typst-library = { workspace = true } typst-library = { workspace = true }
typst-render = { workspace = true } typst-render = { workspace = true }
typst-svg = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
codespan-reporting = { workspace = true } codespan-reporting = { workspace = true }

View File

@ -226,7 +226,7 @@ fn export_image(
.map_err(|err| eco_format!("failed to write PNG file ({err})"))?; .map_err(|err| eco_format!("failed to write PNG file ({err})"))?;
} }
ImageExportFormat::Svg => { ImageExportFormat::Svg => {
let svg = typst::export::svg(frame); let svg = typst_svg::svg(frame);
fs::write(path, svg.as_bytes()) fs::write(path, svg.as_bytes())
.map_err(|err| eco_format!("failed to write SVG file ({err})"))?; .map_err(|err| eco_format!("failed to write SVG file ({err})"))?;
} }

View File

@ -0,0 +1,27 @@
[package]
name = "typst-svg"
description = "SVG exporter for Typst."
version.workspace = true
rust-version.workspace = true
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
categories.workspace = true
keywords.workspace = true
[lib]
doctest = false
bench = false
[dependencies]
typst = { workspace = true }
base64 = { workspace = true }
comemo = { workspace = true }
ecow = { workspace = true}
flate2 = { workspace = true }
tracing = { workspace = true }
ttf-parser = { workspace = true }
xmlparser = { workspace = true }
xmlwriter = { workspace = true }

View File

@ -6,18 +6,17 @@ use std::io::Read;
use base64::Engine; use base64::Engine;
use ecow::{eco_format, EcoString}; use ecow::{eco_format, EcoString};
use ttf_parser::{GlyphId, OutlineBuilder}; use ttf_parser::{GlyphId, OutlineBuilder};
use xmlwriter::XmlWriter; use typst::doc::{Frame, FrameItem, FrameKind, GroupItem, TextItem};
use typst::eval::Repr;
use crate::doc::{Frame, FrameItem, FrameKind, GroupItem, TextItem}; use typst::font::Font;
use crate::eval::Repr; use typst::geom::{
use crate::font::Font;
use crate::geom::{
self, Abs, Angle, Axes, Color, FixedStroke, Geometry, Gradient, LineCap, LineJoin, self, Abs, Angle, Axes, Color, FixedStroke, Geometry, Gradient, LineCap, LineJoin,
Paint, PathItem, Point, Quadrant, Ratio, RatioOrAngle, Relative, Shape, Size, Paint, PathItem, Point, Quadrant, Ratio, RatioOrAngle, Relative, Shape, Size,
Transform, Transform,
}; };
use crate::image::{Image, ImageFormat, RasterFormat, VectorFormat}; use typst::image::{Image, ImageFormat, RasterFormat, VectorFormat};
use crate::util::hash128; use typst::util::hash128;
use xmlwriter::XmlWriter;
/// The number of segments in a conic gradient. /// The number of segments in a conic gradient.
/// This is a heuristic value that seems to work well. /// This is a heuristic value that seems to work well.

View File

@ -53,8 +53,6 @@ unicode-segmentation = { workspace = true }
unscanny = { workspace = true } unscanny = { workspace = true }
usvg = { workspace = true } usvg = { workspace = true }
wasmi = { workspace = true } wasmi = { workspace = true }
xmlparser = { workspace = true }
xmlwriter = { workspace = true }
xmp-writer = { workspace = true } xmp-writer = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -1,7 +1,5 @@
//! Exporting into external formats. //! Exporting into external formats.
mod pdf; mod pdf;
mod svg;
pub use self::pdf::{pdf, PdfPageLabel, PdfPageLabelStyle}; pub use self::pdf::{pdf, PdfPageLabel, PdfPageLabelStyle};
pub use self::svg::{svg, svg_merged};

View File

@ -10,6 +10,7 @@ publish = false
typst = { workspace = true } typst = { workspace = true }
typst-library = { workspace = true } typst-library = { workspace = true }
typst-render = { workspace = true } typst-render = { workspace = true }
typst-svg = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
comemo = { workspace = true } comemo = { workspace = true }
ecow = { workspace = true } ecow = { workspace = true }

View File

@ -439,7 +439,7 @@ fn test(
fs::create_dir_all(png_path.parent().unwrap()).unwrap(); fs::create_dir_all(png_path.parent().unwrap()).unwrap();
canvas.save_png(png_path).unwrap(); canvas.save_png(png_path).unwrap();
let svg = typst::export::svg_merged(&document.pages, Abs::pt(5.0)); let svg = typst_svg::svg_merged(&document.pages, Abs::pt(5.0));
fs::create_dir_all(svg_path.parent().unwrap()).unwrap(); fs::create_dir_all(svg_path.parent().unwrap()).unwrap();
std::fs::write(svg_path, svg.as_bytes()).unwrap(); std::fs::write(svg_path, svg.as_bytes()).unwrap();