oxedyne/fe2o3/fe2o3_graphics/tests/svg_doc_skew.rs
2.0 KiB, 1 run
created by r1870400018:76009, 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 | //! An SVG `skewX`/`skewY` transform shears the outline it frames. Inkscape writes them for isometric |
| 2 | //! labels; read as the identity, the PreSeed AI infographic's "WEIGHTS" and white "COMPUTE" labels on |
| 3 | //! the server were drawn unsheared, far from their places (30 Oct 2026). |
| 4 | //! |
| 5 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 6 | //! Anthropic Claude |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_graphics::{ |
| 10 | path::Bounds, |
| 11 | svg_doc::{ |
| 12 | self, |
| 13 | SvgOp, |
| 14 | }, |
| 15 | transform::Transform, |
| 16 | }; |
| 17 | |
| 18 | /// The bounds of each filled path in the document, in the picture frame. |
| 19 | fn fills(src: &str) -> Outcome<Vec<Bounds>> { |
| 20 | let pic = res!(svg_doc::read_document(src)); |
| 21 | let mut out = Vec::new(); |
| 22 | for op in &pic.ops { |
| 23 | if let SvgOp::Fill { path, .. } = op { |
| 24 | out.push(res!(path.bounds(&Transform::IDENTITY).ok_or_else(|| err!("Empty fill"; Missing)))); |
| 25 | } |
| 26 | } |
| 27 | Ok(out) |
| 28 | } |
| 29 | |
| 30 | fn near(a: f32, b: f32) -> bool { |
| 31 | (a - b).abs() < 1e-3 |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn a_skew_y_and_a_skew_x_shear_their_outlines() -> Outcome<()> { |
| 36 | let src = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> |
| 37 | <path transform="skewY(-30)" d="M10 50h10v10h-10z"/> |
| 38 | <path transform="skewX(45)" d="M0 10h10v10h-10z"/> |
| 39 | </svg>"#; |
| 40 | let b = res!(fills(src)); |
| 41 | if b.len() != 2 { |
| 42 | return Err(err!("Expected 2 fills, read {}", b.len(); Invalid, Output)); |
| 43 | } |
| 44 | // skewY(-30): y' = y - tan(30°) x, so x in [10, 20] lifts the box's top from 50 to 50 - 11.547. |
| 45 | let t = 30f32.to_radians().tan(); |
| 46 | let (y0, y1) = (50.0 - 20.0 * t, 60.0 - 10.0 * t); |
| 47 | if !(near(b[0].x0, 10.0) && near(b[0].x1, 20.0) && near(b[0].y0, y0) && near(b[0].y1, y1)) { |
| 48 | return Err(err!("skewY(-30) box is {:?}, expected x 10..20, y {}..{}", b[0], y0, y1; Invalid, Output)); |
| 49 | } |
| 50 | // skewX(45): x' = x + y, so the box spans x 10..30 at y 10..20. |
| 51 | if !(near(b[1].x0, 10.0) && near(b[1].x1, 30.0) && near(b[1].y0, 10.0) && near(b[1].y1, 20.0)) { |
| 52 | return Err(err!("skewX(45) box is {:?}, expected x 10..30, y 10..20", b[1]; Invalid, Output)); |
| 53 | } |
| 54 | Ok(()) |
| 55 | } |