184 lines
5.8 KiB
PHP
184 lines
5.8 KiB
PHP
|
<?php
|
||
|
/*
|
||
|
SVG to dojox.gfx Parser
|
||
|
Copyright (C) 2009 Aaron Lindsay <aclindsay@aclindsay.com>
|
||
|
|
||
|
This program is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU General Public License
|
||
|
as published by the Free Software Foundation; either version 2
|
||
|
of the License, or (at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program; if not, write to the Free Software
|
||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
class Element {
|
||
|
private $id;
|
||
|
private $styles;
|
||
|
private $transforms;
|
||
|
private $transform;
|
||
|
|
||
|
public function __construct() {
|
||
|
$this->id = "";
|
||
|
$this->styles = array();
|
||
|
$this->transforms = array();
|
||
|
$this->transform = new Transform();
|
||
|
}
|
||
|
|
||
|
public function setId($newId) {
|
||
|
$this->id = $newId;
|
||
|
}
|
||
|
public function getId() {
|
||
|
return $this->id;
|
||
|
}
|
||
|
|
||
|
public function setStyles($newStyles) {
|
||
|
$this->styles = array();
|
||
|
$smallerStyles = split(";", $newStyles);
|
||
|
foreach($smallerStyles as $small) {
|
||
|
$parts = split(":", $small);
|
||
|
$this->styles[$parts[0]] = $parts[1];
|
||
|
}
|
||
|
}
|
||
|
public function setStyle($key, $style) {
|
||
|
$this->styles[$key] = $style;
|
||
|
}
|
||
|
public function getStyles() {
|
||
|
return $this->styles;
|
||
|
}
|
||
|
public function getStyle($key) {
|
||
|
if ($this->styles && array_key_exists($key, $this->styles))
|
||
|
return $this->styles[$key];
|
||
|
else
|
||
|
return null;
|
||
|
}
|
||
|
public function setStroke($stroke) {
|
||
|
$this->styles["stroke"] = $stroke;
|
||
|
}
|
||
|
public function setStrokeWidth($width) {
|
||
|
$this->styles["stroke-width"] = $width;
|
||
|
}
|
||
|
public function setStrokeLinecap($linecap) {
|
||
|
$this->styles["stroke-linecap"] = $linecap;
|
||
|
}
|
||
|
|
||
|
public function setFill($fill) {
|
||
|
$this->styles["fill"] = $fill;
|
||
|
}
|
||
|
|
||
|
//$transform is a string of transformations
|
||
|
public function setTransforms($transform) {
|
||
|
$this->transforms = array();
|
||
|
$this->addTransforms($transform);
|
||
|
$this->combineTransforms();
|
||
|
}
|
||
|
|
||
|
//$transform is a string of transformations
|
||
|
public function addTransforms($transform) {
|
||
|
$parts = split("\\)", $transform);
|
||
|
foreach ($parts as $part) {
|
||
|
//because ')' is the last character, we'll get an empty string here if we don't watch out
|
||
|
if ($part == "")
|
||
|
break;
|
||
|
|
||
|
$transform = split("\\(", trim($part));
|
||
|
switch (trim($transform[0])) {
|
||
|
case "matrix":
|
||
|
//get all the matrix elements, create a new transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
$transformObject = new Transform();
|
||
|
$transformObject->setFullMatrix($elements[0], $elements[1], $elements[2], $elements[3], $elements[4], $elements[5]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
break;
|
||
|
case "translate":
|
||
|
//get all the matrix elements, create a new translate transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
$transformObject = new Transform();
|
||
|
$transformObject->setTranslate($elements[0], $elements[1]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
break;
|
||
|
case "scale":
|
||
|
//get all the matrix elements, create a new scale transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
$transformObject = new Transform();
|
||
|
if (count($elements) > 1)
|
||
|
$transformObject->setScale($elements[0], $elements[1]);
|
||
|
else
|
||
|
$transformObject->setScale($elements[0]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
break;
|
||
|
case "rotate":
|
||
|
//get all the matrix elements, create a new rotate transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
//if there are 3 arguments, they are angle, and (x,y) coordinates of the point to rotate about
|
||
|
//to handle this, we translate, rotate, and translate back
|
||
|
if (count($elements) >= 3) {
|
||
|
$transformObject1 = new Transform();
|
||
|
$transformObject1->setTranslate(-$elements[1], -$elements[2]);
|
||
|
$this->transforms[] = $transformObject1;
|
||
|
|
||
|
$transformObject2 = new Transform();
|
||
|
$transformObject2->setRotate($elements[0]);
|
||
|
$this->transforms[] = $transformObject2;
|
||
|
|
||
|
$transformObject3 = new Transform();
|
||
|
$transformObject3->setTranslate($elements[1], $elements[2]);
|
||
|
$this->transforms[] = $transformObject3;
|
||
|
} else {
|
||
|
$transformObject = new Transform();
|
||
|
$transformObject->setRotate($elements[0]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
}
|
||
|
break;
|
||
|
case "skewX":
|
||
|
//get all the matrix elements, create a new skew transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
$transformObject = new Transform();
|
||
|
$transformObject->setSkewX($elements[0]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
break;
|
||
|
case "skewY":
|
||
|
//get all the matrix elements, create a new skew transform and set them
|
||
|
$elements = split("[[:blank:],]+", trim($transform[1]));
|
||
|
$transformObject = new Transform();
|
||
|
$transformObject->setSkewY($elements[0]);
|
||
|
$this->transforms[] = $transformObject;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
//combine all these into one transform
|
||
|
$this->combineTransforms();
|
||
|
}
|
||
|
|
||
|
|
||
|
//combine all transforms into one transform and store in $this->transform
|
||
|
private function combineTransforms() {
|
||
|
$one = new Transform();
|
||
|
foreach ($this->transforms as $transform)
|
||
|
$one->multiplyBy($transform);
|
||
|
$this->transform = $one;
|
||
|
}
|
||
|
|
||
|
public function setTransform($transform) {
|
||
|
$this->transform = $transform;
|
||
|
$this->transforms = array();
|
||
|
$this->transforms[] = $transform;
|
||
|
}
|
||
|
|
||
|
public function getTransform() {
|
||
|
return $this->transform;
|
||
|
}
|
||
|
|
||
|
//an ARRAY of Transform objects is returned
|
||
|
public function getTransforms() {
|
||
|
return $this->transforms;
|
||
|
}
|
||
|
}
|
||
|
?>
|