arklumpus/VectSharp

Styles in the style tag instead of as attributes on the tags?

Closed this issue · 4 comments

Hey Giorgio, nice work on the library!

I'm experimenting with using your library to handle widget rendering in my Map Projection software and one feature I'd like is the ability to have all the style information output as css in the style tag, rather than as attributes on the various paths.

My use case involves passing the svg file to the user for further editing, and all the styles for every line will be identical, so for file size and ease of editing, I'd like to consolidate the style information.

Does VectSharp already support such a thing, or is it something you're interested in adding?

Hi! Sorry for the delay.

Unfortuately, VectSharp.SVG currently does not support this; I can think about it, but I have no idea when/if I will be able to implement it.

In the meantime, if you know (or have a way of knowing) the attributes that are the same for all objects, maybe you could "post-process" the SVG code, e.g. by replacing stroke="#000000" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" fill="none" transform="matrix(1,0,0,1,0,0)" with class="myClass" or something like this?

From what I understand, that code uses XML entities to compress the attributes; this is not really necessary for an SVG document, because you can just use CSS classes.

I just released VectSharp.SVG version 1.7.0, which adds an optional useStyles parameter to the SaveAsSVG extension methods. The default value (false) results in the usual behaviour (SVG presentation attributes as individual attributes on the SVG elements), but if you set this to true, CSS classes will be used instead. Classes are reused if possible, but only if multiple elements have exactly the same style (e.g., stroke colour, stroke thickness, etc.).

Hopefully this will be helpful with your use case!

I'm closing this for now, feel free to reopen or to create another issue if you have any problems!