Merge pull request #7917 from dotCooCoo/fix/pdf-trim-artboards-keep-bleed

Import PDF: artboards on trim boxes keep the bleed between them
This commit is contained in:
BFlat
2026-11-16 04:29:59 +07:00
committed by GitHub
5 changed files with 43 additions and 11 deletions
File diff suppressed because one or more lines are too long
+18 -8
View File
@@ -239,6 +239,14 @@ pub fn imports_of(bytes: &[u8]) -> usize {
IMPORTS.lock().ok().and_then(|i| i.get(&(bytes.as_ptr() as usize)).copied()).unwrap_or(0)
}
/// The widest bleed of `pages` on each side (`[top, bottom, left, right]`, see [`crate::pages::bleed`]).
fn widest_bleed<'p, 'd: 'p>(pages: impl Iterator<Item = &'p craft_pdf::syntax::page::Page<'d>>) -> [f64; 4] {
pages.fold([0.0; 4], |widest, page| {
let bleed = crate::pages::bleed(page);
std::array::from_fn(|i| widest[i].max(bleed[i]))
})
}
/// [`read_with_reader_options`] of `original`, the PDF `bytes` read with `reader`.
fn read_opened(
bytes: &[u8],
@@ -286,15 +294,17 @@ fn read_opened(
let mut nesting = Nesting::new();
// The top-level groups in the order the pages mark them, bottom first.
let (mut order, mut known) = (vec![], HashSet::new());
// Artboards on the trim boxes have the bleed boxes round them as the document's bleed, so the
// art there shows and prints as bleed.
if opts.crop == CropTo::Trim {
doc.setup.bleed = widest_bleed(picked.iter().filter_map(|&n| pages.get(n)));
}
// The artboards are the gap apart plus the document's bleed on both sides, which export adds
// round every artboard, so no page's bleed reaches into the next artboard.
let [_, _, bleed_left, bleed_right] = doc.setup.bleed;
let spacing = opts.artboard_gap + bleed_left + bleed_right;
for (i, &number) in picked.iter().enumerate() {
let Some(page) = pages.get(number) else { continue };
// Artboards on the trim boxes have the bleed boxes round them as the document's bleed
// (the widest of the pages' on each side), so the art there shows and prints as bleed.
if opts.crop == CropTo::Trim {
for (side, b) in doc.setup.bleed.iter_mut().zip(crate::pages::bleed(page)) {
*side = side.max(b);
}
}
// The chosen box sits at (x, 0); the page draws round it.
let (init, frame) = crate::pages::frame(page, opts.crop);
let mut ab = Rect::new(x, 0.0, x + frame.width(), frame.height());
@@ -371,7 +381,7 @@ fn read_opened(
right = right.max(art.x1);
ab = art;
}
x = right + opts.artboard_gap;
x = right + spacing;
clear = right.max(art_right) + opts.artboard_gap;
doc.artboards.push(Artboard {
id: i as u32 + 1,
+2 -1
View File
@@ -134,7 +134,8 @@ impl PdfOptions {
pub struct ImportOptions {
/// Import at most this many pages (of those picked); `None` = all of them.
pub max_pages: Option<usize>,
/// Horizontal gap in points between the artboards created for consecutive pages.
/// Horizontal gap in points between the artboards created for consecutive pages (on trim
/// boxes, plus the document's bleed on both sides).
pub artboard_gap: f64,
/// 0-based pages to import, in this order; `None` = every page.
pub pages: Option<Vec<usize>>,
+21
View File
@@ -91,6 +91,27 @@ fn trim_box_artboards_take_the_bleed_box_round_them_as_the_document_bleed() {
assert_eq!((d.artboards[0].rect.width(), d.setup.bleed), (300.0, [0.0; 4]));
}
#[test]
fn trim_box_artboards_keep_the_bleed_between_them() {
// The second page's bleed box is its trim box: the document's bleed (10 mm, over half the
// 36 pt gap) still goes round its artboard.
let edge = Some([50.0, 50.0, 250.0, 150.0]);
let plain = PdfPage { bleed: edge, trim: edge, ..PdfPage::new(300.0, 200.0, "0 g 50 50 200 100 re f") };
let bytes = pdf(&[bled_page(), plain, bled_page()], None);
let d = open(&bytes, ImportOptions { crop: CropTo::Trim, ..Default::default() });
let starts: Vec<f64> = d.artboards.iter().map(|a| a.rect.x0).collect();
let step = 200.0 + 36.0 + 2.0 * BLEED;
assert!(near([starts[0], starts[1], starts[2], 0.0], [0.0, step, 2.0 * step, 0.0]), "{starts:?}");
// Each artboard's bleed ends 36 pt before the next one's begins.
for pair in d.artboards.windows(2) {
let (a, b) = (d.setup.bleed_rect(pair[0].rect), d.setup.bleed_rect(pair[1].rect));
assert!((b.x0 - a.x1 - 36.0).abs() < 1e-3, "{a:?} {b:?}");
}
// On the crop boxes there is no bleed, and the artboards are 36 pt apart.
let d = open(&bytes, ImportOptions::default());
assert_eq!(d.artboards.iter().map(|a| a.rect.x0).collect::<Vec<_>>(), [0.0, 336.0, 672.0]);
}
#[test]
fn missing_boxes_fall_back_to_the_crop_box_and_boxes_are_clipped_to_the_media_box() {
let page = PdfPage { crop: Some([10.0, 10.0, 290.0, 390.0]), art: Some([-50.0, 0.0, 100.0, 100.0]), ..PdfPage::new(300.0, 400.0, "") };
+1 -1
View File
@@ -1317,7 +1317,7 @@ unknown label is refused. The `warnings` of the result name the first three such
Opening a PDF (or `.ai`) imports every page as an artboard and layer; `document.open` takes `pages` ("2-3, 5", 1-based),
`cropTo` (`bounding` (the art's bounds), `art`, `crop` (default), `trim`, `bleed`, `media`: the box each artboard gets;
with `trim` the bleed box round it becomes the document's bleed, and a PDF-compatible `.ai` or a PDF saved with its editing data opens on its trim boxes for
with `trim` the bleed box round it becomes the document's bleed and the artboards are 36 pt apart plus that bleed on both sides, and a PDF-compatible `.ai` or a PDF saved with its editing data opens on its trim boxes for
`crop`, its crop box taking the bleed in) and `password` for an encrypted file. `document.pdfInfo` reads a file without opening it: the page count, each page's
size and boxes, `needsPassword`, and with `thumbnail: n` a PNG of page n. It reads a PostScript file (an EPS, a `.ps`
file, a `.ai` in PostScript form) as one page the size of its bounding box (a US Letter page without one), with an