This commit is contained in:
bflatastic
2026-11-19 01:26:00 +07:00
36 changed files with 580 additions and 9 deletions
File diff suppressed because one or more lines are too long
+5 -1
View File
@@ -1349,10 +1349,14 @@ fn encode_and_write(export: ScreenExport, writer: &mut dyn FnMut(&str, &[u8]) ->
}
// The document a PDF row writes: with Include Bleed, the artboards as they are.
let pdf_doc = ungrown.as_deref();
// A PDF page of an artboard has the artboard as its trim, with or without Include Bleed. A
// page of its art's bounds has every object at its edge: it doesn't say whether art stops
// short of the bleed.
let artboard_page = o.assets.is_none() && o.layers.is_none() && o.selection.is_none();
let mut write = |doc: &Document, format: &str, options: &Value| -> Result<Vec<u8>> {
// The steps are the files: an encoder's own steps (artboards, pages) are not reported.
let (bytes, given) = vectorcraft_doc::cancel::quiet(|| encode_with_notes(doc, format, options))?;
for n in given {
for n in given.into_iter().filter(|n| artboard_page || !super::pdf::is_short_of_bleed(&n.text)) {
add_note(&mut notes, n);
}
Ok(bytes)
+220
View File
@@ -171,6 +171,226 @@ fn a_pdfx_file_that_leaves_out_the_document_bleed_warns() {
assert!(!left_out(&warnings), "{warnings:?}");
}
/// A 200 × 100 pt document with no art, bleed `bleed` (pt).
fn bleed_session(bleed: Value) -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
s.execute("document.setup", &json!({ "bleed": bleed })).unwrap();
s
}
/// A rectangle from (x0, y0) to (x1, y1) without a stroke, on the current layer.
fn plain_rect(s: &mut Session, [x0, y0, x1, y1]: [f64; 4]) -> Value {
let id = s.execute("shape.rectangle", &json!({"x": x0, "y": y0, "width": x1 - x0, "height": y1 - y0})).unwrap()["id"].clone();
s.execute("paint.setStroke", &json!({"ids": [id], "none": true})).unwrap();
id
}
/// The warnings of a PDF that carries the document's bleed that say art stops short of it.
fn short_of_bleed(s: &mut Session) -> Vec<String> {
let (_, warnings) = pdf(s, json!({"bleed": {"useDocument": true}}));
warnings.into_iter().filter(|w| super::pdf::is_short_of_bleed(w)).collect()
}
const MM: f64 = 72.0 / 25.4;
/// A bleed raised from 2 mm to 5 mm leaves a background that filled the old bleed, and a band
/// placed against the trim edge, short of the new one: the export warns once, counting both.
#[test]
fn art_that_reaches_the_trim_edge_but_stops_short_of_the_bleed_warns() {
let mut s = bleed_session(json!(2.0 * MM));
let (b, w, h) = (2.0 * MM, 200.0, 100.0);
plain_rect(&mut s, [-b, -b, w + b, h + b]);
plain_rect(&mut s, [0.0, 0.0, w, 20.0]);
// At 2 mm the background reaches the bleed; the band stops at the trim edge.
assert_eq!(short_of_bleed(&mut s), ["1 object reaches the trim edge but stops short of the bleed"]);
s.execute("document.setup", &json!({"bleed": 5.0 * MM})).unwrap();
assert_eq!(short_of_bleed(&mut s), ["2 objects reach the trim edge but stop short of the bleed"]);
// The PDF/X presets carry the document's bleed.
let (_, warnings) = pdf(&mut s, json!({"preset": "PDF/X-4:2010"}));
assert!(warnings.iter().any(|w| super::pdf::is_short_of_bleed(w)), "{warnings:?}");
// A PDF/X file with a bleed on the top alone leaves out the rest of the document's bleed, and
// the background and the band stop short at the top: the warning comes right after
// BLEED_LEFT_OUT.
let (_, warnings) = pdf(&mut s, json!({"standard": "pdfX4", "bleed": {"top": 9}}));
assert_eq!(warnings.first().map(String::as_str), Some(super::pdf::BLEED_LEFT_OUT), "{warnings:?}");
assert_eq!(warnings.get(1).map(String::as_str), Some("2 objects reach the trim edge but stop short of the bleed"), "{warnings:?}");
// A page without the bleed (the default preset) doesn't warn, and neither does a page of the art's bounds.
let (_, warnings) = pdf(&mut s, json!({}));
assert!(!warnings.iter().any(|w| super::pdf::is_short_of_bleed(w)), "{warnings:?}");
let (_, warnings) = pdf(&mut s, json!({"bleed": {"useDocument": true}, "useArtboards": false}));
assert!(!warnings.iter().any(|w| super::pdf::is_short_of_bleed(w)), "{warnings:?}");
// An explicit bleed counts too, and the Summary lists the warning.
let (_, warnings) = pdf(&mut s, json!({"bleed": {"top": 9, "bottom": 9, "left": 9, "right": 9}}));
assert!(warnings.iter().any(|w| super::pdf::is_short_of_bleed(w)), "{warnings:?}");
let v = s.execute("document.pdfSettings", &json!({"bleed": {"useDocument": true}, "includeDocument": true})).unwrap();
assert!(v["warnings"].as_array().unwrap().iter().any(|w| super::pdf::is_short_of_bleed(w.as_str().unwrap())), "{v}");
// A group counts once, measured by its visible members: a hidden member that fills the bleed
// doesn't hide a visible one at the trim edge.
let mut s = bleed_session(json!(9));
let fill = plain_rect(&mut s, [-9.0, -9.0, 209.0, 109.0]);
let band = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let side = plain_rect(&mut s, [0.0, 40.0, 20.0, 60.0]);
s.execute("object.group", &json!({"ids": [fill, band, side]})).unwrap();
s.execute("object.hide", &json!({"ids": [fill]})).unwrap();
assert_eq!(short_of_bleed(&mut s), ["1 object reaches the trim edge but stops short of the bleed"]);
// A clip group is measured by the art inside its clipping path, not by a clipping path that
// reaches the bleed edge.
let mut s = bleed_session(json!(9));
let art = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let clip = plain_rect(&mut s, [-9.0, -9.0, 209.0, 109.0]);
s.execute("select.set", &json!({"ids": [art, clip]})).unwrap();
s.execute("object.clippingMask.make", &json!({})).unwrap();
assert_eq!(short_of_bleed(&mut s).len(), 1);
// A clipping path that paints a fill reaching the trim edge counts, though the art it clips is
// inside: in a clip group, and as an object of its own in a clipping layer.
let mut s = bleed_session(json!(9));
let art = plain_rect(&mut s, [20.0, 30.0, 60.0, 60.0]);
let clip = plain_rect(&mut s, [0.0, 0.0, 200.0, 100.0]);
s.execute("select.set", &json!({"ids": [art, clip]})).unwrap();
s.execute("object.clippingMask.make", &json!({})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
s.execute("paint.setFill", &json!({"ids": [clip], "color": "#ff0000"})).unwrap();
assert_eq!(short_of_bleed(&mut s).len(), 1);
let mut s = bleed_session(json!(9));
let clipping = s.execute("layer.new", &json!({})).unwrap()["id"].clone();
plain_rect(&mut s, [20.0, 30.0, 60.0, 60.0]);
let clip = plain_rect(&mut s, [0.0, 0.0, 200.0, 100.0]);
s.execute("layer.clippingMask.toggle", &json!({"id": clipping})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
s.execute("paint.setFill", &json!({"ids": [clip], "color": "#ff0000"})).unwrap();
assert_eq!(short_of_bleed(&mut s).len(), 1);
// A hidden brushed member doesn't keep a group's visible art at the trim edge from counting.
let mut s = bleed_session(json!(9));
let brushed = plain_rect(&mut s, [-9.0, -9.0, 209.0, 109.0]);
s.execute("brush.apply", &json!({"ids": [brushed], "name": "Charcoal"})).unwrap();
let band = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("object.group", &json!({"ids": [brushed, band]})).unwrap();
s.execute("object.hide", &json!({"ids": [brushed]})).unwrap();
assert_eq!(short_of_bleed(&mut s).len(), 1);
// A clipping layer clips its art to the clipping path's geometry, not to the stroke around it:
// the stroke reaches the trim edge, but the art it clips stops 5 pt inside.
let mut s = bleed_session(json!(9));
let clipping = s.execute("layer.new", &json!({})).unwrap()["id"].clone();
plain_rect(&mut s, [-9.0, -9.0, 209.0, 109.0]);
let clip = plain_rect(&mut s, [5.0, 5.0, 195.0, 95.0]);
s.execute("layer.clippingMask.toggle", &json!({"id": clipping})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [clip], "color": "#ff0000"})).unwrap();
s.execute("stroke.set", &json!({"ids": [clip], "weight": 10})).unwrap();
assert_eq!(short_of_bleed(&mut s), ["1 object reaches the trim edge but stops short of the bleed"]);
}
/// No warning for art that already fills the bleed, art inside the trim, a document without a
/// bleed, art on hidden, non-printing or template layers, a side without a bleed, area type, a
/// masked object, or a `.ai` save.
#[test]
fn art_that_fills_the_bleed_or_stays_inside_does_not_warn() {
let mut s = bleed_session(json!(9));
plain_rect(&mut s, [-9.0, -9.0, 209.0, 109.0]);
plain_rect(&mut s, [-20.0, 40.0, 50.0, 60.0]);
plain_rect(&mut s, [10.0, 10.0, 190.0, 90.0]);
assert!(short_of_bleed(&mut s).is_empty());
// A document without a bleed has nothing to fall short of.
let mut s = bleed_session(json!(0));
plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
assert!(short_of_bleed(&mut s).is_empty());
// Layers that don't print.
for props in [json!({"visible": false}), json!({"printable": false}), json!({"template": true})] {
let mut s = bleed_session(json!(9));
let layer = s.execute("layer.new", &json!({})).unwrap()["id"].clone();
plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let mut p = props.clone();
p["id"] = layer;
s.execute("layer.setProps", &p).unwrap();
assert!(short_of_bleed(&mut s).is_empty(), "{props}");
}
// A hidden object, and a group whose hidden member is the one at the trim edge.
let mut s = bleed_session(json!(9));
let id = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("object.hide", &json!({"ids": [id]})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
let mut s = bleed_session(json!(9));
let edge = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let inside = plain_rect(&mut s, [10.0, 30.0, 50.0, 60.0]);
s.execute("object.group", &json!({"ids": [edge, inside]})).unwrap();
s.execute("object.hide", &json!({"ids": [edge]})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// Art that lies in the bleed alone, away from the trim edge.
let mut s = bleed_session(json!(9));
plain_rect(&mut s, [20.0, -5.0, 60.0, -3.0]);
assert!(short_of_bleed(&mut s).is_empty());
// A guide in a group at the trim edge.
let mut s = bleed_session(json!(9));
let guide = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("view.guides.make", &json!({})).unwrap();
let inside = plain_rect(&mut s, [10.0, 30.0, 50.0, 60.0]);
s.execute("object.group", &json!({"ids": [guide, inside]})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// A brushed stroke, whose brush art can paint past its visual bounds, is not measured.
let mut s = bleed_session(json!(9));
let id = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("brush.apply", &json!({"ids": [id], "name": "Charcoal"})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// A path with no fill and no stroke paints nothing.
let mut s = bleed_session(json!(9));
let id = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("paint.setFill", &json!({"ids": [id], "none": true})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// An object at 0% opacity draws nothing.
let mut s = bleed_session(json!(9));
let id = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
s.execute("object.setProps", &json!({"ids": [id], "opacity": 0})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// A clipping layer whose clipping path lies inside the trim draws none of its art at the edge.
let mut s = bleed_session(json!(9));
let clipping = s.execute("layer.new", &json!({})).unwrap()["id"].clone();
plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
plain_rect(&mut s, [10.0, 10.0, 60.0, 60.0]);
s.execute("layer.clippingMask.toggle", &json!({"id": clipping})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// A bleed on the top alone: art short at the left doesn't count, art short at the top does.
let mut s = bleed_session(json!([9, 0, 0, 0]));
plain_rect(&mut s, [0.0, 30.0, 40.0, 60.0]);
assert!(short_of_bleed(&mut s).is_empty());
plain_rect(&mut s, [60.0, 0.0, 100.0, 40.0]);
assert_eq!(short_of_bleed(&mut s).len(), 1);
// Area type is bounded by its frame, not its glyphs: a frame at the trim edge is not measured.
let mut s = bleed_session(json!(9));
s.execute("text.create", &json!({"x": 0, "y": 0, "text": "Title", "size": 12, "area": {"width": 200, "height": 40}})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// An opacity mask decides what shows of the object, so a masked object is not measured.
let mut s = bleed_session(json!(9));
let band = plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let mask = plain_rect(&mut s, [20.0, 30.0, 60.0, 60.0]);
s.execute("transparency.makeOpacityMask", &json!({"ids": [band, mask]})).unwrap();
assert!(short_of_bleed(&mut s).is_empty());
// A `.ai` save with a bleed doesn't warn: the check is PDF export's.
let mut s = bleed_session(json!(9));
plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
assert_eq!(short_of_bleed(&mut s).len(), 1);
let r = s.execute("document.export", &json!({"format": "ai", "bleed": {"useDocument": true}})).unwrap();
assert!(!r["warnings"].to_string().contains("short of the bleed"), "{}", r["warnings"]);
}
/// The warning changes neither the art nor the page boxes.
#[test]
fn the_bleed_warning_changes_no_geometry_and_no_page_box() {
let mut s = bleed_session(json!(9));
plain_rect(&mut s, [0.0, 0.0, 200.0, 20.0]);
let before = s.doc().unwrap().doc.clone();
let v = s.execute("document.exportPdf", &json!({"bleed": {"useDocument": true}})).unwrap();
assert!(v["warnings"].as_array().unwrap().iter().any(|w| super::pdf::is_short_of_bleed(w.as_str().unwrap())), "{v}");
assert_eq!(s.doc().unwrap().doc, before);
let info = s.execute("document.pdfInfo", &json!({"name": "x.pdf", "dataBase64": v["dataBase64"]})).unwrap();
let boxes = &info["pageInfo"][0]["boxes"];
let side = |b: &Value| {
let n: Vec<f64> = b.as_array().unwrap().iter().map(|x| x.as_f64().unwrap()).collect();
(((n[2] - n[0]) * 100.0).round() / 100.0, ((n[3] - n[1]) * 100.0).round() / 100.0)
};
assert_eq!(side(&boxes["trim"]), (200.0, 100.0), "{info}");
assert_eq!(side(&boxes["bleed"]), (218.0, 118.0), "{info}");
}
/// The ink amounts (0..1) `doc` shows at `p` in its images (the topmost image there).
fn image_inks_at(doc: &vectorcraft_doc::Document, p: vectorcraft_geom::Point) -> Option<[f32; 4]> {
fn visit(n: &vectorcraft_doc::Node, out: &mut Vec<vectorcraft_doc::ImageObject>) {
@@ -610,6 +610,35 @@ fn include_bleed_pdfx_rows_do_not_warn_about_the_bleed() {
assert!(warned(&export(&mut s, json!({"includeBleed": true, "selection": "one", "formats": row}))));
}
/// A PDF row with a bleed warns about art that reaches an artboard's edge and stops short of the
/// bleed, with or without Include Bleed: the page's trim is the artboard either way. It doesn't
/// warn about art that reaches the bleed, or on a page of a selection's bounds.
#[test]
fn pdf_rows_warn_about_art_short_of_the_bleed_on_artboard_pages_only() {
let warned = |r: &Value| r["warnings"].as_array().unwrap().iter().any(|w| super::pdf::is_short_of_bleed(w.as_str().unwrap()));
let row = json!([{"format": "pdf", "bleed": {"useDocument": true}}]);
let mut s = session(1);
s.execute("document.setup", &json!({"bleed": 6})).unwrap();
// Against the artboard's top edge.
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 40, "height": 10})).unwrap();
assert!(warned(&export(&mut s, json!({"formats": row}))));
assert!(warned(&export(&mut s, json!({"includeBleed": true, "formats": row}))));
assert!(warned(&export(&mut s, json!({"includeBleed": true, "formats": [{"format": "pdf"}]}))), "the row takes the document's bleed");
assert!(!warned(&export(&mut s, json!({"selection": "one", "formats": row}))));
assert!(!warned(&export(&mut s, json!({"includeBleed": true, "selection": "one", "formats": row}))));
// Out to the bleed edge on the top, left and right.
let mut s = session(1);
s.execute("document.setup", &json!({"bleed": 6})).unwrap();
s.execute("shape.rectangle", &json!({"x": -6, "y": -6, "width": 52, "height": 16})).unwrap();
assert!(!warned(&export(&mut s, json!({"formats": row}))));
assert!(!warned(&export(&mut s, json!({"includeBleed": true, "formats": row}))));
// Without a document bleed, a row's own bleed warns.
let mut s = session(1);
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 40, "height": 10})).unwrap();
let row = json!([{"format": "pdf", "bleed": {"top": 6, "bottom": 6, "left": 6, "right": 6}}]);
assert!(warned(&export(&mut s, json!({"includeBleed": true, "formats": row}))));
}
/// Each `[x0 y0 x1 y1]` that follows `key` in a PDF's bytes, in the order the file has them, read
/// as text without VectorCraft's PDF reader (PDF 32000-1:2008, 14.11.2, Page Boundaries).
fn raw_boxes(pdf: &[u8], key: &str) -> Vec<[f64; 4]> {