1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\container; |
3
|
|
|
|
4
|
|
|
use DOMElement; |
5
|
|
|
use Mimey\MimeTypes; |
6
|
|
|
use nstdio\svg\Base; |
7
|
|
|
use nstdio\svg\ElementFactoryInterface; |
8
|
|
|
use nstdio\svg\ElementStorage; |
9
|
|
|
use nstdio\svg\output\IOFormat; |
10
|
|
|
use nstdio\svg\output\Output; |
11
|
|
|
use nstdio\svg\output\OutputInterface; |
12
|
|
|
use nstdio\svg\output\SVGOutputInterface; |
13
|
|
|
use nstdio\svg\traits\ChildTrait; |
14
|
|
|
use nstdio\svg\traits\ElementTrait; |
15
|
|
|
use nstdio\svg\util\KeyValueWriter; |
16
|
|
|
use nstdio\svg\XMLDocumentInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class SVG |
20
|
|
|
* |
21
|
|
|
* @property string viewBox |
22
|
|
|
* @package nstdio\svg |
23
|
|
|
* @author Edgar Asatryan <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SVG extends Base implements ContainerInterface, ElementFactoryInterface, SVGOutputInterface |
26
|
|
|
{ |
27
|
|
|
use ElementTrait, ChildTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var XMLDocumentInterface |
31
|
|
|
*/ |
32
|
|
|
private $svg; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var OutputInterface |
36
|
|
|
*/ |
37
|
|
|
private $outputImpl; |
38
|
|
|
|
39
|
127 |
|
public function __construct($width = 640, $height = 480, XMLDocumentInterface $dom = null) |
40
|
|
|
{ |
41
|
127 |
|
parent::__construct($dom); |
42
|
|
|
|
43
|
127 |
|
$this->svg = $this->element('svg'); |
44
|
127 |
|
$this->child = new ElementStorage(); |
45
|
127 |
|
$this->outputImpl = new Output(); |
46
|
|
|
|
47
|
127 |
|
$this->apply([ |
48
|
127 |
|
'width' => $width, |
49
|
127 |
|
'height' => $height, |
50
|
127 |
|
'version' => '1.1', |
51
|
127 |
|
'xmlns' => self::XML_NS, |
52
|
127 |
|
'viewBox' => sprintf("0 0 %d %d", $width, $height), |
53
|
127 |
|
]); |
54
|
|
|
|
55
|
127 |
|
$this->domImpl->appendChild($this->svg); |
56
|
|
|
|
57
|
127 |
|
$this->svg->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', self::XML_NS_XLINK); |
58
|
|
|
|
59
|
127 |
|
new Defs($this); |
60
|
127 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $assoc |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
127 |
|
public function apply(array $assoc) |
68
|
|
|
{ |
69
|
127 |
|
KeyValueWriter::apply($this->svg, $assoc); |
70
|
|
|
|
71
|
127 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function getRoot() |
75
|
|
|
{ |
76
|
3 |
|
return $this->svg; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getName() |
80
|
|
|
{ |
81
|
2 |
|
return 'svg'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return DOMElement |
86
|
|
|
*/ |
87
|
127 |
|
public function getElement() |
88
|
|
|
{ |
89
|
127 |
|
return $this->svg->getElement(); |
90
|
|
|
} |
91
|
|
|
|
92
|
127 |
|
public function createElement($name, $value = null, $attributes = []) |
93
|
|
|
{ |
94
|
127 |
|
$element = $this->domImpl->createElement($name, $value); |
95
|
|
|
|
96
|
127 |
|
KeyValueWriter::apply($element, $attributes); |
97
|
|
|
|
98
|
127 |
|
return $element; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function __toString() |
102
|
|
|
{ |
103
|
1 |
|
return $this->draw(); |
104
|
|
|
} |
105
|
|
|
|
106
|
45 |
|
public function draw() |
107
|
|
|
{ |
108
|
45 |
|
return $this->domImpl->saveHTML(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
1 |
|
public function copy(array $apply = [], array $ignore = [], ContainerInterface $parent = null) |
115
|
|
|
{ |
116
|
1 |
|
throw new \BadMethodCallException("You cannot copy SVG element."); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns escaped svg as plain text. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function asString() |
125
|
|
|
{ |
126
|
1 |
|
return htmlspecialchars($this->draw()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
1 |
|
public function asSVG() |
133
|
|
|
{ |
134
|
1 |
|
return $this->draw(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
2 |
|
public function asSVGZ($sendHeader = false) |
141
|
|
|
{ |
142
|
2 |
|
if ($sendHeader && !headers_sent()) { |
143
|
1 |
|
header("Content-Encoding: gzip"); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
2 |
|
return gzencode($this->draw(), 9); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
1 |
|
public function asDataUriBase64() |
153
|
|
|
{ |
154
|
1 |
|
$encoded = base64_encode($this->draw()); |
155
|
1 |
|
$mimeType = (new MimeTypes)->getMimeType('svg'); |
156
|
|
|
|
157
|
1 |
|
return sprintf("data:%s;base64,%s", $mimeType, $encoded); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
2 |
|
public function asFile($name, $prettyPrint = false, $override = false) |
164
|
|
|
{ |
165
|
2 |
|
return $this->outputImpl->file( |
166
|
2 |
|
$name, |
167
|
2 |
|
$this->domImpl->saveXML($prettyPrint), |
168
|
|
|
$override |
169
|
2 |
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritdoc |
174
|
|
|
*/ |
175
|
|
|
public function asImageFile($name, $format = IOFormat::PNG, $override = false) |
176
|
|
|
{ |
177
|
|
|
return $this->outputImpl->imageFile( |
178
|
|
|
$this->domImpl->saveXML(), |
|
|
|
|
179
|
|
|
$name, |
180
|
|
|
$format, |
181
|
|
|
$override |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritdoc |
187
|
|
|
*/ |
188
|
|
|
public function asImage($format = IOFormat::PNG, $sendHeader = false) |
189
|
|
|
{ |
190
|
|
|
return $this->outputImpl->image( |
191
|
|
|
$this->domImpl->saveXML(), |
|
|
|
|
192
|
|
|
$format, |
193
|
|
|
$sendHeader |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
This check looks for function calls that miss required arguments.