Debian Codegraph @

src/Renderer/Module/DotsModule.php

raw
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\Path\Path;
9
10 /**
11 * Renders individual modules as dots.
12 */
13 final class DotsModule implements ModuleInterface
14 {
15 public const LARGE = 1;
16 public const MEDIUM = .8;
17 public const SMALL = .6;
18
19 public function __construct(private readonly float $size)
20 {
21 if ($size <= 0 || $size > 1) {
22 throw new InvalidArgumentException('Size must between 0 (exclusive) and 1 (inclusive)');
23 }
24 }
25
26 public function createPath(ByteMatrix $matrix) : Path
27 {
28 $width = $matrix->getWidth();
29 $height = $matrix->getHeight();
30 $path = new Path();
31 $halfSize = $this->size / 2;
32 $margin = (1 - $this->size) / 2;
33
34 for ($y = 0; $y < $height; ++$y) {
35 for ($x = 0; $x < $width; ++$x) {
36 if (! $matrix->get($x, $y)) {
37 continue;
38 }
39
40 $pathX = $x + $margin;
41 $pathY = $y + $margin;
42
43 $path = $path
44 ->move($pathX + $this->size, $pathY + $halfSize)
45 ->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $halfSize, $pathY + $this->size)
46 ->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX, $pathY + $halfSize)
47 ->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $halfSize, $pathY)
48 ->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $this->size, $pathY + $halfSize)
49 ->close()
50 ;
51 }
52 }
53
54 return $path;
55 }
56 }