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