| 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 |
public function __construct(private float $intensity) |
| 21 |
{ |
| 22 |
if ($intensity <= 0 || $intensity > 1) { |
| 23 |
throw new InvalidArgumentException('Intensity must between 0 (exclusive) and 1 (inclusive)'); |
| 24 |
} |
| 25 |
|
| 26 |
$this->intensity = $intensity / 2; |
| 27 |
} |
| 28 |
|
| 29 |
public function createPath(ByteMatrix $matrix) : Path |
| 30 |
{ |
| 31 |
$path = new Path(); |
| 32 |
|
| 33 |
foreach (new EdgeIterator($matrix) as $edge) { |
| 34 |
$points = $edge->getSimplifiedPoints(); |
| 35 |
$length = count($points); |
| 36 |
|
| 37 |
$currentPoint = $points[0]; |
| 38 |
$nextPoint = $points[1]; |
| 39 |
$horizontal = ($currentPoint[1] === $nextPoint[1]); |
| 40 |
|
| 41 |
if ($horizontal) { |
| 42 |
$right = $nextPoint[0] > $currentPoint[0]; |
| 43 |
$path = $path->move( |
| 44 |
$currentPoint[0] + ($right ? $this->intensity : -$this->intensity), |
| 45 |
$currentPoint[1] |
| 46 |
); |
| 47 |
} else { |
| 48 |
$up = $nextPoint[0] < $currentPoint[0]; |
| 49 |
$path = $path->move( |
| 50 |
$currentPoint[0], |
| 51 |
$currentPoint[1] + ($up ? -$this->intensity : $this->intensity) |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
for ($i = 1; $i <= $length; ++$i) { |
| 56 |
if ($i === $length) { |
| 57 |
$previousPoint = $points[$length - 1]; |
| 58 |
$currentPoint = $points[0]; |
| 59 |
$nextPoint = $points[1]; |
| 60 |
} else { |
| 61 |
$previousPoint = $points[(0 === $i ? $length : $i) - 1]; |
| 62 |
$currentPoint = $points[$i]; |
| 63 |
$nextPoint = $points[($length - 1 === $i ? -1 : $i) + 1]; |
| 64 |
} |
| 65 |
|
| 66 |
$horizontal = ($previousPoint[1] === $currentPoint[1]); |
| 67 |
|
| 68 |
if ($horizontal) { |
| 69 |
$right = $previousPoint[0] < $currentPoint[0]; |
| 70 |
$up = $nextPoint[1] < $currentPoint[1]; |
| 71 |
$sweep = ($up xor $right); |
| 72 |
|
| 73 |
if ($this->intensity < 0.5 |
| 74 |
|| ($right && $previousPoint[0] !== $currentPoint[0] - 1) |
| 75 |
|| (! $right && $previousPoint[0] - 1 !== $currentPoint[0]) |
| 76 |
) { |
| 77 |
$path = $path->line( |
| 78 |
$currentPoint[0] + ($right ? -$this->intensity : $this->intensity), |
| 79 |
$currentPoint[1] |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
$path = $path->ellipticArc( |
| 84 |
$this->intensity, |
| 85 |
$this->intensity, |
| 86 |
0, |
| 87 |
false, |
| 88 |
$sweep, |
| 89 |
$currentPoint[0], |
| 90 |
$currentPoint[1] + ($up ? -$this->intensity : $this->intensity) |
| 91 |
); |
| 92 |
} else { |
| 93 |
$up = $previousPoint[1] > $currentPoint[1]; |
| 94 |
$right = $nextPoint[0] > $currentPoint[0]; |
| 95 |
$sweep = ! ($up xor $right); |
| 96 |
|
| 97 |
if ($this->intensity < 0.5 |
| 98 |
|| ($up && $previousPoint[1] !== $currentPoint[1] + 1) |
| 99 |
|| (! $up && $previousPoint[0] + 1 !== $currentPoint[0]) |
| 100 |
) { |
| 101 |
$path = $path->line( |
| 102 |
$currentPoint[0], |
| 103 |
$currentPoint[1] + ($up ? $this->intensity : -$this->intensity) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
$path = $path->ellipticArc( |
| 108 |
$this->intensity, |
| 109 |
$this->intensity, |
| 110 |
0, |
| 111 |
false, |
| 112 |
$sweep, |
| 113 |
$currentPoint[0] + ($right ? $this->intensity : -$this->intensity), |
| 114 |
$currentPoint[1] |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$path = $path->close(); |
| 120 |
} |
| 121 |
|
| 122 |
return $path; |
| 123 |
} |
| 124 |
} |