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 / Style.php

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

566 lines 18.8 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\Tag\AbstractTag;
12
13 class Style
14 {
15 const TYPE_COLOR = 1;
16 const TYPE_LENGTH = 2;
17 const TYPE_NAME = 3;
18 const TYPE_ANGLE = 4;
19 const TYPE_NUMBER = 5;
20
21 private $_document;
22 private $_parentStyle;
23
24 public $color;
25 public $opacity;
26 public $display;
27
28 public $fill;
29 public $fillOpacity;
30 public $fillRule;
31
32 public $stroke;
33 public $strokeOpacity;
34 public $strokeLinecap;
35 public $strokeLinejoin;
36 public $strokeMiterlimit;
37 public $strokeWidth;
38 public $strokeDasharray;
39 public $strokeDashoffset;
40
41 public $fontFamily = 'serif';
42 public $fontSize = 12;
43 public $fontWeight = 'normal';
44 public $fontStyle = 'normal';
45 public $textAnchor = 'start';
46
47 public function __construct($document = null) {
48 if ($document !== null) {
49 $this->_document = $document;
50 }
51 }
52
53 protected function getStyleMap()
54 {
55 return array(
56 'color' => array('color', self::TYPE_COLOR),
57 'opacity' => array('opacity', self::TYPE_NUMBER),
58 'display' => array('display', self::TYPE_NAME),
59
60 'fill' => array('fill', self::TYPE_COLOR),
61 'fill-opacity' => array('fillOpacity', self::TYPE_NUMBER),
62 'fill-rule' => array('fillRule', self::TYPE_NAME),
63
64 'stroke' => array('stroke', self::TYPE_COLOR),
65 'stroke-dasharray' => array('strokeDasharray', self::TYPE_NAME),
66 'stroke-dashoffset' => array('strokeDashoffset', self::TYPE_NUMBER),
67 'stroke-linecap' => array('strokeLinecap', self::TYPE_NAME),
68 'stroke-linejoin' => array('strokeLinejoin', self::TYPE_NAME),
69 'stroke-miterlimit' => array('strokeMiterlimit', self::TYPE_NUMBER),
70 'stroke-opacity' => array('strokeOpacity', self::TYPE_NUMBER),
71 'stroke-width' => array('strokeWidth', self::TYPE_NUMBER),
72
73 'font-family' => array('fontFamily', self::TYPE_NAME),
74 'font-size' => array('fontSize', self::TYPE_NUMBER),
75 'font-weight' => array('fontWeight', self::TYPE_NAME),
76 'font-style' => array('fontStyle', self::TYPE_NAME),
77 'text-anchor' => array('textAnchor', self::TYPE_NAME),
78 );
79 }
80
81 /**
82 * @param $attributes
83 *
84 * @return Style
85 */
86 public function fromAttributes($attributes)
87 {
88 $this->fillStyles($attributes);
89
90 if (isset($attributes["style"])) {
91 $styles = self::parseCssStyle($attributes["style"]);
92 $this->fillStyles($styles);
93 }
94 }
95
96 public function inherit(AbstractTag $tag) {
97 $group = $tag->getParentGroup();
98 if ($group) {
99 $parent_style = $group->getStyle();
100 $this->_parentStyle = $parent_style;
101 foreach ($parent_style as $_key => $_value) {
102 if ($_value !== null) {
103 $this->$_key = $_value;
104 }
105 }
106 }
107 }
108
109 public function fromStyleSheets(AbstractTag $tag, $attributes) {
110 $class = isset($attributes["class"]) ? preg_split('/\s+/', trim($attributes["class"])) : null;
111
112 $stylesheets = $tag->getDocument()->getStyleSheets();
113
114 $styles = array();
115
116 foreach ($stylesheets as $_sc) {
117
118 /** @var \Sabberworm\CSS\RuleSet\DeclarationBlock $_decl */
119 foreach ($_sc->getAllDeclarationBlocks() as $_decl) {
120
121 /** @var \Sabberworm\CSS\Property\Selector $_selector */
122 foreach ($_decl->getSelectors() as $_selector) {
123 $_selector = $_selector->getSelector();
124
125 // Match class name
126 if ($class !== null) {
127 foreach ($class as $_class) {
128 if ($_selector === ".$_class") {
129 /** @var \Sabberworm\CSS\Rule\Rule $_rule */
130 foreach ($_decl->getRules() as $_rule) {
131 $styles[$_rule->getRule()] = $_rule->getValue() . "";
132 }
133
134 break 2;
135 }
136 }
137 }
138
139 // Match tag name
140 if ($_selector === $tag->tagName) {
141 /** @var \Sabberworm\CSS\Rule\Rule $_rule */
142 foreach ($_decl->getRules() as $_rule) {
143 $styles[$_rule->getRule()] = $_rule->getValue() . "";
144 }
145
146 break;
147 }
148 }
149 }
150 }
151
152 $this->fillStyles($styles);
153 }
154
155 protected function fillStyles($styles)
156 {
157 $style_map = $this->getStyleMap();
158 foreach ($style_map as $from => $spec) {
159 if (isset($styles[$from])) {
160 list($to, $type) = $spec;
161 $value = null;
162 switch ($type) {
163 case self::TYPE_COLOR:
164 $value = self::parseColor($styles[$from]);
165 if ($value === "currentcolor") {
166 if ($type === "color") {
167 $value = $this->_parentStyle->color;
168 } else {
169 $value = $this->color;
170 }
171 }
172 if (is_array($value) && $value[3] !== 1.0 && array_key_exists("{$from}-opacity", $style_map) === true) {
173 $styles["{$from}-opacity"] = $value[3];
174 }
175 break;
176
177 case self::TYPE_NUMBER:
178 $value = ($styles[$from] === null) ? null : (float)$styles[$from];
179 break;
180
181 default:
182 $value = $styles[$from];
183 }
184
185 if ($from === "font-family") {
186 $scheme = \strtolower(parse_url($value, PHP_URL_SCHEME) ?: "");
187 if (
188 $scheme === "phar" || \strtolower(\substr($value, 0, 7)) === "phar://"
189 || ($this->_document !== null && $this->_document->allowExternalReferences === false && $scheme !== "data")
190 ) {
191 continue;
192 }
193 }
194
195 if ($value !== null) {
196 $this->$to = $value;
197 }
198 }
199 }
200 }
201
202 static function parseColor($color)
203 {
204 $color = strtolower(trim($color));
205
206 $parts = preg_split('/[^,]\s+/', $color, 2);
207
208 if (count($parts) == 2) {
209 $color = $parts[1];
210 } else {
211 $color = $parts[0];
212 }
213
214 if ($color === "none") {
215 return "none";
216 }
217
218 if ($color === "currentcolor") {
219 return "currentcolor";
220 }
221
222 if ($color === "transparent") {
223 return [0.0, 0.0, 0.0, 0.0];
224 }
225
226 // SVG color name
227 if (isset(self::$colorNames[$color])) {
228 return self::parseHexColor(self::$colorNames[$color]);
229 }
230
231 // Hex color
232 if ($color[0] === "#") {
233 return self::parseHexColor($color);
234 }
235
236 // RGB color
237 if (strpos($color, "rgb") !== false) {
238 return self::getQuad($color);
239 }
240
241 // HSL color
242 if (strpos($color, "hsl") !== false) {
243 $quad = self::getQuad($color, true);
244
245 if ($quad == null) {
246 return null;
247 }
248
249 list($h, $s, $l, $a) = $quad;
250
251 $r = $l;
252 $g = $l;
253 $b = $l;
254 $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
255 if ($v > 0) {
256 $m = $l + $l - $v;
257 $sv = ($v - $m) / $v;
258 $h *= 6.0;
259 $sextant = floor($h);
260 $fract = $h - $sextant;
261 $vsf = $v * $sv * $fract;
262 $mid1 = $m + $vsf;
263 $mid2 = $v - $vsf;
264
265 switch ($sextant) {
266 case 0:
267 $r = $v;
268 $g = $mid1;
269 $b = $m;
270 break;
271 case 1:
272 $r = $mid2;
273 $g = $v;
274 $b = $m;
275 break;
276 case 2:
277 $r = $m;
278 $g = $v;
279 $b = $mid1;
280 break;
281 case 3:
282 $r = $m;
283 $g = $mid2;
284 $b = $v;
285 break;
286 case 4:
287 $r = $mid1;
288 $g = $m;
289 $b = $v;
290 break;
291 case 5:
292 $r = $v;
293 $g = $m;
294 $b = $mid2;
295 break;
296 }
297 }
298 $a = $a * 255;
299
300 return array(
301 $r * 255.0,
302 $g * 255.0,
303 $b * 255.0,
304 $a
305 );
306 }
307
308 // Gradient
309 if (strpos($color, "url(#") !== false) {
310 $i = strpos($color, "(");
311 $j = strpos($color, ")");
312
313 // Bad url format
314 if ($i === false || $j === false) {
315 return null;
316 }
317
318 //FIXME: gradients not supported?
319 return null; // trim(substr($color, $i + 1, $j - $i - 1));
320 }
321
322 return null;
323 }
324
325 static function getQuad($color, $percent = false) {
326 $i = strpos($color, "(");
327 $j = strpos($color, ")");
328
329 // Bad color value
330 if ($i === false || $j === false) {
331 return null;
332 }
333
334 $quad = preg_split("/\\s*[,\\/]\\s*/", trim(substr($color, $i + 1, $j - $i - 1)));
335 if (!isset($quad[3])) {
336 $quad[3] = "1";
337 }
338
339 if (count($quad) != 3 && count($quad) != 4) {
340 return null;
341 }
342
343 foreach (array_keys($quad) as $c) {
344 $quad[$c] = trim($quad[$c]);
345
346 if ($percent) {
347 if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
348 $quad[$c] = floatval($quad[$c]) / 100;
349 } else {
350 $quad[$c] = floatval($quad[$c]) / 255;
351 }
352 } else {
353 if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
354 $quad[$c] = floatval($quad[$c]) * 2.55;
355 } else {
356 $quad[$c] = floatval($quad[$c]);
357 }
358 }
359 }
360
361 return $quad;
362 }
363
364 static function parseHexColor($hex)
365 {
366 $c = array(0.0, 0.0, 0.0, 1.0);
367
368 // #FFFFFF
369 if (isset($hex[6])) {
370 $c[0] = hexdec(substr($hex, 1, 2));
371 $c[1] = hexdec(substr($hex, 3, 2));
372 $c[2] = hexdec(substr($hex, 5, 2));
373
374 if (isset($hex[7])) {
375 $alpha = substr($hex, 7, 2);
376 if (ctype_xdigit($alpha)) {
377 $c[3] = round(hexdec($alpha)/255, 2);
378 }
379 }
380 } else {
381 $c[0] = hexdec($hex[1] . $hex[1]);
382 $c[1] = hexdec($hex[2] . $hex[2]);
383 $c[2] = hexdec($hex[3] . $hex[3]);
384
385 if (isset($hex[4])) {
386 if (ctype_xdigit($hex[4])) {
387 $c[3] = round(hexdec($hex[4] . $hex[4])/255, 2);
388 }
389 }
390 }
391
392 return $c;
393 }
394
395 /**
396 * Simple CSS parser
397 *
398 * @param $style
399 *
400 * @return array
401 */
402 static function parseCssStyle($style)
403 {
404 $matches = array();
405 preg_match_all("/([a-z-]+)\\s*:\\s*([^;$]+)/si", $style, $matches, PREG_SET_ORDER);
406
407 $styles = array();
408 foreach ($matches as $match) {
409 $styles[$match[1]] = $match[2];
410 }
411
412 return $styles;
413 }
414
415 static $colorNames = array(
416 'antiquewhite' => '#FAEBD7',
417 'aqua' => '#00FFFF',
418 'aquamarine' => '#7FFFD4',
419 'beige' => '#F5F5DC',
420 'black' => '#000000',
421 'blue' => '#0000FF',
422 'brown' => '#A52A2A',
423 'cadetblue' => '#5F9EA0',
424 'chocolate' => '#D2691E',
425 'cornflowerblue' => '#6495ED',
426 'crimson' => '#DC143C',
427 'darkblue' => '#00008B',
428 'darkgoldenrod' => '#B8860B',
429 'darkgreen' => '#006400',
430 'darkmagenta' => '#8B008B',
431 'darkorange' => '#FF8C00',
432 'darkred' => '#8B0000',
433 'darkseagreen' => '#8FBC8F',
434 'darkslategray' => '#2F4F4F',
435 'darkviolet' => '#9400D3',
436 'deepskyblue' => '#00BFFF',
437 'dodgerblue' => '#1E90FF',
438 'firebrick' => '#B22222',
439 'forestgreen' => '#228B22',
440 'fuchsia' => '#FF00FF',
441 'gainsboro' => '#DCDCDC',
442 'gold' => '#FFD700',
443 'gray' => '#808080',
444 'green' => '#008000',
445 'greenyellow' => '#ADFF2F',
446 'hotpink' => '#FF69B4',
447 'indigo' => '#4B0082',
448 'khaki' => '#F0E68C',
449 'lavenderblush' => '#FFF0F5',
450 'lemonchiffon' => '#FFFACD',
451 'lightcoral' => '#F08080',
452 'lightgoldenrodyellow' => '#FAFAD2',
453 'lightgreen' => '#90EE90',
454 'lightsalmon' => '#FFA07A',
455 'lightskyblue' => '#87CEFA',
456 'lightslategray' => '#778899',
457 'lightyellow' => '#FFFFE0',
458 'lime' => '#00FF00',
459 'limegreen' => '#32CD32',
460 'magenta' => '#FF00FF',
461 'maroon' => '#800000',
462 'mediumaquamarine' => '#66CDAA',
463 'mediumorchid' => '#BA55D3',
464 'mediumseagreen' => '#3CB371',
465 'mediumspringgreen' => '#00FA9A',
466 'mediumvioletred' => '#C71585',
467 'midnightblue' => '#191970',
468 'mintcream' => '#F5FFFA',
469 'moccasin' => '#FFE4B5',
470 'navy' => '#000080',
471 'olive' => '#808000',
472 'orange' => '#FFA500',
473 'orchid' => '#DA70D6',
474 'palegreen' => '#98FB98',
475 'palevioletred' => '#D87093',
476 'peachpuff' => '#FFDAB9',
477 'pink' => '#FFC0CB',
478 'powderblue' => '#B0E0E6',
479 'purple' => '#800080',
480 'red' => '#FF0000',
481 'royalblue' => '#4169E1',
482 'salmon' => '#FA8072',
483 'seagreen' => '#2E8B57',
484 'sienna' => '#A0522D',
485 'silver' => '#C0C0C0',
486 'skyblue' => '#87CEEB',
487 'slategray' => '#708090',
488 'springgreen' => '#00FF7F',
489 'steelblue' => '#4682B4',
490 'tan' => '#D2B48C',
491 'teal' => '#008080',
492 'thistle' => '#D8BFD8',
493 'turquoise' => '#40E0D0',
494 'violetred' => '#D02090',
495 'white' => '#FFFFFF',
496 'yellow' => '#FFFF00',
497 'aliceblue' => '#f0f8ff',
498 'azure' => '#f0ffff',
499 'bisque' => '#ffe4c4',
500 'blanchedalmond' => '#ffebcd',
501 'blueviolet' => '#8a2be2',
502 'burlywood' => '#deb887',
503 'chartreuse' => '#7fff00',
504 'coral' => '#ff7f50',
505 'cornsilk' => '#fff8dc',
506 'cyan' => '#00ffff',
507 'darkcyan' => '#008b8b',
508 'darkgray' => '#a9a9a9',
509 'darkgrey' => '#a9a9a9',
510 'darkkhaki' => '#bdb76b',
511 'darkolivegreen' => '#556b2f',
512 'darkorchid' => '#9932cc',
513 'darksalmon' => '#e9967a',
514 'darkslateblue' => '#483d8b',
515 'darkslategrey' => '#2f4f4f',
516 'darkturquoise' => '#00ced1',
517 'deeppink' => '#ff1493',
518 'dimgray' => '#696969',
519 'dimgrey' => '#696969',
520 'floralwhite' => '#fffaf0',
521 'ghostwhite' => '#f8f8ff',
522 'goldenrod' => '#daa520',
523 'grey' => '#808080',
524 'honeydew' => '#f0fff0',
525 'indianred' => '#cd5c5c',
526 'ivory' => '#fffff0',
527 'lavender' => '#e6e6fa',
528 'lawngreen' => '#7cfc00',
529 'lightblue' => '#add8e6',
530 'lightcyan' => '#e0ffff',
531 'lightgray' => '#d3d3d3',
532 'lightgrey' => '#d3d3d3',
533 'lightpink' => '#ffb6c1',
534 'lightseagreen' => '#20b2aa',
535 'lightslategrey' => '#778899',
536 'lightsteelblue' => '#b0c4de',
537 'linen' => '#faf0e6',
538 'mediumblue' => '#0000cd',
539 'mediumpurple' => '#9370db',
540 'mediumslateblue' => '#7b68ee',
541 'mediumturquoise' => '#48d1cc',
542 'mistyrose' => '#ffe4e1',
543 'navajowhite' => '#ffdead',
544 'oldlace' => '#fdf5e6',
545 'olivedrab' => '#6b8e23',
546 'orangered' => '#ff4500',
547 'palegoldenrod' => '#eee8aa',
548 'paleturquoise' => '#afeeee',
549 'papayawhip' => '#ffefd5',
550 'peru' => '#cd853f',
551 'plum' => '#dda0dd',
552 'rosybrown' => '#bc8f8f',
553 'saddlebrown' => '#8b4513',
554 'sandybrown' => '#f4a460',
555 'seashell' => '#fff5ee',
556 'slateblue' => '#6a5acd',
557 'slategrey' => '#708090',
558 'snow' => '#fffafa',
559 'tomato' => '#ff6347',
560 'violet' => '#ee82ee',
561 'wheat' => '#f5deb3',
562 'whitesmoke' => '#f5f5f5',
563 'yellowgreen' => '#9acd32',
564 );
565 }
566