Skip to content

Commit ab18c6a

Browse files
committed
Add SVGDoc and SVGSerialize functions for SVG document creation and
serialization. [Feature] Signed-off-by: Markus Alexander Kuppe <[email protected]>
1 parent 3923f46 commit ab18c6a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

modules/SVG.tla

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ Group(children, attrs) ==
122122
Svg(children, attrs) ==
123123
SVGElem("svg", attrs, children, "")
124124

125+
(**************************************************************************)
126+
(* Creates a complete SVG document with viewBox and additional attributes.*)
127+
(* *)
128+
(* Parameters: *)
129+
(* children: A sequence of SVG elements to include in the document *)
130+
(* vbX, vbY: The x,y coordinates of the top-left corner of the viewBox *)
131+
(* vbW, vbH: The width and height of the viewBox *)
132+
(* attrs: Additional attributes to merge with the SVG root element *)
133+
(* *)
134+
(* The viewBox defines the coordinate system and viewport dimensions for *)
135+
(* the SVG content. This is essential for proper scaling and positioning *)
136+
(* of elements within the document. *)
137+
(**************************************************************************)
138+
SVGDoc(children, vbX, vbY, vbW, vbH, attrs) ==
139+
LET svgAttrs == ("xmlns:xlink" :> "http://www.w3.org/1999/xlink" @@
140+
"xmlns" :> "http://www.w3.org/2000/svg" @@
141+
"viewBox" :> ToString(vbX) \o " " \o
142+
ToString(vbY) \o " " \o
143+
ToString(vbW) \o " " \o
144+
ToString(vbH)) IN
145+
Svg(<<children>>, Merge(svgAttrs, attrs))
146+
125147
(**************************************************************************)
126148
(* Convert an SVG element record into its string representation. *)
127149
(* *)
@@ -190,4 +212,33 @@ PointOnLine(from, to, segment) ==
190212
[x |-> from.x + ((to.x - from.x) \div segment),
191213
y |-> from.y + ((to.y - from.y) \div segment)]
192214

215+
-------------------------------------------------------------------------------
216+
217+
(**************************************************************************)
218+
(* Serializes an SVG element to a file on disk. *)
219+
(* *)
220+
(* Parameters: *)
221+
(* svg: The SVG element/document to serialize *)
222+
(* frameNamePrefix: String prefix for the output filename *)
223+
(* frameNumber: Numeric identifier appended to create unique files *)
224+
(* *)
225+
(* Creates a file named "<frameNamePrefix><frameNumber>.svg" containing *)
226+
(* the serialized SVG content. This is useful for generating animation *)
227+
(* frames or saving visualization snapshots. *)
228+
(* *)
229+
(* Example usage: *)
230+
(* SVGSerialize(SVGDoc(myElements, 0, 0, 800, 600, <<>>), *)
231+
(* "svg_frame_", TLCGet("level")) *)
232+
(* *)
233+
(* This creates files like: svg_frame_1.svg, *)
234+
(* svg_frame_2.svg, etc. *)
235+
(**************************************************************************)
236+
SVGSerialize(svg, frameNamePrefix, frameNumber) ==
237+
LET IO == INSTANCE IOUtils IN
238+
IO!Serialize(
239+
SVGElemToString(svg),
240+
frameNamePrefix \o ToString(frameNumber) \o ".svg",
241+
[format |-> "TXT", charset |-> "UTF-8",
242+
openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>])
243+
193244
=============================================================================

0 commit comments

Comments
 (0)