|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\svg\shape; |
|
3
|
|
|
|
|
4
|
|
|
use nstdio\svg\animation\BaseAnimation; |
|
5
|
|
|
use nstdio\svg\ElementInterface; |
|
6
|
|
|
use nstdio\svg\filter\BaseFilter; |
|
7
|
|
|
use nstdio\svg\filter\DiffuseLighting; |
|
8
|
|
|
use nstdio\svg\filter\Filter; |
|
9
|
|
|
use nstdio\svg\filter\GaussianBlur; |
|
10
|
|
|
use nstdio\svg\gradient\Gradient; |
|
11
|
|
|
use nstdio\svg\gradient\UniformGradient; |
|
12
|
|
|
use nstdio\svg\SVGElement; |
|
13
|
|
|
use nstdio\svg\traits\StyleTrait; |
|
14
|
|
|
use nstdio\svg\traits\TransformTrait; |
|
15
|
|
|
use nstdio\svg\util\Transform; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Shape |
|
19
|
|
|
* |
|
20
|
|
|
* @property float height The height of shape. |
|
21
|
|
|
* @property float width The width of shape. |
|
22
|
|
|
* @property string stroke Stroke color. |
|
23
|
|
|
* @property float strokeWidth Width of stroke. |
|
24
|
|
|
* @property string strokeLocation |
|
25
|
|
|
* @property string style |
|
26
|
|
|
* @property string fill |
|
27
|
|
|
* @property string fillUrl The id part of fill attribute. |
|
28
|
|
|
* @property float fillOpacity specifies the opacity of the painting operation used to paint the interior the |
|
29
|
|
|
* current |
|
30
|
|
|
* @property string filter |
|
31
|
|
|
* @property string filterUrl The id part of filter attribute. |
|
32
|
|
|
* @property string transform |
|
33
|
|
|
* @package nstdio\svg\shape |
|
34
|
|
|
* @author Edgar Asatryan <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract class Shape extends SVGElement implements ShapeInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use StyleTrait, TransformTrait; |
|
39
|
|
|
|
|
40
|
|
|
abstract protected function getCenterX(); |
|
41
|
|
|
|
|
42
|
|
|
abstract protected function getCenterY(); |
|
43
|
|
|
|
|
44
|
|
|
abstract public function getBoundingBox(); |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(ElementInterface $parent) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct($parent); |
|
49
|
|
|
|
|
50
|
|
|
$this->transformImpl = Transform::newInstance($this->getTransformAttribute()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getTransformAttribute() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->transform; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function setTransformAttribute($transformList) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->transform = $transformList; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function applyGradient(Gradient $gradient) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->fillUrl = $gradient->id; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function animate(BaseAnimation $animation) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->element->appendChild($animation->getElement()); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function filterGaussianBlur($stdDeviation, $in = null, $filterId = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$filter = new Filter($this->getRoot(), $filterId); |
|
83
|
|
|
$blur = new GaussianBlur($filter); |
|
84
|
|
|
$blur->stdDeviation = $stdDeviation; |
|
85
|
|
|
$blur->in = $in; |
|
86
|
|
|
|
|
87
|
|
|
$this->applyFilter($filter); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function diffusePointLight(array $pointLightConfig = [], array $diffuseLightingConfig = [], $filterId = null) |
|
93
|
|
|
{ |
|
94
|
|
|
$pointConfig = [ |
|
95
|
|
|
'x' => $this->getCenterX(), |
|
96
|
|
|
'y' => $this->getCenterY(), |
|
97
|
|
|
'z' => 25, |
|
98
|
|
|
]; |
|
99
|
|
|
foreach ($pointConfig as $key => $value) { |
|
100
|
|
|
if (isset($pointLightConfig[$key])) { |
|
101
|
|
|
$pointConfig[$key] = $value + $pointLightConfig[$key]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
$filter = DiffuseLighting::diffusePointLight($this->getRoot(), $pointConfig, $diffuseLightingConfig, $filterId); |
|
|
|
|
|
|
105
|
|
|
$this->applyFilter($filter); |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function applyFilter(BaseFilter $filter) |
|
111
|
|
|
{ |
|
112
|
|
|
$svg = $this->getSVG(); |
|
113
|
|
|
if ($this->filter === null) { |
|
114
|
|
|
$this->filterUrl = $filter->id; |
|
115
|
|
|
$defs = $svg->getFirstChild(); |
|
116
|
|
|
$filter->selfRemove(); |
|
|
|
|
|
|
117
|
|
|
$defs->append($filter); |
|
118
|
|
|
} else { |
|
119
|
|
|
$currentFilter = $svg->getFirstChild()->getChildById($this->filterUrl); |
|
120
|
|
|
if ($currentFilter !== null) { |
|
121
|
|
|
foreach ($filter->getChildren() as $child) { |
|
122
|
|
|
$currentFilter->append($child); |
|
123
|
|
|
} |
|
124
|
|
|
$filter->selfRemove(); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function linearGradientFromTop(array $colors, $id = null) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->applyGradient(UniformGradient::verticalFromTop($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function linearGradientFromBottom(array $colors, $id = null) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->applyGradient(UniformGradient::verticalFromBottom($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function linearGradientFromLeft(array $colors, $id = null) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->applyGradient(UniformGradient::horizontalFromLeft($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function linearGradientFromRight(array $colors, $id = null) |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->applyGradient(UniformGradient::horizontalFromRight($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function linearGradientFromTopLeft(array $colors, $id = null) |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->applyGradient(UniformGradient::diagonalFromTopLeft($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function linearGradientFromTopRight(array $colors, $id = null) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->applyGradient(UniformGradient::diagonalFromTopRight($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function linearGradientFromBottomLeft(array $colors, $id = null) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->applyGradient(UniformGradient::diagonalFromBottomLeft($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function linearGradientFromBottomRight(array $colors, $id = null) |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->applyGradient(UniformGradient::diagonalFromBottomRight($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function radialGradientFromTopLeft(array $colors, $id = null) |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->applyGradient(UniformGradient::radialTopLeft($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function radialGradientFromTopRight(array $colors, $id = null) |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->applyGradient(UniformGradient::radialTopRight($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function radialGradientFromBottomLeft(array $colors, $id = null) |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->applyGradient(UniformGradient::radialBottomLeft($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function radialGradientFromBottomRight(array $colors, $id = null) |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->applyGradient(UniformGradient::radialBottomRight($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function radialGradientFromTopCenter(array $colors, $id = null) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->applyGradient(UniformGradient::radialTopCenter($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function radialGradientFromLeftCenter(array $colors, $id = null) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->applyGradient(UniformGradient::radialLeftCenter($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function radialGradientFromBottomCenter(array $colors, $id = null) |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->applyGradient(UniformGradient::radialBottomCenter($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function radialGradientFromRightCenter(array $colors, $id = null) |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->applyGradient(UniformGradient::radialRightCenter($this->getRoot(), $colors, $id)); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: