Repository navigation
Avoid unnecessary groups around elements to reduce size of exported SVGs - #6247
Conversation
792064e to
397f487
Compare
330b93c to
f22f724
Compare
|
All should be fixed. Thank you for your review, it was helpful :) |
8a0e7ee to
ae187fa
Compare
|
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. |
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. |
|
No worries, there's no rush. The label is just to keep things organized. |
805c2e9 to
c6827c8
Compare
|
All should be fixed :) |
| pub(super) fn render_image(&mut self, image: &Image, size: &Axes<Abs>) { | ||
| pub(super) fn render_image( | ||
| &mut self, | ||
| transform: &Transform, |
There was a problem hiding this comment.
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())), |
There was a problem hiding this comment.
If the initial point is zero, we could skip the M 0 0 (I think?). this happens a lot in the tests.
There was a problem hiding this comment.
I tried it and found out that you can't :( The SVG renderer needs the first M to set the origin point.
| self.xml.write_attribute("transform", &SvgMatrix(state.transform)); | ||
| state = state.with_transform(Transform::identity()); | ||
|
|
||
| let path = convert_geometry_to_path(&state.transform, &shape.geometry); |
There was a problem hiding this comment.
The transform is always identity at this point, so we could remove this argument, right?
| // reset transform because we applied it as element's transform attribute | ||
| // we kept it before for shape paint transform |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I decided to remove it entirely. When I removed the argument and the setting of the state to identity transform, it is more obvious.
| self.xml.write_attribute("transform", "scale(1, -1)"); | ||
| self.xml.write_attribute( | ||
| "transform", | ||
| &format!("{} scale(1, -1)", &SvgMatrix(state.transform)), |
There was a problem hiding this comment.
maybe we could directly integrate the scale into the transform?
| } | ||
|
|
||
| /// A builder for SVG path. This is used to build the path for a glyph. | ||
| struct SvgGlyphPathBuilder(pub EcoString, pub Ratio); |
There was a problem hiding this comment.
is there a particular reason why SvgGlyphPathBuilder and SvgRelativePathBuilder are separate? can glyphs not use the new, smarter builder?
There was a problem hiding this comment.
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.
47e17ba to
f46a904
Compare
f46a904 to
9181a7a
Compare
laurmaedje
left a comment
There was a problem hiding this comment.
I'm sorry that this keeps going back and forth slowly. I tried to find all things worth mentioning each time.
| FrameItem::Image(image, size, _) => { | ||
| self.render_image(&state.pre_translate(*pos), image, size) | ||
| } | ||
| FrameItem::Link(dest, size) => self.render_link(dest, *size), |
There was a problem hiding this comment.
The link case that was recently added also needs to consider the position now.
|
|
||
| self.render_frame(state, ts, &page.frame); | ||
| // frame is a hard frame -> apply transform | ||
| self.xml.start_element("g"); |
There was a problem hiding this comment.
isn't this group unnecessary when we have an identify transform? Previously, render_frame had a check for that.
| 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)); |
There was a problem hiding this comment.
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.
|
|
||
| pub fn convert_curve(curve: &Curve) -> EcoString { | ||
| let mut builder = SvgPathBuilder::default(); | ||
| pub fn convert_curve(transform: &Transform, curve: &Curve) -> EcoString { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| ) { | ||
| let url = convert_image_to_base64_url(image); | ||
| self.xml.start_element("image"); | ||
| self.xml.write_attribute("transform", &SvgMatrix(state.transform)); |
There was a problem hiding this comment.
In the shape case there was a check against identity here. It would be nice if that check was consistently applied.
9181a7a to
47ebef4
Compare
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.
47ebef4 to
2aaccdf
Compare
|
Hello, everything is fixed. Sorry for the delay, I forgot :D |
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`.
|
Thanks! |
…VGs (typst#6247) Co-authored-by: Laurenz <[email protected]>
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 asSvgGlyphPathBuilder. Using relative coordinates allows to transform the path without changing value of thedattribute.To transform images,
xandyattributes 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.