From f987648e96b0238df46c6ec788de64863ff4f744 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 20 Nov 2026 03:01:58 -0500 Subject: [PATCH] eps: delegate scene-writer shading, image stream, and line style syntax to craft-ps (#8810) * eps: delegate scene-writer shading, image stream, and line style syntax to craft-ps * chore(eps): repin craft-ps to merged craft-libs main (cfcc86242) --- Cargo.lock | 6 ++-- Cargo.toml | 2 +- crates/eps/src/ai_ops.rs | 28 +++++---------- crates/eps/src/ps.rs | 31 ++++++++++++++++- crates/eps/src/scene.rs | 75 +++++++--------------------------------- 5 files changed, 54 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c793b70f9d..7da3db5266 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,7 +1051,7 @@ dependencies = [ [[package]] name = "craft-eps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=7258e42d8#7258e42d8dd909f2176fb08f09aae048611f3eaf" +source = "git+https://github.com/storytold/craft-libs?rev=cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec#cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec" [[package]] name = "craft-eps" @@ -1253,9 +1253,9 @@ dependencies = [ [[package]] name = "craft-ps" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=7258e42d8#7258e42d8dd909f2176fb08f09aae048611f3eaf" +source = "git+https://github.com/storytold/craft-libs?rev=cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec#cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec" dependencies = [ - "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7258e42d8)", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec)", "flate2", "kurbo 0.13.1", "miniz_oxide 0.8.9", diff --git a/Cargo.toml b/Cargo.toml index d7d90bb1a5..0279fa081a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } -craft-ps = { git = "https://github.com/storytold/craft-libs", rev = "7258e42d8", default-features = false, features = ["read", "write"] } +craft-ps = { git = "https://github.com/storytold/craft-libs", rev = "cfcc86242d56fe68117cd5dc2e5dd3b7a0e5b0ec", default-features = false, features = ["read", "write"] } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "d7c9632bfb3040cf45db394e7500cc93c81c49e0" } diff --git a/crates/eps/src/ai_ops.rs b/crates/eps/src/ai_ops.rs index d74932e74a..9d0ff5ddc0 100644 --- a/crates/eps/src/ai_ops.rs +++ b/crates/eps/src/ai_ops.rs @@ -3,8 +3,6 @@ //! Format Specification, version 7.0, §5.3, §5.4 and §6), and the colors they write in place of //! the paint they can't hold. -use std::fmt::Write; - use vectorcraft_color::{Color, Gradient}; use vectorcraft_doc::{LAYER_COLORS, LayerColor, LineCap, LineJoin, StrokeLayer}; use vectorcraft_geom::{Affine, Anchor, AnchorKind, PathData, Point, Shape, SubPath}; @@ -184,25 +182,15 @@ pub(crate) fn window_samples(samples: &[u8], w: u32, channels: usize, [x0, y0, x /// The stroke's width, cap, join, miter limit and dashes: `w J j M d`. pub(crate) fn line_style(s: &mut String, st: &StrokeLayer) { let cap = match st.cap { - LineCap::Butt => 0, - LineCap::Round => 1, - LineCap::Square => 2, + LineCap::Butt => ps::LineCap::Butt, + LineCap::Round => ps::LineCap::Round, + LineCap::Square => ps::LineCap::Square, }; let join = match st.join { - LineJoin::Miter => 0, - LineJoin::Round => 1, - LineJoin::Bevel => 2, + LineJoin::Miter => ps::LineJoin::Miter, + LineJoin::Round => ps::LineJoin::Round, + LineJoin::Bevel => ps::LineJoin::Bevel, }; - ps::push_num(s, st.width); - let _ = write!(s, " w {cap} J {join} j "); - ps::push_num(s, if st.miter_limit.is_finite() { st.miter_limit.max(1.0) } else { 10.0 }); - s.push_str(" M ["); - let dash = st.dash.as_ref().filter(|d| d.is_dashed()); - if let Some(d) = dash { - ps::push_nums(s, &d.pattern); - s.pop(); - } - s.push_str("] "); - ps::push_num(s, dash.map_or(0.0, |d| d.offset)); - s.push_str(" d\n"); + let dash = st.dash.as_ref().filter(|d| d.is_dashed()).map(|d| (d.pattern.as_slice(), d.offset)); + ps::push_line_style(s, st.width, cap, join, st.miter_limit, dash); } diff --git a/crates/eps/src/ps.rs b/crates/eps/src/ps.rs index f43b08fcb7..a2b075df67 100644 --- a/crates/eps/src/ps.rs +++ b/crates/eps/src/ps.rs @@ -3,7 +3,10 @@ use vectorcraft_geom::{BezPath, FillRule}; -pub(crate) use craft_ps::write::{bytes, deflate, inflate, matrix, num, push_num, push_num_places, push_nums, string, written}; +pub(crate) use craft_ps::write::{ + LineCap, LineJoin, ShadingKind, bytes, deflate, inflate, matrix, num, push_image_array_ref, push_inline_image, push_line_style, push_num, + push_num_places, push_nums, push_reusable_image_ref, push_reusable_image_resource, push_shading, string, written, +}; /// The procedures the page uses, in `VCdict`. pub(crate) const PROLOG: &str = "/VCdict 64 dict def @@ -121,4 +124,30 @@ mod tests { assert_eq!(places(2.5, 7), "2.5"); assert_eq!(places(f64::NAN, 7), "0"); } + + #[test] + fn shared_scene_syntax_preserves_vc_names_and_shading() { + let mut line = String::new(); + super::push_line_style(&mut line, 3.0, super::LineCap::Square, super::LineJoin::Round, 4.0, Some((&[5.0, 1.5], 0.5))); + assert_eq!(line, "3 w 2 J 1 j 4 M [5 1.5] 0.5 d\n"); + + let mut shading = String::new(); + super::push_shading( + &mut shading, + super::ShadingKind::Axial, + "/DeviceCMYK", + &[0.0, 0.0, 100.0, 0.0], + 4, + &[(0.0, vec![0.0, 1.0, 1.0, 0.0]), (1.0, vec![1.0, 0.0, 1.0, 0.0])], + ); + assert!(shading.contains("/ShadingType 2 /ColorSpace /DeviceCMYK")); + assert!(shading.ends_with(">> shfill\n")); + + let mut inline = String::new(); + super::push_inline_image(&mut inline, "VCsrc", "", "<< /ImageType 1", "/FlateDecode", "image", "~>\n"); + assert_eq!( + inline, + "{ /VCsrc currentfile /ASCII85Decode filter def\n<< /ImageType 1\n/DataSource VCsrc /FlateDecode filter >> image\nVCsrc flushfile } exec\n~>\n" + ); + } } diff --git a/crates/eps/src/scene.rs b/crates/eps/src/scene.rs index 349bc2a16b..be26ba8c4f 100644 --- a/crates/eps/src/scene.rs +++ b/crates/eps/src/scene.rs @@ -50,8 +50,6 @@ const SPREAD_CAPPED: &str = "gradients that repeat or reflect are written with at most 256 runs of their stops (at most 16384 stops); they pad past those"; /// Wedges a conic gradient is written as: one a degree. const CONIC_WEDGE_COUNT: usize = 360; -/// Decimals of a smooth shading's stitching function bounds (stop offsets, 0 to 1). -const BOUNDS_PLACES: u32 = 7; /// The most colorants a DeviceN color space may have (PostScript's limit). const MAX_DEVICE_N: usize = 32; const MISSING_PATTERN: &str = "missing patterns are written as mid-grey"; @@ -63,8 +61,6 @@ const MAX_BODY: usize = 256 << 20; /// Most compressed bytes in one string of an image's samples at Level 2 (under PostScript's /// 65,535-byte string limit). const IMAGE_CHUNK: usize = 48 << 10; -/// The start of the procedure that reads an image's data from the lines after it. -const INLINE_SOURCE: &str = "{ /VCsrc currentfile /ASCII85Decode filter def\n"; /// An image as written: what sets up its color, its image dictionary and its compressed samples. /// Placed once, it is written where it is drawn, its data following the operators that read it. @@ -108,33 +104,20 @@ impl ImageCode { /// The operators and data that draw the image where it is placed. fn push_inline(&self, s: &mut String) { - s.push_str(&self.prelude); - s.push_str(INLINE_SOURCE); - s.push_str(&self.dict); - s.push_str(&self.inline_read()); - s.push_str(&self.ascii); - } - - /// The end of the procedure [`ImageCode::push_inline`] writes. The procedure is read whole - /// before it runs: the image data follows it, and flushing the ASCII85 filter afterwards reads - /// past its end marker. - fn inline_read(&self) -> String { - format!("\n/DataSource VCsrc {} filter >> {}\nVCsrc flushfile }} exec\n", self.filter, self.op) + ps::push_inline_image(s, "VCsrc", &self.prelude, &self.dict, self.filter, self.op, &self.ascii); } /// The operators that draw the image `VCim` holds, for an image placed more than once. fn reference(&self, i: usize, level: Level) -> String { + let mut s = String::new(); + let name = format!("VCim{i}"); match level { // The stream is read again from its start. - Level::Three => { - format!("{}VCim{i} resetfile\n{}\n/DataSource VCim{i} {} filter >> {}\n", self.prelude, self.dict, self.filter, self.op) - } + Level::Three => ps::push_reusable_image_ref(&mut s, &name, &self.prelude, &self.dict, self.filter, self.op), // A procedure gives the strings in turn, then an empty string: the end of the data. - Level::Two => format!( - "{}/VCi 0 def\n{}\n/DataSource {{VCi VCim{i} length lt {{VCim{i} VCi get /VCi VCi 1 add def}} {{()}} ifelse}} {} filter >> {}\n", - self.prelude, self.dict, self.filter, self.op - ), + Level::Two => ps::push_image_array_ref(&mut s, "VCi", &name, &self.prelude, &self.dict, self.filter, self.op), } + s } /// The setup's definition of `VCim`: the image's compressed samples, once. @@ -145,11 +128,7 @@ impl ImageCode { // The stream reads all of its data when it is made, so the data follows the // procedure that makes it. let intent = if self.op == "imagemask" { 1 } else { 0 }; - let _ = writeln!( - s, - "{{ /VCsrc currentfile /ASCII85Decode filter def\n/VCim{i} VCsrc << /Intent {intent} /AsyncRead false /CloseSource false >> /ReusableStreamDecode filter def\nVCsrc flushfile }} exec" - ); - s.push_str(&self.ascii); + ps::push_reusable_image_resource(&mut s, "VCsrc", &format!("VCim{i}"), intent, &self.ascii); } Level::Two => { let _ = writeln!(s, "/VCim{i} ["); @@ -1489,44 +1468,12 @@ impl<'a> Scene<'a> { /// A Level 3 smooth shading of `kind` (2 axial, 3 radial) at `coords`, extended both ways. fn shading(&mut self, kind: u8, space: &GradientSpace, coords: &[f64], stops: &[(f64, Vec)]) { - let n = space.n(); - let comps = |c: &[f32]| -> String { - let mut s = String::from("["); - push_nums(&mut s, &c.iter().take(n).map(|v| f64::from(*v)).collect::>()); - s.pop(); - s.push(']'); - s - }; - let segments: Vec<_> = stops.windows(2).filter(|w| w[1].0 > w[0].0).collect(); - let interp = |w: &&[(f64, Vec)]| format!("<< /FunctionType 2 /Domain [0 1] /C0 {} /C1 {} /N 1 >>", comps(&w[0].1), comps(&w[1].1)); - let function = match segments.as_slice() { - [one] => interp(one), - many => { - let mut f = String::from("<< /FunctionType 3 /Domain [0 1] /Functions [\n"); - for w in many { - f.push_str(&interp(w)); - f.push('\n'); - } - f.push_str("] /Bounds ["); - // Seven decimals: a gradient's stops repeated hundreds or thousands of times - // (`Gradient::padded_over`) keep their places in each run. - for w in many.iter().skip(1) { - ps::push_num_places(&mut f, w[0].0, BOUNDS_PLACES); - f.push(' '); - } - f.push_str("] /Encode ["); - f.push_str(&"0 1 ".repeat(many.len())); - f.push_str("] >>"); - f - } - }; + let kind = if kind == 3 { ps::ShadingKind::Radial } else { ps::ShadingKind::Axial }; let name = match space { GradientSpace::Process(s) => s.name(), GradientSpace::Inks(name, _) => name, }; - let _ = write!(self.body, "<< /ShadingType {kind} /ColorSpace {name} /Coords ["); - push_nums(&mut self.body, coords); - let _ = writeln!(self.body, "] /Extend [true true]\n/Function {function} >> shfill"); + ps::push_shading(&mut self.body, kind, name, coords, space.n(), stops); } /// The bands of each stretch between stops: how many, for colours `a` to `b` over `length` @@ -2090,7 +2037,9 @@ impl<'a> Scene<'a> { /// `data`, not yet placed. fn image_code(prelude: String, dict: String, filter: &'static str, op: &'static str, data: Vec) -> ImageCode { let mut code = ImageCode { prelude, dict, filter, op, ascii: ps::ascii85(&data), data, uses: 0, inline_len: 0, ref_len: 0 }; - code.inline_len = code.prelude.len() + INLINE_SOURCE.len() + code.dict.len() + code.inline_read().len() + code.ascii.len(); + let mut inline = String::new(); + ps::push_inline_image(&mut inline, "VCsrc", &code.prelude, &code.dict, code.filter, code.op, ""); + code.inline_len = inline.len() + code.ascii.len(); code }