Skip to content

Avoid unnecessary groups around elements to reduce size of exported SVGs - #6247

Merged
laurmaedje merged 6 commits into
typst:mainfrom
Bequen:svg-optimize-away-groups
Oct 3, 2025
Merged

laurmaedje merged 6 commits into
typst:mainfrom
Bequen:svg-optimize-away-groups

Conversation

@Bequen

@Bequen Bequen commented May 5, 2025

Copy link
Copy Markdown
Contributor

Every element like path had group around it that defined it's transform. These changes remove the groups and move the transformations to the element itself. This reduces the overall size of the output SVG.

Added new SVG path builder using relative coordinates SvgRelativePathBuilder. The previous with global coordinates is still used for glyph paths as SvgGlyphPathBuilder. Using relative coordinates allows to transform the path without changing value of the d attribute.

To transform images, x and y attributes are used. To transform groups, the group's transform is added to the current transform, which is passed further down. The reason is that the exported SVG had many nested groups with transforms. This change merges all these transforms into a single one.

Changes include basic visual test.

@Bequen
Bequen force-pushed the svg-optimize-away-groups branch 5 times, most recently from 792064e to 397f487 Compare May 5, 2025 16:28

@mattfbacon mattfbacon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of thoughts

Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/lib.rs Resolved
Comment thread crates/typst-svg/src/text.rs Resolved
@Bequen
Bequen force-pushed the svg-optimize-away-groups branch 2 times, most recently from 330b93c to f22f724 Compare May 31, 2025 12:28
@Bequen

Bequen commented May 31, 2025

Copy link
Copy Markdown
Contributor Author

All should be fixed. Thank you for your review, it was helpful :)

@Bequen
Bequen force-pushed the svg-optimize-away-groups branch 2 times, most recently from 8a0e7ee to ae187fa Compare May 31, 2025 12:39
@MDLC01 MDLC01 mentioned this pull request May 31, 2025
3 tasks
@laurmaedje laurmaedje added the svg Anything about the file format for people who like to have fun. label Jun 4, 2025
@laurmaedje

laurmaedje commented Jun 10, 2025 •

Copy link
Copy Markdown
Member

I ran a script to compare the output of the old vs updated SVG export and there were some changes. I attached them and the script (with txt extension because GitHub) for your convenience. In each image, left is your branch, right is main, and center is the diff. Most changes are very minor and can be ignored, but some files (e.g. curve-move-multiple-even-odd) changed more.

diff.zip
typst-svg-cmp.sh.txt

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Jun 23, 2025
@Bequen

Bequen commented Jun 24, 2025

Copy link
Copy Markdown
Contributor Author

I ran a script to compare the output of the old vs updated SVG export and there were some changes. I attached them and the script (with txt extension because GitHub) for your convenience. In each image, left is your branch, right is main, and center is the diff. Most changes are very minor and can be ignored, but some files (e.g. curve-move-multiple-even-odd) changed more.

diff.zip typst-svg-cmp.sh.txt

Hello, sorry for taking so long. I did not have much time lately. I fixed the tiling issues, but I cannot seem to figure out the gradients yet.

@laurmaedje

Copy link
Copy Markdown
Member

No worries, there's no rush. The label is just to keep things organized.

@Bequen
Bequen force-pushed the svg-optimize-away-groups branch 4 times, most recently from 805c2e9 to c6827c8 Compare July 27, 2025 13:29
@Bequen

Bequen commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

All should be fixed :)

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Aug 1, 2025
Comment thread crates/typst-svg/src/image.rs Outdated
pub(super) fn render_image(&mut self, image: &Image, size: &Axes<Abs>) {
pub(super) fn render_image(
&mut self,
transform: &Transform,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice if this took state as well for consistency

fn with_translate(pos: Point) -> Self {
// add initial M node to transform the entire path
Self {
path: EcoString::from(format!("M {} {}", pos.x.to_pt(), pos.y.to_pt())),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the initial point is zero, we could skip the M 0 0 (I think?). this happens a lot in the tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread crates/typst-svg/src/shape.rs Outdated
self.xml.write_attribute("transform", &SvgMatrix(state.transform));
state = state.with_transform(Transform::identity());

let path = convert_geometry_to_path(&state.transform, &shape.geometry);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transform is always identity at this point, so we could remove this argument, right?

Comment thread crates/typst-svg/src/shape.rs Outdated
Comment on lines +35 to +36
// reset transform because we applied it as element's transform attribute
// we kept it before for shape paint transform

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is hard to understand for me. Could you rewrite it? It would also be great if you could start comments with a capital letter and end with a full stop to keep it in line with the codebase's style.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to remove it entirely. When I removed the argument and the setting of the state to identity transform, it is more obvious.

Comment thread crates/typst-svg/src/text.rs Outdated
self.xml.write_attribute("transform", "scale(1, -1)");
self.xml.write_attribute(
"transform",
&format!("{} scale(1, -1)", &SvgMatrix(state.transform)),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could directly integrate the scale into the transform?

Comment thread crates/typst-svg/src/lib.rs Outdated
}

/// A builder for SVG path. This is used to build the path for a glyph.
struct SvgGlyphPathBuilder(pub EcoString, pub Ratio);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a particular reason why SvgGlyphPathBuilder and SvgRelativePathBuilder are separate? can glyphs not use the new, smarter builder?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not wanted too broad changes. But you are right that there is no reason for two path builders. I applied the new one to glyphs too.

@Bequen
Bequen force-pushed the svg-optimize-away-groups branch 3 times, most recently from 47e17ba to f46a904 Compare August 11, 2025 12:36
Comment thread crates/typst-svg/src/lib.rs Resolved
@Bequen
Bequen force-pushed the svg-optimize-away-groups branch from f46a904 to 9181a7a Compare August 11, 2025 12:51

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry that this keeps going back and forth slowly. I tried to find all things worth mentioning each time.

Comment thread crates/typst-svg/src/lib.rs Outdated
FrameItem::Image(image, size, _) => {
self.render_image(&state.pre_translate(*pos), image, size)
}
FrameItem::Link(dest, size) => self.render_link(dest, *size),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link case that was recently added also needs to consider the position now.

Comment thread crates/typst-svg/src/lib.rs Outdated

self.render_frame(state, ts, &page.frame);
// frame is a hard frame -> apply transform
self.xml.start_element("g");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this group unnecessary when we have an identify transform? Previously, render_frame had a check for that.

Comment thread crates/typst-svg/src/lib.rs Outdated
self.clip_paths.insert_with(hash, || shape::convert_curve(clip_curve));
let id = self
.clip_paths
.insert_with(hash, || shape::convert_curve(&state.transform, clip_curve));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this sound? The clip_paths are memoized by hash, but the hash only includes the group, not state.transform. I also wonder why it hashes the whole group rather than just the path, but that's unrelated to your PR.

Comment thread crates/typst-svg/src/shape.rs Outdated

pub fn convert_curve(curve: &Curve) -> EcoString {
let mut builder = SvgPathBuilder::default();
pub fn convert_curve(transform: &Transform, curve: &Curve) -> EcoString {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit misleading that the entire transform is passed, but only the translation is used. I wonder whether the could even mask a bug for clip paths?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it's misleading. I changed it to initial_point and the data type to Point to clearly tell it's purpose.

It does not require the entire transform, because it only uses the point for Z tag in path, to know to which point to return. The rotation, scaling, and rest of the transformation is done with transform attribute in the path element.

Comment thread crates/typst-svg/src/image.rs Outdated
) {
let url = convert_image_to_base64_url(image);
self.xml.start_element("image");
self.xml.write_attribute("transform", &SvgMatrix(state.transform));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the shape case there was a check against identity here. It would be nice if that check was consistently applied.

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Sep 2, 2025
@Bequen
Bequen force-pushed the svg-optimize-away-groups branch from 9181a7a to 47ebef4 Compare September 29, 2025 15:30
Every element, like path, text, etc., had a group around them, that defined it's
transform. These changes accumulate the transformations of these groups and release
them to the element itself. This reduces the overall size of the exported SVG,
because those group elements can be removed.

Changed the SVG path builder to use relative coordinates. Using relative
coordinates allows to transform the entire element without changing the entire path.
@Bequen
Bequen force-pushed the svg-optimize-away-groups branch from 47ebef4 to 2aaccdf Compare September 29, 2025 15:32
@Bequen

Bequen commented Sep 29, 2025

Copy link
Copy Markdown
Contributor Author

Hello, everything is fixed. Sorry for the delay, I forgot :D

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Sep 29, 2025
@laurmaedje laurmaedje changed the title SVG Export: Removed unnecessary group around every element to reduce size. Avoid unnecessary groups around elements to reduce size of exported SVGs Oct 3, 2025
This is basically not observable in practice because all transformation elements create `BlockElem`s and these are hard frames so it's not possible to accumulate a transform in both `state.transform` and `group.transform`.
@laurmaedje
laurmaedje enabled auto-merge October 3, 2025 10:24
@laurmaedje
laurmaedje added this pull request to the merge queue Oct 3, 2025
Merged via the queue into typst:main with commit 4f9b9a9 Oct 3, 2025
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

svg Anything about the file format for people who like to have fun.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants