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