Xah Talk Show 2025-09-05 Ep697 Wolfram Language Coding Session, Write Custom Command Export 2D Graphics to SVG

xah talk show ep697 14e90
xah talk show ep697 14e90

video summary

geoInv = ((With[{x662 = #.#}, If[ x662 < 0.00000001, #, #/x662] ]) &);
cArray = CoordinateBoundsArray[{{-1, 1}, {-1, 1}}, Into[10] ];
hLines= (BlockMap[Line,#,2,1]&) /@ cArray;
vLines= (BlockMap[Line,#,2,1]&) /@ Transpose @ cArray;
gp1 = {hLines , vLines};
gp2 = gp1 /. Line[x_] :> Line[ geoInv /@ x ];

Graphics[gp1, Axes->True ]

Graphics[gp2, Axes->True ]

Export[
"xxy.svg",
Graphics[gp2, Axes->True ]
]
(*
write a command to export 2d graphics to svg.
we want to do this because the bultin Export function creates huge file size.
 *)

prim = Circle[{0,0}, 1]
Graphics[ prim, Axes -> True ]

Export["xxcircle.svg" , Graphics[ prim]]

(* HHHH------------------------------ *)

Clear[graphicsToString]

graphicsToString::usage = "
graphicsToString[g]
return a string, that is a svg format, representing Graphics object g.";

graphicsToString[ gra_Graphics ] := "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<svg xmlns=\"http://www.w3.org/2000/svg\"
xmlns:xlink=\"http://www.w3.org/1999/xlink\"
width=\"1000\" height=\"1000\"

viewBox=\"0 0
1000 * x-scale + xtranvector,
1000 * y-scale + ytranvector \"
version=\"1.1\">
</svg>
"
Graphics[
{
Red,
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Circle[{0,0}, 1], {
Red,
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Blue, Thickness[],
Circle[{0,0}, 1],
Circle[{0,0}, 1],{
Red,
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Blue, Thickness[],
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]]},
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]]},
Blue, Thickness[],
Circle[{0,0}, 1],
Circle[{0,0}, 1],
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]],
Line[[{0,0}, {1,2}]]}
]

<circle cx="0" cy="0" r="1"
 style="fill:yellow; stroke:blue; stroke-width:5" />

(* HHHH------------------------------ *)

exportGraphics::usage = "
exportGraphics[g, name]
export Graphics object g, to a file named name in current dir, in svg format.
g is a Graphics[] oject.
name is a string.";

exportGraphics[ gra_Graphics, filename_ ] := ""

(* HHHH------------------------------ *)

ExportString

Wolfram language coding, export to SVG