diff --git a/Cargo.lock b/Cargo.lock index b267423d8..a2e846d04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1258,6 +1258,15 @@ dependencies = [ "serde", ] +[[package]] +name = "craft-pdf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "krilla", + "thiserror 2.0.21", +] + [[package]] name = "craft-penpot" version = "0.1.0" @@ -2272,6 +2281,7 @@ dependencies = [ name = "figcraft-pdf" version = "0.18.1" dependencies = [ + "craft-pdf", "figcraft-color", "figcraft-doc", "figcraft-geom", diff --git a/Cargo.toml b/Cargo.toml index 03ac7eb0e..95cfc2a23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ repository = "https://github.com/storytold/figcraft" craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "010702ee9b030844c852939095b1da0d0656a890", default-features = false, features = ["tree"] } craft-vectordrawable = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5cd16f2b9ac2a84c4c0794e609914e2bbf848bd7" } +craft-pdf = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "010702ee9b030844c852939095b1da0d0656a890" } # internal figcraft-geom = { path = "crates/geom" } figcraft-color = { path = "crates/color" } diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 63e776c21..e86bf9169 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -9,6 +9,7 @@ description = "FigCraft PDF export (krilla): vector shapes, gradients, embedded workspace = true [dependencies] +craft-pdf = { workspace = true, features = ["write"] } figcraft-geom = { workspace = true } figcraft-color = { workspace = true } figcraft-doc = { workspace = true } diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 46c92ee4c..4fd035d7a 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -78,6 +78,12 @@ pub enum PdfError { Write(String), } +impl From for PdfError { + fn from(e: craft_pdf::write::WriteError) -> Self { + Self::Write(e.to_string()) + } +} + /// Largest page side, in points (PDF 1.7 has no hard limit; viewers cope with this). pub const MAX_PAGE_SIDE: f64 = 200_000.0; @@ -149,7 +155,7 @@ fn link_annotation(r: Rect, url: String, w: f64, h: f64) -> Option 0.0 && r.height() > 0.0) || url.is_empty() || url.len() > MAX_URL { + if !(r.width() > 0.0 && r.height() > 0.0) || url.is_empty() || url.len() > MAX_URL || !craft_pdf::safe_url(url) { return None; } let rect = krilla::geom::Rect::from_ltrb(r.x0 as f32, r.y0 as f32, r.x1 as f32, r.y1 as f32)?; @@ -162,6 +168,10 @@ fn rect_finite(r: Rect) -> bool { } fn write(doc: &Document, pages: &[(Vec, Rect)], opts: &PdfOptions) -> Result, PdfError> { + craft_pdf::write::guard(|| write_inner(doc, pages, opts)) +} + +fn write_inner(doc: &Document, pages: &[(Vec, Rect)], opts: &PdfOptions) -> Result, PdfError> { let mut pdf = krilla::Document::new(); let mut meta = krilla::metadata::Metadata::new().creator("FigCraft".into()).producer("FigCraft".into()); if let Some(t) = opts.title.as_ref().filter(|t| !t.trim().is_empty()) {