Skip to content

Idea: Lossless precision reduction #791

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

Open
loominade opened this issue Sep 12, 2017 · 2 comments
Open

Idea: Lossless precision reduction #791

loominade opened this issue Sep 12, 2017 · 2 comments

Comments

@loominade
Copy link

here is just a quick Idea. I have no idea what an algorithm would look like that actually do that.

The following two svgs are visually identical:

<svg xmlns="http://www.w3.org/2000/svg" width="250" height="250" viewBox="0, 0, 250, 250">
<path d="M37.5 37.5l25 50-25 25 25 25-25 50h25l25 50 25-50 75 50 25-25-100-75 125-50-25-50-25 25-50-25-25 25h-25l-25-50-25 25z" fill="#FFF"/>
<path d="M37.5 37.5l25 50-25 25 25 25-25 50h25l25 50 25-50 75 50 25-25-100-75 125-50-25-50-25 25-50-25-25 25h-25l-25-50-25 25z" stroke="#000" stroke-width="25" fill="none"/></svg>
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="250" viewBox="0, 0, 10, 10">
<path d="M1.5 1.5l1 2-1 1 1 1-1 2h1l1 2 1-2 3 2 1-1-4-3 5-2-1-2-1 1-2-1-1 1h-1l-1-2-1 1z" fill="#FFF"/>
<path d="M1.5 1.5l1 2-1 1 1 1-1 2h1l1 2 1-2 3 2 1-1-4-3 5-2-1-2-1 1-2-1-1 1h-1l-1-2-1 1z" stroke="#000" fill="none"/></svg>

But the second one is 100 byte smaller because it uses fewer digits. the viewbox-relative coordinates however are the same.

so the idea is so find if all or most of the coordinate inside a document have common factor. In this scenario the number 25 appears very often. we scale all these numbers down so that they can be expressed with a single digit. also we scale up the viewbox so everything will appear the same.

This only creates smaller results if the graphic was created using a grid. This is true for graphics like flags. See for instance the Flag of Italy:
Flag of Italy which can be expressed in a 3 by 2 viewbox.

@GreLI
Copy link
Member

GreLI commented Sep 12, 2017

Yep, you aren't first. See for example #293.

However, it's difficult to implement in real life. What if there is some point that doesn't fit the pattern. Several points? Notable count of points? How would you count this? (I mean float numbers, that requires rounding.)

I don't see an effective algorithm other than scaling the whole image with trials and errors, which is not that effective in matter of execution time. If you know such, please, let us know.

There even an article describing such an algorithm using SVGO. However, in real life with non-integer coordinates it depends on judging is image “breaks”, which cannot be automatized due to subjective nature.

@mahnunchik
Copy link

+1

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

No branches or pull requests

3 participants