| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package php-svg-lib |
| 5 |
* @link http://github.com/dompdf/php-svg-lib |
| 6 |
* @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html |
| 7 |
*/ |
| 8 |
namespace WCPOS\Vendor\Svg; |
| 9 |
|
| 10 |
use WCPOS\Vendor\Svg\Tag\AbstractTag; |
| 11 |
class Style |
| 12 |
{ |
| 13 |
const TYPE_COLOR = 1; |
| 14 |
const TYPE_LENGTH = 2; |
| 15 |
const TYPE_NAME = 3; |
| 16 |
const TYPE_ANGLE = 4; |
| 17 |
const TYPE_NUMBER = 5; |
| 18 |
private $_document; |
| 19 |
private $_parentStyle; |
| 20 |
public $color; |
| 21 |
public $opacity; |
| 22 |
public $display; |
| 23 |
public $fill; |
| 24 |
public $fillOpacity; |
| 25 |
public $fillRule; |
| 26 |
public $stroke; |
| 27 |
public $strokeOpacity; |
| 28 |
public $strokeLinecap; |
| 29 |
public $strokeLinejoin; |
| 30 |
public $strokeMiterlimit; |
| 31 |
public $strokeWidth; |
| 32 |
public $strokeDasharray; |
| 33 |
public $strokeDashoffset; |
| 34 |
public $fontFamily = 'serif'; |
| 35 |
public $fontSize = 12; |
| 36 |
public $fontWeight = 'normal'; |
| 37 |
public $fontStyle = 'normal'; |
| 38 |
public $textAnchor = 'start'; |
| 39 |
public function __construct($document = null) |
| 40 |
{ |
| 41 |
if ($document !== null) { |
| 42 |
$this->_document = $document; |
| 43 |
} |
| 44 |
} |
| 45 |
protected function getStyleMap() |
| 46 |
{ |
| 47 |
return array('color' => array('color', self::TYPE_COLOR), 'opacity' => array('opacity', self::TYPE_NUMBER), 'display' => array('display', self::TYPE_NAME), 'fill' => array('fill', self::TYPE_COLOR), 'fill-opacity' => array('fillOpacity', self::TYPE_NUMBER), 'fill-rule' => array('fillRule', self::TYPE_NAME), 'stroke' => array('stroke', self::TYPE_COLOR), 'stroke-dasharray' => array('strokeDasharray', self::TYPE_NAME), 'stroke-dashoffset' => array('strokeDashoffset', self::TYPE_NUMBER), 'stroke-linecap' => array('strokeLinecap', self::TYPE_NAME), 'stroke-linejoin' => array('strokeLinejoin', self::TYPE_NAME), 'stroke-miterlimit' => array('strokeMiterlimit', self::TYPE_NUMBER), 'stroke-opacity' => array('strokeOpacity', self::TYPE_NUMBER), 'stroke-width' => array('strokeWidth', self::TYPE_NUMBER), 'font-family' => array('fontFamily', self::TYPE_NAME), 'font-size' => array('fontSize', self::TYPE_NUMBER), 'font-weight' => array('fontWeight', self::TYPE_NAME), 'font-style' => array('fontStyle', self::TYPE_NAME), 'text-anchor' => array('textAnchor', self::TYPE_NAME)); |
| 48 |
} |
| 49 |
/** |
| 50 |
* @param $attributes |
| 51 |
* |
| 52 |
* @return Style |
| 53 |
*/ |
| 54 |
public function fromAttributes($attributes) |
| 55 |
{ |
| 56 |
$this->fillStyles($attributes); |
| 57 |
if (isset($attributes["style"])) { |
| 58 |
$styles = self::parseCssStyle($attributes["style"]); |
| 59 |
$this->fillStyles($styles); |
| 60 |
} |
| 61 |
} |
| 62 |
public function inherit(AbstractTag $tag) |
| 63 |
{ |
| 64 |
$group = $tag->getParentGroup(); |
| 65 |
if ($group) { |
| 66 |
$parent_style = $group->getStyle(); |
| 67 |
$this->_parentStyle = $parent_style; |
| 68 |
foreach ($parent_style as $_key => $_value) { |
| 69 |
if ($_value !== null) { |
| 70 |
$this->{$_key} = $_value; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
public function fromStyleSheets(AbstractTag $tag, $attributes) |
| 76 |
{ |
| 77 |
$class = isset($attributes["class"]) ? \preg_split('/\\s+/', \trim($attributes["class"])) : null; |
| 78 |
$stylesheets = $tag->getDocument()->getStyleSheets(); |
| 79 |
$outputFormat = \WCPOS\Vendor\Sabberworm\CSS\OutputFormat::createCompact(); |
| 80 |
$styles = array(); |
| 81 |
foreach ($stylesheets as $_sc) { |
| 82 |
/** @var \Sabberworm\CSS\RuleSet\DeclarationBlock $_decl */ |
| 83 |
foreach ($_sc->getAllDeclarationBlocks() as $_decl) { |
| 84 |
/** @var \Sabberworm\CSS\Property\Selector $_selector */ |
| 85 |
foreach ($_decl->getSelectors() as $_selector) { |
| 86 |
$_selector = $_selector->getSelector(); |
| 87 |
// Match class name |
| 88 |
if ($class !== null) { |
| 89 |
foreach ($class as $_class) { |
| 90 |
if ($_selector === ".{$_class}") { |
| 91 |
/** @var \Sabberworm\CSS\Rule\Rule $_rule */ |
| 92 |
foreach ($_decl->getRules() as $_rule) { |
| 93 |
$value = $_rule->getValue(); |
| 94 |
if ($value instanceof \WCPOS\Vendor\Sabberworm\CSS\Value\Value) { |
| 95 |
$styles[$_rule->getRule()] = $value->render($outputFormat) . ""; |
| 96 |
} else { |
| 97 |
$styles[$_rule->getRule()] = $value . ""; |
| 98 |
} |
| 99 |
} |
| 100 |
break 2; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
// Match tag name |
| 105 |
if ($_selector === $tag->tagName) { |
| 106 |
/** @var \Sabberworm\CSS\Rule\Rule $_rule */ |
| 107 |
foreach ($_decl->getRules() as $_rule) { |
| 108 |
$value = $_rule->getValue(); |
| 109 |
if ($value instanceof \WCPOS\Vendor\Sabberworm\CSS\Value\Value) { |
| 110 |
$styles[$_rule->getRule()] = $value->render($outputFormat) . ""; |
| 111 |
} else { |
| 112 |
$styles[$_rule->getRule()] = $value . ""; |
| 113 |
} |
| 114 |
} |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
$this->fillStyles($styles); |
| 121 |
} |
| 122 |
protected function fillStyles($styles) |
| 123 |
{ |
| 124 |
$style_map = $this->getStyleMap(); |
| 125 |
foreach ($style_map as $from => $spec) { |
| 126 |
if (isset($styles[$from])) { |
| 127 |
list($to, $type) = $spec; |
| 128 |
$value = null; |
| 129 |
switch ($type) { |
| 130 |
case self::TYPE_COLOR: |
| 131 |
$value = self::parseColor($styles[$from]); |
| 132 |
if ($value === "currentcolor") { |
| 133 |
if ($type === "color") { |
| 134 |
$value = $this->_parentStyle->color; |
| 135 |
} else { |
| 136 |
$value = $this->color; |
| 137 |
} |
| 138 |
} |
| 139 |
if (\is_array($value) && $value[3] !== 1.0 && \array_key_exists("{$from}-opacity", $style_map) === \true) { |
| 140 |
$styles["{$from}-opacity"] = $value[3]; |
| 141 |
} |
| 142 |
break; |
| 143 |
case self::TYPE_NUMBER: |
| 144 |
$value = $styles[$from] === null ? null : (float) $styles[$from]; |
| 145 |
break; |
| 146 |
default: |
| 147 |
$value = $styles[$from]; |
| 148 |
} |
| 149 |
if ($from === "font-family") { |
| 150 |
$scheme = \strtolower(\parse_url($value, \PHP_URL_SCHEME) ?: ""); |
| 151 |
if ($scheme === "phar" || \strtolower(\substr($value, 0, 7)) === "phar://" || $this->_document !== null && $this->_document->allowExternalReferences === \false && $scheme !== "data") { |
| 152 |
continue; |
| 153 |
} |
| 154 |
} |
| 155 |
if ($value !== null) { |
| 156 |
$this->{$to} = $value; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
static function parseColor($color) |
| 162 |
{ |
| 163 |
$color = \strtolower(\trim($color)); |
| 164 |
$parts = \preg_split('/[^,]\\s+/', $color, 2); |
| 165 |
if (\count($parts) == 2) { |
| 166 |
$color = $parts[1]; |
| 167 |
} else { |
| 168 |
$color = $parts[0]; |
| 169 |
} |
| 170 |
if ($color === "none") { |
| 171 |
return "none"; |
| 172 |
} |
| 173 |
if ($color === "currentcolor") { |
| 174 |
return "currentcolor"; |
| 175 |
} |
| 176 |
if ($color === "transparent") { |
| 177 |
return [0.0, 0.0, 0.0, 0.0]; |
| 178 |
} |
| 179 |
// SVG color name |
| 180 |
if (isset(self::$colorNames[$color])) { |
| 181 |
return self::parseHexColor(self::$colorNames[$color]); |
| 182 |
} |
| 183 |
// Hex color |
| 184 |
if ($color[0] === "#") { |
| 185 |
return self::parseHexColor($color); |
| 186 |
} |
| 187 |
// RGB color |
| 188 |
if (\strpos($color, "rgb") !== \false) { |
| 189 |
return self::getQuad($color); |
| 190 |
} |
| 191 |
// HSL color |
| 192 |
if (\strpos($color, "hsl") !== \false) { |
| 193 |
$quad = self::getQuad($color, \true); |
| 194 |
if ($quad == null) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
list($h, $s, $l, $a) = $quad; |
| 198 |
$r = $l; |
| 199 |
$g = $l; |
| 200 |
$b = $l; |
| 201 |
$v = $l <= 0.5 ? $l * (1.0 + $s) : $l + $s - $l * $s; |
| 202 |
if ($v > 0) { |
| 203 |
$m = $l + $l - $v; |
| 204 |
$sv = ($v - $m) / $v; |
| 205 |
$h *= 6.0; |
| 206 |
$sextant = \floor($h); |
| 207 |
$fract = $h - $sextant; |
| 208 |
$vsf = $v * $sv * $fract; |
| 209 |
$mid1 = $m + $vsf; |
| 210 |
$mid2 = $v - $vsf; |
| 211 |
switch ($sextant) { |
| 212 |
case 0: |
| 213 |
$r = $v; |
| 214 |
$g = $mid1; |
| 215 |
$b = $m; |
| 216 |
break; |
| 217 |
case 1: |
| 218 |
$r = $mid2; |
| 219 |
$g = $v; |
| 220 |
$b = $m; |
| 221 |
break; |
| 222 |
case 2: |
| 223 |
$r = $m; |
| 224 |
$g = $v; |
| 225 |
$b = $mid1; |
| 226 |
break; |
| 227 |
case 3: |
| 228 |
$r = $m; |
| 229 |
$g = $mid2; |
| 230 |
$b = $v; |
| 231 |
break; |
| 232 |
case 4: |
| 233 |
$r = $mid1; |
| 234 |
$g = $m; |
| 235 |
$b = $v; |
| 236 |
break; |
| 237 |
case 5: |
| 238 |
$r = $v; |
| 239 |
$g = $m; |
| 240 |
$b = $mid2; |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
$a = $a * 255; |
| 245 |
return array($r * 255.0, $g * 255.0, $b * 255.0, $a); |
| 246 |
} |
| 247 |
// Gradient |
| 248 |
if (\strpos($color, "url(#") !== \false) { |
| 249 |
$i = \strpos($color, "("); |
| 250 |
$j = \strpos($color, ")"); |
| 251 |
// Bad url format |
| 252 |
if ($i === \false || $j === \false) { |
| 253 |
return null; |
| 254 |
} |
| 255 |
//FIXME: gradients not supported? |
| 256 |
return null; |
| 257 |
// trim(substr($color, $i + 1, $j - $i - 1)); |
| 258 |
} |
| 259 |
return null; |
| 260 |
} |
| 261 |
static function getQuad($color, $percent = \false) |
| 262 |
{ |
| 263 |
$i = \strpos($color, "("); |
| 264 |
$j = \strpos($color, ")"); |
| 265 |
// Bad color value |
| 266 |
if ($i === \false || $j === \false) { |
| 267 |
return null; |
| 268 |
} |
| 269 |
$quad = \preg_split("/\\s*[,\\/]\\s*/", \trim(\substr($color, $i + 1, $j - $i - 1))); |
| 270 |
if (!isset($quad[3])) { |
| 271 |
$quad[3] = "1"; |
| 272 |
} |
| 273 |
if (\count($quad) != 3 && \count($quad) != 4) { |
| 274 |
return null; |
| 275 |
} |
| 276 |
foreach (\array_keys($quad) as $c) { |
| 277 |
$quad[$c] = \trim($quad[$c]); |
| 278 |
if ($percent) { |
| 279 |
if ($quad[$c][\strlen($quad[$c]) - 1] === "%") { |
| 280 |
$quad[$c] = \floatval($quad[$c]) / 100; |
| 281 |
} else { |
| 282 |
$quad[$c] = \floatval($quad[$c]) / 255; |
| 283 |
} |
| 284 |
} else { |
| 285 |
if ($quad[$c][\strlen($quad[$c]) - 1] === "%") { |
| 286 |
$quad[$c] = \floatval($quad[$c]) * 2.55; |
| 287 |
} else { |
| 288 |
$quad[$c] = \floatval($quad[$c]); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
return $quad; |
| 293 |
} |
| 294 |
static function parseHexColor($hex) |
| 295 |
{ |
| 296 |
$c = array(0.0, 0.0, 0.0, 1.0); |
| 297 |
// #FFFFFF |
| 298 |
if (isset($hex[6])) { |
| 299 |
$c[0] = \hexdec(\substr($hex, 1, 2)); |
| 300 |
$c[1] = \hexdec(\substr($hex, 3, 2)); |
| 301 |
$c[2] = \hexdec(\substr($hex, 5, 2)); |
| 302 |
if (isset($hex[7])) { |
| 303 |
$alpha = \substr($hex, 7, 2); |
| 304 |
if (\ctype_xdigit($alpha)) { |
| 305 |
$c[3] = \round(\hexdec($alpha) / 255, 2); |
| 306 |
} |
| 307 |
} |
| 308 |
} else { |
| 309 |
$c[0] = \hexdec($hex[1] . $hex[1]); |
| 310 |
$c[1] = \hexdec($hex[2] . $hex[2]); |
| 311 |
$c[2] = \hexdec($hex[3] . $hex[3]); |
| 312 |
if (isset($hex[4])) { |
| 313 |
if (\ctype_xdigit($hex[4])) { |
| 314 |
$c[3] = \round(\hexdec($hex[4] . $hex[4]) / 255, 2); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
return $c; |
| 319 |
} |
| 320 |
/** |
| 321 |
* Simple CSS parser |
| 322 |
* |
| 323 |
* @param $style |
| 324 |
* |
| 325 |
* @return array |
| 326 |
*/ |
| 327 |
static function parseCssStyle($style) |
| 328 |
{ |
| 329 |
$matches = array(); |
| 330 |
\preg_match_all("/([a-z-]+)\\s*:\\s*([^;\$]+)/si", $style, $matches, \PREG_SET_ORDER); |
| 331 |
$styles = array(); |
| 332 |
foreach ($matches as $match) { |
| 333 |
$styles[$match[1]] = $match[2]; |
| 334 |
} |
| 335 |
return $styles; |
| 336 |
} |
| 337 |
static $colorNames = array('antiquewhite' => '#FAEBD7', 'aqua' => '#00FFFF', 'aquamarine' => '#7FFFD4', 'beige' => '#F5F5DC', 'black' => '#000000', 'blue' => '#0000FF', 'brown' => '#A52A2A', 'cadetblue' => '#5F9EA0', 'chocolate' => '#D2691E', 'cornflowerblue' => '#6495ED', 'crimson' => '#DC143C', 'darkblue' => '#00008B', 'darkgoldenrod' => '#B8860B', 'darkgreen' => '#006400', 'darkmagenta' => '#8B008B', 'darkorange' => '#FF8C00', 'darkred' => '#8B0000', 'darkseagreen' => '#8FBC8F', 'darkslategray' => '#2F4F4F', 'darkviolet' => '#9400D3', 'deepskyblue' => '#00BFFF', 'dodgerblue' => '#1E90FF', 'firebrick' => '#B22222', 'forestgreen' => '#228B22', 'fuchsia' => '#FF00FF', 'gainsboro' => '#DCDCDC', 'gold' => '#FFD700', 'gray' => '#808080', 'green' => '#008000', 'greenyellow' => '#ADFF2F', 'hotpink' => '#FF69B4', 'indigo' => '#4B0082', 'khaki' => '#F0E68C', 'lavenderblush' => '#FFF0F5', 'lemonchiffon' => '#FFFACD', 'lightcoral' => '#F08080', 'lightgoldenrodyellow' => '#FAFAD2', 'lightgreen' => '#90EE90', 'lightsalmon' => '#FFA07A', 'lightskyblue' => '#87CEFA', 'lightslategray' => '#778899', 'lightyellow' => '#FFFFE0', 'lime' => '#00FF00', 'limegreen' => '#32CD32', 'magenta' => '#FF00FF', 'maroon' => '#800000', 'mediumaquamarine' => '#66CDAA', 'mediumorchid' => '#BA55D3', 'mediumseagreen' => '#3CB371', 'mediumspringgreen' => '#00FA9A', 'mediumvioletred' => '#C71585', 'midnightblue' => '#191970', 'mintcream' => '#F5FFFA', 'moccasin' => '#FFE4B5', 'navy' => '#000080', 'olive' => '#808000', 'orange' => '#FFA500', 'orchid' => '#DA70D6', 'palegreen' => '#98FB98', 'palevioletred' => '#D87093', 'peachpuff' => '#FFDAB9', 'pink' => '#FFC0CB', 'powderblue' => '#B0E0E6', 'purple' => '#800080', 'red' => '#FF0000', 'royalblue' => '#4169E1', 'salmon' => '#FA8072', 'seagreen' => '#2E8B57', 'sienna' => '#A0522D', 'silver' => '#C0C0C0', 'skyblue' => '#87CEEB', 'slategray' => '#708090', 'springgreen' => '#00FF7F', 'steelblue' => '#4682B4', 'tan' => '#D2B48C', 'teal' => '#008080', 'thistle' => '#D8BFD8', 'turquoise' => '#40E0D0', 'violetred' => '#D02090', 'white' => '#FFFFFF', 'yellow' => '#FFFF00', 'aliceblue' => '#f0f8ff', 'azure' => '#f0ffff', 'bisque' => '#ffe4c4', 'blanchedalmond' => '#ffebcd', 'blueviolet' => '#8a2be2', 'burlywood' => '#deb887', 'chartreuse' => '#7fff00', 'coral' => '#ff7f50', 'cornsilk' => '#fff8dc', 'cyan' => '#00ffff', 'darkcyan' => '#008b8b', 'darkgray' => '#a9a9a9', 'darkgrey' => '#a9a9a9', 'darkkhaki' => '#bdb76b', 'darkolivegreen' => '#556b2f', 'darkorchid' => '#9932cc', 'darksalmon' => '#e9967a', 'darkslateblue' => '#483d8b', 'darkslategrey' => '#2f4f4f', 'darkturquoise' => '#00ced1', 'deeppink' => '#ff1493', 'dimgray' => '#696969', 'dimgrey' => '#696969', 'floralwhite' => '#fffaf0', 'ghostwhite' => '#f8f8ff', 'goldenrod' => '#daa520', 'grey' => '#808080', 'honeydew' => '#f0fff0', 'indianred' => '#cd5c5c', 'ivory' => '#fffff0', 'lavender' => '#e6e6fa', 'lawngreen' => '#7cfc00', 'lightblue' => '#add8e6', 'lightcyan' => '#e0ffff', 'lightgray' => '#d3d3d3', 'lightgrey' => '#d3d3d3', 'lightpink' => '#ffb6c1', 'lightseagreen' => '#20b2aa', 'lightslategrey' => '#778899', 'lightsteelblue' => '#b0c4de', 'linen' => '#faf0e6', 'mediumblue' => '#0000cd', 'mediumpurple' => '#9370db', 'mediumslateblue' => '#7b68ee', 'mediumturquoise' => '#48d1cc', 'mistyrose' => '#ffe4e1', 'navajowhite' => '#ffdead', 'oldlace' => '#fdf5e6', 'olivedrab' => '#6b8e23', 'orangered' => '#ff4500', 'palegoldenrod' => '#eee8aa', 'paleturquoise' => '#afeeee', 'papayawhip' => '#ffefd5', 'peru' => '#cd853f', 'plum' => '#dda0dd', 'rosybrown' => '#bc8f8f', 'saddlebrown' => '#8b4513', 'sandybrown' => '#f4a460', 'seashell' => '#fff5ee', 'slateblue' => '#6a5acd', 'slategrey' => '#708090', 'snow' => '#fffafa', 'tomato' => '#ff6347', 'violet' => '#ee82ee', 'wheat' => '#f5deb3', 'whitesmoke' => '#f5f5f5', 'yellowgreen' => '#9acd32'); |
| 338 |
} |
| 339 |
|