Chlumsky / msdfgen

Multi-channel signed distance field generator
MIT License
3.9k stars 404 forks source link

Svg import confusion #138

Closed ill closed 1 year ago

ill commented 2 years ago

Right now, the only error message we see is: "Failed to load shape from SVG file." which could happen for many reasons. It could be as simple as a typo in the svg path, to some complex internal reasons.

I know the svg importer is limited, but when things fail it's not obvious why. I did a build from source to debug exactly where things failed. It could be helpful to print that info to the console since it's all available in the internal svg importer.

One thing I noticed is if I export an svg from illustrator or some other more complex program, there tend to be all sorts of extra tags with metadata. So even if I have a perfectly valid svg file with a single closed path, it fails unless I remove some of the extra tag data. This wasn't obvious and I could've sworn it wouldn't cause any issues since every other svg loader opened the files successfully, so I thought it was some other issue. Some better error messages would at least help if there aren't plans to make the svg importer fully featured.

I also had an issue where illustrator decided to export my shape as a polygon and not a path, so I had to go in and fix that too. Now that I've dug deep in, I know about these issues. It would be especially good to have an error message saying, svg file can only use single closed paths, found some other element of type polygon, etc...

Also, polygons should in theory be a potentially pretty easy thing to add support for? That's a guaranteed closed path. I might even take a crack at adding that support myself.

ill commented 2 years ago

I ultimately found this little thing online to convert polygon to path.

https://codepen.io/jakealbaugh/pen/GZwgzV

It seems like the conversion is fairly trivial between polygon and path:

The data is virtually identical here:

<polygon class="cls-2" points="0 56 19.56 64 43.56 64 64 56 60 8 44 0 20 0 4 8 0 56"/>
<path class="cls-2" d="M0 56 19.56 64 43.56 64 64 56 60 8 44 0 20 0 4 8 0 56z"/>
nullhook commented 2 years ago

+1 - would be great to handle edge cases here

if (ret == tinyxml2::XMLError::XML_ERROR_FILE_COULD_NOT_BE_OPENED) {
        std::cout << "Can't open file\n";
        return false;
} 

if (ret == tinyxml2::XMLError::XML_ERROR_FILE_NOT_FOUND) {
        std::cout << "File not found\n";
        return false;
}

also, if you need to clean up svg you can use this tool

Chlumsky commented 1 year ago

Support for <polygon> and other SVG elements added in 0286aeb6eb452ec1cd9636b43cf303d33f61eb31.