Skip to content

Provide capability to modify SVG before str serialization #13

@dsgallups

Description

@dsgallups

Hello! I'm working on a project that requires adding additional attributes to particular xml nodes of the svg, of which are derived by the svg's external environment. In other words, I would like to target particular shapes on the SVG using css animations via an id attribute or inline style.

Since the ToSvg accepts a Box<dyn ToSvgStr>, it is difficult to add additional attributes to the nodes. If it's alright, I'd like to suggest some approaches (and implement one) to allow users to edit values for an SVG prior to and/or during serialization. Both invert/detach the connection between ToSvg and ToSvgStr

  1. Allow passing in a Vec of targeting attributes during serialization via .to_svg()

An implementation could include attaching an identifier to svg's Node types. i.e:

pub struct SvgNode {
   id: Option<String>,
   node: Box<dyn svg::Node>
}

and using the assign method to update attributes.

The drawback is that after processing of the svg data into an svg::Document, it's difficult (impossible?) to add additional attributes to the SVG, since there is no way to identify a type that implements Node.

This partially does what exists now...so maybe would it be alright to "target" particular nodes with additional attributes by passing in a Vec<(Identifier, (String, String))> into .to_svg()?

  1. Create a tree of nodes prior to implementing to_string() and serialize via quick_xml::Writer.
    Something of this sort?
pub struct SvgNode {
    tag: String,
    id: Option<String>,
    attrs: Vec<(String, String)>,
    children: Option<Vec<SvgNode>>
}

The drawback of course is that this is slower than the existing implementation of ToSvgStr. What if there could be a hybrid situation of the two where only ToSvg utilizes this?

If an approach here feels like it would be an improvement to the crate, I would be more than happy to contribute an implementation. Thank you for your consideration!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions