| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-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 |
* A class for the <defs> element |
| 26 |
*/ |
| 27 |
class Defs { |
| 28 |
|
| 29 |
private $graph; |
| 30 |
private $defs = []; |
| 31 |
private $gradients = null; |
| 32 |
private $patterns = null; |
| 33 |
private $symbols = null; |
| 34 |
private $filters = null; |
| 35 |
private $elements = []; |
| 36 |
|
| 37 |
public function __construct(&$graph) |
| 38 |
{ |
| 39 |
$this->graph =& $graph; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add a string to the defs block |
| 44 |
*/ |
| 45 |
public function add($def) |
| 46 |
{ |
| 47 |
$this->defs[] = $def; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Adds an element to the defs, returning its ID, or the ID |
| 52 |
* of an existing def with same content |
| 53 |
*/ |
| 54 |
public function addElement($element, $attrs, $content = '') |
| 55 |
{ |
| 56 |
$ehash = hash('md5', $element . ':' . serialize($attrs) . ':' . $content); |
| 57 |
if(isset($this->elements[$ehash])) |
| 58 |
return $this->elements[$ehash]; |
| 59 |
|
| 60 |
$attrs['id'] = $this->graph->newID(); |
| 61 |
$this->elements[$ehash] = $attrs['id']; |
| 62 |
$this->add($this->graph->element($element, $attrs, null, $content)); |
| 63 |
return $attrs['id']; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Return the defs block, or an empty string if none |
| 68 |
*/ |
| 69 |
public function get() |
| 70 |
{ |
| 71 |
// insert gradients, patterns, symbols |
| 72 |
if($this->gradients !== null) |
| 73 |
$this->gradients->makeGradients($this); |
| 74 |
if($this->patterns !== null) |
| 75 |
$this->patterns->makePatterns($this); |
| 76 |
if($this->symbols !== null) |
| 77 |
$this->defs[] = $this->symbols->definitions(); |
| 78 |
if($this->filters !== null) |
| 79 |
$this->filters->makeFilters($this); |
| 80 |
|
| 81 |
if(count($this->defs) == 0) |
| 82 |
return ''; |
| 83 |
|
| 84 |
return $this->graph->element('defs', null, null, implode('', $this->defs)); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Adds a gradient to the list, returning the element ID for use in url |
| 89 |
*/ |
| 90 |
public function addGradient($colours, $key = null, $radial = false) |
| 91 |
{ |
| 92 |
if($this->gradients === null) |
| 93 |
$this->gradients = new GradientList($this->graph); |
| 94 |
return $this->gradients->addGradient($colours, $key, $radial); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the colour at a point in the selected gradient |
| 99 |
*/ |
| 100 |
public function getGradientColour($key, $position) |
| 101 |
{ |
| 102 |
if($this->gradients === null) |
| 103 |
return 'none'; |
| 104 |
|
| 105 |
return $this->gradients->getColour($key, $position); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Adds a pattern, returning the element ID |
| 110 |
*/ |
| 111 |
public function addPattern($pattern) |
| 112 |
{ |
| 113 |
if($this->patterns === null) |
| 114 |
$this->patterns = new PatternList($this->graph); |
| 115 |
return $this->patterns->add($pattern); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Defines a symbol |
| 120 |
*/ |
| 121 |
public function defineSymbol($content) |
| 122 |
{ |
| 123 |
if($this->symbols === null) |
| 124 |
$this->symbols = new Symbols($this->graph); |
| 125 |
return $this->symbols->define($content); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Uses a symbol |
| 130 |
*/ |
| 131 |
public function useSymbol($id, $attr, $style = null) |
| 132 |
{ |
| 133 |
// this should not happen - Symbols class will throw anyway |
| 134 |
if($this->symbols === null) |
| 135 |
$this->symbols = new Symbols($this->graph); |
| 136 |
return $this->symbols->useSymbol($id, $attr, $style); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns the use count for a symbol |
| 141 |
*/ |
| 142 |
public function symbolUseCount($id) |
| 143 |
{ |
| 144 |
if($this->symbols === null) |
| 145 |
return 0; |
| 146 |
return $this->symbols->useCount($id); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds a filter |
| 151 |
*/ |
| 152 |
public function addFilter($type, $params = null) |
| 153 |
{ |
| 154 |
if($this->filters === null) |
| 155 |
$this->filters = new FilterList($this->graph); |
| 156 |
return $this->filters->add($type, $params); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns id of shadow, if enabled |
| 161 |
*/ |
| 162 |
public function getShadow() |
| 163 |
{ |
| 164 |
if(!$this->graph->getOption('show_shadow')) |
| 165 |
return null; |
| 166 |
|
| 167 |
$filter_id = $this->addFilter('shadow', [ |
| 168 |
'opacity' => $this->graph->getOption('shadow_opacity'), |
| 169 |
'offset_x' => $this->graph->getOption('shadow_offset_x'), |
| 170 |
'offset_y' => $this->graph->getOption('shadow_offset_y'), |
| 171 |
'blur' => $this->graph->getOption('shadow_blur'), |
| 172 |
]); |
| 173 |
return $filter_id; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|