///| The screenshot editor: its pixel work (blur, pixelate, redact, crop, strokes) checked pixel by /// pixel, and its keys and buttons driven headlessly. ///| A `w` x `h` picture where pixel (x, y) is `f(x, y)` as grey, opaque. fn grey_pic(w : Int, h : Int, f : (Int, Int) -> Int) -> @renderer.Pixmap { let p = @renderer.Pixmap::new(w, h) for y = 0; y < h; y = y + 1 { for x = 0; x < w; x = x + 1 { let o = (y * w + x) * 4 let v = f(x, y).to_byte() p.data[o] = v p.data[o + 1] = v p.data[o + 2] = v p.data[o + 3] = b'\xff' } } p } ///| Pixel (x, y) as (r, g, b, a). fn px(p : @renderer.Pixmap, x : Int, y : Int) -> (Int, Int, Int, Int) { let o = (y * p.width + x) * 4 (p.data[o].to_int(), p.data[o + 1].to_int(), p.data[o + 2].to_int(), p.data[o + 3].to_int()) } ///| Whether every pixel outside `r` is the same in `a` and `b`. fn same_outside(a : @renderer.Pixmap, b : @renderer.Pixmap, r : @scene.Rect) -> Bool { for y = 0; y < a.height; y = y + 1 { for x = 0; x < a.width; x = x + 1 { if !r.contains(x, y) && px(a, x, y) != px(b, x, y) { return false } } } true } ///| An annotation with `tool` from `p0` to `p1`. fn ann(tool : @host.Tool, p0 : (Int, Int), p1 : (Int, Int), color? : @scene.Rgba = @scene.Rgba::new(255, 0, 0, 255), width? : Int = 4) -> @host.Annotation { { tool, color, width, points: [p0, p1], text: "", number: 0 } } ///| Text-like detail: 1-pixel black/white stripes (vertical in `a`, a checkerboard in `b`), both /// half white. fn stripes(x : Int, _y : Int) -> Int { if x % 2 == 0 { 255 } else { 0 } } ///| fn checker(x : Int, y : Int) -> Int { if (x + y) % 2 == 0 { 0 } else { 255 } } ///| test "blur changes only the pixels in its region, and leaves nothing of what was there" { let r = @scene.Rect::new(40, 20, 90, 50) let a = grey_pic(200, 100, stripes) let b = grey_pic(200, 100, checker) let a0 = @host.copy_pixmap(a) @host.blur_region(a, r) @host.blur_region(b, r) // Outside the region: untouched. assert_true(same_outside(a, a0, r)) // Inside: no fine detail left (the stripes went from 0 to 255 between neighbours), and two // different patterns with the same average come out the same. let mut lo = 255 let mut hi = 0 let mut diff = 0 for y = r.y; y < r.y + r.h; y = y + 1 { for x = r.x; x < r.x + r.w; x = x + 1 { let (v, _, _, al) = px(a, x, y) assert_eq(al, 255) lo = lo.min(v) hi = hi.max(v) diff = diff.max((v - px(b, x, y).0).abs()) } } assert_true(hi - lo <= 8) assert_true(diff <= 8) // Every pixel of the region changed (each was 0 or 255, now mid-grey). assert_true(lo > 100 && hi < 160) } ///| test "blur hides a dark word on a light background: the region ends up nearly flat" { // A 'word': dark bars of different widths on white, like glyph stems. let word = fn(x : Int, y : Int) { if y >= 30 && y < 44 && (x % 7 == 0 || x % 11 == 0 || x % 13 < 2) { 20 } else { 240 } } let p = grey_pic(160, 80, word) let r = @scene.Rect::new(20, 24, 120, 26) @host.blur_region(p, r) // Across the blurred word, neighbouring columns differ by at most 4 levels. let mut worst = 0 for y = r.y; y < r.y + r.h; y = y + 1 { for x = r.x + 1; x < r.x + r.w; x = x + 1 { worst = worst.max((px(p, x, y).0 - px(p, x - 1, y).0).abs()) } } assert_true(worst <= 4) // the word was 220 levels against its background } ///| test "pixelate makes each block one colour, and nothing outside changes" { let p = grey_pic(100, 60, fn(x, y) { (x * 7 + y * 13) % 256 }) let p0 = @host.copy_pixmap(p) let r = @scene.Rect::new(10, 10, 48, 32) @host.pixelate_region(p, r, 16) assert_true(same_outside(p, p0, r)) for by = 0; by < 2; by = by + 1 { for bx = 0; bx < 3; bx = bx + 1 { let c = px(p, r.x + bx * 16, r.y + by * 16) for y = 0; y < 16; y = y + 1 { for x = 0; x < 16; x = x + 1 { assert_eq(px(p, r.x + bx * 16 + x, r.y + by * 16 + y), c) } } } } // A block's colour is its average: block (0, 0) of the gradient isn't any one of its pixels' old values only. assert_true(px(p, r.x, r.y) != px(p0, r.x, r.y)) } ///| test "redact is fully opaque, even over a picture with alpha, and stays in its box" { let p = grey_pic(64, 64, checker) // Make the picture translucent: redact must still leave nothing showing through. for i = 3; i < p.data.length(); i = i + 4 { p.data[i] = b'\x40' } let p0 = @host.copy_pixmap(p) let r = @scene.Rect::new(8, 12, 30, 20) @host.apply_annotation(p, ann(@host.Tool::Redact, (8, 12), (38, 32), color=@scene.Rgba::new(10, 20, 30, 255))) assert_true(same_outside(p, p0, r)) for y = r.y; y < r.y + r.h; y = y + 1 { for x = r.x; x < r.x + r.w; x = x + 1 { assert_eq(px(p, x, y), (10, 20, 30, 255)) } } } ///| test "crop: the last crop sets the saved size; crops are clipped to the picture" { let base = grey_pic(300, 200, fn(x, _) { x % 256 }) let anns = [ ann(@host.Tool::Crop, (10, 10), (110, 60)), ann(@host.Tool::Line, (0, 100), (299, 100)), ann(@host.Tool::Crop, (250, 150), (20, 40)), ] let out = @host.render_annotations(base, anns) assert_eq((out.width, out.height), (230, 110)) // The saved pixels are the picture's, from the crop's corner. assert_eq(px(out, 0, 0), px(base, 20, 40)) assert_eq(px(out, 229, 0), px(base, 249, 40)) // The line went through the crop: it's in the saved picture, red. assert_eq(px(out, 100, 60), (255, 0, 0, 255)) // A crop that runs off the picture is cut to it. let off = @host.render_annotations(base, [ann(@host.Tool::Crop, (200, 150), (400, 300))]) assert_eq((off.width, off.height), (100, 50)) // No crop: the whole picture. let all = @host.render_annotations(base, []) assert_eq((all.width, all.height), (300, 200)) } ///| test "strokes: lines, arrows, rectangles, ellipses and the pen draw where they go and nowhere else" { let white = fn(_x : Int, _y : Int) { 255 } let red = (255, 0, 0, 255) // A line: on it red, 10 px away untouched. let p = grey_pic(120, 80, white) @host.apply_annotation(p, ann(@host.Tool::Line, (10, 40), (110, 40))) assert_eq(px(p, 60, 40), red) assert_eq(px(p, 60, 50), (255, 255, 255, 255)) // An arrow: its head is wider than its shaft. let q = grey_pic(120, 80, white) @host.apply_annotation(q, ann(@host.Tool::Arrow, (10, 40), (110, 40))) assert_eq(px(q, 30, 40), red) assert_eq(px(q, 30, 46), (255, 255, 255, 255)) assert_eq(px(q, 100, 44), red) // inside the head, beside the shaft's line // A rectangle: its edges, not its inside. let r = grey_pic(120, 80, white) @host.apply_annotation(r, ann(@host.Tool::Rectangle, (20, 20), (100, 60))) assert_eq(px(r, 20, 40), red) assert_eq(px(r, 60, 20), red) assert_eq(px(r, 60, 40), (255, 255, 255, 255)) // An ellipse: through the middle of each side of its box, not in its centre or the corners. let e = grey_pic(120, 80, white) @host.apply_annotation(e, ann(@host.Tool::Ellipse, (20, 20), (100, 60))) assert_eq(px(e, 60, 20), red) assert_eq(px(e, 20, 40), red) assert_eq(px(e, 60, 40), (255, 255, 255, 255)) assert_eq(px(e, 21, 21), (255, 255, 255, 255)) // The pen follows its points. let pen = grey_pic(120, 80, white) @host.apply_annotation(pen, { ..ann(@host.Tool::Pen, (10, 10), (50, 10)), points: [(10, 10), (50, 10), (50, 60)] }) assert_eq(px(pen, 30, 10), red) assert_eq(px(pen, 50, 35), red) assert_eq(px(pen, 30, 35), (255, 255, 255, 255)) } ///| test "the highlighter is translucent and even where its stroke crosses itself" { let p = grey_pic(120, 120, fn(_, _) { 255 }) let yellow = @scene.Rgba::new(255, 220, 0, 255) // Across, then back down through the first part. @host.apply_annotation(p, { ..ann(@host.Tool::Highlighter, (10, 60), (110, 60), color=yellow), points: [(10, 60), (110, 60), (60, 10), (60, 110)] }) let once = px(p, 30, 60) let twice = px(p, 60, 60) assert_eq(once, twice) // Translucent: the white shows through the blue channel. assert_true(once.2 > 100 && once.2 < 255) } ///| test "a number marker is a filled disc of its colour with the number on it" { let p = grey_pic(100, 100, fn(_, _) { 255 }) let m : @host.Annotation = { ..ann(@host.Tool::Number, (50, 50), (50, 50), color=@scene.Rgba::new(30, 136, 229, 255)), number: 3 } @host.apply_annotation(p, m) let r = @host.annotation_marker_radius(4) // The rim of the disc is the marker colour; outside it, white. assert_eq(px(p, 50 + r - 2, 50), (30, 136, 229, 255)) assert_eq(px(p, 50 + r + 3, 50), (255, 255, 255, 255)) // The number: some white pixels inside the disc. let mut white = 0 for y = 50 - r / 2; y < 50 + r / 2; y = y + 1 { for x = 50 - r / 2; x < 50 + r / 2; x = x + 1 { if px(p, x, y).0 > 200 { white = white + 1 } } } assert_true(white > 5) } ///| Open the editor on a full-screen picture (Print). fn editor_comp(tag : String) -> (@host.ChandraCompositor, String) raise { let (comp, dir) = shot_comp(tag) key(comp, 99U, true, 1U) key(comp, 99U, false, 2U) assert_true(comp.screenshot_editing()) ignore(comp.compose_frame()) (comp, dir) } ///| Picture pixel (x, y) on screen in the editor. fn on_screen(comp : @host.ChandraCompositor, x : Int, y : Int) -> (Int, Int) { let (v, s) = comp.screenshot_editor().unwrap().view() (v.x + (x.to_double() * s).round().to_int(), v.y + (y.to_double() * s).round().to_int()) } ///| Drag in the editor between picture pixels. fn edit_drag(comp : @host.ChandraCompositor, x0 : Int, y0 : Int, x1 : Int, y1 : Int, t : UInt) -> Unit { let (a, b) = on_screen(comp, x0, y0) let (c, d) = on_screen(comp, x1, y1) drag(comp, a, b, c, d, t) } ///| Ctrl + `code`. fn ctrl_key(comp : @host.ChandraCompositor, code : UInt, t : UInt, shift? : Bool = false) -> Unit { key(comp, 29U, true, t) if shift { key(comp, 42U, true, t) } comp.handle_input(@nested.InputEvent::Modifiers(4U | (if shift { 1U } else { 0U }), 0U, 0U, 0U), t) key(comp, code, true, t + 1U) key(comp, code, false, t + 2U) if shift { key(comp, 42U, false, t + 3U) } key(comp, 29U, false, t + 3U) comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), t + 3U) } ///| Press and let go of a key. fn tap(comp : @host.ChandraCompositor, code : UInt, t : UInt) -> Unit { key(comp, code, true, t) key(comp, code, false, t + 1U) } ///| test "editor: Print opens it with the keyboard; Esc discards without saving and gives the keyboard back" { let (comp, dir) = editor_comp("ed-esc") // The picture fits between the bars, scaled down. let (v, s) = comp.screenshot_editor().unwrap().view() assert_true(s < 1.0 && v.y > 40 && v.y + v.h < 720 - 40) // Keys go to the editor: B picks Blur. tap(comp, 48U, 10U) assert_eq(comp.screenshot_editor().unwrap().current_tool(), @host.Tool::Blur) tap(comp, 1U, 20U) assert_false(comp.screenshot_editing()) assert_eq(comp.last_screenshot(), "") assert_eq(@sys.read_file_bytes(dir).length(), 0) } ///| test "editor: draw, undo, redo, crop, then Enter saves exactly what is shown" { let (comp, dir) = editor_comp("ed-flow") // R: rectangle, then a drag. tap(comp, 19U, 10U) edit_drag(comp, 100, 100, 300, 200, 20U) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 1) // D: redact a box; X: pixelate one. tap(comp, 32U, 30U) edit_drag(comp, 400, 300, 500, 360, 40U) tap(comp, 45U, 50U) edit_drag(comp, 600, 100, 700, 200, 60U) let e = comp.screenshot_editor().unwrap() assert_eq(e.annotations().length(), 3) // The redacted box is opaque in the picture shown (red: the first colour). assert_eq(px(e.picture(), 450, 330), (0xe5, 0x39, 0x35, 255)) // Ctrl+Z takes the pixelation back out, Ctrl+Shift+Z puts it back; Ctrl+Y after another undo too. ctrl_key(comp, 44U, 70U) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 2) ctrl_key(comp, 44U, 80U, shift=true) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 3) ctrl_key(comp, 44U, 90U) ctrl_key(comp, 21U, 100U) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 3) // A click without a drag adds no shape. tap(comp, 19U, 110U) let (cx, cy) = on_screen(comp, 900, 500) click_at(comp, cx, cy, 120U) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 3) // C: crop to (380, 280)-(520, 380). tap(comp, 46U, 130U) edit_drag(comp, 380, 280, 520, 380, 140U) let e = comp.screenshot_editor().unwrap() let crop = @host.final_crop(1280, 720, e.annotations()) let expected = e.export() assert_eq((expected.width, expected.height), (crop.w, crop.h)) assert_true(crop.w >= 138 && crop.w <= 142 && crop.h >= 98 && crop.h <= 102) ignore(comp.compose_frame()) // Enter saves it (and copies it). tap(comp, 28U, 150U) assert_false(comp.screenshot_editing()) let path = comp.last_screenshot() assert_true(path.has_prefix(dir + "/Screenshot_")) let saved = @renderer.decode_png(@sys.read_file_bytes(path)).unwrap() assert_eq((saved.width, saved.height), (crop.w, crop.h)) assert_eq(saved.data, expected.data) assert_true(comp.server.owns_selection()) } ///| test "editor: Ctrl+C and the Copy button copy without saving a file" { let (comp, dir) = editor_comp("ed-copy") ctrl_key(comp, 46U, 10U) assert_false(comp.screenshot_editing()) assert_true(comp.server.owns_selection()) assert_eq(comp.last_screenshot(), "") assert_eq(@sys.read_file_bytes(dir).length(), 0) assert_true(comp.screenshot_toast_shown()) } ///| test "editor: the toolbar and bottom bar buttons work" { let (comp, dir) = editor_comp("ed-buttons") ignore(comp.compose_frame()) // The tool buttons are along the top, in order: the 9th is Blur. let y = 12 + 16 let mut found = false let mut x = 0 while x < 1280 && !found { click_at(comp, x, y, 10U + x.reinterpret_as_uint()) if comp.screenshot_editor().unwrap().current_tool() == @host.Tool::Blur { found = true } x = x + 6 } assert_true(found) edit_drag(comp, 50, 50, 250, 150, 3000U) assert_eq(comp.screenshot_editor().unwrap().annotations().length(), 1) // Save is the right-most button of the bottom bar. click_at(comp, 1280 - 12 - 10, 720 - 12 - 16, 3100U) assert_false(comp.screenshot_editing()) assert_true(comp.last_screenshot().has_prefix(dir)) } ///| test "editor: text is typed through the keymap; numbers count up" { let (comp, _) = editor_comp("ed-text") tap(comp, 20U, 10U) // T let (x, y) = on_screen(comp, 200, 200) click_at(comp, x, y, 20U) // Shift+H, i, Backspace, i, ! -> "Hi!" key(comp, 42U, true, 30U) comp.handle_input(@nested.InputEvent::Modifiers(1U, 0U, 0U, 0U), 30U) tap(comp, 35U, 31U) key(comp, 42U, false, 33U) comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), 33U) tap(comp, 23U, 34U) tap(comp, 23U, 36U) tap(comp, 14U, 38U) key(comp, 42U, true, 40U) comp.handle_input(@nested.InputEvent::Modifiers(1U, 0U, 0U, 0U), 40U) tap(comp, 2U, 41U) key(comp, 42U, false, 43U) comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), 43U) ignore(comp.compose_frame()) // Enter ends the text (and doesn't save yet). tap(comp, 28U, 50U) assert_true(comp.screenshot_editing()) let anns = comp.screenshot_editor().unwrap().annotations() assert_eq(anns.length(), 1) assert_eq(anns[0].text, "Hi!") // N: two markers, numbered 1 and 2. tap(comp, 49U, 60U) let (a, b) = on_screen(comp, 400, 300) click_at(comp, a, b, 70U) let (c, d) = on_screen(comp, 500, 300) click_at(comp, c, d, 80U) let anns = comp.screenshot_editor().unwrap().annotations() assert_eq(anns.map(a => a.number), [0, 1, 2]) tap(comp, 1U, 90U) assert_false(comp.screenshot_editing()) } ///| test "editor: a region screenshot opens at full size when it fits" { let (comp, _) = shot_comp("ed-region") chord(comp, [125U, 42U], 65U, 31U, 1U) drag(comp, 100, 100, 500, 400, 10U) tap(comp, 28U, 20U) let (v, s) = comp.screenshot_editor().unwrap().view() assert_eq(s, 1.0) assert_eq((v.w, v.h), (400, 300)) tap(comp, 1U, 30U) } ///| Headless pictures of the editor for the docs: written when CHANDRA_EDITOR_SHOTS names a folder. test "editor: headless screenshots" { let out = @sys.getenv("CHANDRA_EDITOR_SHOTS") if out.length() == 0 { return } let socket_path = "/tmp/wayland-moon-edshots-" + @sys.getpid().to_string() + ".sock" let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() comp.shot_dir = "/tmp/chandra-edshots-" + @sys.getpid().to_string() ignore(open_and_configure_client(comp, socket_path, "Notes", "notes")) ignore(open_and_configure_client(comp, socket_path, "Terminal", "term")) ignore(comp.compose_frame()) key(comp, 99U, true, 1U) key(comp, 99U, false, 2U) ignore(comp.compose_frame()) // An arrow, a rectangle, a blur, a redaction, a highlight, text and two markers. tap(comp, 30U, 10U) edit_drag(comp, 200, 560, 420, 380, 20U) tap(comp, 19U, 30U) tap(comp, 6U, 31U) // blue edit_drag(comp, 430, 200, 760, 360, 40U) tap(comp, 48U, 50U) edit_drag(comp, 750, 628, 1275, 674, 60U) // the notification tap(comp, 32U, 70U) tap(comp, 8U, 71U) // black edit_drag(comp, 895, 12, 1025, 42, 80U) // a title tap(comp, 45U, 85U) edit_drag(comp, 930, 686, 1060, 719, 86U) // the clock tap(comp, 35U, 90U) tap(comp, 4U, 91U) // yellow let (h0, h1) = on_screen(comp, 436, 590) let (h2, h3) = on_screen(comp, 640, 590) drag(comp, h0, h1, h2, h3, 100U) tap(comp, 49U, 110U) tap(comp, 2U, 111U) // red let (m0, m1) = on_screen(comp, 450, 190) click_at(comp, m0, m1, 120U) let (m2, m3) = on_screen(comp, 810, 110) click_at(comp, m2, m3, 130U) tap(comp, 20U, 140U) tap(comp, 9U, 141U) // white let (t0, t1) = on_screen(comp, 440, 590) click_at(comp, t0, t1, 150U) for c in [20U, 35U, 18U, 57U, 48U, 22U, 34U, 57U, 23U, 31U, 57U, 35U, 18U, 19U, 18U] { tap(comp, c, 160U) } tap(comp, 28U, 170U) // A crop being drawn. tap(comp, 46U, 180U) let (c0, c1) = on_screen(comp, 60, 60) let (c2, c3) = on_screen(comp, 1220, 690) comp.handle_input(@nested.InputEvent::PointerMove(c0.to_double(), c1.to_double()), 190U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 191U) comp.handle_input(@nested.InputEvent::PointerMove(c2.to_double(), c3.to_double()), 192U) let frame = comp.compose_frame() ignore(@sys.mkdir_p(out)) assert_eq(@sys.write_file_bytes(out + "/screenshot-editor.png", @renderer.encode_png_compressed(frame)), 0) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 193U) let saved = comp.screenshot_editor().unwrap().export() assert_eq(@sys.write_file_bytes(out + "/screenshot-editor-saved.png", @renderer.encode_png_compressed(saved)), 0) }