A drawing app built for agent/human collaboration
Rust
Exporting a clipped layer alone keeps its clip
Indicode BVd4ca78f8369d
diff --git a/crates/render/src/export.rs b/crates/render/src/export.rsindex 3993592..615d6d2 100644--- a/crates/render/src/export.rs+++ b/crates/render/src/export.rs@@ -1,4 +1,4 @@-use crate::{Ctx, Rasters, background, draw_layer, draw_layers, f16_info, rgba8_info};+use crate::{Ctx, Rasters, background, draw_clipped_alone, draw_layer, draw_layers, f16_info, rgba8_info};use anyhow::{Result, anyhow, bail};use klad_core::doc::{Blend, Document, Layer};use rayon::prelude::*;@@ -74,12 +74,25 @@ pub fn render(doc: &Document, frame: u32, rasters: &Rasters, target: &Target, oplet mut alone = (*layer).clone();alone.visible = true;alone.blend = Blend::Normal;- draw_layer(canvas, &alone, ctx)?;+ match clip_base(&doc.layers, &layer.id).filter(|_| layer.clip) {+ Some(base) => {+ alone.clip = false;+ draw_clipped_alone(canvas, base, &alone, ctx)?+ }+ None => draw_layer(canvas, &alone, ctx)?,+ }}}Ok(surface.image_snapshot())}+fn clip_base<'a>(layers: &'a [Layer], id: &str) -> Option<&'a Layer> {+ match layers.iter().position(|l| l.id == id) {+ Some(at) => layers[..at].iter().rev().find(|l| !l.clip),+ None => layers.iter().find_map(|l| clip_base(l.children(), id)),+ }+}+pub(crate) fn f16_pixels(image: &Image, alpha: AlphaType) -> Result<Vec<half::f16>> {let (w, h) = (image.width() as usize, image.height() as usize);let info = ImageInfo::new((w as i32, h as i32), ColorType::RGBAF16, alpha, ColorSpace::new_srgb());@@ -326,4 +339,46 @@ mod tests {.is_err());}++ #[test]+ fn a_clipped_layer_exported_alone_keeps_its_clip() {+ let rect = |id: &str, x: f32, w: f32, colour: &str| {+ Node::new(+ id,+ Shape::Rect {+ x,+ y: 0.0,+ w,+ h: 32.0,+ radius: 0.0,+ },+ )+ .filled(colour)+ };+ let mut doc = Document::new(64, 32);+ doc.layers.push(Layer::vector(+ "base",+ "base",+ "c1",+ vec![rect("n1", 0.0, 32.0, "#ff0000")],+ ));+ let mut shade = Layer::vector("shade", "shade", "c2", vec![rect("n2", 0.0, 64.0, "#0000ff")]);+ shade.clip = true;+ doc.layers.push(shade);+ let options = Options {+ scale: 1.0,+ background: false,+ references: false,+ detail: 1.0,+ };+ let layer = doc.layers[1].clone();+ let image = render(&doc, 0, &Rasters::new(), &Target::Layer(&layer), options).unwrap();+ let frame = crate::read_rgba8(&image).unwrap();+ assert_eq!(+ frame.pixel(16, 16),+ [0, 0, 255, 255],+ "inside the base: the layer's own colour"+ );+ assert_eq!(frame.pixel(48, 16)[3], 0, "outside the base: clipped away");+ }}diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rsindex 0c23d42..54fe626 100644--- a/crates/render/src/lib.rs+++ b/crates/render/src/lib.rs@@ -330,6 +330,18 @@ fn draw_clipped(canvas: &Canvas, base: &Layer, clipped: &[&Layer], ctx: Ctx) ->drawn}+pub(crate) fn draw_clipped_alone(canvas: &Canvas, base: &Layer, layer: &Layer, ctx: Ctx) -> Result<()> {+ layer_paint_isolated(canvas, layer.opacity, Blend::Normal);+ let drawn = draw_styled(canvas, layer, ctx).and_then(|_| {+ mode_layer(canvas, BlendMode::DstIn);+ let base_drawn = draw_content(canvas, base, ctx.nested());+ canvas.restore();+ base_drawn+ });+ canvas.restore();+ drawn.with_context(|| format!("layer {} clipped to {}", layer.id, base.id))+}+fn draw_clip(canvas: &Canvas, base: &Layer, layer: &Layer, ctx: Ctx) -> Result<()> {layer_paint_isolated(canvas, layer.opacity, layer.blend);let drawn = draw_styled(canvas, layer, ctx).and_then(|_| {diff --git a/docs/DESIGN.md b/docs/DESIGN.mdindex 358c724..c33a06d 100644--- a/docs/DESIGN.md+++ b/docs/DESIGN.md@@ -339,7 +339,7 @@ A brush is JSON. The library is built in (`crates/core/brushes/library/`: `ink`- Frame keys read a stroke file only when its size, time or inode changed (writes are atomic renames, so a rewrite always gets a new inode); an edit no longer clears them.- **Onion skin** (M3) is computed in `klad-render` (`onion`), one piece of code for the CLI, the editor's canvas and its frame cache. For a layer at a frame it lists the drawings before and after the current one: counted in drawings (the exposures before and after, each drawing once at its nearest distance, the current drawing and links to it skipped) or in frames (the drawings shown 1, 2, ... frames away). Opacity is `opacity x falloff^(distance - 1)` (defaults 0.5 and 0.6, 2 before and 2 after); those before are tinted red and those after green (`rgb x 0.25 + tint x alpha x 0.75`) unless the tint is off. In a looping range the count wraps around inside the range, so the last drawing of a cycle shows the first. The ghosts of a layer are composed (furthest first, f32 per pixel, on tiles in parallel, deterministic) into one raster, which the compositor draws right under that layer, outside its opacity, blend and mask; the ghosts of a clip group's layers go under the whole group. Hidden layers show none.- **Sheets** (M3): `klad render --sheet` lays frames out in a grid (columns about square, `--columns`), each cell the frame at `--cell` px on its longest side with its frame number, a `hold` mark when it looks exactly like the frame before, and per layer (top first, listed in the heading) the cel it shows: `c5` starts a drawing, `|c5` holds it, `c9>c1` a link, `.` blank. `--drawings` merges frames that look the same into one cell titled with their range. A clip's sheet is headed with the clip's name and its fps. Frames that look the same are composed once; drawings load in batches under 768 MB, so long sheets of big canvases stay in memory.-- Export (`klad export`) renders the image, or one layer or group alone, at a frame, at a scale from 1/16 to 8, on transparency unless `--background`. Vector nodes are drawn at the export scale. Above full size the paint is drawn again at the export size: each drawing's marks are scaled (`markedit::transform_mark`: samples, sizes, tapers and cuts; fill coverage resampled bilinearly) and replayed with the brushes at that scale (`BrushSet::at_scale`: grain scaled with it, speed and bristle spacing measured in canvas px), the rasters drawn one to one (`Ctx::detail`), so lines stay crisp and textures keep their size; sprite sheets and frames do the same with their vector cels rasterized at that size. That replays without the cache (the lemons at 2x: 5.5 s instead of 0.7 s). Below full size the cached rasters are mipmapped down. 8-bit PNGs are ordered-dithered (8x8 Bayer) from the f16 pixels, colour and alpha, so gradients do not band; values within an eighth of a level of an 8-bit value are written as that value, so flat colours stay flat. `--depth 16` writes 16-bit PNG. Working renders (`klad render`, `klad hash`) stay undithered so their hashes hold.+- Export (`klad export`) renders the image, or one layer or group alone (a clipped layer alone stays clipped to its base's shape), at a frame, at a scale from 1/16 to 8, on transparency unless `--background`. Vector nodes are drawn at the export scale. Above full size the paint is drawn again at the export size: each drawing's marks are scaled (`markedit::transform_mark`: samples, sizes, tapers and cuts; fill coverage resampled bilinearly) and replayed with the brushes at that scale (`BrushSet::at_scale`: grain scaled with it, speed and bristle spacing measured in canvas px), the rasters drawn one to one (`Ctx::detail`), so lines stay crisp and textures keep their size; sprite sheets and frames do the same with their vector cels rasterized at that size. That replays without the cache (the lemons at 2x: 5.5 s instead of 0.7 s). Below full size the cached rasters are mipmapped down. 8-bit PNGs are ordered-dithered (8x8 Bayer) from the f16 pixels, colour and alpha, so gradients do not band; values within an eighth of a level of an 8-bit value are written as that value, so flat colours stay flat. `--depth 16` writes 16-bit PNG. Working renders (`klad render`, `klad hash`) stay undithered so their hashes hold.## The agent interface: `klad` CLIdiff --git a/docs/skill/SKILL.md b/docs/skill/SKILL.mdindex 0a5aa0e..5b49dd8 100644--- a/docs/skill/SKILL.md+++ b/docs/skill/SKILL.md@@ -343,7 +343,7 @@ Cell labels read like `klad frames`: `c5` starts a drawing, `|c5` holds it, `c9>- `klad path` numbers points across all subpaths of the shape; re-read the list after `--add`, `--insert` or `--rm`.- A layer created with `lockAlpha=true` takes no paint at all until something without the lock lands on it first; paint the flats, then lock.- A clipped layer at the bottom of its list has no base and draws unclipped. Hiding a base hides its clipped layers.-- `klad export --layer` on a clipped layer draws it alone, unclipped; export the group or the whole image to see the clip.+- `klad export --layer` on a clipped layer draws it alone but still clipped to its base (without the base's own colour).- Animation exports draw every visible layer, so a held background (a floor) is in every picture and trimming keeps its width. Hide it, or pass `--layer <character group>`, for a sprite.- `--sheet` on `render` is a labelled look at the frames; `--sheet` on `export` is the game asset. They take the same specs.- `klad inbetween --to` is the frame where the second key starts; a frame inside its exposure is refused with the right frame named.