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 mixed |
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 |
|
public function getRoot() |
76
|
|
|
{ |
77
|
3 |
|
return $this->svg; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function getName() |
81
|
|
|
{ |
82
|
3 |
|
return 'svg'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return DOMElement |
87
|
|
|
*/ |
88
|
149 |
|
public function getElement() |
89
|
|
|
{ |
90
|
149 |
|
return $this->svg->getElement(); |
91
|
|
|
} |
92
|
|
|
|
93
|
149 |
|
public function createElement($name, $value = null, $attributes = []) |
94
|
|
|
{ |
95
|
149 |
|
$element = $this->domImpl->createElement($name, $value); |
96
|
|
|
|
97
|
149 |
|
KeyValueWriter::apply($element, $attributes); |
98
|
|
|
|
99
|
149 |
|
return $element; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function __toString() |
103
|
|
|
{ |
104
|
1 |
|
return $this->draw(); |
105
|
|
|
} |
106
|
|
|
|
107
|
62 |
|
public function draw() |
108
|
|
|
{ |
109
|
62 |
|
return $this->domImpl->saveHTML(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
1 |
|
public function copy(array $apply = [], array $ignore = [], ContainerInterface $parent = null) |
116
|
|
|
{ |
117
|
1 |
|
throw new \BadMethodCallException("You cannot copy SVG element."); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns escaped svg as plain text. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
1 |
|
public function asString() |
126
|
|
|
{ |
127
|
1 |
|
return htmlspecialchars($this->draw()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
1 |
|
public function asSVG() |
134
|
|
|
{ |
135
|
1 |
|
return $this->draw(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
2 |
|
public function asSVGZ($sendHeader = false) |
142
|
|
|
{ |
143
|
2 |
|
if ($sendHeader && !headers_sent()) { |
144
|
1 |
|
header("Content-Encoding: gzip"); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
2 |
|
return gzencode($this->draw(), 9); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
1 |
|
public function asDataUriBase64() |
154
|
|
|
{ |
155
|
1 |
|
$encoded = base64_encode($this->draw()); |
156
|
1 |
|
$mimeType = (new MimeTypes)->getMimeType('svg'); |
157
|
|
|
|
158
|
1 |
|
return sprintf("data:%s;base64,%s", $mimeType, $encoded); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
2 |
|
public function asFile($name, $prettyPrint = false, $override = false) |
165
|
|
|
{ |
166
|
2 |
|
return $this->outputImpl->file( |
167
|
2 |
|
$name, |
168
|
2 |
|
$this->domImpl->saveXML($prettyPrint), |
169
|
|
|
$override |
170
|
2 |
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function asImageFile($name, $format = IOFormat::PNG, $override = false) |
177
|
|
|
{ |
178
|
|
|
return $this->outputImpl->imageFile( |
179
|
|
|
$this->domImpl->saveXML(), |
|
|
|
|
180
|
|
|
$name, |
181
|
|
|
$format, |
182
|
|
|
$override |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
*/ |
189
|
|
|
public function asImage($format = IOFormat::PNG, $sendHeader = false) |
190
|
|
|
{ |
191
|
|
|
return $this->outputImpl->image( |
192
|
|
|
$this->domImpl->saveXML(), |
|
|
|
|
193
|
|
|
$format, |
194
|
|
|
$sendHeader |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public static function fromString($svgString) |
199
|
|
|
{ |
200
|
|
|
return (new Importer)->fromString($svgString); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for function calls that miss required arguments.