1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\container; |
3
|
|
|
|
4
|
|
|
use nstdio\svg\ElementInterface; |
5
|
|
|
use nstdio\svg\shape\Line; |
6
|
|
|
use nstdio\svg\shape\Shape; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Pattern |
10
|
|
|
* |
11
|
|
|
* @package nstdio\svg\container |
12
|
|
|
* @author Edgar Asatryan <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Pattern extends Container |
15
|
|
|
{ |
16
|
|
|
public function __construct(ElementInterface $parent, $id = null) |
17
|
|
|
{ |
18
|
|
|
if ($parent instanceof SVG) { |
19
|
|
|
$defs = $parent->getFirstChild(); |
20
|
|
|
$parent = $defs; |
21
|
|
|
} |
22
|
|
|
parent::__construct($parent); |
23
|
|
|
|
24
|
|
|
$this->id = $id; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getName() |
28
|
|
|
{ |
29
|
|
|
return 'pattern'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function withShape(ContainerInterface $container, Shape $shape, array $patternConfig = [], $id = null) |
33
|
|
|
{ |
34
|
|
|
$patternConfig = array_merge(self::getDefaultConfig(), $patternConfig); |
35
|
|
|
|
36
|
|
|
$shapeBox = $shape->getBoundingBox(); |
37
|
|
|
$patternConfig['width'] = $shapeBox['width']; |
38
|
|
|
$patternConfig['height'] = $shapeBox['height']; |
39
|
|
|
|
40
|
|
|
$pattern = (new self($container, $id))->apply($patternConfig); |
41
|
|
|
$shape->selfRemove(); |
|
|
|
|
42
|
|
|
$pattern->append($shape); |
43
|
|
|
|
44
|
|
|
return $pattern; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function verticalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
48
|
|
|
{ |
49
|
|
|
return self::hatch($container, $patternConfig, $lineConfig, $id); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function horizontalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
53
|
|
|
{ |
54
|
|
|
$patternConfig['patternTransform'] = "rotate(90)"; |
55
|
|
|
|
56
|
|
|
return self::hatch($container, $patternConfig, $lineConfig, $id); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function diagonalHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
60
|
|
|
{ |
61
|
|
|
$patternConfig['patternTransform'] = "rotate(45)"; |
62
|
|
|
|
63
|
|
|
return self::hatch($container, $patternConfig, $lineConfig, $id); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function crossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
67
|
|
|
{ |
68
|
|
|
if (isset($patternConfig['width'])) { |
69
|
|
|
$patternConfig['height'] = $patternConfig['width']; |
70
|
|
|
} |
71
|
|
|
if (isset($patternConfig['height'])) { |
72
|
|
|
$patternConfig['width'] = $patternConfig['height']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$pattern = self::hatch($container, $patternConfig, $lineConfig, $id); |
76
|
|
|
|
77
|
|
|
/** @var Line $firstLine */ |
78
|
|
|
$firstLine = $pattern->getFirstChild()->apply(['x1' => 0, 'y1' => 0, 'x2' => $pattern->height, 'y2' => $pattern->width]); |
|
|
|
|
79
|
|
|
$attrs = $firstLine->allAttributes(['x1', 'y1', 'x2', 'y2', 'id']); |
80
|
|
|
$line = new Line($pattern, 0, $pattern->width, $pattern->height, 0); |
|
|
|
|
81
|
|
|
$line->id = null; |
82
|
|
|
$line->apply($attrs); |
83
|
|
|
|
84
|
|
|
return $pattern; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function straightCrossHatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
88
|
|
|
{ |
89
|
|
|
$patternConfig['patternTransform'] = 'rotate(45)'; |
90
|
|
|
|
91
|
|
|
return self::crossHatch($container, $patternConfig, $lineConfig, $id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected static function hatch(ContainerInterface $container, array $patternConfig = [], array $lineConfig = [], $id = null) |
95
|
|
|
{ |
96
|
|
|
$patternConfig = array_merge(self::getDefaultConfig(), $patternConfig); |
97
|
|
|
$lineDefaultConfig = ['stroke' => 'black', 'stroke-width' => 1, 'fill' => 'none']; |
98
|
|
|
$lineConfig = array_merge($lineDefaultConfig, $lineConfig); |
99
|
|
|
|
100
|
|
|
$pattern = (new self($container, $id))->apply($patternConfig); |
101
|
|
|
|
102
|
|
|
(new Line($pattern, 0, 0, 0, $pattern->height))->apply($lineConfig); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $pattern; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected static function getDefaultConfig() |
108
|
|
|
{ |
109
|
|
|
return ['x' => 0, 'y' => 0, 'height' => 4, 'width' => 4, 'patternUnits' => 'userSpaceOnUse']; |
110
|
|
|
} |
111
|
|
|
} |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.