Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_graphics/tests/svg_doc_gradient_href.rs

2.3 KiB, 7 runs

created by r1870400018:76421, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A gradient with no stops of its own takes them from the gradient its `xlink:href` (or `href`) names,
2//! per SVG 1.1 §13.2.2. Inkscape writes every positioned gradient this way, a stopless
3//! `<linearGradient gradientTransform=.. xlink:href="#redBody"/>` over a shared swatch. Read as a
4//! gradient with no stops, the fill drew nothing, and the red fire hydrant in the PreSeed AI infographic
5//! came out transparent (UNBLOCK-85, 1 Nov 2026).
6//!
7//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
8//! Anthropic Claude
9
10use oxedyne_fe2o3_core::prelude::*;
11use oxedyne_fe2o3_graphics::{
12 colour::Rgba,
13 svg_doc::{
14 self,
15 SvgOp,
16 },
17};
18
19/// The colour of each filled path in the document, in document order.
20fn fill_colours(src: &str) -> Outcome<Vec<Rgba>> {
21 let pic = res!(svg_doc::read_document(src));
22 let mut out = Vec::new();
23 for op in &pic.ops {
24 if let SvgOp::Fill { colour, .. } = op {
25 out.push(*colour);
26 }
27 }
28 Ok(out)
29}
30
31#[test]
32fn a_stopless_gradient_inherits_the_stops_its_href_names() -> Outcome<()> {
33 // `#a` is a stopless link to `#b`, which links on to the swatch `#s`: the chain resolves to the
34 // swatch's mean, (200, 0, 0) and (0, 0, 100) averaged. `#own` has stops of its own, which win over
35 // its link. `#loop` and `#pool` name each other with no stops anywhere, and draw nothing.
36 let src = r##"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100">
37 <defs>
38 <linearGradient id="a" x1="0" x2="10" gradientUnits="userSpaceOnUse" xlink:href="#b"/>
39 <linearGradient id="s"><stop stop-color="#c80000" offset="0"/><stop stop-color="#000064" offset="1"/></linearGradient>
40 <linearGradient id="b" gradientTransform="scale(2 1)" href="#s"/>
41 <linearGradient id="own" xlink:href="#s"><stop stop-color="#00ff00" offset="0"/></linearGradient>
42 <linearGradient id="loop" xlink:href="#pool"/>
43 <linearGradient id="pool" xlink:href="#loop"/>
44 </defs>
45 <path d="M0 0h10v10h-10z" fill="url(#a)"/>
46 <path d="M20 0h10v10h-10z" fill="url(#own)"/>
47 <path d="M40 0h10v10h-10z" fill="url(#loop)"/>
48 </svg>"##;
49 let c = res!(fill_colours(src));
50 let want = vec![Rgba::new(100, 0, 50, 255), Rgba::new(0, 255, 0, 255)];
51 if c != want {
52 return Err(err!("Fills read {:?}, expected {:?}", c, want; Invalid, Output));
53 }
54 Ok(())
55}