mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 19:11:26 +00:00
Merge pull request #8178 from dotCooCoo/feat/image-trace-split-view
Image Trace: Split View shows the source image and the result on either side of a divider
This commit is contained in:
@@ -499,7 +499,7 @@ impl Renderer {
|
||||
fn draw_content(&mut self, ctx: &mut RenderContext, f: &Frame, n: &Node, knockout: bool) {
|
||||
match &n.kind {
|
||||
NodeKind::Group { children, clip: false } if f.opts.trace_views && !f.opts.outline && Self::trace_view(f, n) != TraceView::Result => {
|
||||
self.draw_trace_view(ctx, f, children, Self::trace_view(f, n), knockout)
|
||||
self.draw_trace_view(ctx, f, children, (Self::trace_view(f, n), n.trace_split()), knockout)
|
||||
}
|
||||
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } => {
|
||||
self.draw_children(ctx, f, children, knockout)
|
||||
@@ -641,9 +641,56 @@ impl Renderer {
|
||||
}
|
||||
|
||||
/// An Image Trace object's `children` (the hidden source image, then the traced shapes) as
|
||||
/// `view` shows them on screen.
|
||||
fn draw_trace_view(&mut self, ctx: &mut RenderContext, f: &Frame, children: &[Arc<Node>], view: TraceView, knockout: bool) {
|
||||
/// `view` shows them on screen, with `split`, the Split view's divider
|
||||
/// ([`TraceView::split_of`]).
|
||||
fn draw_trace_view(&mut self, ctx: &mut RenderContext, f: &Frame, children: &[Arc<Node>], (view, split): (TraceView, f64), knockout: bool) {
|
||||
let Some((source, shapes)) = children.split_first() else { return };
|
||||
if view == TraceView::Split {
|
||||
// Each side is drawn once under a plain clip (no layer and no second render), in the
|
||||
// image's own pixels so the divider turns with the image: the source image on the part
|
||||
// of the image left of the divider, and the traced shapes everywhere else, art past any
|
||||
// of the image's edges included. Without an image, with one of no size or that can't
|
||||
// be inverted, or with the divider at the image's left edge, the traced shapes draw as
|
||||
// they are.
|
||||
let NodeKind::Image(im) = &source.kind else { return self.draw_children(ctx, f, shapes, knockout) };
|
||||
let (w, h) = (f64::from(im.width), f64::from(im.height));
|
||||
let at = w * split.clamp(0.0, 1.0);
|
||||
let det = im.xf.determinant();
|
||||
if w <= 0.0 || h <= 0.0 || at <= 0.0 || !det.is_finite() || det.abs() < 1e-12 {
|
||||
return self.draw_children(ctx, f, shapes, knockout);
|
||||
}
|
||||
// The result's side covers the image and the traced shapes' cached bounds, two screen
|
||||
// pixels more so anti-aliased edges are not cut, in the image's pixels, less the
|
||||
// source's side.
|
||||
let inverse = im.xf.inverse();
|
||||
let (mut x0, mut x1, mut y0, mut y1) = (0.0_f64, w, 0.0_f64, h);
|
||||
for c in shapes {
|
||||
if let Some(b) = self.bounds_of(f.symbols, c).map(|b| b.inflate(f.px * 2.0, f.px * 2.0)) {
|
||||
for q in [(b.x0, b.y0), (b.x1, b.y0), (b.x1, b.y1), (b.x0, b.y1)] {
|
||||
let p = inverse * kurbo::Point::new(q.0, q.1);
|
||||
if p.x.is_finite() && p.y.is_finite() {
|
||||
(x0, x1, y0, y1) = (x0.min(p.x), x1.max(p.x), y0.min(p.y), y1.max(p.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let part = |p: &mut BezPath, x0: f64, x1: f64, y0: f64, y1: f64| {
|
||||
p.move_to(im.xf * kurbo::Point::new(x0, y0));
|
||||
p.line_to(im.xf * kurbo::Point::new(x1, y0));
|
||||
p.line_to(im.xf * kurbo::Point::new(x1, y1));
|
||||
p.line_to(im.xf * kurbo::Point::new(x0, y1));
|
||||
p.close_path();
|
||||
};
|
||||
let (mut left, mut right) = (BezPath::new(), BezPath::new());
|
||||
part(&mut left, 0.0, at, 0.0, h);
|
||||
part(&mut right, x0, x1, y0, y1);
|
||||
part(&mut right, 0.0, at, 0.0, h);
|
||||
let comp = Composite { clip: Some((&left, FillRule::NonZero)), blends: true, ..Default::default() };
|
||||
self.group(ctx, f, comp, &mut |r, c, fr| r.draw_forced_arc(c, fr, source));
|
||||
let comp = Composite { clip: Some((&right, FillRule::EvenOdd)), blends: true, ..Default::default() };
|
||||
self.group(ctx, f, comp, &mut |r, c, fr| r.draw_children(c, fr, shapes, knockout));
|
||||
return;
|
||||
}
|
||||
if view.shows_source() && matches!(source.kind, NodeKind::Image(_)) {
|
||||
self.draw_forced_arc(ctx, f, source);
|
||||
}
|
||||
|
||||
@@ -841,6 +841,79 @@ fn trace_source_applies_the_hidden_image_mask() {
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
|
||||
}
|
||||
|
||||
/// The Split view draws the source image left of its divider and the traced shapes right of it,
|
||||
/// along the image's own width (also when the image is turned), with traced art past any of the
|
||||
/// image's edges drawn whatever the divider, in the same frames a renderer that keeps its caches
|
||||
/// draws, and in a zoomed and scrolled view and a turned view; exports draw the tracing result.
|
||||
#[test]
|
||||
fn split_view_draws_the_source_on_one_side_of_the_divider_and_the_result_on_the_other() {
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject};
|
||||
let mut png = vec![];
|
||||
image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let red = |p: [u8; 4]| p[0] > 200 && p[1] < 60 && p[2] < 60;
|
||||
let blue = |p: [u8; 4]| p[2] > 200 && p[0] < 60;
|
||||
// A red 80 × 80 source image at 10, 10, turned a half turn about its center when `turned`, with
|
||||
// a blue traced square over all of it.
|
||||
for turned in [false, true] {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
d.images.insert("red".into(), ImageBlob::new("image/png", png.clone()));
|
||||
let turn = if turned { Affine::rotate_about(std::f64::consts::PI, kurbo::Point::new(50.0, 50.0)) } else { Affine::IDENTITY };
|
||||
let xf = turn * Affine::translate((10.0, 10.0)) * Affine::scale(80.0);
|
||||
let im = ImageObject { key: "red".into(), width: 1, height: 1, xf, link: None, placement: Default::default(), pixels: None };
|
||||
let mut source = Node::new(d.alloc_id(), NodeKind::Image(Box::new(im)));
|
||||
source.visible = false;
|
||||
let shape = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
|
||||
// Traced art past the image's right edge on the page (a stroke along it, say).
|
||||
let past = rect_node(&mut d, Rect::new(92.0, 46.0, 98.0, 54.0), Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
|
||||
// And above the image's top edge, near its left edge.
|
||||
let above = rect_node(&mut d, Rect::new(14.0, 2.0, 26.0, 8.0), Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
|
||||
let mut g = Node::group(d.alloc_id(), vec![Arc::new(source), Arc::new(shape), Arc::new(past), Arc::new(above)]);
|
||||
let id = g.id;
|
||||
g.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "split", "split": 0.25 })));
|
||||
d.insert(Some(d.layers[0].id), 0, g).unwrap();
|
||||
let screen = RenderOptions { trace_views: true, background: Some([255; 4]), ..Default::default() };
|
||||
let export = RenderOptions { background: Some([255; 4]), ..Default::default() };
|
||||
let mut kept = Renderer::new();
|
||||
for split in [0.25, 0.0, 1.0, 0.25] {
|
||||
if let Some(n) = d.node_mut(id) {
|
||||
n.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "split", "split": split })));
|
||||
}
|
||||
// The divider in page space: 30 across from the image's left edge, or from its right
|
||||
// edge on the page once the image is turned.
|
||||
let at = if turned { 90.0 - 80.0 * split } else { 10.0 + 80.0 * split };
|
||||
// The view: as is, zoomed in twice and scrolled, and turned a half turn about the page's
|
||||
// middle; `px` finds the pixel showing page point (x, y).
|
||||
let views: [(Affine, u32); 3] = [
|
||||
(Affine::IDENTITY, 100),
|
||||
(Affine::translate((5.0, 7.0)) * Affine::scale(2.0), 210),
|
||||
(Affine::rotate_about(std::f64::consts::PI, kurbo::Point::new(50.0, 50.0)), 100),
|
||||
];
|
||||
for (view, size) in views {
|
||||
let px = |r: &Rendered, x: f64, y: f64| {
|
||||
let q = view * kurbo::Point::new(x, y);
|
||||
r.pixel(q.x.floor() as u32, q.y.floor() as u32)
|
||||
};
|
||||
for r in [kept.render(&d, size, size, view, &screen), Renderer::new().render(&d, size, size, view, &screen)] {
|
||||
for x in [12.5, 30.5, 48.5, 52.5, 70.5, 88.5] {
|
||||
let p = px(&r, x, 50.5);
|
||||
let source = if turned { x > at } else { x < at };
|
||||
assert!(if source { red(p) } else { blue(p) }, "turned {turned}, split {split}, view {view:?}, x = {x}: {p:?}");
|
||||
}
|
||||
assert_eq!(px(&r, 5.5, 50.5), [255; 4], "nothing outside the image");
|
||||
// The art past the image's edges shows on either side of the divider.
|
||||
let green = |p: [u8; 4]| p[1] > 200 && p[0] < 60 && p[2] < 60;
|
||||
for (x, y) in [(95.5, 50.5), (20.5, 5.5)] {
|
||||
let p = px(&r, x, y);
|
||||
assert!(green(p), "turned {turned}, split {split}, view {view:?}, ({x}, {y}): {p:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
let r = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &export);
|
||||
assert!([12u32, 50, 88].iter().all(|x| blue(r.pixel(*x, 50))), "exports draw the result");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artboards_white_on_pasteboard() {
|
||||
let d = Document::new(50.0, 50.0);
|
||||
|
||||
Reference in New Issue
Block a user