The data is there, but it is not visible.
Seems that the foreignObject tags do not render properly in a standalone SVG document.
I have tried to give the span tag the attribute (i.e. <span xmlns="http://www.w3.org/1999/xhtml">_1592</span>
) but it doesn’t work.
I would suggest to simplify the document, using your favorite text processor to change the combination <foreignObject><span>PAYLOAD</span></foreignObject>
to <text>PAYLOAD</text>
:
<polyline class="branch" points="21.4583,49 59.3833,35"></polyline>
<foreignObject width="38.91667175292969" height="21" class="tree-label" x="2px" y="49px">
<span xmlns="http://www.w3.org/1999/xhtml">_1592</span>
</foreignObject>
to
<polyline class="branch" points="21.4583,49 59.3833,35"></polyline>
<text x="2px" y="49px">
_1592
</text>
A simple DCG that performs the transformation:
/* File: foreignObject_to_text.pl
Author: Carlo,,,
Created: Aug 28 2021
Purpose: see https://swi-prolog.discourse.group/t/saving-svgtree/4343
*/
:- module(foreignObject_to_text,
[transform/0,
transform/2
]).
:- use_module(library(dcg/basics)).
:- use_module(library(debug)).
transform :-
transform('~/swish-rendered.svg', '~/modified.svg').
transform(S,T) :-
maplist(expand_file_name,[S,T],[[Se],[Te]]),
open(Te,write,Ts),
( phrase_from_file(foreignObject_to_text(Ts),Se)
-> true
; writeln('something goes wrong')
),
close(Ts).
foreignObject_to_text(_) --> [].
foreignObject_to_text(Ts) -->
foreignObject(Payload, Attrs),
{memberchk(x=X,Attrs),
memberchk(y=Y,Attrs),
format(Ts,'<text x=~s y=~s dx="0" dy="10">~s</text>', [X,Y,Payload]),
debug(foreignObject_to_text,'transformed ~q', [(Payload,X,Y)])
},
!, foreignObject_to_text(Ts).
foreignObject_to_text(Ts) -->
[C],
{put(Ts,C)},
foreignObject_to_text(Ts).
foreignObject(Payload, Attrs) -->
blanks,
"<foreignObject", attrs(Attrs), ">",
span(Payload,_),
"</foreignObject>",
blanks.
span(Payload, Attrs) -->
{debug(foreignObject_to_text,in_span,[])},
blanks,
"<span", attrs(Attrs), ">",
blanks,
string(Payload),
"</span>",
blanks,
{debug(foreignObject_to_text,'out_span ~q',[span(Attrs,Payload)])}.
attrs([]) --> [].
attrs([A|Attrs]) -->
attr(A),
attrs(Attrs).
attr(Key=Value) -->
whites,
key(Key_),
"=",
value(Value_),
{maplist(atom_codes,[Key,Value],[Key_,Value_])}.
key(Key) --> string(Key).
value(Value) -->
string_without([0' ,0'>],Value).
The dy attribute should be computed, right now it’s hardcoded to 10