mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 02:03:02 +00:00
Merge main into fix/transform-copies-raster-effects-groups
This commit is contained in:
@@ -31,7 +31,41 @@ pub(crate) fn fill_hex(n: &Node) -> String {
|
||||
fn red_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -108,13 +142,47 @@ fn an_embedded_image_is_adjusted_for_export_without_changing_its_pixels() {
|
||||
let baked = vectorcraft_render::effects::bake_document(&doc).unwrap();
|
||||
let NodeKind::Image(im) = &baked.node(id).unwrap().kind else { panic!("not an image") };
|
||||
assert_ne!(im.key, "pic", "a recoloured copy");
|
||||
let px = image::load_from_memory(&baked.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&baked.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(px.get_pixel(1, 1).0, [0, 255, 0, 255]);
|
||||
assert_eq!(doc.images.len(), 1, "the document keeps its one image");
|
||||
// Through Expand Appearance too.
|
||||
s.execute("effect.expandAppearance", &json!({})).unwrap();
|
||||
let NodeKind::Image(im) = &node(&s, id).kind else { panic!("not an image") };
|
||||
let px = image::load_from_memory(&s.doc().unwrap().doc.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let px = craft_image_codecs::decode(&s.doc().unwrap().doc.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(px.get_pixel(0, 0).0, [0, 255, 0, 255]);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,24 @@ fn an_asset_exports_its_art_alone() {
|
||||
run(&mut s, "assets.add", json!({"ids": [red]}));
|
||||
let out = files(&run(&mut s, "assets.export", json!({"formats": [{"format": "png", "scale": "2x"}]})));
|
||||
assert_eq!(out[0].0, "Asset-1@2x.png");
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 40));
|
||||
assert!(img.pixels().all(|p| p.0 == [255, 0, 0, 255]), "only the red square");
|
||||
// Vector formats crop to it too.
|
||||
@@ -163,7 +180,24 @@ fn an_assets_crop_takes_in_its_drop_shadow() {
|
||||
// The shadow's reach (10 pt) on every side.
|
||||
assert_eq!(assets(&mut s)[0]["bounds"], json!([40.0, 40.0, 40.0, 40.0]));
|
||||
let out = files(&run(&mut s, "assets.export", json!({"formats": [{"format": "png", "scale": "1x"}]})));
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 40));
|
||||
// The shadow's right half, x 70 to 80.
|
||||
assert!(img.get_pixel(35, 20)[3] > 0, "the shadow is in the image");
|
||||
@@ -417,7 +451,29 @@ fn export_for_screens_writes_chosen_assets_and_keeps_its_rules() {
|
||||
let out = files(&r);
|
||||
assert_eq!(out.len(), 1);
|
||||
assert_eq!(out[0].0, "Asset-2@60w.png");
|
||||
assert_eq!(image::load_from_memory(&out[0].1).unwrap().to_rgba8().dimensions(), (60, 40), "the width of the asset, not the artboard");
|
||||
assert_eq!(
|
||||
craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.dimensions(),
|
||||
(60, 40),
|
||||
"the width of the asset, not the artboard"
|
||||
);
|
||||
// The choice of art isn't remembered; the formats are.
|
||||
let settings = run(&mut s, "document.exportSettings", json!({}))["settings"].clone();
|
||||
assert!(settings.get("assets").is_none() && settings["formats"][0]["scale"] == "60w");
|
||||
@@ -466,7 +522,24 @@ fn assets_export_leaves_no_seam_between_abutting_shapes() {
|
||||
}),
|
||||
));
|
||||
assert_eq!(out[0].0, "Asset-1.png");
|
||||
let img = image::load_from_memory(&out[0].1).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&out[0].1)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (40, 20));
|
||||
// Column 20 is the shared edge in crop space; green must stay red (no white bleed).
|
||||
let palest_g = (2..18).map(|y| img.get_pixel(20, y).0[1].max(img.get_pixel(19, y).0[1])).max().unwrap();
|
||||
|
||||
@@ -2599,3 +2599,50 @@ fn symbol_sort_by_name_orders_the_symbols_in_one_undo_step() {
|
||||
s.execute("symbol.sortByName", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), before);
|
||||
}
|
||||
|
||||
/// `cmd::current_symbol` answers what `symbol.list` answers as `current`, and what `rule` (the
|
||||
/// stored name while a symbol has it, else the first of all the names) answers:
|
||||
/// with no symbols, with no stored name, with a stored name that is not first, after the current
|
||||
/// symbol is deleted, with a stored name no symbol has and with a stored value that is not text.
|
||||
#[test]
|
||||
fn current_symbol_is_the_current_symbol_list_answers() {
|
||||
fn rule(d: &Document) -> Option<String> {
|
||||
let names: Vec<String> = d.symbols.iter().map(|s| s.name.clone()).collect();
|
||||
let stored = d.unknown.get("currentSymbol").and_then(Value::as_str).filter(|n| d.symbols.iter().any(|s| s.name == *n)).map(str::to_string);
|
||||
stored.or_else(|| names.into_iter().next())
|
||||
}
|
||||
fn check(s: &mut Session, want: Option<&str>, case: &str) {
|
||||
let listed = s.execute("symbol.list", &json!({})).unwrap()["current"].clone();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let helper = crate::cmd::current_symbol(d);
|
||||
assert_eq!(helper.as_deref(), want, "{case}");
|
||||
assert_eq!(json!(helper), listed, "{case}: symbol.list");
|
||||
assert_eq!(helper, rule(d), "{case}: the rule");
|
||||
}
|
||||
fn store(s: &mut Session, v: Value) {
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.insert("currentSymbol".into(), v);
|
||||
}
|
||||
let mut s = session();
|
||||
check(&mut s, None, "no symbols");
|
||||
store(&mut s, json!("Gone"));
|
||||
check(&mut s, None, "no symbols and a stored name");
|
||||
for (i, name) in ["A", "B", "C"].into_iter().enumerate() {
|
||||
rect(&mut s, 20.0 * i as f64, 0.0, 10.0, 10.0);
|
||||
s.execute("symbol.new", &json!({"name": name})).unwrap();
|
||||
}
|
||||
check(&mut s, Some("C"), "the newest symbol is current");
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.remove("currentSymbol");
|
||||
check(&mut s, Some("A"), "no stored name");
|
||||
s.execute("symbol.setCurrent", &json!({"name": "B"})).unwrap();
|
||||
check(&mut s, Some("B"), "a stored name that is not first");
|
||||
s.execute("symbol.delete", &json!({"name": "B"})).unwrap();
|
||||
check(&mut s, Some("A"), "the current symbol deleted");
|
||||
store(&mut s, json!("Renamed"));
|
||||
check(&mut s, Some("A"), "a stored name no symbol has");
|
||||
store(&mut s, json!(3));
|
||||
check(&mut s, Some("A"), "a stored number");
|
||||
store(&mut s, json!(["C"]));
|
||||
check(&mut s, Some("A"), "a stored list");
|
||||
store(&mut s, json!("C"));
|
||||
check(&mut s, Some("C"), "a stored name that is last");
|
||||
}
|
||||
|
||||
@@ -5198,6 +5198,137 @@ fn live_paint_bucket_drag_rejects_invalid_points_atomically() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The edge of `g` through (`x`, 25), halfway up the squares of [`live_squares`].
|
||||
fn vertical_edge(s: &Session, g: NodeId, x: f64) -> NodeId {
|
||||
vectorcraft_tools::builder::edge_near(&node(s, g), Point::new(x, 25.0), 0.5).map(|e| e.id).unwrap_or_else(|| panic!("an edge at x {x}"))
|
||||
}
|
||||
|
||||
/// A Shift-drag from an edge strokes every visible edge of that group the drag crosses, between
|
||||
/// pointer events and on the release too, as one undo step that Escape cancels; hidden edges and
|
||||
/// another group's edges keep their strokes.
|
||||
#[test]
|
||||
fn live_paint_bucket_drag_strokes_crossed_edges_as_one_undo() {
|
||||
for cancel in [false, true] {
|
||||
let mut s = session();
|
||||
let g = live_squares(&mut s, &[0.0, 50.0, 100.0, 150.0, 200.0]);
|
||||
let other = live_squares(&mut s, &[300.0]);
|
||||
let black = Paint::solid(Color::rgb(0.0, 0.0, 0.0));
|
||||
let green = Paint::solid(Color::from_hex("#00ff00").unwrap());
|
||||
let all: Vec<NodeId> = [g, other].iter().flat_map(|g| edges_in(&s, *g, Rect::new(-1.0, -1.0, 351.0, 51.0))).collect();
|
||||
paint_parts(&mut s, &all, &black, &[]);
|
||||
let hidden = vertical_edge(&s, g, 100.0);
|
||||
s.edit("t", |d, _| {
|
||||
d.node_mut(hidden).unwrap().visible = false;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let depth = s.doc().unwrap().history.undo.len();
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("livePaintBucket", v).unwrap();
|
||||
s.paint.stroke = green.clone();
|
||||
s.paint.stroke_width = 3.0;
|
||||
let shift = Mods { shift: true, ..Default::default() };
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 50.0, 25.0).with_mods(shift), v).unwrap();
|
||||
// Past the hidden edge and the edge at 150, then back across it, between pointer events.
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, 160.0, 25.0).with_mods(shift), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, 140.0, 30.0).with_mods(shift), v).unwrap();
|
||||
if cancel {
|
||||
assert!(s.tool_busy());
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
|
||||
assert!(!s.tool_busy());
|
||||
}
|
||||
// The release crosses the edges at 200 and 250 and goes on into the other group.
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 325.0, 25.0).with_mods(shift), v).unwrap();
|
||||
if cancel {
|
||||
assert!(s.doc().unwrap().doc == before, "Escape cancels the whole drag");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), depth);
|
||||
continue;
|
||||
}
|
||||
let stroke = |s: &Session, id: NodeId| {
|
||||
let n = node(s, id);
|
||||
(n.appearance.stroke_paint(), n.appearance.stroke().map(|st| st.width))
|
||||
};
|
||||
for x in [50.0, 150.0, 200.0, 250.0] {
|
||||
assert_eq!(stroke(&s, vertical_edge(&s, g, x)), (green.clone(), Some(3.0)), "edge at {x}");
|
||||
}
|
||||
for (group, x) in [(g, 0.0), (g, 100.0), (other, 300.0), (other, 350.0)] {
|
||||
assert_eq!(stroke(&s, vertical_edge(&s, group, x)), (black.clone(), Some(1.0)), "edge at {x}");
|
||||
}
|
||||
for e in edges_in(&s, g, Rect::new(-1.0, -1.0, 201.0, 1.0)) {
|
||||
assert_eq!(stroke(&s, e).0, black, "a top edge is not crossed");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), depth + 1, "one undo step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.doc().unwrap().doc == before, "one Undo restores every edge");
|
||||
}
|
||||
}
|
||||
|
||||
/// A stroke drag over edges that already have the stroke records no undo step and keeps the redo
|
||||
/// steps.
|
||||
#[test]
|
||||
fn live_paint_bucket_stroke_drag_no_op_keeps_history() {
|
||||
let mut s = session();
|
||||
let g = live_squares(&mut s, &[0.0, 50.0]);
|
||||
let green = Paint::solid(Color::from_hex("#00ff00").unwrap());
|
||||
let all = edges_in(&s, g, Rect::new(-1.0, -1.0, 101.0, 51.0));
|
||||
paint_parts(&mut s, &all, &green, &[]);
|
||||
paint_parts(&mut s, &all, &green, &[]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let (undo, redo) = (s.doc().unwrap().history.undo.len(), s.doc().unwrap().history.redo.len());
|
||||
assert_eq!(redo, 1);
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("livePaintBucket", v).unwrap();
|
||||
s.paint.stroke = green;
|
||||
s.paint.stroke_width = 1.0;
|
||||
let shift = Mods { shift: true, ..Default::default() };
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 0.0, 25.0).with_mods(shift), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, 60.0, 25.0).with_mods(shift), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 110.0, 25.0).with_mods(shift), v).unwrap();
|
||||
assert!(s.doc().unwrap().doc == before);
|
||||
assert_eq!((s.doc().unwrap().history.undo.len(), s.doc().unwrap().history.redo.len()), (undo, redo));
|
||||
}
|
||||
|
||||
/// `livePaint.strokeEdge` with `points` paints the crossed edges, counting the edge at the point;
|
||||
/// a malformed path or a scope other than "one" with `points` is an error that changes nothing.
|
||||
#[test]
|
||||
fn live_paint_stroke_drag_rejects_invalid_points_atomically() {
|
||||
let mut s = session();
|
||||
let g = live_squares(&mut s, &[0.0, 50.0, 100.0]);
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let stroke = |s: &mut Session, extra: Value| {
|
||||
let mut p = json!({"group": g.0, "point": [50.0, 25.0], "color": "#00ff00"});
|
||||
for (k, v) in extra.as_object().unwrap() {
|
||||
p[k] = v.clone();
|
||||
}
|
||||
s.execute("livePaint.strokeEdge", &p)
|
||||
};
|
||||
for points in
|
||||
[json!([]), json!([[51.0, 25.0]]), json!([[50.0, 25.0], [null, 0]]), json!([[50.0, 25.0], [1e30, 0]]), json!(vec![[50.0, 25.0]; 4097])]
|
||||
{
|
||||
assert!(stroke(&mut s, json!({ "points": points })).is_err(), "{points}");
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
}
|
||||
for continuation in [json!(null), json!([[[51.0, 25.0], [125.0, 25.0]]]), json!([vec![[50.0, 25.0]; 4097]])] {
|
||||
assert!(stroke(&mut s, json!({ "points": [[50.0, 25.0]], "continuation": continuation })).is_err(), "{continuation}");
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
}
|
||||
assert!(stroke(&mut s, json!({ "continuation": [[[50.0, 25.0]]] })).is_err());
|
||||
for scope in ["connected", "samePaint"] {
|
||||
assert!(stroke(&mut s, json!({ "points": [[50.0, 25.0], [125.0, 25.0]], "scope": scope })).is_err(), "{scope}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
// A path in two chunks reaches the edges at 50, 100 and 150, not the one at 0.
|
||||
let r = stroke(&mut s, json!({ "points": [[50.0, 25.0], [90.0, 25.0]], "continuation": [[[90.0, 25.0], [160.0, 25.0]]] })).unwrap();
|
||||
assert_eq!(r["count"], 3);
|
||||
let green = Paint::solid(Color::from_hex("#00ff00").unwrap());
|
||||
for x in [50.0, 100.0, 150.0] {
|
||||
assert_eq!(node(&s, vertical_edge(&s, g, x)).appearance.stroke_paint(), green, "edge at {x}");
|
||||
}
|
||||
assert_ne!(node(&s, vertical_edge(&s, g, 0.0)).appearance.stroke_paint(), green);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_paint_bucket_tool_fills_face() {
|
||||
let mut s = session();
|
||||
@@ -5391,7 +5522,41 @@ fn live_paint_bucket_alt_image_samples_fill_and_clears_stroke() {
|
||||
let mut s = session();
|
||||
let img = image::RgbaImage::from_raw(1, 1, vec![0, 255, 0, 255]).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -59,7 +59,24 @@ fn exported_png_of_a_red_square_is_red() {
|
||||
copy_red_square(&mut s);
|
||||
let r = run(&mut s, "clipboard.exportPng", json!({"scale": 2}));
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(80), Some(80)));
|
||||
let img = image::load_from_memory(&decode(&r)).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&decode(&r))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.dimensions(), (80, 80));
|
||||
assert_eq!(img.get_pixel(40, 40).0, [255, 0, 0, 255]);
|
||||
assert_eq!(img.get_pixel(2, 77).0, [255, 0, 0, 255], "cropped to the square");
|
||||
@@ -155,7 +172,41 @@ fn import_image_then_paste_gives_an_image() {
|
||||
#[test]
|
||||
fn an_image_read_apart_loads_as_import_image_loads_it() {
|
||||
let mut bmp = vec![];
|
||||
image::RgbImage::from_pixel(30, 20, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut bmp), image::ImageFormat::Bmp).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(30, 20, image::Rgb([200, 40, 40]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bmp.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let (mut at_once, mut apart) = (session(), session());
|
||||
let p = json!({"mime": "image/bmp", "center": [100, 80]});
|
||||
let mut with_data = p.clone();
|
||||
@@ -195,7 +246,41 @@ fn a_pasted_bitmap_is_drawn_without_decoding_it_again() {
|
||||
// The image has pixels no other test uses, since every test shares the image store.
|
||||
let img = image::RgbImage::from_fn(300, 200, |x, y| image::Rgb([(x % 253) as u8, (y % 233) as u8, 17]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = session();
|
||||
run(&mut s, "clipboard.importImage", json!({"dataBase64": vectorcraft_format::base64_encode(&png), "mime": "image/png"}));
|
||||
run(&mut s, "edit.paste", json!({}));
|
||||
@@ -278,7 +363,41 @@ fn copied_files_paste_by_their_content() {
|
||||
let png = decode(&run(&mut s, "clipboard.exportPng", json!({})));
|
||||
let pdf = decode(&run(&mut s, "clipboard.exportPdf", json!({})));
|
||||
let mut jpeg = vec![];
|
||||
image::RgbImage::new(2, 2).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::new(2, 2)).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| jpeg.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"4\" height=\"4\"/></svg>";
|
||||
let of = |name: &str, bytes: &[u8]| file_flavour(name, &bytes[..bytes.len().min(FILE_HEAD)], &PASTE_ORDER);
|
||||
assert_eq!(of("red.png", &png), Some(PNG));
|
||||
@@ -402,7 +521,23 @@ fn copy_leaves_out_the_png_of_art_too_large_to_scale_down() {
|
||||
run(&mut s, "select.set", json!({"ids": [id]}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
let png = s.clipboard.flavours(false, false).into_iter().find(|f| f.mime == PNG).expect("a PNG");
|
||||
let img = image::load_from_memory(&png.data).unwrap();
|
||||
let img = craft_image_codecs::decode(&png.data)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert!(img.width() <= 4096 && img.height() <= 4096, "{}x{}", img.width(), img.height());
|
||||
}
|
||||
|
||||
@@ -445,7 +580,41 @@ fn noise_png(side: u32) -> Vec<u8> {
|
||||
})
|
||||
.collect();
|
||||
let mut out = vec![];
|
||||
image::RgbImage::from_raw(side, side, px).unwrap().write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_raw(side, side, px).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,41 @@ fn session() -> Session {
|
||||
|
||||
fn png(img: image::DynamicImage) -> ImageBlob {
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
ImageBlob::png(out)
|
||||
}
|
||||
|
||||
|
||||
@@ -412,3 +412,97 @@ fn constrain_angle_turns_a_click_made_grid_about_its_reference_point() {
|
||||
let b = bounds(&s, id);
|
||||
assert_eq!((b.x0, b.y0, b.x1, b.y1), (175.0, 150.0, 225.0, 250.0), "turned about the click: {b:?}");
|
||||
}
|
||||
|
||||
/// The anchor points of the path `id`.
|
||||
fn path_points(s: &Session, id: NodeId) -> Vec<Point> {
|
||||
match &s.doc().unwrap().doc.node(id).unwrap().kind {
|
||||
NodeKind::Path { path, .. } => path.subpaths.iter().flat_map(|sp| sp.anchors.iter().map(|a| a.p)).collect(),
|
||||
other => panic!("a path: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The direction (degrees, counter-clockwise on the page) of the arc `id`'s first handle.
|
||||
fn first_handle_angle(s: &Session, id: NodeId) -> f64 {
|
||||
let NodeKind::Path { path, .. } = &s.doc().unwrap().doc.node(id).unwrap().kind else { panic!("a path") };
|
||||
let a = &path.subpaths[0].anchors[0];
|
||||
let v = a.h_out - a.p;
|
||||
(-v.y).atan2(v.x).to_degrees()
|
||||
}
|
||||
|
||||
fn near(p: Point, x: f64, y: f64) -> bool {
|
||||
close(p.x, x) && close(p.y, y)
|
||||
}
|
||||
|
||||
/// An arc dragged out runs along the turned axes: at 30° it still runs from the drag's start to
|
||||
/// its end, its base leaves the start along the turned x axis and its bounding box turns 30°; at
|
||||
/// 0° it is as before.
|
||||
#[test]
|
||||
fn constrain_angle_draws_a_dragged_arc_along_the_turned_axes() {
|
||||
let mut s = new_doc();
|
||||
for (deg, handle) in [(0.0, 0.0), (30.0, 30.0)] {
|
||||
set_angle(&mut s, deg);
|
||||
s.select_tool("arc", ViewInfo::default()).unwrap();
|
||||
gesture(&mut s, Mods::default(), &[(Down, 100.0, 100.0), (Drag, 130.0, 110.0), (Drag, 160.0, 120.0), (Up, 160.0, 120.0)]);
|
||||
let id = last_id(&s);
|
||||
let pts = path_points(&s, id);
|
||||
assert!(near(pts[0], 100.0, 100.0) && near(pts[1], 160.0, 120.0), "{deg}°: {pts:?}");
|
||||
assert!(close(first_handle_angle(&s, id), handle), "{deg}°: {}", first_handle_angle(&s, id));
|
||||
if deg == 0.0 {
|
||||
assert_eq!(bbox_angle(&s, id).to_bits(), 0.0f64.to_bits());
|
||||
} else {
|
||||
assert!(close(bbox_angle(&s, id), 30.0), "{}", bbox_angle(&s, id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An arc placed by the click dialog keeps its reference point at the click and runs its lengths
|
||||
/// along the turned axes: at 30° from the top-left reference point, and at 90° about the center
|
||||
/// one; at 0° the dialog's params are unchanged.
|
||||
#[test]
|
||||
fn constrain_angle_turns_a_click_made_arc_about_its_reference_point() {
|
||||
let mut s = new_doc();
|
||||
let params = |reference: u64, x: f64, y: f64| json!({"x": x, "y": y, "lengthX": 100, "lengthY": 50, "reference": reference, "baseAlong": "x", "slope": 50, "closed": false, "fillArc": false});
|
||||
let click = Point::new(200.0, 200.0);
|
||||
let p = params(0, 200.0, 200.0);
|
||||
assert_eq!(constrain_click(&s, "shape.arc", p.clone(), click), p, "0°");
|
||||
set_angle(&mut s, 30.0);
|
||||
let turned = constrain_click(&s, "shape.arc", p, click);
|
||||
assert_eq!(turned["rotation"], json!(-30.0));
|
||||
let id = NodeId(s.execute("shape.arc", &turned).unwrap()["id"].as_u64().unwrap());
|
||||
let pts = path_points(&s, id);
|
||||
let r = 30f64.to_radians();
|
||||
let end = (200.0 + 100.0 * r.cos() + 50.0 * r.sin(), 200.0 - 100.0 * r.sin() + 50.0 * r.cos());
|
||||
assert!(near(pts[0], 200.0, 200.0) && near(pts[1], end.0, end.1), "{pts:?} vs {end:?}");
|
||||
assert!(close(bbox_angle(&s, id), 30.0));
|
||||
set_angle(&mut s, 90.0);
|
||||
let p = constrain_click(&s, "shape.arc", params(4, 200.0, 200.0), click);
|
||||
let id = NodeId(s.execute("shape.arc", &p).unwrap()["id"].as_u64().unwrap());
|
||||
// The ends at opposite corners of the 100 × 50 box centered on the click, turned exactly.
|
||||
assert_eq!(path_points(&s, id), vec![Point::new(175.0, 250.0), Point::new(225.0, 150.0)]);
|
||||
let b = bounds(&s, id);
|
||||
assert!(close(b.x0, 175.0) && close(b.y0, 150.0) && close(b.x1, 225.0) && close(b.y1, 250.0), "turned about the click: {b:?}");
|
||||
assert_eq!(bbox_angle(&s, id), 90.0);
|
||||
}
|
||||
|
||||
/// `shape.arc` turns by an explicit `rotation` (clockwise) about `x1`, `y1` or about its reference
|
||||
/// point; a `rotation` that is not a number is refused and adds nothing.
|
||||
#[test]
|
||||
fn arc_rotation_turns_it_and_must_be_a_number() {
|
||||
let mut s = new_doc();
|
||||
let id = NodeId(s.execute("shape.arc", &json!({"x1": 0, "y1": 0, "x2": 10, "y2": 20, "rotation": 90})).unwrap()["id"].as_u64().unwrap());
|
||||
let pts = path_points(&s, id);
|
||||
assert!(near(pts[0], 0.0, 0.0) && near(pts[1], -20.0, 10.0), "{pts:?}");
|
||||
assert_eq!(bbox_angle(&s, id), -90.0, "a quarter turn clockwise");
|
||||
let id =
|
||||
NodeId(s.execute("shape.arc", &json!({"x": 50, "y": 50, "lengthX": 10, "lengthY": 20, "rotation": 360})).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(path_points(&s, id), vec![Point::new(50.0, 50.0), Point::new(60.0, 70.0)], "a whole turn leaves it upright");
|
||||
assert_eq!(bbox_angle(&s, id).to_bits(), 0.0f64.to_bits());
|
||||
for bad in [json!("x"), Value::Null] {
|
||||
for p in
|
||||
[json!({"x1": 0, "y1": 0, "x2": 10, "y2": 20, "rotation": bad}), json!({"x": 0, "y": 0, "lengthX": 10, "lengthY": 20, "rotation": bad})]
|
||||
{
|
||||
assert!(s.execute("shape.arc", &p).is_err(), "{p}");
|
||||
assert_eq!(last_id(&s), id, "nothing added for {p}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,9 +186,41 @@ fn type_with_an_inline_graphic_stays_type_when_its_own_stroke_has_an_effect() {
|
||||
/// A selected 30×30 red image placed with its frame from (50, 50) to (80, 80).
|
||||
fn image(s: &mut Session) -> NodeId {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
NodeId(r["ids"][0].as_u64().unwrap())
|
||||
}
|
||||
@@ -259,11 +291,44 @@ fn an_images_stroke_with_outline_object_draws_exports_and_expands_along_its_fram
|
||||
/// A 100×100 PNG, transparent but for an opaque red disc of radius 30 in its middle.
|
||||
fn disc_png() -> Vec<u8> {
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_fn(100, 100, |x, y| {
|
||||
let r = (f64::from(x) + 0.5 - 50.0).hypot(f64::from(y) + 0.5 - 50.0);
|
||||
image::Rgba([255, 0, 0, if r <= 30.0 { 255 } else { 0 }])
|
||||
})
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(100, 100, |x, y| {
|
||||
let r = (f64::from(x) + 0.5 - 50.0).hypot(f64::from(y) + 0.5 - 50.0);
|
||||
image::Rgba([255, 0, 0, if r <= 30.0 { 255 } else { 0 }])
|
||||
}))
|
||||
.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
@@ -316,9 +381,41 @@ fn an_images_stroke_with_outline_object_follows_its_opaque_area() {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("opaque.png");
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(100, 100, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(100, 100, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
std::fs::write(&path, png).unwrap();
|
||||
run(&mut s, "links.relink", json!({"ids": [im.0], "path": path}));
|
||||
assert_eq!([shade(&s, 41, 100), shade(&s, 45, 100), shade(&s, 64, 100)], ["white", "black", "red"]);
|
||||
|
||||
@@ -174,6 +174,27 @@ fn language_picks_its_quotes_unless_quotes_are_given() {
|
||||
assert_eq!((setup(&s).quotes.double, setup(&s).quotes.single), (['“', '”'], ['‹', '›']));
|
||||
}
|
||||
|
||||
/// Sanskrit, Nepali, Assamese, Konkani and Sindhi (#4943): each is a document language, new type
|
||||
/// takes its tag, and each can be set on a text by name or tag.
|
||||
#[test]
|
||||
fn the_further_indian_languages_are_document_and_text_languages() {
|
||||
let mut s = session();
|
||||
let id = create_text(&mut s, "a");
|
||||
for (name, tag) in [("Sanskrit", "sa"), ("Nepali", "ne"), ("Assamese", "as"), ("Konkani", "kok"), ("Sindhi", "sd")] {
|
||||
s.execute("document.setup", &json!({"language": name.to_lowercase()})).unwrap();
|
||||
assert_eq!((setup(&s).language.as_str(), setup(&s).quotes.double), (name, ['“', '”']));
|
||||
let new = create_text(&mut s, "b");
|
||||
assert_eq!(text(&s, new).runs[0].style.language.as_deref(), Some(tag), "new type in {name}");
|
||||
s.execute("document.setup", &json!({"language": "English: USA"})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("text.setFormat", &json!({"language": name})).unwrap();
|
||||
assert_eq!(text(&s, id).runs[0].style.language.as_deref(), Some(tag), "{name} by name");
|
||||
s.execute("text.setFormat", &json!({"language": "English: USA"})).unwrap();
|
||||
s.execute("text.setFormat", &json!({"language": tag.to_uppercase()})).unwrap();
|
||||
assert_eq!(text(&s, id).runs[0].style.language.as_deref(), Some(tag), "{name} by tag");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typographers_quotes_setting_changes_typed_quotes() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -2658,6 +2658,66 @@ fn shape_arc_closed_along_y_closes_through_the_top_left_corner() {
|
||||
assert!((area - std::f64::consts::FRAC_PI_4).abs() < 0.01, "{area}");
|
||||
}
|
||||
|
||||
/// A `reference` that is not a whole number from 0 to 8, a `baseAlong` other than x or y, and a
|
||||
/// `slope` outside -100 to 100 are errors naming the key in both forms of `shape.arc`, null
|
||||
/// included, and nothing is drawn and no undo step is added.
|
||||
#[test]
|
||||
fn shape_arc_rejects_an_invalid_reference_base_along_or_slope() {
|
||||
let mut s = session();
|
||||
let objects = |s: &Session| s.doc().unwrap().doc.layers[0].children().unwrap().len();
|
||||
let undo = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let (before, steps) = (objects(&s), undo(&s));
|
||||
let sized = json!({"x": 100, "y": 50, "lengthX": 80, "lengthY": 40});
|
||||
let ends = json!({"x1": 0, "y1": 0, "x2": 50, "y2": 50});
|
||||
let mut cases = vec![];
|
||||
for v in [json!("4"), json!(-1), json!(4.5), json!(9), json!(9.0), json!(true), json!(null), json!([0])] {
|
||||
cases.push(("reference", "`reference` must be a whole number from 0 to 8".to_string(), v));
|
||||
}
|
||||
for v in [json!("z"), json!("yes"), json!(""), json!(1), json!(false), json!(null), json!({"axis": "y"})] {
|
||||
cases.push(("baseAlong", "`baseAlong` must be one of x, y".to_string(), v));
|
||||
}
|
||||
for (v, shown) in [(json!(150), "150"), (json!(-100.5), "-100.5")] {
|
||||
cases.push(("slope", format!("`slope` must be from -100 to 100, not {shown}"), v));
|
||||
}
|
||||
for form in [&sized, &ends] {
|
||||
for (key, want, v) in &cases {
|
||||
let mut p = form.clone();
|
||||
p[*key] = v.clone();
|
||||
let r = s.execute("shape.arc", &p);
|
||||
assert!(matches!(&r, Err(EngineError::BadParams { msg, .. }) if msg == want), "{p}: {r:?}");
|
||||
assert_eq!((objects(&s), undo(&s)), (before, steps), "{p}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `shape.arc` takes each of the nine reference points (`4.0` as 4) and x or y in either case for
|
||||
/// `baseAlong`; a left-out `reference` is 0 and a left-out `baseAlong` is x.
|
||||
#[test]
|
||||
fn shape_arc_takes_every_reference_point_and_either_base_axis() {
|
||||
let mut s = session();
|
||||
let arc = |s: &mut Session, extra: serde_json::Value| {
|
||||
let mut p = json!({"x": 100, "y": 50, "lengthX": 80, "lengthY": 40});
|
||||
for (k, v) in extra.as_object().unwrap() {
|
||||
p[k] = v.clone();
|
||||
}
|
||||
let id = NodeId(s.execute("shape.arc", &p).unwrap()["id"].as_u64().unwrap());
|
||||
s.doc().unwrap().doc.node(id).unwrap().path_data().unwrap().clone()
|
||||
};
|
||||
let start = |path: &PathData| path.subpaths[0].anchors[0].p;
|
||||
for r in 0..=8u64 {
|
||||
let p = start(&arc(&mut s, json!({ "reference": r })));
|
||||
let (col, row) = ((r % 3) as f64, (r / 3) as f64);
|
||||
assert_eq!(p, Point::new(100.0 - 40.0 * col, 50.0 - 20.0 * row), "reference {r}");
|
||||
}
|
||||
assert_eq!(arc(&mut s, json!({"reference": 4.0})), arc(&mut s, json!({"reference": 4})));
|
||||
assert_eq!(arc(&mut s, json!({})), arc(&mut s, json!({"reference": 0, "baseAlong": "x"})));
|
||||
assert_eq!(arc(&mut s, json!({"baseAlong": "X"})), arc(&mut s, json!({"baseAlong": "x"})));
|
||||
assert_eq!(arc(&mut s, json!({"baseAlong": "Y"})), arc(&mut s, json!({"baseAlong": "y"})));
|
||||
assert_ne!(arc(&mut s, json!({"baseAlong": "y"})), arc(&mut s, json!({"baseAlong": "x"})));
|
||||
let ends = json!({"x1": 0, "y1": 0, "x2": 50, "y2": 50, "slope": -100});
|
||||
assert!(s.execute("shape.arc", &ends).is_ok());
|
||||
}
|
||||
|
||||
/// A spiral dragged out with Alt pressed part way gets four more segments per wind as its outer end
|
||||
/// moves out. Ctrl/Cmd pressed part way fits the decay so the inner end keeps its distance from the
|
||||
/// center. The Spiral tool keeps both values for the next spiral.
|
||||
@@ -2822,7 +2882,7 @@ fn polar_grid_options_compound_fill_and_skew() {
|
||||
assert!(tips(&inward)[1..].iter().all(|t| t.x < 50.0), "counterclockwise of the top: {:?}", tips(&inward));
|
||||
}
|
||||
|
||||
/// A divider count written as a whole float (`3.0`) is the same as `3`; a fraction gives the default 5.
|
||||
/// A divider count written as a whole float (`3.0`) is the same as `3`.
|
||||
#[test]
|
||||
fn grid_divider_counts_accept_whole_floats() {
|
||||
let mut s = session();
|
||||
@@ -2834,8 +2894,30 @@ fn grid_divider_counts_accept_whole_floats() {
|
||||
let float = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2.0, "radial": 3.0})));
|
||||
let int = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2, "radial": 3})));
|
||||
assert_eq!(float.len(), int.len());
|
||||
let frac = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 2.5, "columns": 2.5})));
|
||||
assert_eq!(frac.len(), 11);
|
||||
}
|
||||
|
||||
/// A divider count that is not a whole number of 0 or more (a fraction, a negative) is an error
|
||||
/// naming the key, and no grid is made; a left-out or null count is 5.
|
||||
#[test]
|
||||
fn grid_divider_counts_reject_fractions_and_negatives() {
|
||||
let mut s = session();
|
||||
let size = json!({"x": 0, "y": 0, "width": 100, "height": 100});
|
||||
let undo = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let before = undo(&s);
|
||||
for (cmd, key, value, shown) in [
|
||||
("shape.rectangularGrid", "rows", json!(2.5), "2.5"),
|
||||
("shape.rectangularGrid", "columns", json!(-1), "-1"),
|
||||
("shape.polarGrid", "concentric", json!(0.5), "0.5"),
|
||||
("shape.polarGrid", "radial", json!(-3.0), "-3"),
|
||||
] {
|
||||
let r = s.execute(cmd, &with(&size, json!({ key: value })));
|
||||
let want = format!("`{key}` must be a whole number of at least 0, not {shown}");
|
||||
assert!(matches!(&r, Err(EngineError::BadParams { msg, .. }) if *msg == want), "{cmd} {key}={value}: {r:?}");
|
||||
assert_eq!(undo(&s), before, "{cmd} {key}={value}");
|
||||
}
|
||||
let omitted = grid(&mut s, "shape.rectangularGrid", size.clone());
|
||||
let null = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": null, "columns": null})));
|
||||
assert_eq!((omitted.len(), null.len()), (11, 11));
|
||||
}
|
||||
|
||||
/// The grid options are checked: a divider count or a skew is a number and a switch true or false,
|
||||
@@ -3219,6 +3301,88 @@ fn split_refuses_a_subpath_anchor_or_segment_that_is_not_a_whole_number() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Give `id` a 4 pt round-capped Inside stroke and, above it, a hidden red copy aligned Outside.
|
||||
/// Returns the appearance it now has.
|
||||
fn side_strokes(s: &mut Session, id: NodeId) -> vectorcraft_doc::Appearance {
|
||||
use vectorcraft_doc::{AppearanceItem, LineCap, StrokeAlign};
|
||||
s.edit("Side Strokes", |d, _| {
|
||||
let ap = &mut d.node_mut(id).unwrap().appearance;
|
||||
let st = ap.stroke_mut().unwrap();
|
||||
(st.width, st.cap, st.align) = (4.0, LineCap::Round, StrokeAlign::Inside);
|
||||
let mut top = st.clone();
|
||||
(top.align, top.visible, top.paint) =
|
||||
(StrokeAlign::Outside, false, vectorcraft_color::Paint::solid(vectorcraft_color::Color::rgb(1.0, 0.0, 0.0)));
|
||||
ap.items.push(AppearanceItem::Stroke(top));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
appearance(s, id)
|
||||
}
|
||||
|
||||
fn appearance(s: &Session, id: NodeId) -> vectorcraft_doc::Appearance {
|
||||
(*s.doc().unwrap().doc.node(id).unwrap().appearance).clone()
|
||||
}
|
||||
|
||||
/// `ap` with every stroke aligned Center and nothing else changed.
|
||||
fn centered(ap: &vectorcraft_doc::Appearance) -> vectorcraft_doc::Appearance {
|
||||
let mut ap = ap.clone();
|
||||
for it in &mut ap.items {
|
||||
if let vectorcraft_doc::AppearanceItem::Stroke(st) = it {
|
||||
st.align = vectorcraft_doc::StrokeAlign::Center;
|
||||
}
|
||||
}
|
||||
ap
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scissors_cut_paths_have_their_strokes_centered() {
|
||||
// The paths a Scissors cut makes keep the source's fill and strokes, every stroke centered,
|
||||
// in the cut's one undo step; redo centers them again.
|
||||
let mut s = session();
|
||||
let r = rect(&mut s, 100.0, 100.0, 100.0, 100.0);
|
||||
let before = side_strokes(&mut s, r);
|
||||
assert_ne!(before, centered(&before));
|
||||
let undo = undo_steps(&s);
|
||||
s.execute("path.split", &json!({"id": r.0, "anchor": 2})).unwrap();
|
||||
assert_eq!(undo_steps(&s), undo + 1);
|
||||
assert!(!path(&s, r).subpaths[0].closed);
|
||||
assert_eq!(appearance(&s, r), centered(&before), "closed path");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(path(&s, r).subpaths[0].closed);
|
||||
assert_eq!(appearance(&s, r), before, "undo");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(appearance(&s, r), centered(&before), "redo");
|
||||
// An open path becomes two, at an anchor or inside a segment, both centered.
|
||||
let anchors = json!([{"x": 100, "y": 300}, {"x": 200, "y": 300}, {"x": 300, "y": 300}]);
|
||||
for at in [json!({"anchor": 1}), json!({"segment": 0, "t": 0.5})] {
|
||||
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
|
||||
let before = side_strokes(&mut s, id);
|
||||
let made = s.execute("path.split", &with(&at, json!({"id": id.0}))).unwrap();
|
||||
let ids: Vec<NodeId> = made["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
|
||||
assert_eq!(ids.len(), 2, "{at}");
|
||||
for i in ids {
|
||||
assert_eq!(appearance(&s, i), centered(&before), "{at}: {i:?}");
|
||||
}
|
||||
}
|
||||
// A path of several subpaths keeps one node: its other subpath's strokes are centered too.
|
||||
let id = two_subpaths(&mut s);
|
||||
let before = side_strokes(&mut s, id);
|
||||
s.execute("path.split", &json!({"id": id.0, "anchor": 1})).unwrap();
|
||||
assert_eq!(path(&s, id).subpaths.len(), 3);
|
||||
assert_eq!(appearance(&s, id), centered(&before), "several subpaths");
|
||||
// A cut that fails, at an open path's end point, changes neither the strokes nor the history.
|
||||
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
|
||||
let before = side_strokes(&mut s, id);
|
||||
let undo = undo_steps(&s);
|
||||
assert!(s.execute("path.split", &json!({"id": id.0, "anchor": 0})).is_err());
|
||||
assert_eq!((undo_steps(&s), appearance(&s, id)), (undo, before));
|
||||
// Strokes already centered stay shared with the undo history's copy.
|
||||
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
|
||||
let shared = s.doc().unwrap().doc.node(id).unwrap().appearance.clone();
|
||||
s.execute("path.split", &json!({"id": id.0, "anchor": 1})).unwrap();
|
||||
assert!(vectorcraft_doc::Shared::ptr_eq(&s.doc().unwrap().doc.node(id).unwrap().appearance, &shared));
|
||||
}
|
||||
|
||||
/// A subpath, anchor or segment of 2^32 + k fails, where k names one that exists, and the document
|
||||
/// and the undo history stay as they were. A 32-bit build read the number as k.
|
||||
#[test]
|
||||
@@ -3938,6 +4102,54 @@ fn cut_at_anchors_splits_paths_and_leaves_one_end_selected() {
|
||||
assert_eq!(undo_steps(&s), undo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cut_at_anchors_paths_have_their_strokes_centered() {
|
||||
// Every path the cut makes keeps the source's fill and strokes, every stroke centered, in one
|
||||
// undo step; a path whose anchors were not selected keeps its strokes.
|
||||
let mut s = session();
|
||||
let anchors = json!([{"x": 100, "y": 100}, {"x": 200, "y": 100}, {"x": 300, "y": 100}, {"x": 400, "y": 100}]);
|
||||
let id = NodeId(s.execute("path.create", &json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap());
|
||||
let open = side_strokes(&mut s, id);
|
||||
let r = rect(&mut s, 500.0, 100.0, 100.0, 100.0);
|
||||
let closed = side_strokes(&mut s, r);
|
||||
let other = rect(&mut s, 100.0, 300.0, 100.0, 100.0);
|
||||
let untouched = side_strokes(&mut s, other);
|
||||
s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 1], [0, 2]]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": r.0, "anchors": [[0, 2]], "mode": "add"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.anchors.len(), 2);
|
||||
let undo = undo_steps(&s);
|
||||
let made = s.execute("path.cutAtAnchors", &json!({})).unwrap();
|
||||
let ids: Vec<NodeId> = made["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
|
||||
assert_eq!(undo_steps(&s), undo + 1);
|
||||
assert_eq!(ids.len(), 4, "three pieces and the opened rectangle");
|
||||
for i in &ids {
|
||||
let want = if *i == r { centered(&closed) } else { centered(&open) };
|
||||
assert_eq!(appearance(&s, *i), want, "{i:?}");
|
||||
}
|
||||
assert_eq!(appearance(&s, other), untouched);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((appearance(&s, id), appearance(&s, r)), (open.clone(), closed.clone()), "undo");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!((appearance(&s, id), appearance(&s, r)), (centered(&open), centered(&closed)), "redo");
|
||||
// A cut member of a compound path: the compound path's strokes, which it shows, are centered.
|
||||
let a = rect(&mut s, 100.0, 450.0, 100.0, 100.0);
|
||||
let b = rect(&mut s, 125.0, 475.0, 50.0, 50.0);
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
s.execute("object.compoundPath.make", &json!({})).unwrap();
|
||||
let compound = s.doc().unwrap().selection.objects[0];
|
||||
let before = side_strokes(&mut s, compound);
|
||||
let members: Vec<NodeId> = match &s.doc().unwrap().doc.node(compound).unwrap().kind {
|
||||
NodeKind::Compound { children, .. } => children.iter().map(|c| c.id).collect(),
|
||||
k => panic!("not a compound path: {k:?}"),
|
||||
};
|
||||
let held = path(&s, members[1]);
|
||||
s.execute("select.anchors", &json!({"id": members[0].0, "anchors": [[0, 2]]})).unwrap();
|
||||
s.execute("path.cutAtAnchors", &json!({})).unwrap();
|
||||
assert!(!path(&s, members[0]).subpaths[0].closed);
|
||||
assert_eq!(path(&s, members[1]), held, "the other member keeps its shape");
|
||||
assert_eq!(appearance(&s, compound), centered(&before), "compound path");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_segment_delete_keeps_the_other_rectangle_edges_and_one_undo() {
|
||||
for initially_selected in [false, true] {
|
||||
|
||||
@@ -798,7 +798,41 @@ fn a_blend_around_a_picked_objects_instance_is_left_alone() {
|
||||
fn image_symbol(s: &mut Session) -> (NodeId, Value) {
|
||||
let mut png = Vec::new();
|
||||
let pixels = image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(2, 1, vec![0, 255]).unwrap());
|
||||
pixels.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = pixels;
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let im = s
|
||||
.edit("Place", |d, _| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
|
||||
@@ -403,14 +403,68 @@ fn blend_moves_a_mesh_to_the_blended_colour() {
|
||||
fn png(px: [[u8; 4]; 4]) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_raw(2, 2, px.concat()).unwrap();
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
fn pixels(s: &Session, id: NodeId) -> Vec<[u8; 4]> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id).unwrap().kind else { panic!("not an image") };
|
||||
image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect()
|
||||
craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.pixels()
|
||||
.map(|p| p.0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A selected embedded 2×2 image.
|
||||
@@ -652,7 +706,41 @@ fn convert_to_indexed_refuses_bad_params() {
|
||||
/// An embedded `w` × `h` image of the pixels `f(x, y)`, placed and selected.
|
||||
fn placed(s: &mut Session, w: u32, h: u32, f: impl Fn(u32, u32) -> [u8; 4]) -> NodeId {
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y))).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_fn(w, h, |x, y| image::Rgba(f(x, y)))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "art.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
NodeId(r["ids"][0].as_u64().unwrap())
|
||||
}
|
||||
@@ -681,7 +769,41 @@ fn convert_to_indexed_takes_ids_and_leaves_linked_and_colorized_images() {
|
||||
assert_eq!(image_key(&s, a), ka);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
|
||||
let mut png = Vec::new();
|
||||
image::GrayImage::from_pixel(4, 4, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(4, 4, image::Luma([90]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "gray.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
let gray = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [gray.0], "color": "#ff0000"})).unwrap();
|
||||
|
||||
@@ -117,7 +117,41 @@ fn the_frame_round_trips_natively() {
|
||||
fn red_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -159,7 +193,41 @@ fn a_colorized_image_keeps_its_color_in_an_envelope() {
|
||||
let mut s = session();
|
||||
let img = image::GrayImage::from_pixel(4, 4, image::Luma([0]));
|
||||
let mut png = vec![];
|
||||
image::DynamicImage::ImageLuma8(img).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let im = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
@@ -187,7 +255,41 @@ fn a_colorized_image_keeps_its_color_in_an_envelope() {
|
||||
fn two_color_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_fn(2, 2, |x, _| if x == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) });
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("two".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -562,7 +562,41 @@ fn clicking_an_image_samples_its_pixel() {
|
||||
// Two pixels, orange and teal, 100 pt each, at (300, 0).
|
||||
let img = image::RgbaImage::from_raw(2, 1, [[255, 128, 0, 255], [0, 128, 128, 255]].concat()).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let l = d.layers[0].id;
|
||||
@@ -588,7 +622,41 @@ fn cmyk_doc_with_image(px: &[[u8; 4]]) -> Session {
|
||||
let w = px.len() as u32;
|
||||
let img = image::RgbaImage::from_raw(w, 1, px.concat()).unwrap();
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, _| {
|
||||
d.images.insert("px".into(), ImageBlob::png(png));
|
||||
let l = d.layers[0].id;
|
||||
|
||||
@@ -459,7 +459,24 @@ fn export_selection_keeps_the_effects_in_the_crop() {
|
||||
assert_eq!(plain["bounds"], json!([99.5, 99.5, 21.0, 21.0]), "a plain path keeps its crop: {plain}");
|
||||
s.execute("effect.apply", &json!({"effect": effect, "params": params})).unwrap();
|
||||
let png = |v: &serde_json::Value| {
|
||||
image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
};
|
||||
let v = s.execute("document.exportSelection", &json!({"format": "png", "background": "transparent"})).unwrap();
|
||||
let b: Vec<f64> = v["bounds"].as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect();
|
||||
@@ -485,7 +502,24 @@ fn a_screen_outer_glow_is_in_raster_exports() {
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.outerGlow"})).unwrap();
|
||||
let mut pixel = |p: serde_json::Value| {
|
||||
let v = s.execute("document.export", &p).unwrap();
|
||||
let img = image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// 2 pt past the square's stroke, on the right.
|
||||
*img.get_pixel(122, 110)
|
||||
};
|
||||
@@ -512,7 +546,24 @@ fn export_selection_keeps_a_groups_shadow_in_the_crop() {
|
||||
let b: Vec<f64> = v["bounds"].as_array().unwrap().iter().map(|n| n.as_f64().unwrap()).collect();
|
||||
// The shadow of the right square reaches x = 158.5.
|
||||
assert!(b[0] < 99.5 && b[0] + b[2] >= 158.5, "{v}");
|
||||
let img = image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert!(img.get_pixel((155.0 - b[0]) as u32, img.height() / 2)[3] > 0, "the shadow is in the image");
|
||||
}
|
||||
|
||||
@@ -534,7 +585,24 @@ fn the_arts_bounds_take_in_the_shadow_of_a_group_and_of_type() {
|
||||
s.execute("effect.apply", &json!({"effect": "stylize.dropShadow", "ids": [id], "params": {"x": 8, "y": 0, "blur": 0}})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
let png = |v: &serde_json::Value| {
|
||||
image::load_from_memory(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
};
|
||||
let art = s.execute("document.export", &json!({"format": "png", "useArtboards": false, "background": "transparent"})).unwrap();
|
||||
let sel = s.execute("document.exportSelection", &json!({"format": "png", "background": "transparent"})).unwrap();
|
||||
|
||||
@@ -499,7 +499,26 @@ fn png_exports_carry_text_chunks() {
|
||||
assert_eq!(get("Keywords"), Some("fair, Poster, print"));
|
||||
assert_eq!(get("Copyright"), Some("(c) 2026 Ada"));
|
||||
assert!(get("Creation Time").is_some_and(|t| t.ends_with('Z')));
|
||||
assert!(image::load_from_memory(&png).is_ok(), "still a valid PNG");
|
||||
assert!(
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.is_ok(),
|
||||
"still a valid PNG"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -300,7 +300,41 @@ fn a_radial_gradient_lists_its_stops_from_the_first() {
|
||||
/// A `w`×`h` PNG of one colour (`rgba`), 72 ppi.
|
||||
fn png(w: u32, h: u32, rgba: [u8; 4]) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba(rgba)).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba(rgba))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -402,7 +436,41 @@ fn disk(size: u32, r: f64) -> Vec<u8> {
|
||||
image::Rgba([0, 0, 0, ((r + 0.5 - d).clamp(0.0, 1.0) * 255.0).round() as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -530,7 +598,41 @@ fn colorized_bitmap_over_square() -> (Session, Vec<u8>) {
|
||||
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
|
||||
let gray = vec![0, 255, 0, 255, 255, 0, 255, 0];
|
||||
let mut bytes = vec![];
|
||||
image::GrayImage::from_raw(4, 2, gray.clone()).unwrap().write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_raw(4, 2, gray.clone()).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"name": "b.png", "dataBase64": vectorcraft_format::base64_encode(&bytes), "rect": [30, 10, 40, 20]});
|
||||
s.execute("file.place", &p).unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Ink"})).unwrap();
|
||||
@@ -593,10 +695,41 @@ fn a_colorized_image_of_black_and_clear_pixels_is_written_as_an_image_mask() {
|
||||
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
|
||||
s.execute("swatch.new", &json!({"name": "Ink", "color": {"c": 100, "m": 0, "y": 0, "k": 0}, "spot": true})).unwrap();
|
||||
let mut bytes = vec![];
|
||||
image::GrayAlphaImage::from_raw(2, 1, vec![0, 255, 0, 0])
|
||||
.unwrap()
|
||||
.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayAlphaImage::from_raw(2, 1, vec![0, 255, 0, 0]).unwrap()).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"name": "c.png", "dataBase64": vectorcraft_format::base64_encode(&bytes), "rect": [30, 10, 20, 10]});
|
||||
s.execute("file.place", &p).unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Ink"})).unwrap();
|
||||
@@ -732,7 +865,41 @@ fn an_artboard_the_document_does_not_have_is_refused_as_an_export_refuses_it() {
|
||||
fn checker(a: [u8; 4], b: [u8; 4]) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_fn(32, 32, |x, y| image::Rgba(if (x < 16) == (y < 16) { a } else { b }));
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -760,7 +927,41 @@ fn colorized_pattern_over_blue() -> Session {
|
||||
|
||||
let tile = image::GrayImage::from_fn(32, 32, |x, y| image::Luma([if (x < 16) == (y < 16) { 0 } else { 255 }]));
|
||||
let mut bytes = vec![];
|
||||
tile.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = tile.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = pattern_over_blue(&bytes, 1.0);
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
assert!(d.patterns[0].art.iter_mut().any(|art| colorize_image(std::sync::Arc::make_mut(art))));
|
||||
|
||||
@@ -35,7 +35,41 @@ impl Drop for Folder {
|
||||
pub(crate) fn png(w: u32, h: u32, rgb: [u8; 3]) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
let [r, g, b] = rgb;
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba([r, g, b, 255])).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba([r, g, b, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -200,7 +234,41 @@ fn linked_dib_and_pns_files_place_update_and_relink() {
|
||||
let bmp = |w: u32, h: u32, rgb: [u8; 3]| {
|
||||
let mut out = vec![];
|
||||
let [r, g, b] = rgb;
|
||||
image::RgbImage::from_pixel(w, h, image::Rgb([r, g, b])).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Bmp).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(w, h, image::Rgb([r, g, b]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
};
|
||||
let dir = Folder::new("dib");
|
||||
@@ -1300,7 +1368,41 @@ pub(crate) fn cut_png(w: u32, h: u32) -> Vec<u8> {
|
||||
image::Rgb([v as u8, (v >> 8) as u8, (v >> 16) as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out.truncate(out.len() / 2);
|
||||
out
|
||||
}
|
||||
@@ -1414,12 +1516,27 @@ fn low_res_proxy_eps_export_uses_full_resolution_from_the_linked_file() {
|
||||
let (full, warnings) = cmd::links::full_linked_images(&s.doc().unwrap().doc, Default::default());
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
assert!(!full.images[&key].is_proxy());
|
||||
let dim =
|
||||
image::ImageReader::new(std::io::Cursor::new(full.images[&key].bytes.as_ref())).with_guessed_format().unwrap().into_dimensions().unwrap();
|
||||
let dim = craft_image_codecs::decode(full.images[&key].bytes.as_ref()).unwrap().dimensions();
|
||||
assert_eq!(dim, (600, 300));
|
||||
let png_out = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let bytes = vectorcraft_format::base64_decode(png_out["dataBase64"].as_str().unwrap()).unwrap();
|
||||
let rendered = image::load_from_memory(&bytes).unwrap();
|
||||
let rendered = craft_image_codecs::decode(&bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert!(rendered.width() >= 400 && rendered.height() >= 300);
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED), "canvas still shows the preview");
|
||||
}
|
||||
@@ -1489,7 +1606,41 @@ fn a_file_read_for_opening_has_its_links_read_and_its_type_laid_out() {
|
||||
let dir = Folder::new("read-for-open");
|
||||
let tif = dir.file("art/photo.tif");
|
||||
let mut tiff = vec![];
|
||||
image::RgbImage::from_pixel(600, 300, image::Rgb(RED)).write_to(&mut std::io::Cursor::new(&mut tiff), image::ImageFormat::Tiff).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbImage::from_pixel(600, 300, image::Rgb(RED))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Tiff, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| tiff.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
write(&tif, &tiff);
|
||||
let (gone, changed) = (dir.file("gone.png"), dir.file("changed.png"));
|
||||
write(&gone, &png(300, 100, BLUE));
|
||||
|
||||
@@ -1525,7 +1525,41 @@ fn two_colour_image(s: &mut Session, clear_right: bool, linked: bool) -> NodeId
|
||||
let right = image::Rgba([0, 0, 255, if clear_right { 0 } else { 255 }]);
|
||||
let img = image::RgbaImage::from_fn(8, 4, |x, _| if x < 4 { image::Rgba([255, 0, 0, 255]) } else { right });
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("pic".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -126,7 +126,23 @@ fn preview_is_at_most_256_pixels() {
|
||||
let mut s = session();
|
||||
let bytes = saved(&mut s, json!({"preview": true, "compress": true}));
|
||||
let png = vectorcraft_format::preview(&bytes).expect("a preview");
|
||||
let img = image::load_from_memory(&png).unwrap();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((img.width(), img.height()), (256, 128), "the 400×200 artboard fitted into 256 px");
|
||||
assert!(vectorcraft_format::preview(&saved(&mut s, json!({}))).is_none(), "off by default");
|
||||
}
|
||||
@@ -154,7 +170,41 @@ fn saving_writes_through_a_temporary_file() {
|
||||
fn unused_images_are_left_out_of_saves() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = s.execute("file.place", &json!({"name": "a.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
|
||||
let id = r["ids"][0].as_u64().unwrap();
|
||||
assert_eq!(json_of(&saved(&mut s, json!({})))["images"].as_object().unwrap().len(), 1);
|
||||
|
||||
@@ -90,7 +90,26 @@ fn a_white_background_exports_opaque() {
|
||||
let mut s = new_doc(json!({"width": 20, "height": 20, "backgroundContents": bg}));
|
||||
let r = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let png = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
||||
image::load_from_memory(&png).unwrap().to_rgba8().get_pixel(10, 10).0
|
||||
craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(10, 10)
|
||||
.0
|
||||
};
|
||||
assert_eq!(alpha("white"), [255, 255, 255, 255]);
|
||||
assert_eq!(alpha("transparent")[3], 0);
|
||||
@@ -540,9 +559,41 @@ fn edit_print_profile(s: &mut Session, dir: &std::path::Path) {
|
||||
// One embedded image becomes a symbol's art, another stays on the profile's layer.
|
||||
let png = |rgb: [u8; 3]| {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([rgb[0], rgb[1], rgb[2], 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([rgb[0], rgb[1], rgb[2], 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
vectorcraft_format::base64_encode(&out)
|
||||
};
|
||||
s.execute("file.place", &json!({"name": "red.png", "dataBase64": png([255, 0, 0]), "link": false})).unwrap();
|
||||
|
||||
@@ -746,6 +746,87 @@ fn package_copies_a_linked_pdf_page_as_it_is() {
|
||||
assert!(s.doc().unwrap().doc.has_placed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_copies_a_linked_eps_file_as_it_is() {
|
||||
let dir = Folder::new("package-placed-eps");
|
||||
let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EndComments\n1 0 0 setrgbcolor\nnewpath 0 0 moveto 100 0 lineto 100 50 lineto 0 50 lineto closepath fill\nshowpage\n%%EOF\n";
|
||||
let src = dir.file("art/seal.eps");
|
||||
write(&src, eps);
|
||||
let mut s = session();
|
||||
let placed = run(&mut s, "file.place", json!({"path": src}));
|
||||
assert_eq!(placed["linked"], true, "{placed}");
|
||||
save(&mut s, &dir.file("poster.vectorcraft"));
|
||||
let r = run(&mut s, "file.package", json!({"folder": dir.file("out")}));
|
||||
assert_eq!((r["links"].as_u64(), r["missingLinks"].clone(), r["warnings"].clone()), (Some(1), json!([]), json!([])), "{r}");
|
||||
let root = dir.0.join("out/poster Folder");
|
||||
assert_eq!(std::fs::read(root.join("Links/seal.eps")).unwrap(), eps, "copied as it is");
|
||||
// The packaged document shows the copy.
|
||||
std::fs::remove_dir_all(dir.0.join("art")).unwrap();
|
||||
let opened = open(&mut s, &root.join("poster.vectorcraft").to_string_lossy());
|
||||
assert_eq!(opened["missingLinks"], json!([]), "{opened}");
|
||||
assert!(s.doc().unwrap().doc.has_placed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_copies_a_linked_postscript_ai_file_the_document_holds_after_it_is_gone() {
|
||||
let dir = Folder::new("package-placed-ps-ai");
|
||||
let ai = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EndComments\n0 0 1 setrgbcolor\nnewpath 0 0 moveto 100 0 lineto 100 50 lineto 0 50 lineto closepath fill\nshowpage\n%%EOF\n";
|
||||
let src = dir.file("art/badge.ai");
|
||||
write(&src, ai);
|
||||
let mut s = session();
|
||||
let placed = run(&mut s, "file.place", json!({"path": src}));
|
||||
assert_eq!(placed["linked"], true, "{placed}");
|
||||
save(&mut s, &dir.file("poster.vectorcraft"));
|
||||
std::fs::remove_dir_all(dir.0.join("art")).unwrap();
|
||||
let r = run(&mut s, "file.package", json!({"folder": dir.file("out")}));
|
||||
assert_eq!((r["links"].as_u64(), r["missingLinks"].clone()), (Some(1), json!([])), "{r}");
|
||||
let root = dir.0.join("out/poster Folder");
|
||||
assert_eq!(std::fs::read(root.join("Links/badge.ai")).unwrap(), ai, "the bytes the document holds");
|
||||
}
|
||||
|
||||
/// The PostScript bytes a document holds are copied for a placed document whose file name is one
|
||||
/// PostScript is read from, and not under any other name or for a linked image.
|
||||
#[test]
|
||||
fn package_copies_held_postscript_only_for_a_placed_document_with_a_postscript_name() {
|
||||
let dir = Folder::new("package-placed-ps-names");
|
||||
let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EndComments\n1 0 0 setrgbcolor\nnewpath 0 0 moveto 100 0 lineto 100 50 lineto 0 50 lineto closepath fill\nshowpage\n%%EOF\n";
|
||||
let ai = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EndComments\n0 0 1 setrgbcolor\nnewpath 0 0 moveto 100 0 lineto 100 50 lineto 0 50 lineto closepath fill\nshowpage\n%%EOF\n";
|
||||
write(&dir.file("art/seal.eps"), eps);
|
||||
write(&dir.file("art/badge.ai"), ai);
|
||||
write(&dir.file("art/logo.png"), &png(8, 8, RED));
|
||||
let mut s = session();
|
||||
let seal = run(&mut s, "file.place", json!({"path": dir.file("art/seal.eps")}));
|
||||
assert_eq!(seal["linked"], true, "{seal}");
|
||||
let seal = vectorcraft_doc::NodeId(seal["ids"][0].as_u64().unwrap());
|
||||
run(&mut s, "file.place", json!({"path": dir.file("art/badge.ai")}));
|
||||
let logo = place(&mut s, &dir.file("art/logo.png"));
|
||||
let (tool, logo_ai) = (dir.file("art/tool.exe"), dir.file("art/logo.ai"));
|
||||
s.edit("names", |d, _| {
|
||||
let vectorcraft_doc::NodeKind::PlacedDocument(p) = &mut d.node_mut(seal).unwrap().kind else { panic!("not a placed document") };
|
||||
p.link.path = tool.clone();
|
||||
p.link.relative = None;
|
||||
let vectorcraft_doc::NodeKind::Image(im) = &mut d.node_mut(logo).unwrap().kind else { panic!("not an image") };
|
||||
im.link.as_mut().unwrap().path = logo_ai.clone();
|
||||
im.link.as_mut().unwrap().relative = None;
|
||||
let key = im.key.clone();
|
||||
d.images
|
||||
.insert(key, vectorcraft_doc::ImageBlob { mime: "application/postscript".into(), bytes: std::sync::Arc::new(eps.to_vec()), proxy: None });
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
save(&mut s, &dir.file("poster.vectorcraft"));
|
||||
std::fs::remove_dir_all(dir.0.join("art")).unwrap();
|
||||
let r = run(&mut s, "file.package", json!({"folder": dir.file("out")}));
|
||||
assert_eq!(r["links"].as_u64(), Some(1), "{r}");
|
||||
let mut missing: Vec<&str> = r["missingLinks"].as_array().unwrap().iter().filter_map(Value::as_str).collect();
|
||||
missing.sort_unstable();
|
||||
assert_eq!(missing, ["logo.ai", "tool.exe"], "{r}");
|
||||
let links = dir.0.join("out/poster Folder/Links");
|
||||
assert_eq!(std::fs::read(links.join("badge.ai")).unwrap(), ai, "a PostScript .ai under its own name");
|
||||
assert!(!links.join("tool.exe").exists(), "PostScript under a name it isn't read from");
|
||||
assert!(!links.join("logo.ai").exists(), "PostScript held for a linked image");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_copies_the_fonts_of_the_type_on_a_linked_pdf_page() {
|
||||
let dir = Folder::new("package-placed-pdf-type");
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
//! Pathfinder on clip groups (#8093): a clip group's members take part only where its clipping
|
||||
//! path shows them, so Divide and the other operations leave out the hidden art.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::NodeKind;
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
|
||||
use crate::*;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn id(v: &Value) -> NodeId {
|
||||
NodeId(v["id"].as_u64().unwrap_or_else(|| panic!("no id in {v}")))
|
||||
}
|
||||
|
||||
fn rect(s: &mut Session, x: f64, y: f64, w: f64, h: f64, fill: Color) -> NodeId {
|
||||
let r = id(&s.execute("shape.rectangle", &json!({"x": x, "y": y, "width": w, "height": h})).unwrap());
|
||||
s.edit("fill", |d, _| {
|
||||
d.node_mut(r).unwrap().appearance.set_fill(Paint::solid(fill));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
r
|
||||
}
|
||||
|
||||
fn clip(s: &mut Session, ids: &[NodeId]) -> NodeId {
|
||||
id(&s.execute("object.clippingMask.make", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap())
|
||||
}
|
||||
|
||||
fn red() -> Color {
|
||||
Color::rgb(1.0, 0.0, 0.0)
|
||||
}
|
||||
|
||||
fn blue() -> Color {
|
||||
Color::rgb(0.0, 0.0, 1.0)
|
||||
}
|
||||
|
||||
fn green() -> Color {
|
||||
Color::rgb(0.0, 1.0, 0.0)
|
||||
}
|
||||
|
||||
/// A red square at (0, 0) and a blue one at (50, 50), both 100 pt, clipped by a 100 pt square
|
||||
/// mask at (25, 25): the clip group. The mask shows 8750 pt² of them.
|
||||
fn clipped_pair(s: &mut Session) -> NodeId {
|
||||
let a = rect(s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let b = rect(s, 50.0, 50.0, 100.0, 100.0, blue());
|
||||
let mask = rect(s, 25.0, 25.0, 100.0, 100.0, green());
|
||||
clip(s, &[a, b, mask])
|
||||
}
|
||||
|
||||
/// The shapes `op` makes of the selection, the members of the group it makes or the one shape:
|
||||
/// (fill, path, rule) each.
|
||||
fn pieces(s: &mut Session, op: &str) -> Vec<(Paint, PathData, FillRule)> {
|
||||
let r = s.execute(op, &json!({})).unwrap();
|
||||
let g = s.doc().unwrap().doc.node(NodeId(r["ids"][0].as_u64().unwrap())).cloned().unwrap();
|
||||
let shape = |n: &vectorcraft_doc::Node| match &n.kind {
|
||||
NodeKind::Path { path, rule, .. } => (n.appearance.fill_paint(), path.clone(), *rule),
|
||||
NodeKind::Compound { children, rule } => {
|
||||
let subs = children.iter().flat_map(|c| c.path_data().unwrap().subpaths.clone()).collect();
|
||||
(n.appearance.fill_paint(), PathData::new(subs), *rule)
|
||||
}
|
||||
k => panic!("not a path: {k:?}"),
|
||||
};
|
||||
match &g.kind {
|
||||
NodeKind::Group { children, .. } => children.iter().map(|c| shape(c)).collect(),
|
||||
_ => vec![shape(&g)],
|
||||
}
|
||||
}
|
||||
|
||||
fn area(p: &(Paint, PathData, FillRule)) -> f64 {
|
||||
vectorcraft_pathops::area(&p.1, p.2)
|
||||
}
|
||||
|
||||
fn bounds(ps: &[(Paint, PathData, FillRule)]) -> Rect {
|
||||
ps.iter().filter_map(|p| p.1.bounds()).reduce(|a, b| a.union(b)).unwrap()
|
||||
}
|
||||
|
||||
fn select(s: &mut Session, ids: &[NodeId]) {
|
||||
s.execute("select.set", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
|
||||
}
|
||||
|
||||
fn assert_inside(r: Rect, within: Rect) {
|
||||
assert!(within.inflate(1e-6, 1e-6).contains_rect(r), "{r:?} reaches out of {within:?}");
|
||||
}
|
||||
|
||||
/// The issue's case: Divide on a clip group gives the pieces the mask shows, cut along it, and the
|
||||
/// hidden art goes.
|
||||
#[test]
|
||||
fn divide_on_a_clip_group_keeps_only_what_the_mask_shows() {
|
||||
let mut s = session();
|
||||
let g = clipped_pair(&mut s);
|
||||
select(&mut s, &[g]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.divide");
|
||||
assert_eq!(ps.len(), 3, "red alone, the overlap and blue alone: {ps:?}");
|
||||
let total: f64 = ps.iter().map(area).sum();
|
||||
assert!((total - 8750.0).abs() < 1.0, "the visible area, not the whole art: {total}");
|
||||
assert_inside(bounds(&ps), Rect::new(25.0, 25.0, 125.0, 125.0));
|
||||
let blue: f64 = ps.iter().filter(|p| p.0 == Paint::solid(blue())).map(area).sum();
|
||||
assert!((blue - 5625.0).abs() < 1.0, "the overlap takes the front (blue) fill: {blue}");
|
||||
assert!(ps.iter().all(|p| p.0 != Paint::solid(green())), "the clipping path adds no piece: {ps:?}");
|
||||
}
|
||||
|
||||
/// An irregular mask: a square clipped by a circle divides into a disc.
|
||||
#[test]
|
||||
fn divide_cuts_along_a_curved_clipping_path() {
|
||||
let mut s = session();
|
||||
let square = rect(&mut s, 100.0, 100.0, 300.0, 300.0, red());
|
||||
let circle = id(&s.execute("shape.ellipse", &json!({"x": 150, "y": 150, "width": 200, "height": 200})).unwrap());
|
||||
let g = clip(&mut s, &[square, circle]);
|
||||
select(&mut s, &[g]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.divide");
|
||||
assert_eq!(ps.len(), 1, "{ps:?}");
|
||||
let disc = std::f64::consts::PI * 100.0 * 100.0;
|
||||
assert!((area(&ps[0]) - disc).abs() < disc * 1e-3, "{} vs {disc}", area(&ps[0]));
|
||||
assert_inside(bounds(&ps), Rect::new(150.0, 150.0, 350.0, 350.0));
|
||||
}
|
||||
|
||||
/// A clip group inside a clip group: its members show only where both masks do.
|
||||
#[test]
|
||||
fn nested_clip_groups_cut_by_both_masks() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 200.0, 200.0, red());
|
||||
let inner_mask = rect(&mut s, 0.0, 0.0, 100.0, 200.0, green());
|
||||
let inner = clip(&mut s, &[a, inner_mask]);
|
||||
let outer_mask = rect(&mut s, 0.0, 50.0, 200.0, 100.0, green());
|
||||
let outer = clip(&mut s, &[inner, outer_mask]);
|
||||
select(&mut s, &[outer]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.divide");
|
||||
assert_eq!(ps.len(), 1, "{ps:?}");
|
||||
assert!((area(&ps[0]) - 10000.0).abs() < 1.0, "100 × 100 shows: {}", area(&ps[0]));
|
||||
assert_inside(bounds(&ps), Rect::new(0.0, 50.0, 100.0, 150.0));
|
||||
}
|
||||
|
||||
/// A member the mask hides altogether gives no piece.
|
||||
#[test]
|
||||
fn a_member_the_mask_hides_gives_no_piece() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let b = rect(&mut s, 300.0, 300.0, 100.0, 100.0, blue());
|
||||
let mask = rect(&mut s, 0.0, 0.0, 150.0, 150.0, green());
|
||||
let g = clip(&mut s, &[a, b, mask]);
|
||||
select(&mut s, &[g]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.divide");
|
||||
assert_eq!(ps.len(), 1, "only the red square shows: {ps:?}");
|
||||
assert_eq!(ps[0].0, Paint::solid(red()));
|
||||
}
|
||||
|
||||
/// A cutting line in a clip group cuts only where the mask shows it: its hidden part leaves the
|
||||
/// other selected art alone.
|
||||
#[test]
|
||||
fn a_clipped_cutting_line_cuts_only_where_it_shows() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let line = id(&s.execute("shape.line", &json!({"x1": 50, "y1": -50, "x2": 50, "y2": 400})).unwrap());
|
||||
let mask = rect(&mut s, 0.0, 0.0, 100.0, 100.0, green());
|
||||
let g = clip(&mut s, &[a, line, mask]);
|
||||
let other = rect(&mut s, 0.0, 200.0, 100.0, 100.0, blue());
|
||||
select(&mut s, &[g, other]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.divide");
|
||||
let red = ps.iter().filter(|p| p.0 == Paint::solid(red())).count();
|
||||
let blue = ps.iter().filter(|p| p.0 == Paint::solid(blue())).count();
|
||||
assert_eq!((red, blue), (2, 1), "the line halves the red square, not the blue one: {ps:?}");
|
||||
}
|
||||
|
||||
/// Every Shape Mode and Pathfinder button takes the members as the mask shows them (#3367 asks
|
||||
/// this of Merge). The mask, 100 × 60 pt at (25, 25), cuts through the squares' overlap: it shows
|
||||
/// 4500 pt² of red, 2625 pt² of blue and 1750 pt² of their overlap.
|
||||
#[test]
|
||||
fn every_operation_takes_only_what_the_mask_shows() {
|
||||
for (op, want) in [
|
||||
("unite", Some(5375.0)),
|
||||
("minusFront", Some(2750.0)),
|
||||
("minusBack", Some(875.0)),
|
||||
("intersect", Some(1750.0)),
|
||||
("exclude", Some(3625.0)),
|
||||
("trim", Some(5375.0)),
|
||||
("merge", Some(5375.0)),
|
||||
("crop", Some(1750.0)),
|
||||
("outline", None),
|
||||
] {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let b = rect(&mut s, 50.0, 50.0, 100.0, 100.0, blue());
|
||||
let mask = rect(&mut s, 25.0, 25.0, 100.0, 60.0, green());
|
||||
let g = clip(&mut s, &[a, b, mask]);
|
||||
select(&mut s, &[g]);
|
||||
let ps = pieces(&mut s, &format!("object.pathfinder.{op}"));
|
||||
assert_inside(bounds(&ps), Rect::new(25.0, 25.0, 125.0, 85.0));
|
||||
if let Some(want) = want {
|
||||
let total: f64 = ps.iter().map(area).sum();
|
||||
assert!((total - want).abs() < 1.0, "{op}: {total}, want {want}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Among other objects a clip group acts as one shape: the union of what its mask shows.
|
||||
#[test]
|
||||
fn a_clip_group_among_other_objects_is_the_area_its_mask_shows() {
|
||||
let mut s = session();
|
||||
let g = clipped_pair(&mut s);
|
||||
let other = rect(&mut s, 300.0, 0.0, 50.0, 50.0, blue());
|
||||
select(&mut s, &[g, other]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.unite");
|
||||
let total: f64 = ps.iter().map(area).sum();
|
||||
assert!((total - (8750.0 + 2500.0)).abs() < 1.0, "{total}");
|
||||
}
|
||||
|
||||
/// Among other objects a clip group takes the style of its top member that shows: a member the
|
||||
/// mask hides paints nothing.
|
||||
#[test]
|
||||
fn a_hidden_top_member_gives_no_style() {
|
||||
let mut s = session();
|
||||
// Below the clip group, so the result takes the group's style.
|
||||
let other = rect(&mut s, 50.0, 50.0, 100.0, 100.0, green());
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let b = rect(&mut s, 300.0, 300.0, 100.0, 100.0, blue());
|
||||
let mask = rect(&mut s, 0.0, 0.0, 150.0, 150.0, green());
|
||||
let g = clip(&mut s, &[a, b, mask]);
|
||||
select(&mut s, &[other, g]);
|
||||
let ps = pieces(&mut s, "object.pathfinder.unite");
|
||||
assert_eq!(ps.len(), 1, "{ps:?}");
|
||||
assert_eq!(ps[0].0, Paint::solid(red()), "not the hidden blue member's fill");
|
||||
assert!((area(&ps[0]) - 17500.0).abs() < 1.0, "{}", area(&ps[0]));
|
||||
}
|
||||
|
||||
/// A selection the masks hide altogether says so.
|
||||
#[test]
|
||||
fn art_the_masks_hide_altogether_is_refused_with_a_reason() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 300.0, 300.0, 100.0, 100.0, red());
|
||||
let mask = rect(&mut s, 0.0, 0.0, 100.0, 100.0, green());
|
||||
let g = clip(&mut s, &[a, mask]);
|
||||
select(&mut s, &[g]);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let e = s.execute("object.pathfinder.divide", &json!({})).unwrap_err().to_string();
|
||||
assert!(e.contains("clipping masks hide"), "{e}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing changes");
|
||||
}
|
||||
|
||||
/// A clip group its mask hides altogether, selected with a red square at (0, 0) and a blue one at
|
||||
/// (50, 50), each 100 pt, at `position` in the stacking order (0: at the back).
|
||||
fn with_a_hidden_clip_group(s: &mut Session, position: usize) {
|
||||
let mut ids = vec![];
|
||||
for i in 0..3 {
|
||||
if i == position {
|
||||
// Two members, so among other objects the group is one shape: their union.
|
||||
let a = rect(s, 300.0, 300.0, 100.0, 100.0, green());
|
||||
let b = rect(s, 450.0, 300.0, 100.0, 100.0, green());
|
||||
let mask = rect(s, 0.0, 0.0, 100.0, 100.0, green());
|
||||
ids.push(clip(s, &[a, b, mask]));
|
||||
} else {
|
||||
let at = if ids.len() == usize::from(position == 0) { 0.0 } else { 50.0 };
|
||||
ids.push(rect(s, at, at, 100.0, 100.0, if at == 0.0 { red() } else { blue() }));
|
||||
}
|
||||
}
|
||||
select(s, &ids);
|
||||
}
|
||||
|
||||
/// A selected object its masks hide altogether is an empty operand: Intersect, Minus Front with
|
||||
/// it at the back, Minus Back with it in front and Crop with it on top give nothing, and the other
|
||||
/// operations make what they make without it.
|
||||
#[test]
|
||||
fn a_hidden_operand_is_an_empty_shape_where_it_changes_the_result() {
|
||||
for (op, position) in [("intersect", 1), ("minusFront", 0), ("minusBack", 2), ("crop", 2)] {
|
||||
let mut s = session();
|
||||
with_a_hidden_clip_group(&mut s, position);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let e = s.execute(&format!("object.pathfinder.{op}"), &json!({})).unwrap_err().to_string();
|
||||
assert!(e.contains("empty result"), "{op}: {e}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{op}: nothing changes");
|
||||
}
|
||||
for (op, position, want) in [("unite", 2, 17500.0), ("exclude", 2, 15000.0), ("minusFront", 2, 7500.0), ("minusBack", 0, 7500.0)] {
|
||||
let mut s = session();
|
||||
with_a_hidden_clip_group(&mut s, position);
|
||||
let ps = pieces(&mut s, &format!("object.pathfinder.{op}"));
|
||||
let total: f64 = ps.iter().map(area).sum();
|
||||
assert!((total - want).abs() < 1.0, "{op}: {total}, want {want}");
|
||||
assert!(ps.iter().all(|p| p.0 != Paint::solid(green())), "{op}: the hidden group's style: {ps:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A clip group whose clipping path clips by nothing (here an empty group) hides its members, as on
|
||||
/// the canvas: they are a hidden operand too, not left out of the selection.
|
||||
#[test]
|
||||
fn a_clipping_path_that_clips_by_nothing_hides_the_members() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0, red());
|
||||
let b = rect(&mut s, 50.0, 50.0, 100.0, 100.0, blue());
|
||||
let m1 = rect(&mut s, 300.0, 300.0, 100.0, 100.0, green());
|
||||
let m2 = rect(&mut s, 450.0, 300.0, 100.0, 100.0, green());
|
||||
let mask = rect(&mut s, 300.0, 300.0, 300.0, 100.0, green());
|
||||
let g = clip(&mut s, &[m1, m2, mask]);
|
||||
s.edit("empty clipping path", |d, _| {
|
||||
let empty = vectorcraft_doc::Node::group(d.alloc_id(), vec![]);
|
||||
let NodeKind::Group { children, .. } = &mut d.node_mut(g).unwrap().kind else { panic!("not a group") };
|
||||
children[0] = std::sync::Arc::new(empty);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
select(&mut s, &[a, b, g]);
|
||||
let e = s.execute("object.pathfinder.intersect", &json!({})).unwrap_err().to_string();
|
||||
assert!(e.contains("empty result"), "not the squares' overlap: {e}");
|
||||
select(&mut s, &[g]);
|
||||
let e = s.execute("object.pathfinder.divide", &json!({})).unwrap_err().to_string();
|
||||
assert!(e.contains("clipping masks hide"), "{e}");
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
//! live objects and single fills and strokes are written with images of their shadows, glows and
|
||||
//! blurs (no "left out" warning), and the PDF reads back looking like the canvas.
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
@@ -38,7 +37,41 @@ fn with(mut n: Node, fx: Effect) -> Node {
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_fn(w, h, |x, _| image::Rgba([(x * 255 / w) as u8, 60, 160, 255]));
|
||||
let mut out = Vec::new();
|
||||
image::DynamicImage::from(img).write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::from(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,41 @@ fn session() -> Session {
|
||||
/// A `w`×`h` PNG of one colour (`rgba`), declaring `ppi` when given.
|
||||
fn png(w: u32, h: u32, rgba: [u8; 4], ppi: Option<f64>) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
image::RgbaImage::from_pixel(w, h, image::Rgba(rgba)).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(w, h, image::Rgba(rgba))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
match ppi {
|
||||
Some(r) => cmd::fileio::ppi::with_png_resolution(&out, (r, r)),
|
||||
None => out,
|
||||
@@ -482,13 +516,46 @@ fn keeping_an_image_decodes_nothing() {
|
||||
}
|
||||
|
||||
/// `w`×`h` pixels of noise, which compress little, encoded as `format`.
|
||||
fn noise(w: u32, h: u32, format: image::ImageFormat) -> Vec<u8> {
|
||||
fn noise(w: u32, h: u32, format: craft_image_codecs::Format) -> Vec<u8> {
|
||||
let img = image::RgbImage::from_fn(w, h, |x, y| {
|
||||
let v = x.wrapping_mul(2_654_435_761) ^ y.wrapping_mul(40_503).rotate_left(13);
|
||||
image::Rgb([v as u8, (v >> 8) as u8, (v >> 16) as u8])
|
||||
});
|
||||
let mut out = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), format).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, format, &craft_image_codecs::EncodeOptions::default()).map(|__b| out.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
@@ -497,7 +564,7 @@ fn noise(w: u32, h: u32, format: image::ImageFormat) -> Vec<u8> {
|
||||
/// cut short still decodes, with its missing rows empty, and is placed.
|
||||
#[test]
|
||||
fn an_image_whose_pixels_cant_be_read_is_not_placed() {
|
||||
let mut cut = noise(600, 300, image::ImageFormat::Png);
|
||||
let mut cut = noise(600, 300, craft_image_codecs::Format::Png);
|
||||
cut.truncate(cut.len() / 2);
|
||||
let mut s = session();
|
||||
let info = s.execute("file.place.info", &file("cut.png", &cut)).unwrap();
|
||||
@@ -513,7 +580,7 @@ fn an_image_whose_pixels_cant_be_read_is_not_placed() {
|
||||
assert!(top_children(&s).is_empty(), "{p}: nothing placed");
|
||||
assert_eq!((s.doc().unwrap().history.undo.len(), s.journal.len()), (undo, journal), "{p}");
|
||||
}
|
||||
let mut jpeg = noise(600, 300, image::ImageFormat::Jpeg);
|
||||
let mut jpeg = noise(600, 300, craft_image_codecs::Format::Jpeg);
|
||||
jpeg.truncate(jpeg.len() / 2);
|
||||
let r = place(&mut s, "cut.jpg", &jpeg, json!({}));
|
||||
assert_eq!(r["format"], "jpg");
|
||||
@@ -894,7 +961,41 @@ fn placed_art_brings_its_brushes_renaming_on_conflict() {
|
||||
fn a_placed_brush_whose_art_uses_a_renamed_image_is_copied() {
|
||||
let file = |pixel: u8| {
|
||||
let mut png = vec![];
|
||||
image::GrayImage::from_pixel(2, 1, image::Luma([pixel])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(2, 1, image::Luma([pixel]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let mut s = session();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
s.edit("Brush", |d, _| {
|
||||
@@ -942,7 +1043,41 @@ fn a_placed_brush_whose_art_uses_a_renamed_image_is_copied() {
|
||||
fn info_reports_size_resolution_colour_mode_and_link() {
|
||||
let mut s = session();
|
||||
let mut gray = vec![];
|
||||
image::GrayImage::from_pixel(300, 150, image::Luma([128])).write_to(&mut std::io::Cursor::new(&mut gray), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(300, 150, image::Luma([128]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| gray.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let gray = cmd::fileio::ppi::with_png_resolution(&gray, (300.0, 300.0));
|
||||
let mut p = file("scan.png", &gray);
|
||||
p["thumbnail"] = json!(16);
|
||||
@@ -950,7 +1085,23 @@ fn info_reports_size_resolution_colour_mode_and_link() {
|
||||
assert_eq!((info["colorMode"].as_str(), info["pixelWidth"].as_u64()), (Some("Grayscale"), Some(300)));
|
||||
assert_eq!(info["ppi"], json!([300.0, 300.0]));
|
||||
assert!(close(info["width"].as_f64().unwrap(), 72.0));
|
||||
let thumb = image::load_from_memory(&vectorcraft_format::base64_decode(info["thumbnailBase64"].as_str().unwrap()).unwrap()).unwrap();
|
||||
let thumb = craft_image_codecs::decode(&vectorcraft_format::base64_decode(info["thumbnailBase64"].as_str().unwrap()).unwrap())
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((thumb.width(), thumb.height()), (16, 8));
|
||||
assert!(top_children(&s).is_empty(), "info places nothing");
|
||||
// A path is linked by default.
|
||||
@@ -1090,7 +1241,7 @@ fn files_read_for_the_place_cursor_apart_from_the_session_load_it_as_file_place_
|
||||
/// queue, read with the session or apart from it.
|
||||
#[test]
|
||||
fn the_place_queue_skips_an_image_whose_pixels_cant_be_read() {
|
||||
let mut cut = noise(600, 300, image::ImageFormat::Png);
|
||||
let mut cut = noise(600, 300, craft_image_codecs::Format::Png);
|
||||
cut.truncate(cut.len() / 2);
|
||||
let p = json!({"files": [file("a.png", &png(40, 20, [9, 9, 9, 255], None)), file("cut.png", &cut)], "link": false});
|
||||
let (mut a, mut b) = (session(), session());
|
||||
@@ -1137,7 +1288,41 @@ fn the_place_queue_draws_a_thumbnail_without_decoding_the_image_again() {
|
||||
// Pixels no other test uses, since every test shares the image store.
|
||||
let img = image::RgbImage::from_fn(320, 160, |x, y| image::Rgb([(x % 251) as u8, 23, (y % 241) as u8]));
|
||||
let mut bytes = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| bytes.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let p = json!({"files": [file("q.png", &bytes)], "link": false, "thumbnail": 64});
|
||||
let decodes = vectorcraft_render::image_decodes();
|
||||
let r = session().execute("file.place.queue", &p).unwrap();
|
||||
|
||||
@@ -152,7 +152,26 @@ fn a_placed_document_is_vectors_in_every_output() {
|
||||
assert_eq!(images, 0, "no image in the PDF");
|
||||
assert!(paths > 0);
|
||||
let png = decode(&s.execute("document.serialize", &json!({"format": "png"})).unwrap());
|
||||
let [r, g, b, _] = image::load_from_memory(&png).unwrap().to_rgba8().get_pixel(200, 150).0;
|
||||
let [r, g, b, _] = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(200, 150)
|
||||
.0;
|
||||
assert!(near([r, g, b], RED), "{:?}", [r, g, b]);
|
||||
for format in ["eps", "emf", "dxf"] {
|
||||
let out = s.execute("document.serialize", &json!({"format": format})).unwrap();
|
||||
@@ -192,7 +211,26 @@ fn the_preview_is_saved_so_the_document_shows_it_without_its_file() {
|
||||
// Output with the file gone: the preview, with a warning.
|
||||
let png = s.execute("document.serialize", &json!({"format": "png"})).unwrap();
|
||||
assert!(png["warnings"].to_string().contains("1 placed document output as its preview"), "{png}");
|
||||
let [r, g, b, _] = image::load_from_memory(&decode(&png)).unwrap().to_rgba8().get_pixel(200, 150).0;
|
||||
let [r, g, b, _] = craft_image_codecs::decode(&decode(&png))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
.get_pixel(200, 150)
|
||||
.0;
|
||||
assert!(near([r, g, b], RED), "{:?}", [r, g, b]);
|
||||
// Saved for older apps: the art it shows (here its preview), as plain objects.
|
||||
let old: Value =
|
||||
@@ -888,6 +926,190 @@ fn a_pdf_compatible_ai_file_places_linked_like_a_pdf() {
|
||||
assert_eq!(images, 0);
|
||||
}
|
||||
|
||||
/// An EPS file written by hand at `path`: a 100×50 bounding box holding a 40×20 rectangle of `rgb`
|
||||
/// from (20, 10), and `extra` comment lines (which change the file's size).
|
||||
fn eps_source(path: &str, rgb: [u8; 3], extra: usize) {
|
||||
let [r, g, b] = rgb.map(|c| c as f64 / 255.0);
|
||||
let mut ps = format!(
|
||||
"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EndComments\n{r} {g} {b} setrgbcolor\nnewpath 20 10 moveto 60 10 lineto 60 30 lineto 20 30 lineto closepath fill\n"
|
||||
);
|
||||
for i in 0..extra {
|
||||
ps.push_str(&format!("% line {i}\n"));
|
||||
}
|
||||
ps.push_str("showpage\n%%EOF\n");
|
||||
write(path, ps.as_bytes());
|
||||
}
|
||||
|
||||
/// An EPS file at `path` written by VectorCraft's EPS export: a 100×50 artboard filled with `rgb`
|
||||
/// (`extra` more rectangles change the file's size).
|
||||
fn exported_eps(path: &str, rgb: [u8; 3], extra: usize) {
|
||||
let mut d = Document::new(100.0, 50.0);
|
||||
let layer = d.layers[0].id;
|
||||
for _ in 0..=extra {
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(layer), usize::MAX, Node::path(id, shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 50.0)), look(rgb))).unwrap();
|
||||
}
|
||||
write(path, &cmd::fileio::encode_all(&d, "eps", &json!({})).unwrap().files[0].1);
|
||||
}
|
||||
|
||||
/// The kinds of `n` and everything inside it, in order.
|
||||
fn kinds(n: &Node) -> Vec<&'static str> {
|
||||
let mut k = vec![];
|
||||
n.walk(&mut |c| k.push(c.kind_label()));
|
||||
k
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_link_an_eps_file_places_linked_framed_as_without_link() {
|
||||
let dir = Folder::new("placed-eps-linked");
|
||||
let src = dir.file("badge.eps");
|
||||
eps_source(&src, RED, 0);
|
||||
let mut s = session();
|
||||
let embedded = |s: &mut Session, crop: &str| {
|
||||
let r = s.execute("file.place", &json!({"path": src, "link": false, "crop": crop, "at": [200, 150]})).unwrap();
|
||||
assert_eq!(r["linked"], false, "{r}");
|
||||
let n = s.doc().unwrap().doc.node(NodeId(r["ids"][0].as_u64().unwrap())).unwrap().clone();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
n
|
||||
};
|
||||
let copy = embedded(&mut s, "crop");
|
||||
let id = place(&mut s, &src);
|
||||
let p = placed(&s, id);
|
||||
assert_eq!((p.link.path.as_str(), p.link.page, p.bounding), (src.as_str(), Some(1), false), "{p:?}");
|
||||
assert!((p.width - 100.0).abs() < 1e-6 && (p.height - 50.0).abs() < 1e-6, "{p:?}");
|
||||
assert_eq!(Some(bounds(&s, id)), copy.geometric_bounds(), "the bounding box, as without Link");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED));
|
||||
let row = &s.execute("links.list", &json!({})).unwrap()["links"][0];
|
||||
assert_eq!((row["format"].as_str(), row["document"].as_bool(), row["status"].as_str()), (Some("EPS"), Some(true), Some("ok")), "{row}");
|
||||
assert_eq!(s.doc().unwrap().doc.images[&p.key].mime, "application/postscript");
|
||||
// Break Link gives the group placing it without Link gives.
|
||||
let r = s.execute("links.embed", &json!({"ids": [id.0]})).unwrap();
|
||||
assert_eq!(r["embedded"], json!([id.0]), "{r}");
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
assert_eq!((kinds(&n), n.geometric_bounds()), (kinds(©), copy.geometric_bounds()));
|
||||
assert!(!s.doc().unwrap().doc.has_placed() && near(centre_colour(&s.doc().unwrap().doc), RED));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// Bounding Box: the art's bounds, as without Link.
|
||||
let copy = embedded(&mut s, "bounding");
|
||||
let r = s.execute("file.place", &json!({"path": src, "crop": "bounding", "at": [200, 150]})).unwrap();
|
||||
assert_eq!(r["linked"], true, "{r}");
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let p = placed(&s, id);
|
||||
assert!(p.bounding && (p.width - 40.0).abs() < 1e-6 && (p.height - 20.0).abs() < 1e-6, "{p:?}");
|
||||
assert_eq!(Some(bounds(&s, id)), copy.geometric_bounds());
|
||||
// Object › Expand gives it too (the placed object is selected).
|
||||
let r = s.execute("object.expand", &json!({})).unwrap();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
assert_eq!((kinds(&n), n.geometric_bounds()), (kinds(©), copy.geometric_bounds()), "{r}");
|
||||
// Placed from its bytes, it is embedded.
|
||||
let data = vectorcraft_format::base64_encode(&std::fs::read(&src).unwrap());
|
||||
let r = s.execute("file.place", &json!({"name": "badge.eps", "dataBase64": data})).unwrap();
|
||||
assert_eq!(r["linked"], false, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_changed_eps_file_is_modified_and_update_links_reads_it_again() {
|
||||
let dir = Folder::new("placed-eps-update");
|
||||
let src = dir.file("flyer.eps");
|
||||
exported_eps(&src, RED, 0);
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &src);
|
||||
exported_eps(&src, BLUE, 1);
|
||||
assert_eq!(s.execute("links.check", &json!({})).unwrap()["links"][0]["status"], "modified");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED), "shows the file as it read it");
|
||||
let u = s.execute("links.update", &json!({})).unwrap();
|
||||
assert_eq!(u["updated"], json!([id.0]), "{u}");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), BLUE));
|
||||
assert_eq!(s.execute("links.check", &json!({})).unwrap()["modified"], 0);
|
||||
// Relinked to another EPS file, it shows that file's art.
|
||||
let other = dir.file("other.eps");
|
||||
eps_source(&other, RED, 0);
|
||||
let r = s.execute("links.relink", &json!({"ids": [id.0], "path": other})).unwrap();
|
||||
assert_eq!(r["relinked"], json!([id.0]), "{r}");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED));
|
||||
// A file it can't show is refused.
|
||||
let notes = dir.file("notes.txt");
|
||||
write(¬es, b"not a page");
|
||||
let e = s.execute("links.relink", &json!({"ids": [id.0], "path": notes})).unwrap_err().to_string();
|
||||
assert!(e.contains("isn't a VectorCraft document"), "{e}");
|
||||
assert_eq!(placed(&s, id).link.path, other);
|
||||
// Its file replaced by one that isn't PostScript: updating keeps the art it shows.
|
||||
write(&other, b"not a page");
|
||||
let _ = s.execute("links.update", &json!({}));
|
||||
assert_eq!(placed(&s, id).link.path, other);
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linked_eps_file_outputs_as_vectors_and_shows_its_preview_without_its_file() {
|
||||
let dir = Folder::new("placed-eps-outputs");
|
||||
let src = dir.file("seal.eps");
|
||||
// Content of its own: files read are cached by content, and this one is removed.
|
||||
eps_source(&src, RED, 3);
|
||||
let bytes = std::fs::read(&src).unwrap();
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &src);
|
||||
let doc_path = dir.file("poster.vectorcraft");
|
||||
save(&mut s, &doc_path);
|
||||
// Without its file: missing, shown and output as its preview, and not embedded.
|
||||
std::fs::remove_file(&src).unwrap();
|
||||
open(&mut s, &doc_path);
|
||||
let key = placed(&s, id).key;
|
||||
assert!(s.doc().unwrap().doc.images[&key].is_proxy(), "opened with the preview only");
|
||||
let row = &s.execute("links.list", &json!({})).unwrap()["links"][0];
|
||||
assert_eq!((row["format"].as_str(), row["status"].as_str()), (Some("EPS"), Some("missing")), "{row}");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED), "the preview");
|
||||
let pdf = s.execute("document.serialize", &json!({"format": "pdf"})).unwrap();
|
||||
assert!(pdf["warnings"].to_string().contains("1 placed document output as its preview"), "{}", pdf["warnings"]);
|
||||
let r = s.execute("links.embed", &json!({"ids": [id.0]})).unwrap();
|
||||
assert_eq!((r["embedded"].clone(), r["missing"].clone()), (json!([]), json!([id.0])), "{r}");
|
||||
assert!(s.doc().unwrap().doc.has_placed());
|
||||
// The file back: output reads it again, its paths and not the preview.
|
||||
write(&src, &bytes);
|
||||
assert_eq!(s.execute("links.check", &json!({})).unwrap()["links"][0]["status"], "ok");
|
||||
let pdf = s.execute("document.serialize", &json!({"format": "pdf"})).unwrap();
|
||||
assert_eq!(pdf["warnings"], json!([]), "{}", pdf["warnings"]);
|
||||
let (images, paths) = pdf_contents(&decode(&pdf));
|
||||
assert_eq!(images, 0, "no image in the PDF");
|
||||
assert!(paths > 0);
|
||||
// Include Linked Files keeps the EPS file itself.
|
||||
let full = file_json(&decode(&s.execute("document.serialize", &json!({"format": "vectorcraft", "includeLinked": true})).unwrap()));
|
||||
assert_eq!((full["images"][&key]["proxy"].clone(), full["images"][&key]["mime"].as_str()), (Value::Null, Some("application/postscript")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_postscript_ai_file_places_linked_like_an_eps_file() {
|
||||
let dir = Folder::new("placed-ps-ai-linked");
|
||||
let src = dir.file("badge.ai");
|
||||
eps_source(&src, RED, 0);
|
||||
let mut s = session();
|
||||
let id = place(&mut s, &src);
|
||||
assert_eq!(placed(&s, id).link.path, src);
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), RED));
|
||||
let row = &s.execute("links.list", &json!({})).unwrap()["links"][0];
|
||||
assert_eq!(row["format"], "PostScript", "{row}");
|
||||
eps_source(&src, BLUE, 5);
|
||||
let u = s.execute("links.update", &json!({})).unwrap();
|
||||
assert_eq!(u["updated"], json!([id.0]), "{u}");
|
||||
assert!(near(centre_colour(&s.doc().unwrap().doc), BLUE));
|
||||
// Saved with its preview only and reopened without its file, it is still listed as PostScript.
|
||||
let bytes = std::fs::read(&src).unwrap();
|
||||
let doc_path = dir.file("poster.vectorcraft");
|
||||
save(&mut s, &doc_path);
|
||||
std::fs::remove_file(&src).unwrap();
|
||||
open(&mut s, &doc_path);
|
||||
assert!(s.doc().unwrap().doc.images[&placed(&s, id).key].is_proxy(), "opened with the preview only");
|
||||
let row = &s.execute("links.list", &json!({})).unwrap()["links"][0];
|
||||
assert_eq!((row["format"].as_str(), row["status"].as_str()), (Some("PostScript"), Some("missing")), "{row}");
|
||||
write(&src, &bytes);
|
||||
// The same bytes named .eps are read as an EPS file: a file of their own, not the .ai's art.
|
||||
let eps = dir.file("badge.eps");
|
||||
write(&eps, &std::fs::read(&src).unwrap());
|
||||
let other = place(&mut s, &eps);
|
||||
assert_ne!(placed(&s, other).key, placed(&s, id).key);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_placed_document_inside_a_placed_document_outputs_from_its_own_file() {
|
||||
let dir = Folder::new("placed-nested");
|
||||
|
||||
@@ -1157,9 +1157,41 @@ fn pathfinder_takes_swatch_linked_pieces_of_type_in_a_reshaped_group() {
|
||||
fn an_image_s_painted_fill_is_hit_and_measured_as_drawn() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
let im = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
run(&mut s, "swatch.new", json!({"name": "Leaf", "color": "#00ff00"}));
|
||||
|
||||
@@ -171,7 +171,24 @@ fn print_as_bitmap_honours_rich_black() {
|
||||
}
|
||||
});
|
||||
let bytes = &prepared.images[key.as_ref().expect("bitmap image")].bytes;
|
||||
let img = image::load_from_memory(bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
img.get_pixel(img.width() / 2, img.height() / 2).0
|
||||
};
|
||||
let accurate = centre(&crate::cmd::printadvanced::prepare("file.print", doc(&s), &set, &saved, false).unwrap().unwrap());
|
||||
|
||||
@@ -287,7 +287,41 @@ fn rings_png() -> Vec<u8> {
|
||||
image::Rgba([v as u8, v as u8, 255 - v as u8, 255])
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
png
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,24 @@ fn effect_image(doc: &Document) -> image::RgbaImage {
|
||||
let kids = flat.layers[0].children().unwrap();
|
||||
let NodeKind::Group { children, .. } = &kids[0].kind else { panic!("a shadow becomes a group") };
|
||||
let NodeKind::Image(im) = &children[0].kind else { panic!("the image goes under the object") };
|
||||
image::load_from_memory(&flat.images[&im.key].bytes).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(&flat.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -167,7 +184,24 @@ fn the_color_model_and_anti_alias_reach_the_pixels() {
|
||||
let out = s.execute("object.rasterize", &json!({"colorModel": "rgb"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id(&out)).unwrap().kind else { panic!("an image") };
|
||||
let img = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert!(img.pixels().all(|p| p[3] == 0 || p[3] == 255), "hard edges");
|
||||
assert!(img.pixels().any(|p| p[3] == 0) && img.pixels().any(|p| p[3] == 255));
|
||||
}
|
||||
@@ -184,7 +218,24 @@ fn rasterize_takes_the_document_settings_as_defaults() {
|
||||
assert_eq!((out["width"].as_u64(), out["height"].as_u64()), (Some(60), Some(60)), "10 pt around");
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(im) = &d.node(id(&out)).unwrap().kind else { panic!("an image") };
|
||||
let img = image::load_from_memory(&d.images[&im.key].bytes).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&d.images[&im.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.get_pixel(0, 0).0, [255, 255, 255, 255], "white around the art");
|
||||
let p = img.get_pixel(30, 30).0;
|
||||
assert!(p[0] == p[1] && p[1] == p[2] && p[0] < 128, "red in grayscale: {p:?}");
|
||||
@@ -395,7 +446,24 @@ fn object_rasterize_takes_in_everything_an_effect_stack_paints() {
|
||||
let made = id(&s.execute("object.rasterize", &json!({"ppi": 72, "background": "transparent"})).unwrap());
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let (width, height, xf, png, _) = rasterized(d, d.node(made).unwrap());
|
||||
let img = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// 41 pt of rectangle and stroke, and 6 pt of blur and 10 pt of white on each side: white from
|
||||
// the first column to the last.
|
||||
assert_eq!((width, xf.translation().x), (73, 34.0));
|
||||
@@ -503,7 +571,24 @@ fn rasterize_to_cmyk_makes_a_cmyk_image() {
|
||||
let mask = masked.extras().mask.as_deref().expect("an opacity mask").clone();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let NodeKind::Image(m) = &mask.art.kind else { panic!("the mask is an image") };
|
||||
let alpha = image::load_from_memory(&d.images[&m.key].bytes).unwrap().to_luma8();
|
||||
let alpha = craft_image_codecs::decode(&d.images[&m.key].bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma8();
|
||||
assert_eq!((alpha.get_pixel(1, 1).0, alpha.get_pixel(25, 25).0), ([0], [255]));
|
||||
assert!(mask.clip);
|
||||
}
|
||||
@@ -530,7 +615,24 @@ fn rasterize_to_grayscale_or_bitmap_makes_a_grayscale_image() {
|
||||
assert!(n.extras().mask.is_none(), "{mode} {model} {background}: no opacity mask");
|
||||
let NodeKind::Image(im) = &n.kind else { panic!("an image") };
|
||||
let blob = d.images[&im.key].clone();
|
||||
let pixels = image::load_from_memory(&blob.bytes).unwrap().to_luma_alpha8();
|
||||
let pixels = craft_image_codecs::decode(&blob.bytes)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_luma_alpha8();
|
||||
assert_eq!(pixels.dimensions(), (50, 50));
|
||||
let at = |x, y| pixels.get_pixel(x, y).0;
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
|
||||
@@ -1250,7 +1250,41 @@ fn global_adjust_refuses_params_of_the_wrong_form() {
|
||||
fn halves_image(s: &mut Session, w: u32, h: u32, left: [u8; 4], right: [u8; 4]) -> (NodeId, vectorcraft_geom::Rect) {
|
||||
let img = image::RgbaImage::from_fn(w, h, |x, _| image::Rgba(if x < w / 2 { left } else { right }));
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(s, "file.place", json!({"name": "halves.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
@@ -1375,7 +1409,41 @@ fn theme_picker_reads_symbol_art_and_nested_patterns() {
|
||||
fn theme_picker_reads_the_color_of_a_colorized_image() {
|
||||
let mut s = session();
|
||||
let mut png = Vec::new();
|
||||
image::GrayImage::from_pixel(8, 8, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::GrayImage::from_pixel(8, 8, image::Luma([90]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "gray.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
@@ -1430,7 +1498,41 @@ fn theme_picker_reads_an_image_inside_its_clipping_path() {
|
||||
image::Rgba(if corner { [0, 0, 255, 255] } else { [255, 0, 0, 255] })
|
||||
});
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "corners.png", "dataBase64": vectorcraft_format::base64_encode(&png)}));
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let b = s.doc().unwrap().doc.node(id).unwrap().visual_bounds().unwrap();
|
||||
|
||||
@@ -760,7 +760,23 @@ fn eps_save_bounds_large_page_preview_without_cropping_vector_or_editing_data()
|
||||
let (ps, tiff) = vectorcraft_eps::sections(&bytes).unwrap();
|
||||
let tiff = tiff.unwrap();
|
||||
assert!(tiff.len() < 4 * 1024 * 1024 + 64 * 1024, "compatibility preview grew to {} bytes", tiff.len());
|
||||
let preview = image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff).unwrap();
|
||||
let preview = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, tiff)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((preview.width(), preview.height()), (1024, 1024));
|
||||
assert!(std::str::from_utf8(ps).unwrap().contains("%%BoundingBox: 0 0 4000 4000"));
|
||||
let native = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
|
||||
@@ -168,9 +168,41 @@ fn a_click_on_the_drop_shadow_of_a_symbols_art_does_not_select_the_instance() {
|
||||
fn a_click_on_the_drop_shadow_of_an_image_outside_its_frame_does_not_select_it() {
|
||||
let mut s = session();
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(30, 30, image::Rgba([255, 0, 0, 255]))).into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let r = run(&mut s, "file.place", json!({"name": "red.png", "dataBase64": vectorcraft_format::base64_encode(&png), "at": [65, 65]}));
|
||||
let im = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
// The frame lies from x 50 to 80 and the shadow from 90 to 120.
|
||||
|
||||
@@ -149,6 +149,72 @@ fn a_shape_tool_turns_the_shape_from_just_outside_a_corner() {
|
||||
assert_eq!(angle(&s, id), 0.0, "rounding does not turn it");
|
||||
}
|
||||
|
||||
/// How many objects the first layer holds.
|
||||
fn objects(s: &Session) -> usize {
|
||||
s.doc().unwrap().doc.layers[0].children().map(|c| c.len()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Feed the pointer events `(kind, x, y, backtick held)` to the active tool, with no modifier.
|
||||
fn quote_gesture(s: &mut Session, events: &[(PointerKind, f64, f64, bool)]) {
|
||||
for (k, x, y, held) in events {
|
||||
s.pointer(&PointerEvent { backquote: *held, ..PointerEvent::new(*k, *x, *y) }, view()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// A Rectangle tool drag from (50, 50) with Backtick held from (100, 100) to (140, 100), let go
|
||||
/// before (160, 120), all moved by `o` along both axes: two copies, 70 × 50 and 90 × 50 pt, and
|
||||
/// the 110 × 70 pt rectangle it ends with. With `release`, the drag is let go; without, it is
|
||||
/// still in progress.
|
||||
fn trail(s: &mut Session, o: f64, release: bool) {
|
||||
use PointerKind::{Down, Drag, Up};
|
||||
quote_gesture(
|
||||
s,
|
||||
&[
|
||||
(Down, 50.0 + o, 50.0 + o, false),
|
||||
(Drag, 100.0 + o, 100.0 + o, true),
|
||||
(Drag, 120.0 + o, 100.0 + o, true),
|
||||
(Drag, 140.0 + o, 100.0 + o, true),
|
||||
],
|
||||
);
|
||||
quote_gesture(s, &[(Drag, 160.0 + o, 120.0 + o, false)]);
|
||||
if release {
|
||||
quote_gesture(s, &[(Up, 160.0 + o, 120.0 + o, false)]);
|
||||
}
|
||||
}
|
||||
|
||||
/// The sizes of the first layer's objects, back to front.
|
||||
fn sizes(s: &Session) -> Vec<(f64, f64)> {
|
||||
let ids: Vec<NodeId> = s.doc().unwrap().doc.layers[0].children().map(|c| c.iter().map(|n| n.id).collect()).unwrap_or_default();
|
||||
ids.into_iter().map(|id| bounds(s, id)).map(|r| (r.width(), r.height())).collect()
|
||||
}
|
||||
|
||||
/// Backtick held while a rectangle is dragged out leaves copies of it along the drag: each its own
|
||||
/// object, and the release keeps them with the last rectangle as one undo step, with Real-time
|
||||
/// Drawing and Editing on or off. Escape takes the copies back with the shape being drawn.
|
||||
#[test]
|
||||
fn a_backtick_drag_leaves_copies_of_the_shape_as_one_undo_step() {
|
||||
for real_time in [true, false] {
|
||||
let mut s = new_doc();
|
||||
s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": real_time})).unwrap();
|
||||
s.select_tool("rectangle", view()).unwrap();
|
||||
let steps = undo_steps(&s);
|
||||
trail(&mut s, 0.0, true);
|
||||
assert_eq!(sizes(&s), vec![(70.0, 50.0), (90.0, 50.0), (110.0, 70.0)], "real time {real_time}");
|
||||
assert_eq!(undo_steps(&s), steps + 1, "one undo step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(objects(&s), 0, "Undo takes all three back");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(objects(&s), 3, "Redo puts all three back");
|
||||
// Escape takes back the copies and the shape being drawn, away from the selection.
|
||||
let steps = undo_steps(&s);
|
||||
trail(&mut s, 200.0, false);
|
||||
s.tool_key(ToolKey::Escape, NONE, view()).unwrap();
|
||||
quote_gesture(&mut s, &[(PointerKind::Up, 360.0, 320.0, false)]);
|
||||
assert_eq!(objects(&s), 3, "real time {real_time}: nothing new");
|
||||
assert_eq!(undo_steps(&s), steps, "no step");
|
||||
}
|
||||
}
|
||||
|
||||
/// The Selection tool takes the centre point of an unfilled shape too: a drag there moves it
|
||||
/// instead of starting a marquee.
|
||||
#[test]
|
||||
|
||||
@@ -506,7 +506,24 @@ fn a_copied_instances_png_is_cropped_to_its_art() {
|
||||
let r = run(&mut s, "clipboard.exportPng", json!({}));
|
||||
assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(40), Some(20)));
|
||||
let png = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
||||
let img = image::load_from_memory(&png).unwrap().to_rgba8();
|
||||
let img = craft_image_codecs::decode(&png)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
assert_eq!(img.get_pixel(20, 10).0, [0, 0, 0, 255], "the art fills the picture");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,41 @@ fn quarters(s: &mut Session) -> NodeId {
|
||||
image::Rgba([r, g, b, 255])
|
||||
});
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("quarters".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
@@ -168,7 +202,24 @@ fn a_warped_image_keeps_its_bend_in_png() {
|
||||
let mut s = session();
|
||||
arc_image(&mut s);
|
||||
let out = s.execute("document.export", &json!({"format": "png"})).unwrap();
|
||||
let png = image::load_from_memory(&decoded(&out)).unwrap().to_rgba8();
|
||||
let png = craft_image_codecs::decode(&decoded(&out))
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8();
|
||||
// The artboard starts at (0, 0) and the export draws one pixel a point.
|
||||
assert_eq!((png.width(), png.height()), (800, 600));
|
||||
for &(u, v) in &SAMPLES {
|
||||
@@ -241,7 +292,41 @@ fn expand_appearance_of_a_warped_image_draws_as_the_canvas_did() {
|
||||
fn red_on_clear(s: &mut Session) -> NodeId {
|
||||
let img = image::GrayImage::from_fn(40, 40, |_, y| image::Luma([if y < 20 { 0 } else { 255 }]));
|
||||
let mut png = vec![];
|
||||
image::DynamicImage::ImageLuma8(img).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = image::DynamicImage::ImageLuma8(img);
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
let id = s
|
||||
.edit("Place", |d, sel| {
|
||||
d.images.insert("gray".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
@@ -302,7 +387,41 @@ fn half_clear_arc(s: &mut Session) {
|
||||
fn clear_arc(s: &mut Session, alpha: impl Fn(u32) -> u8) {
|
||||
let img = image::RgbaImage::from_fn(40, 40, |x, _| image::Rgba([255, 0, 0, alpha(x)]));
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
{
|
||||
let __dyn: image::DynamicImage = img.into();
|
||||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||||
let (__layout, __sample, __raw) = match __dyn {
|
||||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||||
image::DynamicImage::ImageLuma16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Gray,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageLumaA16(b) => (
|
||||
craft_image_codecs::ChannelLayout::GrayA,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgb16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
image::DynamicImage::ImageRgba16(b) => (
|
||||
craft_image_codecs::ChannelLayout::Rgba,
|
||||
craft_image_codecs::SampleType::U16,
|
||||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||||
),
|
||||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||||
};
|
||||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||||
.map(|__b| png.extend_from_slice(&__b))
|
||||
}
|
||||
.unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("half".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
|
||||
@@ -64,7 +64,24 @@ fn one(s: &mut Session, p: Value) -> Vec<u8> {
|
||||
}
|
||||
|
||||
fn pixels(file: &[u8]) -> image::RgbaImage {
|
||||
image::load_from_memory(file).unwrap().to_rgba8()
|
||||
craft_image_codecs::decode(file)
|
||||
.map(|__cimg| {
|
||||
let (__w, __h) = __cimg.dimensions();
|
||||
match (__cimg.layout(), __cimg.sample_type()) {
|
||||
(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
(craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => {
|
||||
image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap())
|
||||
}
|
||||
_ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()),
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.to_rgba8()
|
||||
}
|
||||
|
||||
fn distinct(img: &image::RgbaImage) -> usize {
|
||||
|
||||
Reference in New Issue
Block a user