Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 08:26:26 -05:00
commit f40701771e
69 files changed
+7703

No files matched your search

+129
View File
@@ -0,0 +1,129 @@
#! 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("""
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="20">
<rect x="0" y="0" width="10" height="10" fill="red"/>
<circle cx="25" cy="10" r="6" fill="#0000ff"/>
<rect x="10.5" y="15" width="1" height="5" fill="lime"/>
<path d="M30 0 h10 v10 h-10 z m3 3 v4 h4 v-4 z" fill="black" fill-rule="evenodd"/>
</svg>
""");
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 := "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 5'><rect width='5' height='5' fill='#808080'/></svg>";
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("""
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="30">
<line x1="10" y1="5" x2="30" y2="5" stroke="black" stroke-width="4"/>
<line x1="10" y1="15" x2="30" y2="15" stroke="black" stroke-width="4" stroke-linecap="square"/>
<line x1="10" y1="25" x2="30" y2="25" stroke="black" stroke-width="4" stroke-dasharray="5 5"/>
</svg>
""");
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("""
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="4">
<linearGradient id="ramp"><stop offset="0" stop-color="black"/><stop offset="1" stop-color="white"/></linearGradient>
<rect width="256" height="4" fill="url(#ramp)"/>
</svg>
""");
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("""
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="40" height="20">
<defs>
<rect id="block" width="10" height="10"/>
<clipPath id="left"><rect width="25" height="20"/></clipPath>
</defs>
<g opacity="0.5" fill="blue">
<use xlink:href="#block"/>
<use xlink:href="#block" x="5"/>
</g>
<rect x="20" y="0" width="10" height="10" fill="red" clip-path="url(#left)"/>
</svg>
""");
# 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("""
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="10" style="color: #00ff00">
<style>
.warm { fill: orange }
#hot { fill: red }
g rect { stroke: none }
</style>
<rect class="warm" width="10" height="10" fill="blue"/>
<rect class="warm" id="hot" x="10" width="10" height="10"/>
<rect x="20" width="10" height="10" style="fill: currentColor"/>
<rect x="30" width="10" height="10" fill="purple" fill-opacity="0.5"/>
</svg>
""");
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("<svg xmlns='http://www.w3.org/2000/svg'><rect></svg>"), image::image_error::malformed));
t.expects(fails(image::decode("<svg xmlns='http://www.w3.org/2000/svg' width='0' height='10'/>"), image::image_error::malformed));
t.expects(fails(image::decode("<svg xmlns='http://www.w3.org/2000/svg' width='100000' height='100000'/>"), image::image_error::too_large));
# Path data broken partway is drawn as far as it goes.
val partial := draw("<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><path d='M0 0 H10 V10 H0 Z L oops'/></svg>");
t.expects(partial.at(5, 5) == rgba(0, 0, 0, 255));
}