rust-skia / rust-skia

Rust Bindings for the Skia Graphics Library
MIT License
1.41k stars 130 forks source link

Improve SVG support #1014

Open Aiving opened 1 month ago

Aiving commented 1 month ago

Currently, skia-safe provides relatively little of the SVG functionality that Skia has.

There is nothing wrong with this, but the current implementation is only suitable for fairly simple SVG rendering use cases. When, for example, the fill="currentColor" attribute starts to be used, there is no way to set a color value for currentColor other than the default. As a result, currentColor becomes just an alias for black.

Also the following functions are not available in Dom struct: get_root, render_node, container_size (although set_container_size is there), find_node_by_id. As a consequence, there are also no structs like SvgNode, Svg and PresentationContext. Actually adding them is not a complicated process. Almost. I've been trying to implement it myself for a couple of days already. The main headache in this case is the SkSVGProperty class in Skia, which for some reason in bindings turns into type SkSVGProperty_ValueT<T> = T and is not even used anywhere.

I would also like to note that I still don't quite understand how render_node and find_node_by_id should be used, as they both take as one of the parameters the id of a node, the value of which for, for example, root node, I don't know.

DerCommander323 commented 1 month ago

I too hope that SVG support can be improved, as I currently can't find a way to resize an SVG to an absolute size because it's not possible to get the actual dimensions of the SVG.

geom3trik commented 1 month ago

I too hope that SVG support can be improved, as I currently can't find a way to resize an SVG to an absolute size because it's not possible to get the actual dimensions of the SVG.

You can do svg.inner().fContainerSize.fWidth and svg.inner().fContainerSize.fHeight to get its size if specified.

DerCommander323 commented 1 month ago

I too hope that SVG support can be improved, as I currently can't find a way to resize an SVG to an absolute size because it's not possible to get the actual dimensions of the SVG.

You can do svg.inner().fContainerSize.fWidth and svg.inner().fContainerSize.fHeight to get its size if specified.

Oh, thank you so much. I was looking at Skia's api docs which say to use getRoot()->intrinsicSize() which I thought was missing from the bindings.

HinTak commented 3 weeks ago

render_node is used by ot-svg fonts. You'll need to get one of those...

See the skia-python example: https://github.com/rougier/freetype-py/blob/master/examples/ot-svg-example-skia.py

Which loads this skia-python svg code: https://github.com/rougier/freetype-py/blob/master/examples/skia_ot_svg_module.py

Basically in an ot-svg font, many glyphs are collected into one svg document (to allow re-use) but individual glyphs are id'ed as layers with ids.