1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Chart\Primitive;
use DOMElement;
use DOMText;
use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;
/**
* Wrapper for the SVG text element
*/
class Text extends Styleable implements Drawable
{
/**
* Align the text to end at the x and y position
*/
const ALIGN_END = 'end';
/**
* Align the text to start at the x and y position
*/
const ALIGN_START = 'start';
/**
* Align the text to be centered at the x and y position
*/
const ALIGN_MIDDLE = 'middle';
/**
* The x position of the Text
*
* @var int
*/
private $x;
/**
* The y position of the Text
*
* @var int
*/
private $y;
/**
* The text content
*
* @var string
*/
private $text;
/**
* The size of the font
*
* @var string
*/
private $fontSize = '1.5em';
/**
* The weight of the font
*
* @var string
*/
private $fontWeight = 'normal';
/**
* The default fill color
*
* @var string
*/
public $fill = '#000';
/**
* The alignment of the text
*
* @var string
*/
private $alignment = self::ALIGN_START;
/**
* Set the font-stretch property of the text
*/
private $fontStretch = 'semi-condensed';
/**
* Construct a new text drawable
*
* @param int $x The x position of the text
* @param int $y The y position of the text
* @param string $text The text this component should contain
* @param string $fontSize The font size of the text
*/
public function __construct($x, $y, $text, $fontSize = '1.5em')
{
$this->x = $x;
$this->y = $y;
$this->text = $text;
$this->fontSize = $fontSize;
}
/**
* Set the font size of the svg text element
*
* @param string $size The font size including a unit
*
* @return $this Fluid interface
*/
public function setFontSize($size)
{
$this->fontSize = $size;
return $this;
}
/**
* Set the text alignment with one of the ALIGN_* constants
*
* @param String $align Value how to align
*
* @return $this Fluid interface
*/
public function setAlignment($align)
{
$this->alignment = $align;
return $this;
}
/**
* Set the weight of the current font
*
* @param string $weight The weight of the string
*
* @return $this Fluid interface
*/
public function setFontWeight($weight)
{
$this->fontWeight = $weight;
return $this;
}
/**
* Create the SVG representation from this Drawable
*
* @param RenderContext $ctx The context to use for rendering
*
* @return DOMElement The SVG Element
*/
public function toSvg(RenderContext $ctx)
{
list($x, $y) = $ctx->toAbsolute($this->x, $this->y);
$text = $ctx->getDocument()->createElement('text');
$text->setAttribute('x', Format::formatSVGNumber($x - 15));
$text->setAttribute(
'style',
$this->getStyle()
. ';font-size:' . $this->fontSize
. '; font-family: Ubuntu, Calibri, Trebuchet MS, Helvetica, Verdana, sans-serif'
. ';font-weight: ' . $this->fontWeight
. ';font-stretch: ' . $this->fontStretch
. '; font-style: normal;'
. 'text-anchor: ' . $this->alignment
);
$text->setAttribute('y', Format::formatSVGNumber($y));
$text->appendChild(new DOMText($this->text));
return $text;
}
}
|