mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Tests: cover PDF mesh shadings, restroked glyphs, repeated masks and spot midpoints
Adds tests for PDF import and export paths that no test reached: - A tensor-product patch mesh (type 7) imports as the gradient mesh of its boundary, the same mesh a Coons patch over that boundary gives. - A triangle mesh (type 4) imports as one gradient mesh per triangle, each corner in its vertex's color. - Tensor and triangle meshes over the 16,384-patch limit import as a flat gray with a warning. - The last glyph of a run stroked again in another color splits off into its own run; the glyph before it keeps its stroke. - A masked CMYK image drawn twice at two sizes keeps its opacity mask both times, fitted to each drawing; with a mask that hides nothing it has no mask either time. - A one-spot-ink gradient with an off-center midpoint exports a stop at the midpoint with the mean tint, still as a Separation shading. Each test fails with the code it covers broken.
This commit is contained in:
1 parent
4002b2b66c
commit
c2d242eafa
4 files changed
+158
No files matched your search
@@ -133,6 +133,16 @@ fn spot_gradients_export_as_separation_shadings() {
|
||||
assert!(pdf.contains("/ShadingType 2") && pdf.contains("/Separation/Ink/DeviceCMYK"), "an Ink → paper gradient is a Separation shading");
|
||||
// Its function runs over the tints: 80 % (204 / 255) to 0 %.
|
||||
assert!(pdf.contains("/C0[0.8]/C1[0]"), "the shading's tints");
|
||||
// An off-center midpoint adds a stop there at the mean tint, still in the one spot ink.
|
||||
run(
|
||||
&mut s,
|
||||
"paint.editGradient",
|
||||
json!({"stops": [{"offset": 0, "swatch": "Ink", "tint": 80, "midpoint": 30}, {"offset": 1, "color": "#ffffff"}]}),
|
||||
);
|
||||
let (pdf, _) = pdf_of(&s);
|
||||
assert!(pdf.contains("/Separation/Ink/DeviceCMYK") && !pdf.contains("/DeviceN"), "still one spot ink");
|
||||
let functions: Vec<_> = pdf.match_indices("/C0[").filter_map(|(i, _)| pdf.get(i..i + 24)).collect();
|
||||
assert!(pdf.contains("/C0[0.8]/C1[0.4]") && pdf.contains("/C0[0.4]/C1[0]"), "the tints through the midpoint: {functions:?}");
|
||||
// Mixing the spot ink with an RGB color writes the gradient without the spot ink, with a warning.
|
||||
run(&mut s, "paint.editGradient", json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#00ff00"}]}));
|
||||
let (pdf, warnings) = pdf_of(&s);
|
||||
|
||||
@@ -331,6 +331,56 @@ fn masked_cmyk_images_keep_their_inks_and_carry_the_mask_as_an_opacity_mask() {
|
||||
assert!(masked(&r.document)[0].1.is_none());
|
||||
}
|
||||
|
||||
/// A masked CMYK image drawn twice, at two sizes, carries its opacity mask both times: the same
|
||||
/// mask image, fitted to each drawing. Drawn twice with a mask that hides nothing, it has no mask
|
||||
/// either time.
|
||||
#[test]
|
||||
fn a_masked_cmyk_image_drawn_twice_keeps_its_mask_both_times() {
|
||||
let z = |data: &[u8]| crate::output::deflate(data).unwrap();
|
||||
let file = |mask: &[u8]| {
|
||||
let content = "q 50 0 0 50 10 10 cm /Im0 Do Q q 30 0 0 20 100 40 cm /Im0 Do Q";
|
||||
pdf_of(&[
|
||||
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
|
||||
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
|
||||
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 100] /Resources << /XObject << /Im0 5 0 R >> >> /Contents 4 0 R >>".to_vec(),
|
||||
stream("", content.as_bytes()),
|
||||
stream(
|
||||
&format!("/Type /XObject /Subtype /Image {}", cmyk_dict(2, 2, "/SMask 6 0 R /Filter /FlateDecode")),
|
||||
&z(&flat([0, 102, 255, 0], 4)),
|
||||
),
|
||||
stream("/Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8", mask),
|
||||
])
|
||||
};
|
||||
let images = |d: &Document| {
|
||||
let NodeKind::Layer { children, .. } = &d.layers[0].kind else { panic!() };
|
||||
children.iter().filter(|n| matches!(n.kind, NodeKind::Image(_))).cloned().collect::<Vec<_>>()
|
||||
};
|
||||
let r = import_with_report(&file(&[255, 128, 255, 128]), &ImportOptions::default()).unwrap();
|
||||
let drawn = images(&r.document);
|
||||
assert_eq!(drawn.len(), 2);
|
||||
let mut keys = vec![];
|
||||
for n in &drawn {
|
||||
let mask = n.extras().mask.as_ref().expect("an opacity mask on each drawing");
|
||||
let NodeKind::Image(a) = &mask.art.kind else { panic!("mask art is an image: {:?}", mask.art.kind) };
|
||||
assert_eq!((a.width, a.height), (2, 2));
|
||||
assert!(near(n.geometric_bounds(), mask.art.geometric_bounds()), "{:?} {:?}", n.geometric_bounds(), mask.art.geometric_bounds());
|
||||
keys.push(a.key.clone());
|
||||
}
|
||||
assert_eq!(keys[0], keys[1], "one mask image");
|
||||
assert!(drawn[0].geometric_bounds() != drawn[1].geometric_bounds(), "two sizes");
|
||||
let r = import_with_report(&file(&[255; 4]), &ImportOptions::default()).unwrap();
|
||||
let drawn = images(&r.document);
|
||||
assert_eq!(drawn.len(), 2);
|
||||
assert!(drawn.iter().all(|n| n.extras().mask.is_none()), "a mask that hides nothing adds none, either time");
|
||||
}
|
||||
|
||||
fn near(a: Option<Rect>, b: Option<Rect>) -> bool {
|
||||
match (a, b) {
|
||||
(Some(a), Some(b)) => [a.x0 - b.x0, a.y0 - b.y0, a.x1 - b.x1, a.y1 - b.y1].iter().all(|d| d.abs() < 1e-6),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Every image blob a document's art uses (mask art included).
|
||||
fn all_images(d: &Document) -> Vec<&ImageBlob> {
|
||||
let mut keys = vec![];
|
||||
|
||||
@@ -712,6 +712,85 @@ fn a_mesh_shading_of_thousands_of_patches_becomes_gradient_meshes() {
|
||||
assert_eq!(all(&r.document).iter().filter(|n| matches!(n.kind, NodeKind::Mesh(_))).count(), 5000);
|
||||
}
|
||||
|
||||
/// The gradient meshes a page drawing shading `sh` (`/Sh0 sh`) comes in as, with the import's
|
||||
/// warnings.
|
||||
fn meshes_of(sh: &str) -> (Vec<vectorcraft_doc::live::GradientMesh>, Vec<String>) {
|
||||
let s = first_extra(1);
|
||||
let r = open_with(&one_page("/Sh0 sh", &format!("/Shading << /Sh0 {s} 0 R >>"), &[sh]), |_| {});
|
||||
let meshes = all(&r.document).into_iter().filter_map(|n| if let NodeKind::Mesh(m) = n.kind { Some(m) } else { None }).collect();
|
||||
(meshes, r.warnings)
|
||||
}
|
||||
|
||||
/// A tensor-product patch mesh shading's dictionary for [`tensor_patch`] data.
|
||||
const TENSOR_MESH: &str = "/ShadingType 7 /ColorSpace /DeviceRGB /BitsPerCoordinate 8 /BitsPerComponent 8 /BitsPerFlag 8 /Decode [0 255 0 255 0 1 0 1 0 1] /Filter /ASCIIHexDecode";
|
||||
|
||||
/// One tensor patch over the same boundary as [`coons_patch`], with its four inner points far
|
||||
/// outside it at (250, 250).
|
||||
fn tensor_patch() -> Vec<u8> {
|
||||
let mut data = coons_patch();
|
||||
let colors = data.split_off(1 + 24);
|
||||
data.extend([250; 8]);
|
||||
data.extend(colors);
|
||||
data
|
||||
}
|
||||
|
||||
/// A triangle mesh shading's dictionary (types 4 and 5 share their vertex layout).
|
||||
const TRIANGLE_MESH: &str = "/ShadingType 4 /ColorSpace /DeviceRGB /BitsPerCoordinate 8 /BitsPerComponent 8 /BitsPerFlag 8 /Decode [0 255 0 255 0 1 0 1 0 1] /Filter /ASCIIHexDecode";
|
||||
|
||||
/// One triangle: red at (10, 10), green at (90, 10), blue at (50, 90), each vertex with flag 0.
|
||||
fn triangle() -> Vec<u8> {
|
||||
[[0, 10, 10, 255, 0, 0], [0, 90, 10, 0, 255, 0], [0, 50, 90, 0, 0, 255]].concat()
|
||||
}
|
||||
|
||||
/// A tensor-product patch mesh (type 7) comes in as the gradient mesh of its boundary, the same
|
||||
/// mesh a Coons patch over that boundary gives: its inner points do not move it.
|
||||
#[test]
|
||||
fn a_tensor_patch_mesh_becomes_the_gradient_mesh_of_its_boundary() {
|
||||
let (tensor, warnings) = meshes_of(&stream(TENSOR_MESH, &hex(&tensor_patch())));
|
||||
let [m] = tensor.as_slice() else { panic!("one mesh: {tensor:?} {warnings:?}") };
|
||||
assert_eq!((m.rows, m.cols), (1, 1));
|
||||
let at = |r, c| &m.points[m.idx(r, c)];
|
||||
assert!((at(0, 0).p - kurbo::Point::new(10.0, 90.0)).hypot() < 0.01, "{:?}", at(0, 0).p);
|
||||
assert!((at(1, 1).p - kurbo::Point::new(90.0, 10.0)).hypot() < 0.01, "{:?}", at(1, 1).p);
|
||||
assert_eq!(at(0, 0).color.to_rgb(), [1.0, 0.0, 0.0]);
|
||||
assert_eq!(at(1, 0).color.to_rgb(), [0.0, 1.0, 0.0]);
|
||||
assert_eq!(at(1, 1).color.to_rgb(), [0.0, 0.0, 1.0]);
|
||||
assert_eq!(at(0, 1).color.to_rgb(), [1.0, 1.0, 1.0]);
|
||||
let (coons, _) = meshes_of(&stream(COONS_MESH, &hex(&coons_patch())));
|
||||
assert_eq!(Some(m), coons.first(), "the mesh of a Coons patch over the same boundary");
|
||||
}
|
||||
|
||||
/// A triangle mesh (types 4 and 5) comes in as one gradient mesh per triangle, a patch whose last
|
||||
/// two corners are the triangle's third vertex, each corner in its vertex's color.
|
||||
#[test]
|
||||
fn a_triangle_mesh_becomes_a_gradient_mesh() {
|
||||
let (meshes, warnings) = meshes_of(&stream(TRIANGLE_MESH, &hex(&triangle())));
|
||||
let [m] = meshes.as_slice() else { panic!("one mesh: {meshes:?} {warnings:?}") };
|
||||
assert_eq!((m.rows, m.cols), (1, 1));
|
||||
let at = |r, c| &m.points[m.idx(r, c)];
|
||||
// PDF y 10 is page y 90.
|
||||
let want = [((0, 0), (10.0, 90.0), [1.0, 0.0, 0.0]), ((1, 0), (90.0, 90.0), [0.0, 1.0, 0.0]), ((1, 1), (50.0, 10.0), [0.0, 0.0, 1.0])];
|
||||
for ((r, c), (x, y), color) in want {
|
||||
assert!((at(r, c).p - kurbo::Point::new(x, y)).hypot() < 0.01, "({r}, {c}): {:?}", at(r, c).p);
|
||||
assert_eq!(at(r, c).color.to_rgb(), color, "({r}, {c})");
|
||||
}
|
||||
assert_eq!(at(0, 1).p, at(1, 1).p, "the third vertex closes the patch");
|
||||
}
|
||||
|
||||
/// A tensor patch or triangle mesh of more patches than the importer reads is not a gradient mesh:
|
||||
/// it comes in as a flat gray, with a warning.
|
||||
#[test]
|
||||
fn a_tensor_or_triangle_mesh_over_the_patch_limit_is_a_flat_gray() {
|
||||
let over = 16_385;
|
||||
let tensor: Vec<u8> = std::iter::repeat_n(tensor_patch(), over).flatten().collect();
|
||||
let triangles: Vec<u8> = std::iter::repeat_n(triangle(), over).flatten().collect();
|
||||
for (name, sh) in [("tensor", stream(TENSOR_MESH, &hex(&tensor))), ("triangle", stream(TRIANGLE_MESH, &hex(&triangles)))] {
|
||||
let (meshes, warnings) = meshes_of(&sh);
|
||||
assert!(meshes.is_empty(), "{name}: {} meshes", meshes.len());
|
||||
assert!(warnings.iter().any(|w| w.contains("flat grey")), "{name}: {warnings:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A stroke painted with a mesh shading (how Illustrator writes a freeform gradient on a stroke)
|
||||
/// is the mesh clipped to the area the stroke paints, dashes included, not a flat grey.
|
||||
#[test]
|
||||
|
||||
@@ -556,6 +556,25 @@ fn a_glyph_stroked_again_where_it_was_drawn_stays_one_type_object() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The last glyph of a run stroked again where it was drawn, in another stroke color, splits off
|
||||
/// into its own run with that stroke; the glyphs before it keep theirs.
|
||||
#[test]
|
||||
fn the_last_glyph_stroked_again_in_another_color_splits_off_with_that_stroke() {
|
||||
use vectorcraft_testkit::pdf::{PdfPage, pdf};
|
||||
// Helvetica's A is 667 units wide, so in 48-point type B stands at 20 + 32.016.
|
||||
let content = "0 0 0 RG 1 1 0.8 rg 3 w BT /F1 48 Tf 1 0 0 1 20 80 Tm 2 Tr (AB) Tj 1 0 0 RG 1 Tr 1 0 0 1 52.016 80 Tm (B) Tj 0 Tr ET";
|
||||
let resources = "/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >>";
|
||||
let d = import(&pdf(&[PdfPage { resources: resources.into(), ..PdfPage::new(300.0, 200.0, content) }], None)).unwrap();
|
||||
let mut types = vec![];
|
||||
d.walk(|n| {
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
types.push(t.runs.iter().map(|r| (r.text.clone(), !r.style.fill.is_none(), r.style.stroke.color())).collect::<Vec<_>>());
|
||||
}
|
||||
});
|
||||
let want = [vec![("A".to_string(), true, Some(Color::BLACK)), ("B".to_string(), true, Some(Color::rgb(1.0, 0.0, 0.0)))]];
|
||||
assert_eq!(types, want);
|
||||
}
|
||||
|
||||
/// A stroke pass longer than the importer holds at once ([`MAX_HELD`] strokes) goes over its own
|
||||
/// operator's glyphs, filled right before it, also when an operator before it filled the same
|
||||
/// glyphs at the same places: `A` and 4,095 `B`s filled, then `A` and 4,096 `B`s filled and
|
||||
|
||||
Reference in new issue
Block a user