EdgeIterator
11 months ago
DotsModule.php
11 months ago
ModuleInterface.php
11 months ago
RoundnessModule.php
11 months ago
SquareModule.php
11 months ago
RoundnessModule.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WP2FA_Vendor\BaconQrCode\Renderer\Module; |
| 5 | |
| 6 | use WP2FA_Vendor\BaconQrCode\Encoder\ByteMatrix; |
| 7 | use WP2FA_Vendor\BaconQrCode\Exception\InvalidArgumentException; |
| 8 | use WP2FA_Vendor\BaconQrCode\Renderer\Module\EdgeIterator\EdgeIterator; |
| 9 | use WP2FA_Vendor\BaconQrCode\Renderer\Path\Path; |
| 10 | /** |
| 11 | * Rounds the corners of module groups. |
| 12 | */ |
| 13 | final class RoundnessModule implements ModuleInterface |
| 14 | { |
| 15 | public const STRONG = 1; |
| 16 | public const MEDIUM = 0.5; |
| 17 | public const SOFT = 0.25; |
| 18 | /** |
| 19 | * @var float |
| 20 | */ |
| 21 | private $intensity; |
| 22 | public function __construct(float $intensity) |
| 23 | { |
| 24 | if ($intensity <= 0 || $intensity > 1) { |
| 25 | throw new InvalidArgumentException('Intensity must between 0 (exclusive) and 1 (inclusive)'); |
| 26 | } |
| 27 | $this->intensity = $intensity / 2; |
| 28 | } |
| 29 | public function createPath(ByteMatrix $matrix) : Path |
| 30 | { |
| 31 | $path = new Path(); |
| 32 | foreach (new EdgeIterator($matrix) as $edge) { |
| 33 | $points = $edge->getSimplifiedPoints(); |
| 34 | $length = \count($points); |
| 35 | $currentPoint = $points[0]; |
| 36 | $nextPoint = $points[1]; |
| 37 | $horizontal = $currentPoint[1] === $nextPoint[1]; |
| 38 | if ($horizontal) { |
| 39 | $right = $nextPoint[0] > $currentPoint[0]; |
| 40 | $path = $path->move($currentPoint[0] + ($right ? $this->intensity : -$this->intensity), $currentPoint[1]); |
| 41 | } else { |
| 42 | $up = $nextPoint[0] < $currentPoint[0]; |
| 43 | $path = $path->move($currentPoint[0], $currentPoint[1] + ($up ? -$this->intensity : $this->intensity)); |
| 44 | } |
| 45 | for ($i = 1; $i <= $length; ++$i) { |
| 46 | if ($i === $length) { |
| 47 | $previousPoint = $points[$length - 1]; |
| 48 | $currentPoint = $points[0]; |
| 49 | $nextPoint = $points[1]; |
| 50 | } else { |
| 51 | $previousPoint = $points[(0 === $i ? $length : $i) - 1]; |
| 52 | $currentPoint = $points[$i]; |
| 53 | $nextPoint = $points[($length - 1 === $i ? -1 : $i) + 1]; |
| 54 | } |
| 55 | $horizontal = $previousPoint[1] === $currentPoint[1]; |
| 56 | if ($horizontal) { |
| 57 | $right = $previousPoint[0] < $currentPoint[0]; |
| 58 | $up = $nextPoint[1] < $currentPoint[1]; |
| 59 | $sweep = ($up xor $right); |
| 60 | if ($this->intensity < 0.5 || $right && $previousPoint[0] !== $currentPoint[0] - 1 || !$right && $previousPoint[0] - 1 !== $currentPoint[0]) { |
| 61 | $path = $path->line($currentPoint[0] + ($right ? -$this->intensity : $this->intensity), $currentPoint[1]); |
| 62 | } |
| 63 | $path = $path->ellipticArc($this->intensity, $this->intensity, 0, \false, $sweep, $currentPoint[0], $currentPoint[1] + ($up ? -$this->intensity : $this->intensity)); |
| 64 | } else { |
| 65 | $up = $previousPoint[1] > $currentPoint[1]; |
| 66 | $right = $nextPoint[0] > $currentPoint[0]; |
| 67 | $sweep = !($up xor $right); |
| 68 | if ($this->intensity < 0.5 || $up && $previousPoint[1] !== $currentPoint[1] + 1 || !$up && $previousPoint[0] + 1 !== $currentPoint[0]) { |
| 69 | $path = $path->line($currentPoint[0], $currentPoint[1] + ($up ? $this->intensity : -$this->intensity)); |
| 70 | } |
| 71 | $path = $path->ellipticArc($this->intensity, $this->intensity, 0, \false, $sweep, $currentPoint[0] + ($right ? $this->intensity : -$this->intensity), $currentPoint[1]); |
| 72 | } |
| 73 | } |
| 74 | $path = $path->close(); |
| 75 | } |
| 76 | return $path; |
| 77 | } |
| 78 | } |
| 79 |