| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCode\Renderer; |
| 5 |
|
| 6 |
use BaconQrCode\Encoder\MatrixUtil; |
| 7 |
use BaconQrCode\Encoder\QrCode; |
| 8 |
use BaconQrCode\Exception\InvalidArgumentException; |
| 9 |
use BaconQrCode\Renderer\Image\ImageBackEndInterface; |
| 10 |
use BaconQrCode\Renderer\Path\Path; |
| 11 |
use BaconQrCode\Renderer\RendererStyle\EyeFill; |
| 12 |
use BaconQrCode\Renderer\RendererStyle\RendererStyle; |
| 13 |
|
| 14 |
final class ImageRenderer implements RendererInterface |
| 15 |
{ |
| 16 |
public function __construct( |
| 17 |
private readonly RendererStyle $rendererStyle, |
| 18 |
private readonly ImageBackEndInterface $imageBackEnd |
| 19 |
) { |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @throws InvalidArgumentException if matrix width doesn't match height |
| 24 |
*/ |
| 25 |
public function render(QrCode $qrCode) : string |
| 26 |
{ |
| 27 |
$size = $this->rendererStyle->getSize(); |
| 28 |
$margin = $this->rendererStyle->getMargin(); |
| 29 |
$matrix = $qrCode->getMatrix(); |
| 30 |
$matrixSize = $matrix->getWidth(); |
| 31 |
|
| 32 |
if ($matrixSize !== $matrix->getHeight()) { |
| 33 |
throw new InvalidArgumentException('Matrix must have the same width and height'); |
| 34 |
} |
| 35 |
|
| 36 |
$totalSize = $matrixSize + ($margin * 2); |
| 37 |
$moduleSize = $size / $totalSize; |
| 38 |
$fill = $this->rendererStyle->getFill(); |
| 39 |
|
| 40 |
$this->imageBackEnd->new($size, $fill->getBackgroundColor()); |
| 41 |
$this->imageBackEnd->scale((float) $moduleSize); |
| 42 |
$this->imageBackEnd->translate((float) $margin, (float) $margin); |
| 43 |
|
| 44 |
$module = $this->rendererStyle->getModule(); |
| 45 |
$moduleMatrix = clone $matrix; |
| 46 |
MatrixUtil::removePositionDetectionPatterns($moduleMatrix); |
| 47 |
$modulePath = $this->drawEyes($matrixSize, $module->createPath($moduleMatrix)); |
| 48 |
|
| 49 |
if ($fill->hasGradientFill()) { |
| 50 |
$this->imageBackEnd->drawPathWithGradient( |
| 51 |
$modulePath, |
| 52 |
$fill->getForegroundGradient(), |
| 53 |
0, |
| 54 |
0, |
| 55 |
$matrixSize, |
| 56 |
$matrixSize |
| 57 |
); |
| 58 |
} else { |
| 59 |
$this->imageBackEnd->drawPathWithColor($modulePath, $fill->getForegroundColor()); |
| 60 |
} |
| 61 |
|
| 62 |
return $this->imageBackEnd->done(); |
| 63 |
} |
| 64 |
|
| 65 |
private function drawEyes(int $matrixSize, Path $modulePath) : Path |
| 66 |
{ |
| 67 |
$fill = $this->rendererStyle->getFill(); |
| 68 |
|
| 69 |
$eye = $this->rendererStyle->getEye(); |
| 70 |
$externalPath = $eye->getExternalPath(); |
| 71 |
$internalPath = $eye->getInternalPath(); |
| 72 |
|
| 73 |
$modulePath = $this->drawEye( |
| 74 |
$externalPath, |
| 75 |
$internalPath, |
| 76 |
$fill->getTopLeftEyeFill(), |
| 77 |
3.5, |
| 78 |
3.5, |
| 79 |
0, |
| 80 |
$modulePath |
| 81 |
); |
| 82 |
$modulePath = $this->drawEye( |
| 83 |
$externalPath, |
| 84 |
$internalPath, |
| 85 |
$fill->getTopRightEyeFill(), |
| 86 |
$matrixSize - 3.5, |
| 87 |
3.5, |
| 88 |
90, |
| 89 |
$modulePath |
| 90 |
); |
| 91 |
$modulePath = $this->drawEye( |
| 92 |
$externalPath, |
| 93 |
$internalPath, |
| 94 |
$fill->getBottomLeftEyeFill(), |
| 95 |
3.5, |
| 96 |
$matrixSize - 3.5, |
| 97 |
-90, |
| 98 |
$modulePath |
| 99 |
); |
| 100 |
|
| 101 |
return $modulePath; |
| 102 |
} |
| 103 |
|
| 104 |
private function drawEye( |
| 105 |
Path $externalPath, |
| 106 |
Path $internalPath, |
| 107 |
EyeFill $fill, |
| 108 |
float $xTranslation, |
| 109 |
float $yTranslation, |
| 110 |
int $rotation, |
| 111 |
Path $modulePath |
| 112 |
) : Path { |
| 113 |
if ($fill->inheritsBothColors()) { |
| 114 |
return $modulePath |
| 115 |
->append( |
| 116 |
$externalPath->rotate($rotation)->translate($xTranslation, $yTranslation) |
| 117 |
) |
| 118 |
->append( |
| 119 |
$internalPath->rotate($rotation)->translate($xTranslation, $yTranslation) |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
$this->imageBackEnd->push(); |
| 124 |
$this->imageBackEnd->translate($xTranslation, $yTranslation); |
| 125 |
|
| 126 |
if (0 !== $rotation) { |
| 127 |
$this->imageBackEnd->rotate($rotation); |
| 128 |
} |
| 129 |
|
| 130 |
if ($fill->inheritsExternalColor()) { |
| 131 |
$modulePath = $modulePath->append( |
| 132 |
$externalPath->rotate($rotation)->translate($xTranslation, $yTranslation) |
| 133 |
); |
| 134 |
} else { |
| 135 |
$this->imageBackEnd->drawPathWithColor($externalPath, $fill->getExternalColor()); |
| 136 |
} |
| 137 |
|
| 138 |
if ($fill->inheritsInternalColor()) { |
| 139 |
$modulePath = $modulePath->append( |
| 140 |
$internalPath->rotate($rotation)->translate($xTranslation, $yTranslation) |
| 141 |
); |
| 142 |
} else { |
| 143 |
$this->imageBackEnd->drawPathWithColor($internalPath, $fill->getInternalColor()); |
| 144 |
} |
| 145 |
|
| 146 |
$this->imageBackEnd->pop(); |
| 147 |
|
| 148 |
return $modulePath; |
| 149 |
} |
| 150 |
} |