1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\container; |
3
|
|
|
|
4
|
|
|
use nstdio\svg\attributes\Transformable; |
5
|
|
|
use nstdio\svg\ElementInterface; |
6
|
|
|
use nstdio\svg\shape\Line; |
7
|
|
|
use nstdio\svg\shape\Shape; |
8
|
|
|
use nstdio\svg\traits\TransformTrait; |
9
|
|
|
use nstdio\svg\util\transform\Transform; |
10
|
|
|
use nstdio\svg\util\transform\TransformInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Pattern |
14
|
|
|
* |
15
|
|
|
* A pattern is used to fill or stroke an object using a pre-defined graphic object which can be replicated ("tiled") |
16
|
|
|
* at fixed intervals in x and y to cover the areas to be painted. Patterns are defined using a 'pattern' element and |
17
|
|
|
* then referenced by properties 'fill' and 'stroke' on a given graphics element to indicate that the given element |
18
|
|
|
* shall be filled or stroked with the referenced pattern. |
19
|
|
|
* |
20
|
|
|
* @link https://www.w3.org/TR/SVG/pservers.html#Patterns |
21
|
|
|
* @property string $patternUnits "userSpaceOnUse | objectBoundingBox" Defines the coordinate system for |
22
|
|
|
* attributes 'x', |
23
|
|
|
* 'y', 'width' and 'height'. If patternUnits="userSpaceOnUse", 'x', 'y', 'width' and 'height' represent |
24
|
|
|
* values in the coordinate system that results from taking the current user coordinate system in place at |
25
|
|
|
* the time when the 'pattern' element is referenced (i.e., the user coordinate system for the element |
26
|
|
|
* referencing the 'pattern' element via a 'fill' or 'stroke' property) and then applying the transform |
27
|
|
|
* specified by attribute 'patternTransform'. If patternUnits="objectBoundingBox", the user coordinate system |
28
|
|
|
* for attributes 'x', 'y', 'width' and 'height' is established using the bounding box of the element to |
29
|
|
|
* which the pattern is applied (see Object bounding box units) and then applying the transform specified by |
30
|
|
|
* attribute 'patternTransform'. If attribute 'patternUnits' is not specified, then the effect is as if a |
31
|
|
|
* value of 'objectBoundingBox' were specified. |
32
|
|
|
* @property string $patternContentUnits = "userSpaceOnUse | objectBoundingBox" Defines the coordinate system for the |
33
|
|
|
* contents of the ‘pattern’. Note that this attribute has no effect if attribute ‘viewBox’ is specified. If |
34
|
|
|
* patternContentUnits="userSpaceOnUse", the user coordinate system for the contents of the ‘pattern’ element |
35
|
|
|
* is the coordinate system that results from taking the current user coordinate system in place at the time |
36
|
|
|
* when the ‘pattern’ element is referenced (i.e., the user coordinate system for the element referencing the |
37
|
|
|
* ‘pattern’ element via a ‘fill’ or ‘stroke’ property) and then applying the transform specified by |
38
|
|
|
* attribute ‘patternTransform’. If patternContentUnits="objectBoundingBox", the user coordinate system for |
39
|
|
|
* the contents of the ‘pattern’ element is established using the bounding box of the element to which the |
40
|
|
|
* pattern is applied (see Object bounding box units) and then applying the transform specified by attribute |
41
|
|
|
* ‘patternTransform’. If attribute ‘patternContentUnits’ is not specified, then the effect is as if a value |
42
|
|
|
* of 'userSpaceOnUse' were specified. |
43
|
|
|
* @property string $patternTransform = "<transform-list>" Contains the definition of an optional additional |
44
|
|
|
* transformation from the pattern coordinate system onto the target coordinate system (i.e., |
45
|
|
|
* 'userSpaceOnUse' or 'objectBoundingBox'). This allows for things such as skewing the pattern tiles. This |
46
|
|
|
* additional transformation matrix is post-multiplied to (i.e., inserted to the right of) any previously |
47
|
|
|
* defined transformations, including the implicit transformation necessary to convert from object bounding |
48
|
|
|
* box units to user space. If attribute ‘patternTransform’ is not specified, then the effect is as if an |
49
|
|
|
* identity transform were specified. |
50
|
|
|
* @property float $x = "<coordinate>" ‘x’, ‘y’, ‘width’ and ‘height’ indicate how the pattern tiles |
51
|
|
|
* are placed and spaced. These attributes represent coordinates and values in the coordinate space specified |
52
|
|
|
* by the combination of attributes ‘patternUnits’ and ‘patternTransform’. If the attribute is not specified, |
53
|
|
|
* the effect is as if a value of zero were specified. |
54
|
|
|
* @property float $y = "<coordinate>" See ‘x’. If the attribute is not specified, the effect is as |
55
|
|
|
* if a value of zero were specified. |
56
|
|
|
* @property float $width = "<length>" See ‘x’. A negative value is an error (see Error processing). A |
57
|
|
|
* value of zero disables rendering of the element (i.e., no paint is applied). If the attribute is not |
58
|
|
|
* specified, the effect is as if a value of zero were specified. |
59
|
|
|
* @property float $height = "<length>" See ‘x’. A negative value is an error (see Error processing). A |
60
|
|
|
* value of zero disables rendering of the element (i.e., no paint is applied). If the attribute is not |
61
|
|
|
* specified, the effect is as if a value of zero were specified. |
62
|
|
|
* @property float $xlinkHref = "<iri>" An IRI reference to a different ‘pattern’ element within the |
63
|
|
|
* current SVG document fragment. Any attributes which are defined on the referenced element which are not |
64
|
|
|
* defined on this element are inherited by this element. If this element has no children, and the referenced |
65
|
|
|
* element does (possibly due to its own ‘xlink:href’ attribute), then this element inherits the children |
66
|
|
|
* from the referenced element. Inheritance can be indirect to an arbitrary level; thus, if the referenced |
67
|
|
|
* element inherits attributes or children due to its own ‘xlink:href’ attribute, then the current element |
68
|
|
|
* can inherit those attributes or children. |
69
|
|
|
* @package nstdio\svg\container |
70
|
|
|
* @author Edgar Asatryan <[email protected]> |
71
|
|
|
*/ |
72
|
|
|
class Pattern extends Container implements TransformInterface, Transformable |
73
|
|
|
{ |
74
|
|
|
use TransformTrait; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pattern constructor. |
78
|
|
|
* |
79
|
|
|
* @param ElementInterface $parent |
80
|
|
|
* @param null|string $id |
81
|
|
|
*/ |
82
|
11 |
|
public function __construct(ElementInterface $parent, $id = null) |
83
|
|
|
{ |
84
|
11 |
|
if ($parent instanceof SVG) { |
85
|
10 |
|
$defs = $parent->getFirstChild(); |
86
|
10 |
|
$parent = $defs; |
87
|
10 |
|
} |
88
|
11 |
|
parent::__construct($parent); |
|
|
|
|
89
|
|
|
|
90
|
11 |
|
$this->transformImpl = Transform::newInstance($this->getTransformAttribute()); |
91
|
11 |
|
$this->id = $id; |
92
|
11 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
11 |
|
public function getTransformAttribute() |
98
|
|
|
{ |
99
|
11 |
|
return $this->patternTransform; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
11 |
|
public function getName() |
106
|
|
|
{ |
107
|
11 |
|
return 'pattern'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ContainerInterface $container |
112
|
|
|
* @param Shape $shape |
113
|
|
|
* @param array $patternConfig |
114
|
|
|
* @param null $id |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
1 |
|
public static function withShape(ContainerInterface $container, Shape $shape, array $patternConfig = [], $id = null) |
119
|
|
|
{ |
120
|
1 |
|
$patternConfig = array_merge(self::getDefaultConfig(), $patternConfig); |
121
|
|
|
|
122
|
1 |
|
$shapeBox = $shape->getBoundingBox(); |
123
|
1 |
|
$patternConfig['width'] = $shapeBox['width']; |
124
|
1 |
|
$patternConfig['height'] = $shapeBox['height']; |
125
|
|
|
|
126
|
1 |
|
$pattern = (new self($container, $id))->apply($patternConfig); |
127
|
1 |
|
$shape->getRoot()->removeChild($shape); |
|
|
|
|
128
|
1 |
|
$pattern->append($shape); |
129
|
|
|
|
130
|
1 |
|
return $pattern; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
6 |
|
protected static function getDefaultConfig() |
137
|
|
|
{ |
138
|
6 |
|
return ['x' => 0, 'y' => 0, 'height' => 4, 'width' => 4, 'patternUnits' => 'userSpaceOnUse']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ContainerInterface $container |
143
|
|
|
* @param array $patternConfig |
144
|
|
|
* @param array $lineConfig |
145
|
|
|
* @param null|string $id |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
1 |
|
public static function verticalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
150
|
|
|
{ |
151
|
1 |
|
return self::hatch($container, $patternConfig, $lineConfig, $id); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param ContainerInterface $container |
156
|
|
|
* @param array $patternConfig |
157
|
|
|
* @param array $lineConfig |
158
|
|
|
* @param null|string $id |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
5 |
|
protected static function hatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
163
|
|
|
{ |
164
|
5 |
|
$patternConfig = array_merge(self::getDefaultConfig(), $patternConfig); |
165
|
5 |
|
$lineDefaultConfig = ['stroke' => 'black', 'stroke-width' => 1, 'fill' => 'none']; |
166
|
5 |
|
$lineConfig = array_merge($lineDefaultConfig, $lineConfig); |
167
|
|
|
|
168
|
5 |
|
$pattern = (new self($container, $id))->apply($patternConfig); |
169
|
|
|
|
170
|
5 |
|
Line::create($pattern, 0, 0, 0, $pattern->height)->apply($lineConfig); |
|
|
|
|
171
|
|
|
|
172
|
5 |
|
return $pattern; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param ContainerInterface $container |
177
|
|
|
* @param array $patternConfig |
178
|
|
|
* @param array $lineConfig |
179
|
|
|
* @param null|string $id |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
1 |
|
public static function horizontalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
184
|
|
|
{ |
185
|
1 |
|
return self::hatch($container, $patternConfig, $lineConfig, $id)->rotate(90); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param ContainerInterface $container |
190
|
|
|
* @param array $patternConfig |
191
|
|
|
* @param array $lineConfig |
192
|
|
|
* @param null|string $id |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
1 |
|
public static function straightCrossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
197
|
|
|
{ |
198
|
1 |
|
return self::crossHatch($container, $patternConfig, $lineConfig, $id) |
199
|
1 |
|
->clearTransformation() |
200
|
1 |
|
->rotate(90); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param ContainerInterface $container |
205
|
|
|
* @param array $patternConfig |
206
|
|
|
* @param array $lineConfig |
207
|
|
|
* @param null|string $id |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
2 |
|
public static function crossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
212
|
|
|
{ |
213
|
2 |
|
if (isset($patternConfig['width'])) { |
214
|
2 |
|
$patternConfig['height'] = $patternConfig['width']; |
215
|
2 |
|
} |
216
|
2 |
|
if (isset($patternConfig['height'])) { |
217
|
2 |
|
$patternConfig['width'] = $patternConfig['height']; |
218
|
2 |
|
} |
219
|
|
|
|
220
|
|
|
/** @var Pattern $pattern */ |
221
|
2 |
|
$pattern = self::diagonalHatch($container, $patternConfig, $lineConfig, $id); |
222
|
|
|
|
223
|
|
|
/** @var Line $firstLine */ |
224
|
2 |
|
$firstLine = $pattern->getFirstChild()->apply([ |
225
|
2 |
|
'x1' => 0, |
226
|
2 |
|
'y1' => $pattern->height / 2, |
227
|
2 |
|
'x2' => $pattern->width, |
228
|
2 |
|
'y2' => $pattern->height / 2, |
229
|
2 |
|
]); |
230
|
|
|
|
231
|
2 |
|
$attrs = $firstLine->allAttributes(['x1', 'y1', 'x2', 'y2', 'id']); |
232
|
2 |
|
$line = Line::create($pattern, $pattern->width / 2, 0, $pattern->width / 2, $pattern->height) |
233
|
2 |
|
->apply($attrs); |
234
|
2 |
|
$line->id = null; |
|
|
|
|
235
|
|
|
|
236
|
2 |
|
return $pattern; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param ContainerInterface $container |
241
|
|
|
* @param array $patternConfig |
242
|
|
|
* @param array $lineConfig |
243
|
|
|
* @param null|string $id |
244
|
|
|
* |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
3 |
|
public static function diagonalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
248
|
|
|
{ |
249
|
3 |
|
return self::hatch($container, $patternConfig, $lineConfig, $id)->rotate(45); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
5 |
|
public function setTransformAttribute($transformList) |
256
|
|
|
{ |
257
|
5 |
|
$this->patternTransform = $transformList; |
258
|
|
|
|
259
|
5 |
|
return $this; |
260
|
|
|
} |
261
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: