Gradient
3 years ago
Surface
3 years ago
Tag
3 years ago
CssLength.php
3 years ago
DefaultStyle.php
3 years ago
Document.php
3 years ago
Style.php
3 years ago
Style.php
309 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package php-svg-lib |
| 5 | * @link http://github.com/PhenX/php-svg-lib |
| 6 | * @author Fabien Ménager <fabien.menager@gmail.com> |
| 7 | * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html |
| 8 | */ |
| 9 | namespace IAWP\Svg; |
| 10 | |
| 11 | use IAWP\Svg\Tag\AbstractTag; |
| 12 | class Style |
| 13 | { |
| 14 | const TYPE_COLOR = 1; |
| 15 | const TYPE_LENGTH = 2; |
| 16 | const TYPE_NAME = 3; |
| 17 | const TYPE_ANGLE = 4; |
| 18 | const TYPE_NUMBER = 5; |
| 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 | protected function getStyleMap() |
| 40 | { |
| 41 | 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)); |
| 42 | } |
| 43 | /** |
| 44 | * @param $attributes |
| 45 | * |
| 46 | * @return Style |
| 47 | */ |
| 48 | public function fromAttributes($attributes) |
| 49 | { |
| 50 | $this->fillStyles($attributes); |
| 51 | if (isset($attributes["style"])) { |
| 52 | $styles = self::parseCssStyle($attributes["style"]); |
| 53 | $this->fillStyles($styles); |
| 54 | } |
| 55 | } |
| 56 | public function inherit(AbstractTag $tag) |
| 57 | { |
| 58 | $group = $tag->getParentGroup(); |
| 59 | if ($group) { |
| 60 | $parent_style = $group->getStyle(); |
| 61 | $this->_parentStyle = $parent_style; |
| 62 | foreach ($parent_style as $_key => $_value) { |
| 63 | if ($_value !== null) { |
| 64 | $this->{$_key} = $_value; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | public function fromStyleSheets(AbstractTag $tag, $attributes) |
| 70 | { |
| 71 | $class = isset($attributes["class"]) ? \preg_split('/\\s+/', \trim($attributes["class"])) : null; |
| 72 | $stylesheets = $tag->getDocument()->getStyleSheets(); |
| 73 | $styles = array(); |
| 74 | foreach ($stylesheets as $_sc) { |
| 75 | /** @var \Sabberworm\CSS\RuleSet\DeclarationBlock $_decl */ |
| 76 | foreach ($_sc->getAllDeclarationBlocks() as $_decl) { |
| 77 | /** @var \Sabberworm\CSS\Property\Selector $_selector */ |
| 78 | foreach ($_decl->getSelectors() as $_selector) { |
| 79 | $_selector = $_selector->getSelector(); |
| 80 | // Match class name |
| 81 | if ($class !== null) { |
| 82 | foreach ($class as $_class) { |
| 83 | if ($_selector === ".{$_class}") { |
| 84 | /** @var \Sabberworm\CSS\Rule\Rule $_rule */ |
| 85 | foreach ($_decl->getRules() as $_rule) { |
| 86 | $styles[$_rule->getRule()] = $_rule->getValue() . ""; |
| 87 | } |
| 88 | break 2; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | // Match tag name |
| 93 | if ($_selector === $tag->tagName) { |
| 94 | /** @var \Sabberworm\CSS\Rule\Rule $_rule */ |
| 95 | foreach ($_decl->getRules() as $_rule) { |
| 96 | $styles[$_rule->getRule()] = $_rule->getValue() . ""; |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | $this->fillStyles($styles); |
| 104 | } |
| 105 | protected function fillStyles($styles) |
| 106 | { |
| 107 | $style_map = $this->getStyleMap(); |
| 108 | foreach ($style_map as $from => $spec) { |
| 109 | if (isset($styles[$from])) { |
| 110 | list($to, $type) = $spec; |
| 111 | $value = null; |
| 112 | switch ($type) { |
| 113 | case self::TYPE_COLOR: |
| 114 | $value = self::parseColor($styles[$from]); |
| 115 | if ($value === "currentcolor") { |
| 116 | if ($type === "color") { |
| 117 | $value = $this->_parentStyle->color; |
| 118 | } else { |
| 119 | $value = $this->color; |
| 120 | } |
| 121 | } |
| 122 | if ($value !== null && $value[3] !== 1 && \array_key_exists("{$from}-opacity", $style_map) === \true) { |
| 123 | $styles["{$from}-opacity"] = $value[3]; |
| 124 | } |
| 125 | break; |
| 126 | case self::TYPE_NUMBER: |
| 127 | $value = $styles[$from] === null ? null : (float) $styles[$from]; |
| 128 | break; |
| 129 | default: |
| 130 | $value = $styles[$from]; |
| 131 | } |
| 132 | if ($value !== null) { |
| 133 | $this->{$to} = $value; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | static function parseColor($color) |
| 139 | { |
| 140 | $color = \strtolower(\trim($color)); |
| 141 | $parts = \preg_split('/[^,]\\s+/', $color, 2); |
| 142 | if (\count($parts) == 2) { |
| 143 | $color = $parts[1]; |
| 144 | } else { |
| 145 | $color = $parts[0]; |
| 146 | } |
| 147 | if ($color === "none") { |
| 148 | return "none"; |
| 149 | } |
| 150 | if ($color === "currentcolor") { |
| 151 | return "currentcolor"; |
| 152 | } |
| 153 | // SVG color name |
| 154 | if (isset(self::$colorNames[$color])) { |
| 155 | return self::parseHexColor(self::$colorNames[$color]); |
| 156 | } |
| 157 | // Hex color |
| 158 | if ($color[0] === "#") { |
| 159 | return self::parseHexColor($color); |
| 160 | } |
| 161 | // RGB color |
| 162 | if (\strpos($color, "rgb") !== \false) { |
| 163 | return self::getQuad($color); |
| 164 | } |
| 165 | // RGB color |
| 166 | if (\strpos($color, "hsl") !== \false) { |
| 167 | $quad = self::getQuad($color, \true); |
| 168 | if ($quad == null) { |
| 169 | return null; |
| 170 | } |
| 171 | list($h, $s, $l, $a) = $quad; |
| 172 | $r = $l; |
| 173 | $g = $l; |
| 174 | $b = $l; |
| 175 | $v = $l <= 0.5 ? $l * (1.0 + $s) : $l + $s - $l * $s; |
| 176 | if ($v > 0) { |
| 177 | $m = $l + $l - $v; |
| 178 | $sv = ($v - $m) / $v; |
| 179 | $h *= 6.0; |
| 180 | $sextant = \floor($h); |
| 181 | $fract = $h - $sextant; |
| 182 | $vsf = $v * $sv * $fract; |
| 183 | $mid1 = $m + $vsf; |
| 184 | $mid2 = $v - $vsf; |
| 185 | switch ($sextant) { |
| 186 | case 0: |
| 187 | $r = $v; |
| 188 | $g = $mid1; |
| 189 | $b = $m; |
| 190 | break; |
| 191 | case 1: |
| 192 | $r = $mid2; |
| 193 | $g = $v; |
| 194 | $b = $m; |
| 195 | break; |
| 196 | case 2: |
| 197 | $r = $m; |
| 198 | $g = $v; |
| 199 | $b = $mid1; |
| 200 | break; |
| 201 | case 3: |
| 202 | $r = $m; |
| 203 | $g = $mid2; |
| 204 | $b = $v; |
| 205 | break; |
| 206 | case 4: |
| 207 | $r = $mid1; |
| 208 | $g = $m; |
| 209 | $b = $v; |
| 210 | break; |
| 211 | case 5: |
| 212 | $r = $v; |
| 213 | $g = $m; |
| 214 | $b = $mid2; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | $a = $a * 255; |
| 219 | return array($r * 255.0, $g * 255.0, $b * 255.0, $a); |
| 220 | } |
| 221 | // Gradient |
| 222 | if (\strpos($color, "url(#") !== \false) { |
| 223 | $i = \strpos($color, "("); |
| 224 | $j = \strpos($color, ")"); |
| 225 | // Bad url format |
| 226 | if ($i === \false || $j === \false) { |
| 227 | return null; |
| 228 | } |
| 229 | return \trim(\substr($color, $i + 1, $j - $i - 1)); |
| 230 | } |
| 231 | return null; |
| 232 | } |
| 233 | static function getQuad($color, $percent = \false) |
| 234 | { |
| 235 | $i = \strpos($color, "("); |
| 236 | $j = \strpos($color, ")"); |
| 237 | // Bad color value |
| 238 | if ($i === \false || $j === \false) { |
| 239 | return null; |
| 240 | } |
| 241 | $quad = \preg_split("/\\s*[,\\/]\\s*/", \trim(\substr($color, $i + 1, $j - $i - 1))); |
| 242 | if (!isset($quad[3])) { |
| 243 | $quad[3] = 1; |
| 244 | } |
| 245 | if (\count($quad) != 3 && \count($quad) != 4) { |
| 246 | return null; |
| 247 | } |
| 248 | foreach (\array_keys($quad) as $c) { |
| 249 | $quad[$c] = \trim($quad[$c]); |
| 250 | if ($percent) { |
| 251 | if ($quad[$c][\strlen($quad[$c]) - 1] === "%") { |
| 252 | $quad[$c] = \floatval($quad[$c]) / 100; |
| 253 | } else { |
| 254 | $quad[$c] = $quad[$c] / 255; |
| 255 | } |
| 256 | } else { |
| 257 | if ($quad[$c][\strlen($quad[$c]) - 1] === "%") { |
| 258 | $quad[$c] = \round(\floatval($quad[$c]) * 2.55); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | return $quad; |
| 263 | } |
| 264 | static function parseHexColor($hex) |
| 265 | { |
| 266 | $c = array(0, 0, 0, 1); |
| 267 | // #FFFFFF |
| 268 | if (isset($hex[6])) { |
| 269 | $c[0] = \hexdec(\substr($hex, 1, 2)); |
| 270 | $c[1] = \hexdec(\substr($hex, 3, 2)); |
| 271 | $c[2] = \hexdec(\substr($hex, 5, 2)); |
| 272 | if (isset($hex[7])) { |
| 273 | $alpha = \substr($hex, 7, 2); |
| 274 | if (\ctype_xdigit($alpha)) { |
| 275 | $c[3] = \round(\hexdec($alpha) / 255, 2); |
| 276 | } |
| 277 | } |
| 278 | } else { |
| 279 | $c[0] = \hexdec($hex[1] . $hex[1]); |
| 280 | $c[1] = \hexdec($hex[2] . $hex[2]); |
| 281 | $c[2] = \hexdec($hex[3] . $hex[3]); |
| 282 | if (isset($hex[4])) { |
| 283 | if (\ctype_xdigit($hex[4])) { |
| 284 | $c[3] = \round(\hexdec($hex[4] . $hex[4]) / 255, 2); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | return $c; |
| 289 | } |
| 290 | /** |
| 291 | * Simple CSS parser |
| 292 | * |
| 293 | * @param $style |
| 294 | * |
| 295 | * @return array |
| 296 | */ |
| 297 | static function parseCssStyle($style) |
| 298 | { |
| 299 | $matches = array(); |
| 300 | \preg_match_all("/([a-z-]+)\\s*:\\s*([^;\$]+)/si", $style, $matches, \PREG_SET_ORDER); |
| 301 | $styles = array(); |
| 302 | foreach ($matches as $match) { |
| 303 | $styles[$match[1]] = $match[2]; |
| 304 | } |
| 305 | return $styles; |
| 306 | } |
| 307 | 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'); |
| 308 | } |
| 309 |