| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2018-2022 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 |
/** |
| 25 |
* Class for defining marker graphics as symbols |
| 26 |
*/ |
| 27 |
class Markers { |
| 28 |
|
| 29 |
private $graph; |
| 30 |
|
| 31 |
public function __construct(&$graph) |
| 32 |
{ |
| 33 |
$this->graph =& $graph; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Creates contents of a marker |
| 38 |
*/ |
| 39 |
private function createContent($type, $size, $fill, $stroke_width, |
| 40 |
$stroke_colour, $opacity, $angle, $more = null) |
| 41 |
{ |
| 42 |
$marker = ['fill' => $fill]; |
| 43 |
if(!empty($stroke_colour) && $stroke_colour != 'none') { |
| 44 |
$marker['stroke'] = $stroke_colour; |
| 45 |
if(!empty($stroke_width)) |
| 46 |
$marker['stroke-width'] = $stroke_width; |
| 47 |
} |
| 48 |
if($opacity > 0.0 && $opacity < 1.0) |
| 49 |
$marker['opacity'] = $opacity; |
| 50 |
|
| 51 |
// check for custom or image markers |
| 52 |
$content = null; |
| 53 |
if($type[0] == '<') { |
| 54 |
$content = $type; |
| 55 |
$type = 'custom'; |
| 56 |
} elseif(strncmp($type, 'image:', 6) == 0) { |
| 57 |
$svg_text = new Text($this->graph); |
| 58 |
$image_path = $svg_text->substr($type, 6); |
| 59 |
$type = 'image'; |
| 60 |
} elseif(strncmp($type, 'figure:', 7) == 0) { |
| 61 |
$svg_text = new Text($this->graph); |
| 62 |
$figure = $svg_text->substr($type, 7); |
| 63 |
$type = 'figure'; |
| 64 |
} |
| 65 |
|
| 66 |
$a = $size; // will be repeated a lot, and 'a' is smaller |
| 67 |
$element = 'path'; |
| 68 |
switch($type) { |
| 69 |
case 'triangle' : |
| 70 |
$o = $a * tan(M_PI / 6); |
| 71 |
$h = $a / cos(M_PI / 6); |
| 72 |
$marker['d'] = new PathData('M', $a, $o, 'L', 0, -$h, 'L', -$a, $o, 'z'); |
| 73 |
break; |
| 74 |
case 'diamond' : |
| 75 |
$marker['d'] = new PathData('M', 0, -$a, 'L', $a, 0, 0, $a, -$a, 0, 'z'); |
| 76 |
break; |
| 77 |
case 'square' : |
| 78 |
$element = 'rect'; |
| 79 |
$marker['x'] = $marker['y'] = -$a; |
| 80 |
$marker['width'] = $marker['height'] = $a * 2; |
| 81 |
break; |
| 82 |
case 'x' : |
| 83 |
$xform = new Transform; |
| 84 |
$xform->rotate(45); |
| 85 |
$marker['transform'] = $xform; |
| 86 |
// no break - 'x' is a cross rotated by 45 degrees |
| 87 |
case 'cross' : |
| 88 |
$t = $a / 4; |
| 89 |
$marker['d'] = new PathData('M', -$a, -$t); |
| 90 |
$marker['d']->add('L', -$a, $t, -$t, $t, -$t, $a); |
| 91 |
$marker['d']->add($t, $a, $t, $t, $a, $t); |
| 92 |
$marker['d']->add($a, -$t, $t, -$t, $t, -$a); |
| 93 |
$marker['d']->add(-$t, -$a, -$t, -$t, 'z'); |
| 94 |
break; |
| 95 |
case 'octagon' : |
| 96 |
$t = $a * sin(M_PI / 8); |
| 97 |
$marker['d'] = new PathData('M', $t, -$a); |
| 98 |
$marker['d']->add('L', $a, -$t, $a, $t, $t, $a, -$t, $a); |
| 99 |
$marker['d']->add(-$a, $t, -$a, -$t, -$t, -$a, 'z'); |
| 100 |
break; |
| 101 |
case 'star' : |
| 102 |
$t = $a * 0.382; |
| 103 |
$x1 = $t * sin(M_PI * 0.8); |
| 104 |
$y1 = $t * cos(M_PI * 0.8); |
| 105 |
$x2 = $a * sin(M_PI * 0.6); |
| 106 |
$y2 = $a * cos(M_PI * 0.6); |
| 107 |
$x3 = $t * sin(M_PI * 0.4); |
| 108 |
$y3 = $t * cos(M_PI * 0.4); |
| 109 |
$x4 = $a * sin(M_PI * 0.2); |
| 110 |
$y4 = $a * cos(M_PI * 0.2); |
| 111 |
$marker['d'] = new PathData('M', 0, -$a); |
| 112 |
$marker['d']->add('L', $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, 0, $t); |
| 113 |
$marker['d']->add(-$x4, $y4, -$x3, $y3, -$x2, $y2, -$x1, $y1, 'z'); |
| 114 |
break; |
| 115 |
case 'threestar' : |
| 116 |
$t = $a / 4; |
| 117 |
$t1 = $t * cos(M_PI / 6); |
| 118 |
$t2 = $t * sin(M_PI / 6); |
| 119 |
$a1 = $a * cos(M_PI / 6); |
| 120 |
$a2 = $a * sin(M_PI / 6); |
| 121 |
$marker['d'] = new PathData('M', 0, -$a); |
| 122 |
$marker['d']->add('L', $t1, -$t2, $a1, $a2, 0, $t); |
| 123 |
$marker['d']->add(-$a1, $a2, -$t1, -$t2, 'z'); |
| 124 |
break; |
| 125 |
case 'fourstar' : |
| 126 |
$t = $a / 4; |
| 127 |
$marker['d'] = new PathData('M', 0, -$a, 'L', $t, -$t, $a, 0, $t, $t); |
| 128 |
$marker['d']->add(0, $a, -$t, $t, -$a, 0, -$t, -$t, 'z'); |
| 129 |
break; |
| 130 |
case 'eightstar' : |
| 131 |
$t = $a * sin(M_PI / 8); |
| 132 |
$marker['d'] = new PathData('M', 0, -$t); |
| 133 |
$marker['d']->add('L', $t, -$a, $t, -$t, $a, -$t, $t, 0); |
| 134 |
$marker['d']->add($a, $t, $t, $t, $t, $a, 0, $t); |
| 135 |
$marker['d']->add(-$t, $a, -$t, $t, -$a, $t, -$t, 0); |
| 136 |
$marker['d']->add(-$a, -$t, -$t, -$t, -$t, -$a, 'z'); |
| 137 |
break; |
| 138 |
case 'asterisk' : |
| 139 |
$t = $a / 3; |
| 140 |
$x1 = $a * sin(M_PI * 0.9); |
| 141 |
$y1 = $a * cos(M_PI * 0.9); |
| 142 |
$x2 = $t * sin(M_PI * 0.8); |
| 143 |
$y2 = $t * cos(M_PI * 0.8); |
| 144 |
$x3 = $a * sin(M_PI * 0.7); |
| 145 |
$y3 = $a * cos(M_PI * 0.7); |
| 146 |
$x4 = $a * sin(M_PI * 0.5); |
| 147 |
$y4 = $a * cos(M_PI * 0.5); |
| 148 |
$x5 = $t * sin(M_PI * 0.4); |
| 149 |
$y5 = $t * cos(M_PI * 0.4); |
| 150 |
$x6 = $a * sin(M_PI * 0.3); |
| 151 |
$y6 = $a * cos(M_PI * 0.3); |
| 152 |
$x7 = $a * sin(M_PI * 0.1); |
| 153 |
$y7 = $a * cos(M_PI * 0.1); |
| 154 |
$marker['d'] = new PathData('M', $x1, $y1); |
| 155 |
$marker['d']->add('L', $x2, $y2, $x3, $y3, $x4, $y4, $x5, $y5); |
| 156 |
$marker['d']->add($x6, $y6, $x7, $y7, 0, $t, -$x7, $y7); |
| 157 |
$marker['d']->add(-$x6, $y6, -$x5, $y5, -$x4, $y4, -$x3, $y3); |
| 158 |
$marker['d']->add(-$x2, $y2, -$x1, $y1, 'z'); |
| 159 |
break; |
| 160 |
case 'pentagon' : |
| 161 |
$x1 = $a * sin(M_PI * 0.4); |
| 162 |
$y1 = $a * cos(M_PI * 0.4); |
| 163 |
$x2 = $a * sin(M_PI * 0.2); |
| 164 |
$y2 = $a * cos(M_PI * 0.2); |
| 165 |
$marker['d'] = new PathData('M', 0, -$a); |
| 166 |
$marker['d']->add('L', $x1, -$y1, $x2, $y2, -$x2, $y2, -$x1, -$y1, 'z'); |
| 167 |
break; |
| 168 |
case 'hexagon' : |
| 169 |
$x = $a * sin(M_PI / 3); |
| 170 |
$y = $a * cos(M_PI / 3); |
| 171 |
$marker['d'] = new PathData('M', 0, -$a); |
| 172 |
$marker['d']->add('L', $x, -$y, $x, $y, 0, $a, -$x, $y, -$x, -$y, 'z'); |
| 173 |
break; |
| 174 |
case 'image' : |
| 175 |
$element = 'image'; |
| 176 |
$marker['xlink:href'] = $image_path; |
| 177 |
$marker['x'] = $marker['y'] = -$size; |
| 178 |
$marker['width'] = $size * 2; |
| 179 |
$marker['height'] = $size * 2; |
| 180 |
break; |
| 181 |
case 'custom' : |
| 182 |
$element = 'g'; |
| 183 |
break; |
| 184 |
case 'figure' : |
| 185 |
$element = 'g'; |
| 186 |
$figure_id = $this->graph->figures->getFigure($figure); |
| 187 |
if(empty($figure_id)) |
| 188 |
throw new \Exception('Figure [' . $figure . '] not defined'); |
| 189 |
return [$figure_id]; |
| 190 |
break; |
| 191 |
case 'circle' : |
| 192 |
default : |
| 193 |
$element = 'circle'; |
| 194 |
$marker['r'] = $size; |
| 195 |
} |
| 196 |
|
| 197 |
// angle happens here because the shape might already have a transform |
| 198 |
if($angle != 0) { |
| 199 |
$xform = new Transform; |
| 200 |
$xform->rotate($angle); |
| 201 |
if(isset($marker['transform'])) |
| 202 |
$marker['transform']->add($xform); |
| 203 |
else |
| 204 |
$marker['transform'] = $xform; |
| 205 |
} |
| 206 |
|
| 207 |
// extra attributes added at end |
| 208 |
if(is_array($more)) |
| 209 |
$marker = array_merge($marker, $more); |
| 210 |
|
| 211 |
return $this->graph->element($element, $marker, null, $content); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Creates a marker, returning its ID |
| 216 |
*/ |
| 217 |
public function create($type, $size, $fill, $stroke_width, $stroke_colour, |
| 218 |
$opacity, $angle, $more = null) |
| 219 |
{ |
| 220 |
$marker_content = $this->createContent($type, $size, $fill, $stroke_width, |
| 221 |
$stroke_colour, $opacity, $angle, $more); |
| 222 |
|
| 223 |
// if the return is an array, it contains the ID |
| 224 |
if(is_array($marker_content)) |
| 225 |
return $marker_content[0]; |
| 226 |
return $this->graph->defs->defineSymbol($marker_content); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Creates a SVG marker element, returning its ID |
| 231 |
*/ |
| 232 |
public function createSVGMarker($type, $size, $fill, $stroke_width, $stroke_colour, |
| 233 |
$opacity, $angle, $more = null) |
| 234 |
{ |
| 235 |
$marker_content = $this->createContent($type, $size, $fill, $stroke_width, |
| 236 |
$stroke_colour, $opacity, $angle, $more); |
| 237 |
|
| 238 |
$sz = new Number($size); |
| 239 |
$sz2 = new Number($size * 2); |
| 240 |
$marker = [ |
| 241 |
'viewBox' => "-{$sz} -{$sz} {$sz2} {$sz2}", |
| 242 |
'markerWidth' => $sz, |
| 243 |
'markerHeight' => $sz, |
| 244 |
'refX' => $sz, |
| 245 |
'orient' => 'auto', |
| 246 |
]; |
| 247 |
return $this->graph->defs->addElement('marker', $marker, $marker_content); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|