EdgeIterator
8 months ago
DotsModule.php
8 months ago
ModuleInterface.php
8 months ago
RoundnessModule.php
8 months ago
SquareModule.php
8 months ago
RoundnessModule.php
130 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCode\Renderer\Module; |
| 5 | |
| 6 | use BaconQrCode\Encoder\ByteMatrix; |
| 7 | use BaconQrCode\Exception\InvalidArgumentException; |
| 8 | use BaconQrCode\Renderer\Module\EdgeIterator\EdgeIterator; |
| 9 | use BaconQrCode\Renderer\Path\Path; |
| 10 | |
| 11 | /** |
| 12 | * Rounds the corners of module groups. |
| 13 | */ |
| 14 | final class RoundnessModule implements ModuleInterface |
| 15 | { |
| 16 | public const STRONG = 1; |
| 17 | public const MEDIUM = .5; |
| 18 | public const SOFT = .25; |
| 19 | |
| 20 | /** |
| 21 | * @var float |
| 22 | */ |
| 23 | private $intensity; |
| 24 | |
| 25 | public function __construct(float $intensity) |
| 26 | { |
| 27 | if ($intensity <= 0 || $intensity > 1) { |
| 28 | throw new InvalidArgumentException('Intensity must between 0 (exclusive) and 1 (inclusive)'); |
| 29 | } |
| 30 | |
| 31 | $this->intensity = $intensity / 2; |
| 32 | } |
| 33 | |
| 34 | public function createPath(ByteMatrix $matrix) : Path |
| 35 | { |
| 36 | $path = new Path(); |
| 37 | |
| 38 | foreach (new EdgeIterator($matrix) as $edge) { |
| 39 | $points = $edge->getSimplifiedPoints(); |
| 40 | $length = count($points); |
| 41 | |
| 42 | $currentPoint = $points[0]; |
| 43 | $nextPoint = $points[1]; |
| 44 | $horizontal = ($currentPoint[1] === $nextPoint[1]); |
| 45 | |
| 46 | if ($horizontal) { |
| 47 | $right = $nextPoint[0] > $currentPoint[0]; |
| 48 | $path = $path->move( |
| 49 | $currentPoint[0] + ($right ? $this->intensity : -$this->intensity), |
| 50 | $currentPoint[1] |
| 51 | ); |
| 52 | } else { |
| 53 | $up = $nextPoint[0] < $currentPoint[0]; |
| 54 | $path = $path->move( |
| 55 | $currentPoint[0], |
| 56 | $currentPoint[1] + ($up ? -$this->intensity : $this->intensity) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | for ($i = 1; $i <= $length; ++$i) { |
| 61 | if ($i === $length) { |
| 62 | $previousPoint = $points[$length - 1]; |
| 63 | $currentPoint = $points[0]; |
| 64 | $nextPoint = $points[1]; |
| 65 | } else { |
| 66 | $previousPoint = $points[(0 === $i ? $length : $i) - 1]; |
| 67 | $currentPoint = $points[$i]; |
| 68 | $nextPoint = $points[($length - 1 === $i ? -1 : $i) + 1]; |
| 69 | } |
| 70 | |
| 71 | $horizontal = ($previousPoint[1] === $currentPoint[1]); |
| 72 | |
| 73 | if ($horizontal) { |
| 74 | $right = $previousPoint[0] < $currentPoint[0]; |
| 75 | $up = $nextPoint[1] < $currentPoint[1]; |
| 76 | $sweep = ($up xor $right); |
| 77 | |
| 78 | if ($this->intensity < 0.5 |
| 79 | || ($right && $previousPoint[0] !== $currentPoint[0] - 1) |
| 80 | || (! $right && $previousPoint[0] - 1 !== $currentPoint[0]) |
| 81 | ) { |
| 82 | $path = $path->line( |
| 83 | $currentPoint[0] + ($right ? -$this->intensity : $this->intensity), |
| 84 | $currentPoint[1] |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | $path = $path->ellipticArc( |
| 89 | $this->intensity, |
| 90 | $this->intensity, |
| 91 | 0, |
| 92 | false, |
| 93 | $sweep, |
| 94 | $currentPoint[0], |
| 95 | $currentPoint[1] + ($up ? -$this->intensity : $this->intensity) |
| 96 | ); |
| 97 | } else { |
| 98 | $up = $previousPoint[1] > $currentPoint[1]; |
| 99 | $right = $nextPoint[0] > $currentPoint[0]; |
| 100 | $sweep = ! ($up xor $right); |
| 101 | |
| 102 | if ($this->intensity < 0.5 |
| 103 | || ($up && $previousPoint[1] !== $currentPoint[1] + 1) |
| 104 | || (! $up && $previousPoint[0] + 1 !== $currentPoint[0]) |
| 105 | ) { |
| 106 | $path = $path->line( |
| 107 | $currentPoint[0], |
| 108 | $currentPoint[1] + ($up ? $this->intensity : -$this->intensity) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $path = $path->ellipticArc( |
| 113 | $this->intensity, |
| 114 | $this->intensity, |
| 115 | 0, |
| 116 | false, |
| 117 | $sweep, |
| 118 | $currentPoint[0] + ($right ? $this->intensity : -$this->intensity), |
| 119 | $currentPoint[1] |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $path = $path->close(); |
| 125 | } |
| 126 | |
| 127 | return $path; |
| 128 | } |
| 129 | } |
| 130 |