forked from goat1000/SVGGraph
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefs.php
176 lines (155 loc) · 4.47 KB
/
Defs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Copyright (C) 2019-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <graham@goat1000.com>
*/
namespace Goat1000\SVGGraph;
/**
* A class for the <defs> element
*/
class Defs {
private $graph;
private $defs = [];
private $gradients = null;
private $patterns = null;
private $symbols = null;
private $filters = null;
private $elements = [];
public function __construct(&$graph)
{
$this->graph =& $graph;
}
/**
* Add a string to the defs block
*/
public function add($def)
{
$this->defs[] = $def;
}
/**
* Adds an element to the defs, returning its ID, or the ID
* of an existing def with same content
*/
public function addElement($element, $attrs, $content = '')
{
$ehash = hash('md5', $element . ':' . serialize($attrs) . ':' . $content);
if(isset($this->elements[$ehash]))
return $this->elements[$ehash];
$attrs['id'] = $this->graph->newID();
$this->elements[$ehash] = $attrs['id'];
$this->add($this->graph->element($element, $attrs, null, $content));
return $attrs['id'];
}
/**
* Return the defs block, or an empty string if none
*/
public function get()
{
// insert gradients, patterns, symbols
if($this->gradients !== null)
$this->gradients->makeGradients($this);
if($this->patterns !== null)
$this->patterns->makePatterns($this);
if($this->symbols !== null)
$this->defs[] = $this->symbols->definitions();
if($this->filters !== null)
$this->filters->makeFilters($this);
if(count($this->defs) == 0)
return '';
return $this->graph->element('defs', null, null, implode('', $this->defs));
}
/**
* Adds a gradient to the list, returning the element ID for use in url
*/
public function addGradient($colours, $key = null, $radial = false)
{
if($this->gradients === null)
$this->gradients = new GradientList($this->graph);
return $this->gradients->addGradient($colours, $key, $radial);
}
/**
* Returns the colour at a point in the selected gradient
*/
public function getGradientColour($key, $position)
{
if($this->gradients === null)
return 'none';
return $this->gradients->getColour($key, $position);
}
/**
* Adds a pattern, returning the element ID
*/
public function addPattern($pattern)
{
if($this->patterns === null)
$this->patterns = new PatternList($this->graph);
return $this->patterns->add($pattern);
}
/**
* Defines a symbol
*/
public function defineSymbol($content)
{
if($this->symbols === null)
$this->symbols = new Symbols($this->graph);
return $this->symbols->define($content);
}
/**
* Uses a symbol
*/
public function useSymbol($id, $attr, $style = null)
{
// this should not happen - Symbols class will throw anyway
if($this->symbols === null)
$this->symbols = new Symbols($this->graph);
return $this->symbols->useSymbol($id, $attr, $style);
}
/**
* Returns the use count for a symbol
*/
public function symbolUseCount($id)
{
if($this->symbols === null)
return 0;
return $this->symbols->useCount($id);
}
/**
* Adds a filter
*/
public function addFilter($type, $params = null)
{
if($this->filters === null)
$this->filters = new FilterList($this->graph);
return $this->filters->add($type, $params);
}
/**
* Returns id of shadow, if enabled
*/
public function getShadow()
{
if(!$this->graph->getOption('show_shadow'))
return null;
$filter_id = $this->addFilter('shadow', [
'opacity' => $this->graph->getOption('shadow_opacity'),
'offset_x' => $this->graph->getOption('shadow_offset_x'),
'offset_y' => $this->graph->getOption('shadow_offset_y'),
'blur' => $this->graph->getOption('shadow_blur'),
]);
return $filter_id;
}
}