Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(svg): Reduce size of SVG output #74

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

sorairolake
Copy link
Contributor

r#"<rect x="0" y="0" width="{w}" height="{h}" fill="{bg}"/>"#,

<rect x="0" y="0" width="204" height="204" fill="#ffff80"/>

can also be represented as

<path d="M0 0h204v204h-204z" fill="#ffff80"/>

write!(self.svg, "M{left} {top}h{width}v{height}H{left}V{top}").unwrap();

<path fill="#800000" d="M36 24h12v12H36V24"/>

can also be represented as

<path fill="#800000" d="M36 24h12v12h-12z"/>

With these changes, file size of the output SVG is reduced.

@@ -51,7 +51,7 @@ impl<'a> RenderCanvas for Canvas<'a> {
r#"<svg xmlns="http://www.w3.org/2000/svg""#,
r#" version="1.1" width="{w}" height="{h}""#,
r#" viewBox="0 0 {w} {h}" shape-rendering="crispEdges">"#,
r#"<rect x="0" y="0" width="{w}" height="{h}" fill="{bg}"/>"#,
r#"<path d="M0 0h{w}v{h}h-{w}z" fill="{bg}"/>"#,
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
r#"<path d="M0 0h{w}v{h}h-{w}z" fill="{bg}"/>"#,
r#"<path d="M0 0h{w}v{h}H0z" fill="{bg}"/>"#,

this should be even shorter? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which is better "M{left} {top}h{width}v{height}h-{width}z" or "M{left} {top}h{width}v{height}H{left}z"?

Copy link
Owner

Choose a reason for hiding this comment

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

@sorairolake if left < width * 10 use H{left} otherwise h-{width}.

but always using h-{width} should compress better.

@sorairolake sorairolake requested a review from kennytm September 26, 2024 13:38
@kennytm kennytm merged commit c578416 into kennytm:master Sep 26, 2024
6 checks passed
@sorairolake sorairolake deleted the optimize-svg-output branch September 26, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants