| 12 | | protected |
| 13 | | $attributes = array(), |
| 14 | | $definitions = array(), |
| 15 | | $scripts = array(), |
| 16 | | $embed_styles = array(), |
| 17 | | $external_styles = array(), |
| 18 | | $used_definitions = array(), |
| 19 | | $elements = array(), |
| 20 | | $dom_ids = array(), |
| 21 | | $description = NULL, |
| 22 | | $dom_document = NULL, |
| 23 | | $maxdepth = 0, |
| 24 | | $title = 'Untitled SVG Document', |
| 25 | | $embedded = false, |
| 26 | | $strict_mode = false; |
| | 12 | /** |
| | 13 | * Document attributes container |
| | 14 | * @var array |
| | 15 | */ |
| | 16 | protected $attributes = array(); |
| | 17 | |
| | 18 | /** |
| | 19 | * Definitions container |
| | 20 | * @var array |
| | 21 | */ |
| | 22 | protected $definitions = array(); |
| | 23 | |
| | 24 | /** |
| | 25 | * Scripts container |
| | 26 | * @var array |
| | 27 | */ |
| | 28 | protected $scripts = array(); |
| | 29 | |
| | 30 | /** |
| | 31 | * Embeded styles container |
| | 32 | * @var array |
| | 33 | */ |
| | 34 | protected $embed_styles = array(); |
| | 35 | |
| | 36 | /** |
| | 37 | * External styles references container |
| | 38 | * @var array |
| | 39 | */ |
| | 40 | protected $external_styles = array(); |
| | 41 | |
| | 42 | /** |
| | 43 | * Used definitions references container |
| | 44 | * @var array |
| | 45 | */ |
| | 46 | protected $used_definitions = array(); |
| | 47 | |
| | 48 | /** |
| | 49 | * Document elements container |
| | 50 | * @var array |
| | 51 | */ |
| | 52 | protected $elements = array(); |
| | 53 | |
| | 54 | /** |
| | 55 | * DOM document unique id attributes container |
| | 56 | * @var array |
| | 57 | */ |
| | 58 | protected $dom_ids = array(); |
| | 59 | |
| | 60 | /** |
| | 61 | * Document description |
| | 62 | * @var string |
| | 63 | */ |
| | 64 | protected $description = NULL; |
| | 65 | |
| | 66 | /** |
| | 67 | * DOMDocument instance |
| | 68 | * @var DOMDocument |
| | 69 | */ |
| | 70 | protected $dom_document = NULL; |
| | 71 | |
| | 72 | /** |
| | 73 | * Max depth |
| | 74 | * @var int |
| | 75 | */ |
| | 76 | protected $maxdepth = 0; |
| | 77 | |
| | 78 | /** |
| | 79 | * Document title |
| | 80 | * @var string |
| | 81 | */ |
| | 82 | protected $title = 'Untitled SVG Document'; |
| | 83 | |
| | 84 | /** |
| | 85 | * Embeded SVG flag |
| | 86 | * @var boolean |
| | 87 | */ |
| | 88 | protected $embedded = false; |
| | 89 | |
| | 90 | /** |
| | 91 | * Strict mode flag |
| | 92 | * @var boolean |
| | 93 | */ |
| | 94 | protected $strict_mode = false; |
| 48 | | * @param mixed $element SVG element to add |
| 49 | | * @param mixed $depth Depth (default: auto when set to NULL) |
| 50 | | */ |
| 51 | | public function addElement($element, $depth=null) |
| 52 | | { |
| 53 | | if (is_null($element->getDepth())) |
| 54 | | { |
| 55 | | if (!is_int($depth) || isset($this->elements[$depth])) |
| 56 | | { |
| 57 | | $depth = ++$this->maxdepth; |
| 58 | | } |
| 59 | | } |
| 60 | | else |
| 61 | | { |
| 62 | | $depth = $element->getDepth(); |
| 63 | | } |
| 64 | | $this->elements[$depth] = $element; |
| 65 | | |
| | 116 | * @param mixed $element SVG element to add |
| | 117 | * @param mixed $depth Depth (default: auto when set to NULL) |
| | 118 | * @param boolean $replace_at_depth Replace element at depth if exist ? |
| | 119 | * @throws csException |
| | 120 | */ |
| | 121 | public function addElement($element, $depth = null, $replace_at_depth = false) |
| | 122 | { |
| | 134 | |
| | 135 | if (is_null($element->getDepth())) |
| | 136 | { |
| | 137 | if (!$replace_at_depth && array_key_exists($depth, $this->elements)) |
| | 138 | { |
| | 139 | throw new csException( |
| | 140 | sprintf('Unable to add element at depth "%d", a "%s" element already'. |
| | 141 | ' exist at this index', |
| | 142 | $depth, $this->elements[$depth]->getElementName(false))); |
| | 143 | } |
| | 144 | elseif (!is_int($depth)) |
| | 145 | { |
| | 146 | $depth = ++$this->maxdepth; |
| | 147 | } |
| | 148 | } |
| | 149 | else |
| | 150 | { |
| | 151 | $depth = $element->getDepth(); |
| | 152 | } |
| | 153 | $this->elements[$depth] = $element; |
| | 154 | } |
| | 155 | |
| | 156 | /** |
| | 157 | * Create an svg element and returns its instance |
| | 158 | * |
| | 159 | * @param string $name |
| | 160 | * @return csBaseElement |
| | 161 | */ |
| | 162 | public function createElement($name) |
| | 163 | { |
| | 164 | $class_name = sprintf('cs%s', ucfirst($name)); |
| | 165 | if (!class_exists($class_name)) |
| | 166 | { |
| | 167 | throw new csException(sprintf('Element class "%s" does not exist', |
| | 168 | $class_name)); |
| | 169 | } |
| | 170 | return new $class_name; |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Drop an element from document by its DOM id |
| | 175 | * |
| | 176 | * @param string $id |
| | 177 | * @throws csException |
| | 178 | */ |
| | 179 | public function dropElementById($id) |
| | 180 | { |
| | 181 | foreach ($this->getElements() as $index => $element) |
| | 182 | { |
| | 183 | if ($element->getId() == $id) |
| | 184 | { |
| | 185 | foreach ($this->dom_ids as $dom_index => $dom_id) |
| | 186 | { |
| | 187 | if ($dom_id == $id) |
| | 188 | { |
| | 189 | unset($this->dom_ids[$dom_index]); |
| | 190 | } |
| | 191 | } |
| | 192 | unset($this->elements[$index]); |
| | 193 | return; |
| | 194 | } |
| | 195 | } |
| | 196 | } |
| | 197 | |
| | 198 | /** |
| | 199 | * Retrieve an element instance by its DOM id |
| | 200 | * |
| | 201 | * @param string $id |
| | 202 | * @return csBaseElement |
| | 203 | * @throw csException |
| | 204 | */ |
| | 205 | public function getElementById($id) |
| | 206 | { |
| | 207 | foreach ($this->elements as $element) |
| | 208 | { |
| | 209 | if ($element->getId() == $id) |
| | 210 | { |
| | 211 | return $element; |
| | 212 | } |
| | 213 | } |
| | 214 | throw new csException(sprintf('No element with id "%s"', $id)); |
| 271 | | * Swap respective depths of two elements |
| 272 | | * |
| 273 | | * @param sfSvgElement $element1 |
| 274 | | * @param sfSvgElement $element2 |
| 275 | | */ |
| 276 | | public function swapDepths($element1, $element2) |
| 277 | | { |
| 278 | | $tmpdepth = $this->maxdepth + 1; |
| 279 | | $e1depth = $element1->getDepth(); |
| 280 | | $e2depth = $element2->getDepth(); |
| 281 | | $element1->setDepth($tmpdepth); |
| 282 | | $element2->setDepth($e1depth); |
| 283 | | $element1->setDepth($e2depth); |
| | 414 | * Swap respective depths of two elements. Both elements are retrieved by |
| | 415 | * their DOM id. |
| | 416 | * |
| | 417 | * @param string $id1 |
| | 418 | * @param string $id2 |
| | 419 | * @throws csException |
| | 420 | */ |
| | 421 | public function swapDepths($id1, $id2) |
| | 422 | { |
| | 423 | if ($id1 == $id2) |
| | 424 | { |
| | 425 | return; |
| | 426 | } |
| | 427 | $e1 = $this->getElementById($id1); |
| | 428 | $e2 = $this->getElementById($id2); |
| | 429 | $e1depth = $e1->getDepth(); |
| | 430 | $e2depth = $e2->getDepth(); |
| | 431 | $e2->setDepth($e1depth); |
| | 432 | $e1->setDepth($e2depth); |
| | 433 | $this->dropElementById($id1); |
| | 434 | $this->dropElementById($id2); |
| | 435 | $this->addElement($e1, $e1depth, true); |
| | 436 | $this->addElement($e2, $e2depth, true); |