Closed mgarin closed 4 years ago
The libray doesn't currently have a way to do that. If you want to duplicate bits of the SVG, you'll need to manipulate the tree of nodes directly. The SVGDiagram structure is pretty straight forward, so you might be able to get away with reflection for the inner nodes. You could also walk the tree manually and make copies of the nodes you encounter.
Thanks for the answer!
I tried copying SVGRoot
element only & replacing it in the SVGDiagram
through setRoot ( ... )
method, but it seem to be breaking something - root is fully copied, but when icon is painted it's simply empty.
At the same time - copying the whole SVGIcon
along with it's SVGUniverse
seem to work just fine, so I'll stick to this approach for now. That of course forces me to use separate SVGUniverse
for each SVGIcon
so I don't copy a bunch of other unnecessary stuff along whenever icon copy is created.
It is quite a "barbaric" approach, but it doesn't seem to have too much of an overhead to really matter and it also makes using SVGIcon
generally more simple as I had a lot of extra workarounds to make these icons use correct SVGUniverse
s before.
I can post a link to the implementation later on (once I push it to GitHub) if you're curious, but generally I simply used my Clone
implementation to copy SVGIcon
and it does seem to work correctly (although I have a custom SvgIcon
extending SVGIcon
in my code for convenience).
And just a small general question - why was the current approach with SVGUniverse
chosen?
Was there some big benefit of having the same SVGUniverse
reused for different SVGIcon
s?
The SVGUniverse allows multiple documents to have references to each other, so the <use> tag can reference other documents.
If all you need to do is duplicate your leaf object, you could create an empty diagram and then build a tree in it that just contains a copy of the path element with your graphic. But then again, the SVGUniverse is pretty light weight so there won't be much penalty to having multiples of them.
The SVGUniverse allows multiple documents to have references to each other, so the
You mean <use>
references between multiple diagrams or within one diagram?
If all you need to do is duplicate your leaf object, you could create an empty diagram and then build a tree in it that just contains a copy of the path element with your graphic.
Pretty much, but "building a tree" in new empty diagram will have approximately the same complexity as just cloning the whole SVGIcon
with it's universe and diagram since there is no sraightforward way to just copy all SVG elements from existing tree into the new one.
Also as you mentioned - SVGUniverse
is indeed pretty light weight, so there is not much point to overcomplicate the code. And, in the first place, all my SVGIcon
s are already using separate SVGUniverse
s due to various customizations done in runtime, so it won't be changing much for me.
Each SVGDiagram is meant to co-respond to a single SVG file. Since SVG files can refer to elements in other files, the SVGUniverse acts as a manager class to hold a group of them and allow those inter document links to be followed.
Anyway, you seem to have found a solution that works for you, so I'll consider this closed for now.
I understood the relation of 1 SVG file = 1 Diagram, was just wondering about the <use>
as I've never seen it reference anything outside of SVG own tree.
Either way, yes, I found a somewhat working solution. Thanks for the responses!
A bit tricky question here - is there a good way to clone/copy an
SVGIcon
instance with it's current diagram (with all modifications potentially made in runtime) into newSVGUniverse
instance?I know I can load the same SVG icon from it's
URI
again in a differentSVGUniverse
and configure it separately, but I really need to preserve any changes made to it's diagram in runtime.Let me elaborate a little bit -
Let's say I have some small SVG icon -
leaf.svg
:I'm using it in runtime as an
Icon
for a label:A simple frame:
Simple
fill
attribute with red color:Problem is - if I do more adjustments - they will affect all other
SVGIcon
s with the sameURI
within the sameSVGUniverse
. And if I load newSVGIcon
from thatURI
into newSVGUniverse
- I'll end up losing the redfill
attribute I've added earlier.I didn't really find any existing way to clone/copy
SVGIcon
or it'sSVGDiagram
from oneSVGUniverse
to another one.I have some tools for recursive cloning of data & reflection access that can help me with that, but it will be quite a hack & there are also a lot of cross-references to
SVGUniverse
in different classes which will also get copied by those tools which is problem for that approach.