| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2015-2023 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 |
abstract class Shape { |
| 25 |
|
| 26 |
protected $depth = ShapeList::BELOW; |
| 27 |
protected $element = ''; |
| 28 |
protected $link = null; |
| 29 |
protected $link_target = '_blank'; |
| 30 |
protected $coords = null; |
| 31 |
protected $autohide = false; |
| 32 |
|
| 33 |
/** |
| 34 |
* attributes required to draw shape |
| 35 |
*/ |
| 36 |
protected $required = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* attributes that support coordinate transformation |
| 40 |
*/ |
| 41 |
protected $transform = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* map attributes to be transformed to the attribute they are relative to |
| 45 |
* - 'attr_relative' => 'attr_fixed' |
| 46 |
*/ |
| 47 |
protected $transform_from = []; |
| 48 |
|
| 49 |
/** |
| 50 |
* coordinate pairs for dependent transforns - don't include them in |
| 51 |
* $transform or they will be transformed twice |
| 52 |
*/ |
| 53 |
protected $transform_pairs = []; |
| 54 |
|
| 55 |
/** |
| 56 |
* colour gradients/patterns, and whether to allow gradients |
| 57 |
*/ |
| 58 |
private $colour_convert = [ |
| 59 |
'stroke' => false, |
| 60 |
'fill' => true |
| 61 |
]; |
| 62 |
|
| 63 |
/** |
| 64 |
* default attributes for all shapes |
| 65 |
*/ |
| 66 |
protected $attrs = [ |
| 67 |
'stroke' => '#000', |
| 68 |
'fill' => 'none' |
| 69 |
]; |
| 70 |
|
| 71 |
public function __construct(&$attrs, $depth) |
| 72 |
{ |
| 73 |
$this->attrs = array_merge($this->attrs, $attrs); |
| 74 |
$this->depth = $depth; |
| 75 |
|
| 76 |
$missing = []; |
| 77 |
foreach($this->required as $opt) |
| 78 |
if(!isset($this->attrs[$opt])) |
| 79 |
$missing[] = $opt; |
| 80 |
|
| 81 |
if(count($missing)) |
| 82 |
throw new \Exception($this->element . ' attribute(s) not found: ' . |
| 83 |
implode(', ', $missing)); |
| 84 |
|
| 85 |
if(isset($this->attrs['href'])) |
| 86 |
$this->link = $this->attrs['href']; |
| 87 |
if(isset($this->attrs['xlink:href'])) |
| 88 |
$this->link = $this->attrs['xlink:href']; |
| 89 |
if(isset($this->attrs['target'])) |
| 90 |
$this->link_target = $this->attrs['target']; |
| 91 |
if(isset($this->attrs['autohide'])) { |
| 92 |
$hide = 0; |
| 93 |
$show = isset($this->attrs['opacity']) ? $this->attrs['opacity'] : 1; |
| 94 |
if(isset($this->attrs['autohide_opacity'])) { |
| 95 |
if(is_array($this->attrs['autohide_opacity'])) |
| 96 |
list($hide, $show) = $this->attrs['autohide_opacity']; |
| 97 |
else |
| 98 |
$hide = $this->attrs['autohide_opacity']; |
| 99 |
} |
| 100 |
$this->autohide = [$hide, $show]; |
| 101 |
} |
| 102 |
|
| 103 |
$clean = ['href', 'xlink:href', 'target', 'autohide', 'autohide_opacity']; |
| 104 |
foreach($clean as $att) |
| 105 |
unset($this->attrs[$att]); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* returns true if the depth is correct |
| 110 |
*/ |
| 111 |
public function depth($d) |
| 112 |
{ |
| 113 |
return $this->depth == $d; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* draws the shape |
| 118 |
*/ |
| 119 |
public function draw(&$graph) |
| 120 |
{ |
| 121 |
$this->coords = new Coords($graph); |
| 122 |
|
| 123 |
$attributes = []; |
| 124 |
foreach($this->attrs as $attr => $value) { |
| 125 |
if($value !== null) { |
| 126 |
$val = $value; |
| 127 |
if(isset($this->transform[$attr])) { |
| 128 |
$measure_from = 0; |
| 129 |
if(isset($this->transform_from[$attr])) |
| 130 |
$measure_from = $this->attrs[$this->transform_from[$attr]]; |
| 131 |
|
| 132 |
$val = $this->coords->transform($value, $this->transform[$attr], 0, $measure_from); |
| 133 |
} elseif(isset($this->colour_convert[$attr])) { |
| 134 |
$val = new Colour($graph, $value, $this->colour_convert[$attr]); |
| 135 |
} |
| 136 |
$attr = str_replace('_', '-', $attr); |
| 137 |
$attributes[$attr] = $val; |
| 138 |
} |
| 139 |
} |
| 140 |
$this->transformCoordinates($attributes); |
| 141 |
|
| 142 |
if($this->autohide) { |
| 143 |
$graph->getJavascript()->autoHide($attributes, $this->autohide[0], |
| 144 |
$this->autohide[1]); |
| 145 |
} |
| 146 |
|
| 147 |
$element = $this->drawElement($graph, $attributes); |
| 148 |
if($this->link !== null) { |
| 149 |
$link = ['xlink:href' => $this->link]; |
| 150 |
if($this->link_target !== null) |
| 151 |
$link['target'] = $this->link_target; |
| 152 |
$element = $graph->element('a', $link, null, $element); |
| 153 |
} |
| 154 |
return $element; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Transform coordinate pairs |
| 159 |
*/ |
| 160 |
protected function transformCoordinates(&$attr) |
| 161 |
{ |
| 162 |
if(empty($this->transform_pairs)) |
| 163 |
return; |
| 164 |
foreach($this->transform_pairs as $pair) { |
| 165 |
list($x, $y) = $pair; |
| 166 |
$coords = $this->coords->transformCoords($attr[$x], $attr[$y]); |
| 167 |
list($attr[$x], $attr[$y]) = $coords; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Performs the conversion to SVG fragment |
| 173 |
*/ |
| 174 |
protected function drawElement(&$graph, &$attributes) |
| 175 |
{ |
| 176 |
return $graph->element($this->element, $attributes); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|