Debian Codegraph @

vendor/endroid/qr-code/src/Writer/SvgWriter.php

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