Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / canvas / path_tests.lucb

## Tests for pen paths (luce-vector's vector/paths.lucb).

test "a pen path is a curved shape layer: drawn, undone, moved, restyled and saved":
    var canvas = try Canvas(64, 48)
    defer canvas.close()
    let layer = try canvas.add_layer("Path")
    canvas.bind_headless() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    let document = canvas.document else trap("bound")
    let pixels = try new f32[64 * 48 * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    # A "D": a curve from (10, 10) out to x 40 and back to (10, 40), closed by a straight edge.
    try canvas.add_point(10.0, 10.0)
    try canvas.add_point(10.0, 10.0)
    try canvas.add_point(50.0, 10.0)
    try canvas.add_point(10.0, 40.0)
    try canvas.add_point(50.0, 40.0)
    try canvas.add_point(10.0, 40.0)
    try canvas.set_path(layer, true, true, 1.0, 0.0, 0.0, false, 0.0, 0.0, 0.0, 1.0)
    assert(canvas.undo_name() == "Path" and (try canvas.layer_path_count(0)) == 2 and (try canvas.layer_path_closed(0)))
    assert((try canvas.layer_path_value(0, 1, 2)) == 50.0 and (try canvas.layer_shape_value(0, 0)) == 4.0)
    try ((document.find((u64)layer) else trap("layer")).tiles() else trap("tiles")).read(pixels)
    # Inside the bulge (it reaches x 40 at y 25), outside past it, and above the path.
    assert(pixels[(25 * 64 + 35) * 4] > 0.99 and pixels[(25 * 64 + 35) * 4 + 3] > 0.99)
    assert(pixels[(25 * 64 + 45) * 4 + 3] < 0.01 and pixels[(5 * 64 + 20) * 4 + 3] < 0.01)
    # Undone it is an empty pixel layer again; redone, the path.
    try canvas.undo()
    assert((try canvas.layer_path_count(0)) == 0 and not (try canvas.layer_shaped(0)))
    try canvas.redo()
    assert((try canvas.layer_path_count(0)) == 2)
    # Moved: the anchors come back where they went, in document pixels.
    try canvas.move_layer(layer, 5, 0)
    assert((try canvas.layer_path_value(0, 0, 0)) == 15.0 and (try canvas.layer_path_value(0, 1, 3)) == 40.0)
    # Restyled: the anchors stay.
    try canvas.restyle_shape(layer, true, 0.0, 1.0, 0.0, true, 0.0, 0.0, 1.0, 2.0, 0.0, 3)
    assert((try canvas.layer_path_count(0)) == 2 and (try canvas.layer_shape_value(0, 11)) == 1.0)
    # Saved and opened again, it is the same path.
    try canvas.save_package("build/canvas-path.l2d")
    let reopened = try Canvas.open("build/canvas-path.l2d")
    defer reopened.release()
    assert((try reopened.value().layer_path_count(0)) == 2 and (try reopened.value().layer_path_closed(0)))
    assert((try reopened.value().layer_path_value(0, 0, 0)) == 15.0 and (try reopened.value().layer_path_value(0, 1, 2)) == 55.0)
    # An open path of one anchor is refused, and leaves the layer alone.
    try canvas.add_point(1.0, 1.0)
    try canvas.add_point(1.0, 1.0)
    try canvas.add_point(1.0, 1.0)
    var refused = false
    canvas.set_path(layer, false, true, 1.0, 1.0, 1.0, false, 0.0, 0.0, 0.0, 1.0) catch failure:
        refused = failure.code == invalid
    assert(refused and (try canvas.layer_path_count(0)) == 2)