
You open an SVG drawing in CADCraft and everything looks right, except one rectangle. It sits a little left of and above where it should be. Nothing is broken about the file. It opens fine in a browser. The rectangle just ignored an instruction written on it.
CADCraft is an open source CAD app (computer aided design, the kind of program used for technical drawings), written in Rust. Its SVG importer had exactly this gap, and my fix was merged in storytold/cadcraft#2602. There was no issue for it. I found it while writing a test for a different PR.
Two words first: SVG and transform
SVG is a file format that describes a drawing as text. <rect x="1" y="1" width="4" height="3"/> is a rectangle with its corner at (1, 1), 4 wide and 3 tall. CADCraft reads files like this and turns every shape into lines and curves in millimetres, ready for jobs like a laser cutter or a pen plotter. So every shape has to land exactly where the file says.
Any shape can also carry a transform attribute that changes where and how it's drawn. translate(5,2) moves it 5 right and 2 down, rotate(30) turns it, scale(2,1) stretches it, and skewX(20) slants it, a bit like italic text.
Transforms can also sit on a group, <g>, and then they apply to everything inside it. Groups can sit inside groups, and the effects build up from the outside in. Think of sheets of tracing paper on a desk. Each group is a sheet you can slide or turn, and anything drawn on a sheet moves with it and with every sheet holding it. A shape's own transform is one last nudge for that one shape.
What went wrong
The SVG standard says every graphics element can carry a transform, not just groups (SVG 1.1 §7.5). CADCraft's reader only applied the ones on groups. So this rectangle:
<rect x="1" y="1" width="4" height="3" transform="translate(5,2)"/>
came in spanning x 1 to 5 instead of 6 to 10. The same went for all seven shape kinds the reader knows: rect, circle, ellipse, line, polyline, polygon and path.
How I found it
Another PR of mine, #2202, teaches the importer rounded rectangle corners. In review, FredApps listed the corner rules SVG defines and asked for a test of each, plus a rectangle under a transform that isn't a similarity. A similarity keeps proportions: moving, turning, and scaling by the same amount in every direction. A skew doesn't, so round corners should become stretched, oval ones.
I wrote that test with the transform right on the rect, and the rectangle came in with no skew at all. That had nothing to do with corners, so it got its own PR.
Why it happened
The reader stores each transform as a matrix, a small grid of numbers that can be combined with another one to mean "do this, then that". It keeps them on a stack, a list where you only add or remove at the top. When it enters a group, it combines the group's transform with the current top and pushes the result. Trimmed to that part:
let current = *self.stack.last().unwrap_or(&Mat3::IDENTITY);
let next = match attr(e, b"transform").as_deref().and_then(parse_transform) {
Some(local) => current.then_before(local),
None => current,
};
self.stack.push(next);
In words: take what's on top (or "no change" if nothing is), add this group's transform if it has one, and push. When the group closes, its entry is popped off. Every shape reader maps its points through the top of the stack, which the code calls cur().
Shapes, though, go through a different function, on_element, and that function never looked at the shape's own transform.
The fix
on_element now does for one shape what a group does, and then undoes it:
// A shape's own `transform` (SVG 1.1 §7.5) maps it after its groups', for this element only.
let local = attr(e, b"transform").as_deref().and_then(parse_transform);
if let Some(local) = local {
self.stack.push(self.cur().then_before(local));
}
let read = self.shape(name, e);
if local.is_some() {
self.stack.pop();
}
read
If the shape has a transform, push "everything so far, then this shape's transform", read the shape, then pop, so the next shape doesn't inherit it. The code that reads each kind of shape moved, unchanged, into a new function called shape.
Two details mattered. First, the pop has to happen right here. The code that runs at a closing tag only pops for groups and other containers, and a shape can be written self closed (<line/>) or with an end tag (<line></line>), so nothing else would ever remove its entry. Second, the result of reading the shape is kept in read and returned after the pop. Rust has a ? shortcut that returns early on an error; using it here would skip the pop, and every later shape would be drawn with a leftover transform.
Nothing inside the shape readers changed. They already drew through cur(), so a rotated circle stays a circle and a skewed one becomes an ellipse, the same as inside a group.
Testing against what already worked
Group transforms were already right, so I used them as the reference to compare against. The test, svg_element_transforms_apply_like_a_group_around_the_element, takes each of the seven shapes under five transforms (a move, a rotation about a point, an uneven scale, a skew and a raw matrix) and reads it twice:
let on_element = shape.replacen("/>", &format!(r#" transform="{t}"/>"#), 1);
let in_group = format!(r#"<g transform="{t}">{shape}</g>"#);
assert_eq!(geometry(&read_svg(&on_element)), geometry(&read_svg(&in_group)), "{t} on {shape}");
Once with the transform on the shape, once with the shape wrapped in a group carrying the same transform, and the two results must match exactly. That's 35 combinations, and I never had to work out an expected coordinate by hand. The test also checks that a rotated circle stays a circle, a skewed one becomes an ellipse, a shape's transform comes after its parent group's, the end tag form works, and the next shape stays where it was. It fails on main and passes with the change.
Review
austin62jake approved it after merging it onto the current main together with six other PRs, with cargo xtask ci, CADCraft's full check, passing. They called it "the smallest correct change", and pointed out that the push and pop stay balanced even when a shape fails to read. tanish2k09 merged it on 2 November 2026. Thanks to both, and to FredApps, whose test request turned it up.
What I'm keeping from this one
When a feature already works in one place, let it check the place that doesn't. Comparing against the group version covered every combination without a single hand written coordinate.
And edge case tests asked for in review are worth more than they look. The one FredApps asked for on one PR found a bug for another.
Have you had an SVG that looked right in the browser and wrong after importing it somewhere? I'd like to hear what it was.
About the author
- I contribute to open source developer tools: Tauri, Biome, oxc, Julia, Quarkus, Grafana, Burn and a few more. I start from an issue I can reproduce and a test that fails, then fix it. Here I write up the ones worth telling: what broke, why, and how the fix got through review.
Comments
Join the discussionNo comments yet. Be the first to comment.





