-
-
Notifications
You must be signed in to change notification settings - Fork 769
Expand file tree
/
Copy pathSvgWriter.php
More file actions
175 lines (145 loc) · 8.62 KB
/
Copy pathSvgWriter.php
File metadata and controls
175 lines (145 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\ImageData\LogoImageData;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\Matrix\MatrixInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Endroid\QrCode\Writer\Result\SvgResult;
final readonly class SvgWriter implements WriterInterface
{
public const int DECIMAL_PRECISION = 2;
public const string WRITER_OPTION_COMPACT = 'compact';
public const string WRITER_OPTION_BLOCK_ID = 'block_id';
public const string WRITER_OPTION_EXCLUDE_XML_DECLARATION = 'exclude_xml_declaration';
public const string WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT = 'exclude_svg_width_and_height';
public const string WRITER_OPTION_FORCE_XLINK_HREF = 'force_xlink_href';
#[\Override]
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
{
if (!isset($options[self::WRITER_OPTION_COMPACT])) {
$options[self::WRITER_OPTION_COMPACT] = true;
}
if (!isset($options[self::WRITER_OPTION_BLOCK_ID])) {
$options[self::WRITER_OPTION_BLOCK_ID] = 'block';
}
if (!isset($options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION])) {
$options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION] = false;
}
if (!isset($options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT])) {
$options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT] = false;
}
$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode);
$xml = new \SimpleXMLElement('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>');
$xml->addAttribute('version', '1.1');
if (!$options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT]) {
$xml->addAttribute('width', $matrix->getOuterSize().'px');
$xml->addAttribute('height', $matrix->getOuterSize().'px');
}
$xml->addAttribute('viewBox', '0 0 '.$matrix->getOuterSize().' '.$matrix->getOuterSize());
$background = $xml->addChild('rect');
$background->addAttribute('x', '0');
$background->addAttribute('y', '0');
$background->addAttribute('width', strval($matrix->getOuterSize()));
$background->addAttribute('height', strval($matrix->getOuterSize()));
$background->addAttribute('fill', '#'.sprintf('%02x%02x%02x', $qrCode->getBackgroundColor()->getRed(), $qrCode->getBackgroundColor()->getGreen(), $qrCode->getBackgroundColor()->getBlue()));
$background->addAttribute('fill-opacity', strval($qrCode->getBackgroundColor()->getOpacity()));
if ($options[self::WRITER_OPTION_COMPACT]) {
$this->writePath($xml, $qrCode, $matrix);
} else {
$this->writeBlockDefinitions($xml, $qrCode, $matrix, $options);
}
$result = new SvgResult($matrix, $xml, boolval($options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION]));
if ($logo instanceof LogoInterface) {
$this->addLogo($logo, $result, $options);
}
return $result;
}
private function writePath(\SimpleXMLElement $xml, QrCodeInterface $qrCode, MatrixInterface $matrix): void
{
$path = '';
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
$left = $matrix->getMarginLeft();
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
// When we are at the first column or when the previous column was 0 set new left
if (0 === $columnIndex || 0 === $matrix->getBlockValue($rowIndex, $columnIndex - 1)) {
$left = $matrix->getMarginLeft() + $matrix->getBlockSize() * $columnIndex;
}
// When we are at the
if ($columnIndex === $matrix->getBlockCount() - 1 || 0 === $matrix->getBlockValue($rowIndex, $columnIndex + 1)) {
$top = $matrix->getMarginLeft() + $matrix->getBlockSize() * $rowIndex;
$bottom = $matrix->getMarginLeft() + $matrix->getBlockSize() * ($rowIndex + 1);
$right = $matrix->getMarginLeft() + $matrix->getBlockSize() * ($columnIndex + 1);
$path .= 'M'.$this->formatNumber($left).','.$this->formatNumber($top);
$path .= 'L'.$this->formatNumber($right).','.$this->formatNumber($top);
$path .= 'L'.$this->formatNumber($right).','.$this->formatNumber($bottom);
$path .= 'L'.$this->formatNumber($left).','.$this->formatNumber($bottom).'Z';
}
}
}
}
$pathDefinition = $xml->addChild('path');
$pathDefinition->addAttribute('fill', '#'.sprintf('%02x%02x%02x', $qrCode->getForegroundColor()->getRed(), $qrCode->getForegroundColor()->getGreen(), $qrCode->getForegroundColor()->getBlue()));
$pathDefinition->addAttribute('fill-opacity', strval($qrCode->getForegroundColor()->getOpacity()));
$pathDefinition->addAttribute('d', $path);
}
/** @param array<string, mixed> $options */
private function writeBlockDefinitions(\SimpleXMLElement $xml, QrCodeInterface $qrCode, MatrixInterface $matrix, array $options): void
{
$xml->addChild('defs');
$blockDefinition = $xml->defs->addChild('rect');
$blockDefinition->addAttribute('id', strval($options[self::WRITER_OPTION_BLOCK_ID]));
$blockDefinition->addAttribute('width', $this->formatNumber($matrix->getBlockSize()));
$blockDefinition->addAttribute('height', $this->formatNumber($matrix->getBlockSize()));
$blockDefinition->addAttribute('fill', '#'.sprintf('%02x%02x%02x', $qrCode->getForegroundColor()->getRed(), $qrCode->getForegroundColor()->getGreen(), $qrCode->getForegroundColor()->getBlue()));
$blockDefinition->addAttribute('fill-opacity', strval($qrCode->getForegroundColor()->getOpacity()));
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
$block = $xml->addChild('use');
$block->addAttribute('x', $this->formatNumber($matrix->getMarginLeft() + $matrix->getBlockSize() * $columnIndex));
$block->addAttribute('y', $this->formatNumber($matrix->getMarginLeft() + $matrix->getBlockSize() * $rowIndex));
$block->addAttribute('xlink:href', '#'.$options[self::WRITER_OPTION_BLOCK_ID], 'http://www.w3.org/1999/xlink');
}
}
}
}
/** @param array<string, mixed> $options */
private function addLogo(LogoInterface $logo, SvgResult $result, array $options): void
{
if ($logo->getPunchoutBackground()) {
throw new \Exception('The SVG writer does not support logo punchout background');
}
$logoImageData = LogoImageData::createForLogo($logo);
if (!isset($options[self::WRITER_OPTION_FORCE_XLINK_HREF])) {
$options[self::WRITER_OPTION_FORCE_XLINK_HREF] = false;
}
$xml = $result->getXml();
/** @var \SimpleXMLElement $xmlAttributes */
$xmlAttributes = $xml->attributes();
$x = intval($xmlAttributes->width) / 2 - $logoImageData->getWidth() / 2;
$y = intval($xmlAttributes->height) / 2 - $logoImageData->getHeight() / 2;
$imageDefinition = $xml->addChild('image');
$imageDefinition->addAttribute('x', strval($x));
$imageDefinition->addAttribute('y', strval($y));
$imageDefinition->addAttribute('width', strval($logoImageData->getWidth()));
$imageDefinition->addAttribute('height', strval($logoImageData->getHeight()));
$imageDefinition->addAttribute('preserveAspectRatio', 'none');
if ($options[self::WRITER_OPTION_FORCE_XLINK_HREF]) {
$imageDefinition->addAttribute('xlink:href', $logoImageData->createDataUri(), 'http://www.w3.org/1999/xlink');
} else {
$imageDefinition->addAttribute('href', $logoImageData->createDataUri());
}
}
private function formatNumber(float $number): string
{
$string = number_format($number, self::DECIMAL_PRECISION, '.', '');
$string = rtrim($string, '0');
return rtrim($string, '.');
}
}