#! Contains the tests of `image::svg`: shapes, sizes, strokes, gradients, #! groups, and style sheets drawn, and broken SVG refused. import std::testing; import image; # Draws an SVG at its own size. def draw(text: strview): image::image { return read_back(text); } @test def draws_svg_shapes(t: &mut tester): void { val made := draw(""" """); t.expects(made.width == 40 && made.height == 20); t.expects(made.at(5, 5) == rgba(255, 0, 0, 255)); t.expects(made.at(25, 10) == rgba(0, 0, 255, 255)); t.expects(made.at(15, 5) == rgba(0, 0, 0, 0)); # Half a pixel covered is half as opaque, and still the color. val half := made.at(10, 17); t.expects(half[1] == 255 && half[3] >= 126 && half[3] <= 129); # The even-odd rule leaves a hole where the inner square crosses the outer. t.expects(made.at(31, 1) == rgba(0, 0, 0, 255)); t.expects(made.at(35, 5) == rgba(0, 0, 0, 0)); } @test def draws_svg_at_the_size_asked(t: &mut tester): void { val text := ""; val own := draw(text); t.expects(own.width == 10 && own.height == 5); val wide := image::decode(text, image::svg_size { width = 100 }) else (_) { t.expects(false); return; }; t.expects(wide.width == 100 && wide.height == 50); t.expects(wide.at(25, 25) == rgba(128, 128, 128, 255) && wide.at(75, 25) == rgba(0, 0, 0, 0)); val scaled := image::decode(text, image::svg_size { scale = 3 }) else (_) { t.expects(false); return; }; t.expects(scaled.width == 30 && scaled.height == 15); # A view box of another shape is fit within, centered. val square := image::decode(text, image::svg_size { width = 20, height = 20 }) else (_) { t.expects(false); return; }; t.expects(square.at(5, 2) == rgba(0, 0, 0, 0) && square.at(5, 10) == rgba(128, 128, 128, 255)); } @test def strokes_svg_paths(t: &mut tester): void { val made := draw(""" """); t.expects(made.at(20, 5) == rgba(0, 0, 0, 255) && made.at(20, 3) == rgba(0, 0, 0, 255) && made.at(20, 8) == rgba(0, 0, 0, 0)); # A butt cap ends at the end, and a square one past it. t.expects(made.at(8, 5) == rgba(0, 0, 0, 0) && made.at(8, 15) == rgba(0, 0, 0, 255)); # Dashes, then gaps, five long each. t.expects(made.at(12, 25) == rgba(0, 0, 0, 255) && made.at(17, 25) == rgba(0, 0, 0, 0) && made.at(22, 25) == rgba(0, 0, 0, 255)); } @test def paints_svg_gradients(t: &mut tester): void { val made := draw(""" """); val middle := made.at(128, 2); t.expects(middle[0] >= 126 && middle[0] <= 130 && middle[3] == 255); t.expects(made.at(0, 2)[0] <= 2 && made.at(255, 2)[0] >= 253); } @test def composites_svg_groups(t: &mut tester): void { val made := draw(""" """); # The group is drawn whole, then made half as opaque, so where its parts # overlap is no more opaque than where they don't. val alone := made.at(2, 5); val overlapped := made.at(7, 5); t.expects(alone[2] == 255 && alone[3] >= 127 && alone[3] <= 128); t.expects(overlapped[3] == alone[3]); # The clip keeps the red left of 25. t.expects(made.at(22, 5) == rgba(255, 0, 0, 255) && made.at(27, 5) == rgba(0, 0, 0, 0)); } @test def styles_svg_by_its_sheets(t: &mut tester): void { val made := draw(""" """); t.expects(made.at(5, 5) == rgba(255, 165, 0, 255)); t.expects(made.at(15, 5) == rgba(255, 0, 0, 255)); t.expects(made.at(25, 5) == rgba(0, 255, 0, 255)); val faint := made.at(35, 5); t.expects(faint[0] == 128 && faint[3] >= 127 && faint[3] <= 128); } @test def refuses_broken_svg(t: &mut tester): void { t.expects(fails(image::decode(""), image::image_error::malformed)); t.expects(fails(image::decode(""), image::image_error::malformed)); t.expects(fails(image::decode(""), image::image_error::too_large)); # Path data broken partway is drawn as far as it goes. val partial := draw(""); t.expects(partial.at(5, 5) == rgba(0, 0, 0, 255)); }