Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / canvas / brush_tip_tests.lucb

#==============================================================================================
#
#   canvas/brush_tip_tests - The brush's tip, texture, pen and Define Brush Preset
#
#==============================================================================================

## Draw `canvas` once into a 64 × 64 `target`, binding it to the device.
func draw_once(canvas: Canvas*, target: gpu.Texture*) -> !:
    let frame = try target.frame()
    defer frame.release()
    let root = try frame.value().target()
    defer root.release()
    try canvas.draw(root, gpu.Rect(width = 64.0, height = 64.0), 1.0, 0.0, 0.0)
    _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 1.0))

test "a sampled tip and the pen's pressure shape a canvas stroke; Define Brush takes the selection":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var canvas = try Canvas(64, 64)
    defer canvas.close()
    let layer = try canvas.add_layer("Paint")
    var target = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
    defer target.destroy()
    try draw_once(&canvas, &target)
    # A tip whose top half paints; size follows pressure from nothing.
    var tip: u8[256]
    for index in 0..<256:
        tip[index] = 255 if index < 128 else 0
    try canvas.set_brush_tip(tip[..<256], 16, 16)
    try canvas.set_brush(40.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false)
    try canvas.set_brush_dynamic(0, 1)
    try canvas.begin_stroke(layer, 32.0, 32.0, pressure = 0.5)
    # The tip cannot change under a stroke.
    var refused = false
    canvas.set_brush_tip(tip[..<256], 16, 16) catch failure:
        assert(failure.code == invalid)
        refused = true
    assert(refused)
    try canvas.end_stroke()
    try draw_once(&canvas, &target)
    var pixel: u8[4]
    # A 20 pixel dab: its top half painted, its bottom half and past its edge not.
    try target.read(pixel, gpu.Region(32, 26, 1, 1))
    assert(pixel[0] == 255)
    try target.read(pixel, gpu.Region(32, 38, 1, 1))
    assert(pixel[0] == 0)
    try target.read(pixel, gpu.Region(32, 18, 1, 1))
    assert(pixel[0] == 0)
    # Out-of-range dynamics are refused.
    var bad = false
    canvas.set_brush_dynamic(10, 1) catch failure:
        assert(failure.code == invalid)
        bad = true
    assert(bad)
    # Define Brush Preset: the selected pixels as an image, cropped to the selection.
    try canvas.select_rectangle(20, 20, 24, 12)
    let picture = try canvas.selection_image(layer)
    defer picture.release()
    assert((try picture.value().width()) == 24 and (try picture.value().height()) == 12)
    let bytes = try picture.value().rgba8()
    defer free(bytes) in memory.heap
    # Its top-left (document 20, 20) is outside the dab; (12, 6) is the dab's top half, white.
    assert(bytes[3] == 0 and bytes[(6 * 24 + 12) * 4] == 255 and bytes[(6 * 24 + 12) * 4 + 3] == 255)