| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2018-2019 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
class Symbols { |
| 25 |
|
| 26 |
private $graph; |
| 27 |
private $symbols = []; |
| 28 |
private $use_count = []; |
| 29 |
private $empty_use; |
| 30 |
|
| 31 |
public function __construct(&$graph) |
| 32 |
{ |
| 33 |
$this->graph =& $graph; |
| 34 |
$this->empty_use = $graph->getOption('empty_use'); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Defines a symbol, returning its ID |
| 39 |
*/ |
| 40 |
public function define($content) |
| 41 |
{ |
| 42 |
// if this is a duplicate, return existing ID |
| 43 |
foreach($this->symbols as $id => $def) { |
| 44 |
if($def == $content) |
| 45 |
return $id; |
| 46 |
} |
| 47 |
|
| 48 |
$id = $this->graph->newID(); |
| 49 |
$this->symbols[$id] = $content; |
| 50 |
return $id; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Uses an existing symbol |
| 55 |
*/ |
| 56 |
public function useSymbol($id, $attr, $style = null) |
| 57 |
{ |
| 58 |
if(!isset($this->symbols[$id])) |
| 59 |
throw new \Exception('Symbol ' . $id . ' not defined'); |
| 60 |
|
| 61 |
if(isset($this->use_count[$id])) |
| 62 |
++$this->use_count[$id]; |
| 63 |
else |
| 64 |
$this->use_count[$id] = 1; |
| 65 |
|
| 66 |
$uattr = array_merge($attr, ['xlink:href' => '#' . $id]); |
| 67 |
return $this->graph->element('use', $uattr, $style, |
| 68 |
$this->empty_use ? '' : null); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns symbol use count |
| 73 |
*/ |
| 74 |
public function useCount($id) |
| 75 |
{ |
| 76 |
if(isset($this->use_count[$id])) |
| 77 |
return $this->use_count[$id]; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Outputs the list of used definitions |
| 83 |
*/ |
| 84 |
public function definitions() |
| 85 |
{ |
| 86 |
$defs = ''; |
| 87 |
foreach($this->use_count as $id => $count) { |
| 88 |
$defs .= $this->graph->element('symbol', null, null, |
| 89 |
$this->graph->element('g', ['id' => $id], null, |
| 90 |
$this->symbols[$id])); |
| 91 |
} |
| 92 |
|
| 93 |
return $defs; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|