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
Document.php
320 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\Surface\SurfaceInterface; |
| 12 | use IAWP\Svg\Tag\AbstractTag; |
| 13 | use IAWP\Svg\Tag\Anchor; |
| 14 | use IAWP\Svg\Tag\Circle; |
| 15 | use IAWP\Svg\Tag\Ellipse; |
| 16 | use IAWP\Svg\Tag\Group; |
| 17 | use IAWP\Svg\Tag\ClipPath; |
| 18 | use IAWP\Svg\Tag\Image; |
| 19 | use IAWP\Svg\Tag\Line; |
| 20 | use IAWP\Svg\Tag\LinearGradient; |
| 21 | use IAWP\Svg\Tag\Path; |
| 22 | use IAWP\Svg\Tag\Polygon; |
| 23 | use IAWP\Svg\Tag\Polyline; |
| 24 | use IAWP\Svg\Tag\Rect; |
| 25 | use IAWP\Svg\Tag\Stop; |
| 26 | use IAWP\Svg\Tag\Text; |
| 27 | use IAWP\Svg\Tag\StyleTag; |
| 28 | use IAWP\Svg\Tag\UseTag; |
| 29 | class Document extends AbstractTag |
| 30 | { |
| 31 | protected $filename; |
| 32 | public $inDefs = \false; |
| 33 | protected $x; |
| 34 | protected $y; |
| 35 | protected $width; |
| 36 | protected $height; |
| 37 | protected $subPathInit; |
| 38 | protected $pathBBox; |
| 39 | protected $viewBox; |
| 40 | /** @var SurfaceInterface */ |
| 41 | protected $surface; |
| 42 | /** @var AbstractTag[] */ |
| 43 | protected $stack = array(); |
| 44 | /** @var AbstractTag[] */ |
| 45 | protected $defs = array(); |
| 46 | /** @var \Sabberworm\CSS\CSSList\Document[] */ |
| 47 | protected $styleSheets = array(); |
| 48 | public function loadFile($filename) |
| 49 | { |
| 50 | $this->filename = $filename; |
| 51 | } |
| 52 | protected function initParser() |
| 53 | { |
| 54 | $parser = \xml_parser_create("utf-8"); |
| 55 | \xml_parser_set_option($parser, \XML_OPTION_CASE_FOLDING, \false); |
| 56 | \xml_set_element_handler($parser, array($this, "_tagStart"), array($this, "_tagEnd")); |
| 57 | \xml_set_character_data_handler($parser, array($this, "_charData")); |
| 58 | return $parser; |
| 59 | } |
| 60 | public function __construct() |
| 61 | { |
| 62 | } |
| 63 | /** |
| 64 | * @return SurfaceInterface |
| 65 | */ |
| 66 | public function getSurface() |
| 67 | { |
| 68 | return $this->surface; |
| 69 | } |
| 70 | public function getStack() |
| 71 | { |
| 72 | return $this->stack; |
| 73 | } |
| 74 | public function getWidth() |
| 75 | { |
| 76 | return $this->width; |
| 77 | } |
| 78 | public function getHeight() |
| 79 | { |
| 80 | return $this->height; |
| 81 | } |
| 82 | public function getDiagonal() |
| 83 | { |
| 84 | return \sqrt($this->width ** 2 + $this->height ** 2) / \sqrt(2); |
| 85 | } |
| 86 | public function getDimensions() |
| 87 | { |
| 88 | $rootAttributes = null; |
| 89 | $parser = \xml_parser_create("utf-8"); |
| 90 | \xml_parser_set_option($parser, \XML_OPTION_CASE_FOLDING, \false); |
| 91 | \xml_set_element_handler($parser, function ($parser, $name, $attributes) use(&$rootAttributes) { |
| 92 | if ($name === "svg" && $rootAttributes === null) { |
| 93 | $attributes = \array_change_key_case($attributes, \CASE_LOWER); |
| 94 | $rootAttributes = $attributes; |
| 95 | } |
| 96 | }, function ($parser, $name) { |
| 97 | }); |
| 98 | $fp = \fopen($this->filename, "r"); |
| 99 | while ($line = \fread($fp, 8192)) { |
| 100 | \xml_parse($parser, $line, \false); |
| 101 | if ($rootAttributes !== null) { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | \xml_parser_free($parser); |
| 106 | return $this->handleSizeAttributes($rootAttributes); |
| 107 | } |
| 108 | public function handleSizeAttributes($attributes) |
| 109 | { |
| 110 | if ($this->width === null) { |
| 111 | if (isset($attributes["width"])) { |
| 112 | $width = $this->convertSize($attributes["width"], 400); |
| 113 | $this->width = $width; |
| 114 | } |
| 115 | if (isset($attributes["height"])) { |
| 116 | $height = $this->convertSize($attributes["height"], 300); |
| 117 | $this->height = $height; |
| 118 | } |
| 119 | if (isset($attributes['viewbox'])) { |
| 120 | $viewBox = \preg_split('/[\\s,]+/is', \trim($attributes['viewbox'])); |
| 121 | if (\count($viewBox) == 4) { |
| 122 | $this->x = $viewBox[0]; |
| 123 | $this->y = $viewBox[1]; |
| 124 | if (!$this->width) { |
| 125 | $this->width = $viewBox[2]; |
| 126 | } |
| 127 | if (!$this->height) { |
| 128 | $this->height = $viewBox[3]; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | return array(0 => $this->width, 1 => $this->height, "width" => $this->width, "height" => $this->height); |
| 134 | } |
| 135 | public function getDocument() |
| 136 | { |
| 137 | return $this; |
| 138 | } |
| 139 | /** |
| 140 | * Append a style sheet |
| 141 | * |
| 142 | * @param \Sabberworm\CSS\CSSList\Document $stylesheet |
| 143 | */ |
| 144 | public function appendStyleSheet($stylesheet) |
| 145 | { |
| 146 | $this->styleSheets[] = $stylesheet; |
| 147 | } |
| 148 | /** |
| 149 | * Get the document style sheets |
| 150 | * |
| 151 | * @return \Sabberworm\CSS\CSSList\Document[] |
| 152 | */ |
| 153 | public function getStyleSheets() |
| 154 | { |
| 155 | return $this->styleSheets; |
| 156 | } |
| 157 | protected function before($attributes) |
| 158 | { |
| 159 | $surface = $this->getSurface(); |
| 160 | $style = new DefaultStyle(); |
| 161 | $style->inherit($this); |
| 162 | $style->fromAttributes($attributes); |
| 163 | $this->setStyle($style); |
| 164 | $surface->setStyle($style); |
| 165 | } |
| 166 | public function render(SurfaceInterface $surface) |
| 167 | { |
| 168 | $this->inDefs = \false; |
| 169 | $this->surface = $surface; |
| 170 | $parser = $this->initParser(); |
| 171 | if ($this->x || $this->y) { |
| 172 | $surface->translate(-$this->x, -$this->y); |
| 173 | } |
| 174 | $fp = \fopen($this->filename, "r"); |
| 175 | while ($line = \fread($fp, 8192)) { |
| 176 | \xml_parse($parser, $line, \false); |
| 177 | } |
| 178 | \xml_parse($parser, "", \true); |
| 179 | \xml_parser_free($parser); |
| 180 | } |
| 181 | protected function svgOffset($attributes) |
| 182 | { |
| 183 | $this->attributes = $attributes; |
| 184 | $this->handleSizeAttributes($attributes); |
| 185 | } |
| 186 | public function getDef($id) |
| 187 | { |
| 188 | $id = \ltrim($id, "#"); |
| 189 | return isset($this->defs[$id]) ? $this->defs[$id] : null; |
| 190 | } |
| 191 | private function _tagStart($parser, $name, $attributes) |
| 192 | { |
| 193 | $this->x = 0; |
| 194 | $this->y = 0; |
| 195 | $tag = null; |
| 196 | $attributes = \array_change_key_case($attributes, \CASE_LOWER); |
| 197 | switch (\strtolower($name)) { |
| 198 | case 'defs': |
| 199 | $this->inDefs = \true; |
| 200 | return; |
| 201 | case 'svg': |
| 202 | if (\count($this->attributes)) { |
| 203 | $tag = new Group($this, $name); |
| 204 | } else { |
| 205 | $tag = $this; |
| 206 | $this->svgOffset($attributes); |
| 207 | } |
| 208 | break; |
| 209 | case 'path': |
| 210 | $tag = new Path($this, $name); |
| 211 | break; |
| 212 | case 'rect': |
| 213 | $tag = new Rect($this, $name); |
| 214 | break; |
| 215 | case 'circle': |
| 216 | $tag = new Circle($this, $name); |
| 217 | break; |
| 218 | case 'ellipse': |
| 219 | $tag = new Ellipse($this, $name); |
| 220 | break; |
| 221 | case 'image': |
| 222 | $tag = new Image($this, $name); |
| 223 | break; |
| 224 | case 'line': |
| 225 | $tag = new Line($this, $name); |
| 226 | break; |
| 227 | case 'polyline': |
| 228 | $tag = new Polyline($this, $name); |
| 229 | break; |
| 230 | case 'polygon': |
| 231 | $tag = new Polygon($this, $name); |
| 232 | break; |
| 233 | case 'lineargradient': |
| 234 | $tag = new LinearGradient($this, $name); |
| 235 | break; |
| 236 | case 'radialgradient': |
| 237 | $tag = new LinearGradient($this, $name); |
| 238 | break; |
| 239 | case 'stop': |
| 240 | $tag = new Stop($this, $name); |
| 241 | break; |
| 242 | case 'style': |
| 243 | $tag = new StyleTag($this, $name); |
| 244 | break; |
| 245 | case 'a': |
| 246 | $tag = new Anchor($this, $name); |
| 247 | break; |
| 248 | case 'g': |
| 249 | case 'symbol': |
| 250 | $tag = new Group($this, $name); |
| 251 | break; |
| 252 | case 'clippath': |
| 253 | $tag = new ClipPath($this, $name); |
| 254 | break; |
| 255 | case 'use': |
| 256 | $tag = new UseTag($this, $name); |
| 257 | break; |
| 258 | case 'text': |
| 259 | $tag = new Text($this, $name); |
| 260 | break; |
| 261 | case 'desc': |
| 262 | return; |
| 263 | } |
| 264 | if ($tag) { |
| 265 | if (isset($attributes["id"])) { |
| 266 | $this->defs[$attributes["id"]] = $tag; |
| 267 | } else { |
| 268 | /** @var AbstractTag $top */ |
| 269 | $top = \end($this->stack); |
| 270 | if ($top && $top != $tag) { |
| 271 | $top->children[] = $tag; |
| 272 | } |
| 273 | } |
| 274 | $this->stack[] = $tag; |
| 275 | $tag->handle($attributes); |
| 276 | } |
| 277 | } |
| 278 | function _charData($parser, $data) |
| 279 | { |
| 280 | $stack_top = \end($this->stack); |
| 281 | if ($stack_top instanceof Text || $stack_top instanceof StyleTag) { |
| 282 | $stack_top->appendText($data); |
| 283 | } |
| 284 | } |
| 285 | function _tagEnd($parser, $name) |
| 286 | { |
| 287 | /** @var AbstractTag $tag */ |
| 288 | $tag = null; |
| 289 | switch (\strtolower($name)) { |
| 290 | case 'defs': |
| 291 | $this->inDefs = \false; |
| 292 | return; |
| 293 | case 'svg': |
| 294 | case 'path': |
| 295 | case 'rect': |
| 296 | case 'circle': |
| 297 | case 'ellipse': |
| 298 | case 'image': |
| 299 | case 'line': |
| 300 | case 'polyline': |
| 301 | case 'polygon': |
| 302 | case 'radialgradient': |
| 303 | case 'lineargradient': |
| 304 | case 'stop': |
| 305 | case 'style': |
| 306 | case 'text': |
| 307 | case 'g': |
| 308 | case 'symbol': |
| 309 | case 'clippath': |
| 310 | case 'use': |
| 311 | case 'a': |
| 312 | $tag = \array_pop($this->stack); |
| 313 | break; |
| 314 | } |
| 315 | if (!$this->inDefs && $tag) { |
| 316 | $tag->handleEnd(); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 |