PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.16
Yatra – Travel Booking & Tour Operator Software v3.0.16
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / vendor / phenx / php-svg-lib / src / Svg / Document.php

Document.php in Yatra – Travel Booking & Tour Operator Software 3.0.16, at vendor/phenx/php-svg-lib/src/Svg/Document.php

446 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package php-svg-lib
4 * @link http://github.com/PhenX/php-svg-lib
5 * @author Fabien Ménager <[email protected]>
6 * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html
7 */
8
9 namespace Svg;
10
11 use Svg\Surface\SurfaceInterface;
12 use Svg\Tag\AbstractTag;
13 use Svg\Tag\Anchor;
14 use Svg\Tag\Circle;
15 use Svg\Tag\Ellipse;
16 use Svg\Tag\Group;
17 use Svg\Tag\ClipPath;
18 use Svg\Tag\Image;
19 use Svg\Tag\Line;
20 use Svg\Tag\LinearGradient;
21 use Svg\Tag\Path;
22 use Svg\Tag\Polygon;
23 use Svg\Tag\Polyline;
24 use Svg\Tag\Rect;
25 use Svg\Tag\Stop;
26 use Svg\Tag\Symbol;
27 use Svg\Tag\Text;
28 use Svg\Tag\StyleTag;
29 use Svg\Tag\UseTag;
30
31 class Document extends AbstractTag
32 {
33 protected $filename;
34 protected $_defs_depth = 0;
35 public $inDefs = false;
36
37 protected $x;
38 protected $y;
39 protected $width;
40 protected $height;
41
42 protected $subPathInit;
43 protected $pathBBox;
44 protected $viewBox;
45
46 /** @var SurfaceInterface */
47 protected $surface;
48
49 /** @var AbstractTag[] */
50 protected $stack = array();
51
52 /** @var AbstractTag[] */
53 protected $defs = array();
54
55 /** @var \Sabberworm\CSS\CSSList\Document[] */
56 protected $styleSheets = array();
57
58 public $allowExternalReferences = true;
59
60 public function loadFile($filename)
61 {
62 $this->filename = $filename;
63 }
64
65 protected function initParser() {
66 $parser = xml_parser_create("utf-8");
67 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
68 xml_set_element_handler(
69 $parser,
70 array($this, "_tagStart"),
71 array($this, "_tagEnd")
72 );
73 xml_set_character_data_handler(
74 $parser,
75 array($this, "_charData")
76 );
77
78 return $parser;
79 }
80
81 public function __construct() {
82
83 }
84
85 /**
86 * Increase the nesting level for defs-like elements
87 *
88 * @return int
89 */
90 public function enterDefs () {
91 $this->_defs_depth++;
92 $this->inDefs = true;
93 return $this->_defs_depth;
94 }
95
96 /**
97 * Decrease the nesting level for defs-like elements
98 *
99 * @return int
100 */
101 public function exitDefs () {
102 $this->_defs_depth--;
103 if ($this->_defs_depth < 0) {
104 $this->_defs_depth = 0;
105 }
106 $this->inDefs = ($this->_defs_depth > 0 ? true : false);
107 return $this->_defs_depth;
108 }
109
110 /**
111 * @return SurfaceInterface
112 */
113 public function getSurface()
114 {
115 return $this->surface;
116 }
117
118 public function getStack()
119 {
120 return $this->stack;
121 }
122
123 public function getWidth()
124 {
125 return $this->width;
126 }
127
128 public function getHeight()
129 {
130 return $this->height;
131 }
132
133 public function getDiagonal()
134 {
135 return sqrt(($this->width)**2 + ($this->height)**2) / sqrt(2);
136 }
137
138 public function getDimensions() {
139 $rootAttributes = null;
140
141 $parser = xml_parser_create("utf-8");
142 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
143 xml_set_element_handler(
144 $parser,
145 function ($parser, $name, $attributes) use (&$rootAttributes) {
146 if ($name === "svg" && $rootAttributes === null) {
147 $attributes = array_change_key_case($attributes, CASE_LOWER);
148
149 $rootAttributes = $attributes;
150 }
151 },
152 function ($parser, $name) {}
153 );
154
155 $fp = fopen($this->filename, "r");
156 while ($line = fread($fp, 8192)) {
157 xml_parse($parser, $line, false);
158
159 if ($rootAttributes !== null) {
160 break;
161 }
162 }
163 xml_parse($parser, "", true);
164
165 xml_parser_free($parser);
166
167 return $this->handleSizeAttributes($rootAttributes);
168 }
169
170 public function handleSizeAttributes($attributes){
171 if ($this->width === null) {
172 if (isset($attributes["width"])) {
173 $width = $this->convertSize($attributes["width"], 400);
174 $this->width = $width;
175 }
176
177 if (isset($attributes["height"])) {
178 $height = $this->convertSize($attributes["height"], 300);
179 $this->height = $height;
180 }
181
182 if (isset($attributes['viewbox'])) {
183 $viewBox = preg_split('/[\s,]+/is', trim($attributes['viewbox']));
184 if (count($viewBox) == 4) {
185 $this->x = $viewBox[0];
186 $this->y = $viewBox[1];
187
188 if (!$this->width) {
189 $this->width = $viewBox[2];
190 }
191 if (!$this->height) {
192 $this->height = $viewBox[3];
193 }
194 }
195 }
196 }
197
198 return array(
199 0 => $this->width,
200 1 => $this->height,
201
202 "width" => $this->width,
203 "height" => $this->height,
204 );
205 }
206
207 public function getDocument(){
208 return $this;
209 }
210
211 /**
212 * Append a style sheet
213 *
214 * @param \Sabberworm\CSS\CSSList\Document $stylesheet
215 */
216 public function appendStyleSheet($stylesheet) {
217 $this->styleSheets[] = $stylesheet;
218 }
219
220 /**
221 * Get the document style sheets
222 *
223 * @return \Sabberworm\CSS\CSSList\Document[]
224 */
225 public function getStyleSheets() {
226 return $this->styleSheets;
227 }
228
229 protected function before($attributes)
230 {
231 $surface = $this->getSurface();
232
233 $style = new DefaultStyle($this);
234 $style->inherit($this);
235 $style->fromAttributes($attributes);
236
237 $this->setStyle($style);
238
239 $surface->setStyle($style);
240 }
241
242 public function render(SurfaceInterface $surface)
243 {
244 $this->_defs_depth = 0;
245 $this->inDefs = false;
246 $this->surface = $surface;
247
248 $parser = $this->initParser();
249
250 if ($this->x || $this->y) {
251 $surface->translate(-$this->x, -$this->y);
252 }
253
254 $fp = fopen($this->filename, "r");
255 while ($line = fread($fp, 8192)) {
256 xml_parse($parser, $line, false);
257 }
258
259 xml_parse($parser, "", true);
260
261 xml_parser_free($parser);
262 }
263
264 protected function svgOffset($attributes)
265 {
266 $this->attributes = $attributes;
267
268 $this->handleSizeAttributes($attributes);
269 }
270
271 public function getDef($id) {
272 $id = ltrim($id, "#");
273
274 return isset($this->defs[$id]) ? $this->defs[$id] : null;
275 }
276
277 private function _tagStart($parser, $name, $attributes)
278 {
279 $this->x = 0;
280 $this->y = 0;
281
282 $tag = null;
283
284 $attributes = array_change_key_case($attributes, CASE_LOWER);
285
286 switch (strtolower($name)) {
287 case 'defs':
288 $this->enterDefs();
289 return;
290
291 case 'svg':
292 if (count($this->attributes)) {
293 $tag = new Group($this, $name);
294 }
295 else {
296 $tag = $this;
297 $this->svgOffset($attributes);
298 }
299 break;
300
301 case 'path':
302 $tag = new Path($this, $name);
303 break;
304
305 case 'rect':
306 $tag = new Rect($this, $name);
307 break;
308
309 case 'circle':
310 $tag = new Circle($this, $name);
311 break;
312
313 case 'ellipse':
314 $tag = new Ellipse($this, $name);
315 break;
316
317 case 'image':
318 $tag = new Image($this, $name);
319 break;
320
321 case 'line':
322 $tag = new Line($this, $name);
323 break;
324
325 case 'polyline':
326 $tag = new Polyline($this, $name);
327 break;
328
329 case 'polygon':
330 $tag = new Polygon($this, $name);
331 break;
332
333 case 'lineargradient':
334 $tag = new LinearGradient($this, $name);
335 break;
336
337 case 'radialgradient':
338 $tag = new LinearGradient($this, $name);
339 break;
340
341 case 'stop':
342 $tag = new Stop($this, $name);
343 break;
344
345 case 'style':
346 $tag = new StyleTag($this, $name);
347 break;
348
349 case 'a':
350 $tag = new Anchor($this, $name);
351 break;
352
353 case 'g':
354 $tag = new Group($this, $name);
355 break;
356
357 case 'symbol':
358 $this->enterDefs();
359 $tag = new Symbol($this, $name);
360 break;
361
362 case 'clippath':
363 $tag = new ClipPath($this, $name);
364 break;
365
366 case 'use':
367 $tag = new UseTag($this, $name);
368 break;
369
370 case 'text':
371 $tag = new Text($this, $name);
372 break;
373
374 case 'desc':
375 return;
376 }
377
378 if ($tag) {
379 if (isset($attributes["id"])) {
380 $this->defs[$attributes["id"]] = $tag;
381 }
382 else {
383 /** @var AbstractTag $top */
384 $top = end($this->stack);
385 if ($top && $top != $tag) {
386 $top->children[] = $tag;
387 }
388 }
389
390 $this->stack[] = $tag;
391
392 $tag->handle($attributes);
393 }
394 }
395
396 function _charData($parser, $data)
397 {
398 $stack_top = end($this->stack);
399
400 if ($stack_top instanceof Text || $stack_top instanceof StyleTag) {
401 $stack_top->appendText($data);
402 }
403 }
404
405 function _tagEnd($parser, $name)
406 {
407 /** @var AbstractTag $tag */
408 $tag = null;
409 switch (strtolower($name)) {
410 case 'defs':
411 $this->exitDefs();
412 return;
413
414 case 'symbol':
415 $this->exitDefs();
416 $tag = array_pop($this->stack);
417 break;
418
419 case 'svg':
420 case 'path':
421 case 'rect':
422 case 'circle':
423 case 'ellipse':
424 case 'image':
425 case 'line':
426 case 'polyline':
427 case 'polygon':
428 case 'radialgradient':
429 case 'lineargradient':
430 case 'stop':
431 case 'style':
432 case 'text':
433 case 'g':
434 case 'clippath':
435 case 'use':
436 case 'a':
437 $tag = array_pop($this->stack);
438 break;
439 }
440
441 if ((!$this->inDefs && $tag) || $tag instanceof StyleTag) {
442 $tag->handleEnd();
443 }
444 }
445 }
446