Initial commit

This commit is contained in:
2022-11-21 09:47:28 +01:00
commit 76cec83d26
11652 changed files with 1980467 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module;
use BaconQrCode\Encoder\ByteMatrix;
use BaconQrCode\Exception\InvalidArgumentException;
use BaconQrCode\Renderer\Path\Path;
/**
* Renders individual modules as dots.
*/
final class DotsModule implements ModuleInterface
{
public const LARGE = 1;
public const MEDIUM = .8;
public const SMALL = .6;
/**
* @var float
*/
private $size;
public function __construct(float $size)
{
if ($size <= 0 || $size > 1) {
throw new InvalidArgumentException('Size must between 0 (exclusive) and 1 (inclusive)');
}
$this->size = $size;
}
public function createPath(ByteMatrix $matrix) : Path
{
$width = $matrix->getWidth();
$height = $matrix->getHeight();
$path = new Path();
$halfSize = $this->size / 2;
$margin = (1 - $this->size) / 2;
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
if (! $matrix->get($x, $y)) {
continue;
}
$pathX = $x + $margin;
$pathY = $y + $margin;
$path = $path
->move($pathX + $this->size, $pathY + $halfSize)
->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $halfSize, $pathY + $this->size)
->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX, $pathY + $halfSize)
->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $halfSize, $pathY)
->ellipticArc($halfSize, $halfSize, 0, false, true, $pathX + $this->size, $pathY + $halfSize)
->close()
;
}
}
return $path;
}
}

View File

@ -0,0 +1,100 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module\EdgeIterator;
final class Edge
{
/**
* @var bool
*/
private $positive;
/**
* @var array<int[]>
*/
private $points = [];
/**
* @var array<int[]>|null
*/
private $simplifiedPoints;
/**
* @var int
*/
private $minX = PHP_INT_MAX;
/**
* @var int
*/
private $minY = PHP_INT_MAX;
/**
* @var int
*/
private $maxX = -1;
/**
* @var int
*/
private $maxY = -1;
public function __construct(bool $positive)
{
$this->positive = $positive;
}
public function addPoint(int $x, int $y) : void
{
$this->points[] = [$x, $y];
$this->minX = min($this->minX, $x);
$this->minY = min($this->minY, $y);
$this->maxX = max($this->maxX, $x);
$this->maxY = max($this->maxY, $y);
}
public function isPositive() : bool
{
return $this->positive;
}
/**
* @return array<int[]>
*/
public function getPoints() : array
{
return $this->points;
}
public function getMaxX() : int
{
return $this->maxX;
}
public function getSimplifiedPoints() : array
{
if (null !== $this->simplifiedPoints) {
return $this->simplifiedPoints;
}
$points = [];
$length = count($this->points);
for ($i = 0; $i < $length; ++$i) {
$previousPoint = $this->points[(0 === $i ? $length : $i) - 1];
$nextPoint = $this->points[($length - 1 === $i ? -1 : $i) + 1];
$currentPoint = $this->points[$i];
if (($previousPoint[0] === $currentPoint[0] && $currentPoint[0] === $nextPoint[0])
|| ($previousPoint[1] === $currentPoint[1] && $currentPoint[1] === $nextPoint[1])
) {
continue;
}
$points[] = $currentPoint;
}
return $this->simplifiedPoints = $points;
}
}

View File

@ -0,0 +1,169 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module\EdgeIterator;
use BaconQrCode\Encoder\ByteMatrix;
use IteratorAggregate;
use Traversable;
/**
* Edge iterator based on potrace.
*/
final class EdgeIterator implements IteratorAggregate
{
/**
* @var int[]
*/
private $bytes = [];
/**
* @var int
*/
private $size;
/**
* @var int
*/
private $width;
/**
* @var int
*/
private $height;
public function __construct(ByteMatrix $matrix)
{
$this->bytes = iterator_to_array($matrix->getBytes());
$this->size = count($this->bytes);
$this->width = $matrix->getWidth();
$this->height = $matrix->getHeight();
}
/**
* @return Edge[]
*/
public function getIterator() : Traversable
{
$originalBytes = $this->bytes;
$point = $this->findNext(0, 0);
while (null !== $point) {
$edge = $this->findEdge($point[0], $point[1]);
$this->xorEdge($edge);
yield $edge;
$point = $this->findNext($point[0], $point[1]);
}
$this->bytes = $originalBytes;
}
/**
* @return int[]|null
*/
private function findNext(int $x, int $y) : ?array
{
$i = $this->width * $y + $x;
while ($i < $this->size && 1 !== $this->bytes[$i]) {
++$i;
}
if ($i < $this->size) {
return $this->pointOf($i);
}
return null;
}
private function findEdge(int $x, int $y) : Edge
{
$edge = new Edge($this->isSet($x, $y));
$startX = $x;
$startY = $y;
$dirX = 0;
$dirY = 1;
while (true) {
$edge->addPoint($x, $y);
$x += $dirX;
$y += $dirY;
if ($x === $startX && $y === $startY) {
break;
}
$left = $this->isSet($x + ($dirX + $dirY - 1 ) / 2, $y + ($dirY - $dirX - 1) / 2);
$right = $this->isSet($x + ($dirX - $dirY - 1) / 2, $y + ($dirY + $dirX - 1) / 2);
if ($right && ! $left) {
$tmp = $dirX;
$dirX = -$dirY;
$dirY = $tmp;
} elseif ($right) {
$tmp = $dirX;
$dirX = -$dirY;
$dirY = $tmp;
} elseif (! $left) {
$tmp = $dirX;
$dirX = $dirY;
$dirY = -$tmp;
}
}
return $edge;
}
private function xorEdge(Edge $path) : void
{
$points = $path->getPoints();
$y1 = $points[0][1];
$length = count($points);
$maxX = $path->getMaxX();
for ($i = 1; $i < $length; ++$i) {
$y = $points[$i][1];
if ($y === $y1) {
continue;
}
$x = $points[$i][0];
$minY = min($y1, $y);
for ($j = $x; $j < $maxX; ++$j) {
$this->flip($j, $minY);
}
$y1 = $y;
}
}
private function isSet(int $x, int $y) : bool
{
return (
$x >= 0
&& $x < $this->width
&& $y >= 0
&& $y < $this->height
) && 1 === $this->bytes[$this->width * $y + $x];
}
/**
* @return int[]
*/
private function pointOf(int $i) : array
{
$y = intdiv($i, $this->width);
return [$i - $y * $this->width, $y];
}
private function flip(int $x, int $y) : void
{
$this->bytes[$this->width * $y + $x] = (
$this->isSet($x, $y) ? 0 : 1
);
}
}

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module;
use BaconQrCode\Encoder\ByteMatrix;
use BaconQrCode\Renderer\Path\Path;
/**
* Interface describing how modules should be rendered.
*
* A module always receives a byte matrix (with values either being 1 or 0). It returns a path, where the origin
* coordinate (0, 0) equals the top left corner of the first matrix value.
*/
interface ModuleInterface
{
public function createPath(ByteMatrix $matrix) : Path;
}

View File

@ -0,0 +1,129 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module;
use BaconQrCode\Encoder\ByteMatrix;
use BaconQrCode\Exception\InvalidArgumentException;
use BaconQrCode\Renderer\Module\EdgeIterator\EdgeIterator;
use BaconQrCode\Renderer\Path\Path;
/**
* Rounds the corners of module groups.
*/
final class RoundnessModule implements ModuleInterface
{
public const STRONG = 1;
public const MEDIUM = .5;
public const SOFT = .25;
/**
* @var float
*/
private $intensity;
public function __construct(float $intensity)
{
if ($intensity <= 0 || $intensity > 1) {
throw new InvalidArgumentException('Intensity must between 0 (exclusive) and 1 (inclusive)');
}
$this->intensity = $intensity / 2;
}
public function createPath(ByteMatrix $matrix) : Path
{
$path = new Path();
foreach (new EdgeIterator($matrix) as $edge) {
$points = $edge->getSimplifiedPoints();
$length = count($points);
$currentPoint = $points[0];
$nextPoint = $points[1];
$horizontal = ($currentPoint[1] === $nextPoint[1]);
if ($horizontal) {
$right = $nextPoint[0] > $currentPoint[0];
$path = $path->move(
$currentPoint[0] + ($right ? $this->intensity : -$this->intensity),
$currentPoint[1]
);
} else {
$up = $nextPoint[0] < $currentPoint[0];
$path = $path->move(
$currentPoint[0],
$currentPoint[1] + ($up ? -$this->intensity : $this->intensity)
);
}
for ($i = 1; $i <= $length; ++$i) {
if ($i === $length) {
$previousPoint = $points[$length - 1];
$currentPoint = $points[0];
$nextPoint = $points[1];
} else {
$previousPoint = $points[(0 === $i ? $length : $i) - 1];
$currentPoint = $points[$i];
$nextPoint = $points[($length - 1 === $i ? -1 : $i) + 1];
}
$horizontal = ($previousPoint[1] === $currentPoint[1]);
if ($horizontal) {
$right = $previousPoint[0] < $currentPoint[0];
$up = $nextPoint[1] < $currentPoint[1];
$sweep = ($up xor $right);
if ($this->intensity < 0.5
|| ($right && $previousPoint[0] !== $currentPoint[0] - 1)
|| (! $right && $previousPoint[0] - 1 !== $currentPoint[0])
) {
$path = $path->line(
$currentPoint[0] + ($right ? -$this->intensity : $this->intensity),
$currentPoint[1]
);
}
$path = $path->ellipticArc(
$this->intensity,
$this->intensity,
0,
false,
$sweep,
$currentPoint[0],
$currentPoint[1] + ($up ? -$this->intensity : $this->intensity)
);
} else {
$up = $previousPoint[1] > $currentPoint[1];
$right = $nextPoint[0] > $currentPoint[0];
$sweep = ! ($up xor $right);
if ($this->intensity < 0.5
|| ($up && $previousPoint[1] !== $currentPoint[1] + 1)
|| (! $up && $previousPoint[0] + 1 !== $currentPoint[0])
) {
$path = $path->line(
$currentPoint[0],
$currentPoint[1] + ($up ? $this->intensity : -$this->intensity)
);
}
$path = $path->ellipticArc(
$this->intensity,
$this->intensity,
0,
false,
$sweep,
$currentPoint[0] + ($right ? $this->intensity : -$this->intensity),
$currentPoint[1]
);
}
}
$path = $path->close();
}
return $path;
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types = 1);
namespace BaconQrCode\Renderer\Module;
use BaconQrCode\Encoder\ByteMatrix;
use BaconQrCode\Renderer\Module\EdgeIterator\EdgeIterator;
use BaconQrCode\Renderer\Path\Path;
/**
* Groups modules together to a single path.
*/
final class SquareModule implements ModuleInterface
{
/**
* @var self|null
*/
private static $instance;
private function __construct()
{
}
public static function instance() : self
{
return self::$instance ?: self::$instance = new self();
}
public function createPath(ByteMatrix $matrix) : Path
{
$path = new Path();
foreach (new EdgeIterator($matrix) as $edge) {
$points = $edge->getSimplifiedPoints();
$length = count($points);
$path = $path->move($points[0][0], $points[0][1]);
for ($i = 1; $i < $length; ++$i) {
$path = $path->line($points[$i][0], $points[$i][1]);
}
$path = $path->close();
}
return $path;
}
}