1111 https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/text
1212. EXAMPLE
1313 turtle text "hello world" textElement
14+ . EXAMPLE
15+ turtle text "hello world" title "Hi!" textElement
1416#>
1517[OutputType ([xml ])]
1618param ()
1719
20+ # If there is no text, there's no text element
1821if (-not $this.Text ) { return }
1922
20- $textAttributes = [Ordered ]@ {}
23+ # Collect all of our text attributes
24+ $textAttributes = [Ordered ]@ {
25+ id = " $ ( $this.ID ) -text"
26+ }
2127
28+ # If there are no steps
2229if (-not $this.Steps ) {
30+ # default the text to the middle
2331 $textAttributes [' dominant-baseline' ] = ' middle'
2432 $textAttributes [' text-anchor' ] = ' middle'
2533 $textAttributes [' x' ] = ' 50%'
2634 $textAttributes [' y' ] = ' 50%'
2735}
2836
29- foreach ($collection in ' SVGAttribute' , ' Attribute' ) {
37+ # Text Attributes can exist in Attribute or SVGAttribute, as long as they are prefixed.
38+ $prefix = ' ^/?text/'
39+ foreach ($collection in ' Attribute' , ' SVGAttribute' ) {
40+ if (-not $this .$Collection.Count ) { continue }
3041 foreach ($key in $this .$collection.Keys ) {
31- if ($key -match ' ^text/ ' ) {
32- $textAttributes [$key -replace ' ^text/ ' ] = $this .$collection [$key ]
42+ if ($key -match $prefix ) {
43+ $textAttributes [$key -replace $prefix ] = $this .$collection [$key ]
3344 }
3445 }
3546}
3647
48+ # Explicit text attributes will be copied last, so they take precedent.
3749foreach ($key in $this.TextAttribute.Keys ) {
3850 $textAttributes [$key ] = $this.TextAttribute [$key ]
3951}
4052
53+ # Return a constructed element
4154return [xml ]@ (
42- " <text id='$ ( $this.ID ) -text' $ (
55+ # Create the text element
56+ " <text$ (
4357 foreach ($TextAttributeName in $TextAttributes.Keys ) {
4458 " $TextAttributeName ='$ ( $TextAttributes [$TextAttributeName ]) '"
4559 }
4660) >"
61+
62+ # If there is a title
63+ if ($this.Title ) {
64+ # embed it here (so that the text is accessible).
65+ " <title>$ ( [Security.SecurityElement ]::Escape($this.Title )) </title>"
66+ }
67+
68+ # If there are any text animations, include them here.
4769if ($this.TextAnimation ) {$this.TextAnimation }
70+
71+ # Escape our text
72+ $escapedText = [Security.SecurityElement ]::Escape($this.Text )
73+ # If we have steps,
4874if ($this.Steps ) {
49- " <textPath href='#$ ( $this.id ) -path'>$ ( [Security.SecurityElement ]::Escape($this.Text )) </textPath>"
75+ # put the escaped text within a `<textPath>`.
76+ " <textPath href='#$ ( $this.id ) -path'>$escapedText </textPath>"
5077} else {
51- $ ([Security.SecurityElement ]::Escape($this.Text ))
78+ # otherwise, include the escaped text as the content
79+ $escapedText
5280}
81+ # close the element and return our XML.
5382" </text>"
54- )
55-
56-
83+ )
0 commit comments