Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Output/QRMarkupSVG.php

raw
1 <?php
2 /**
3 * Class QRMarkupSVG
4 *
5 * @created 06.06.2022
6 * @author smiley <[email protected]>
7 * @copyright 2022 smiley
8 * @license MIT
9 */
10 declare(strict_types=1);
11
12 namespace chillerlan\QRCode\Output;
13
14 use function array_chunk, implode, is_string, preg_match, sprintf, trim;
15
16 /**
17 * SVG output
18 *
19 * @see https://github.com/codemasher/php-qrcode/pull/5
20 * @see https://developer.mozilla.org/en-US/docs/Web/SVG
21 * @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/
22 * @see https://lea.verou.me/blog/2019/05/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/
23 * @see https://codepen.io/leaverou/full/RmwzKv
24 * @see https://jakearchibald.github.io/svgomg/
25 * @see https://web.archive.org/web/20200220211445/http://apex.infogridpacific.com/SVG/svg-tutorial-contents.html
26 */
27 class QRMarkupSVG extends QRMarkup{
28
29 final public const MIME_TYPE = 'image/svg+xml';
30
31 /**
32 * @todo: XSS proof
33 *
34 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill
35 * @inheritDoc
36 */
37 public static function moduleValueIsValid(mixed $value):bool{
38
39 if(!is_string($value)){
40 return false;
41 }
42
43 $value = trim($value);
44
45 // url(...)
46 if(preg_match('~^url\([-/#a-z\d]+\)$~i', $value)){
47 return true;
48 }
49
50 // otherwise check for standard css notation
51 return parent::moduleValueIsValid($value);
52 }
53
54 protected function getOutputDimensions():array{
55 return [$this->moduleCount, $this->moduleCount];
56 }
57
58 protected function getCssClass(int $M_TYPE = 0):string{
59 return implode(' ', [
60 'qr-'.($this::LAYERNAMES[$M_TYPE] ?? $M_TYPE),
61 $this->matrix->isDark($M_TYPE) ? 'dark' : 'light',
62 $this->options->cssClass,
63 ]);
64 }
65
66 protected function createMarkup(bool $saveToFile):string{
67 $svg = $this->header();
68
69 if($this->options->svgDefs !== ''){
70 $svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->eol);
71 }
72
73 $svg .= $this->paths();
74
75 // close svg
76 $svg .= sprintf('%1$s</svg>%1$s', $this->eol);
77
78 return $svg;
79 }
80
81 /**
82 * returns the value for the SVG viewBox attribute
83 *
84 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
85 * @see https://css-tricks.com/scale-svg/#article-header-id-3
86 */
87 protected function getViewBox():string{
88 [$width, $height] = $this->getOutputDimensions();
89
90 return sprintf('0 0 %s %s', $width, $height);
91 }
92
93 /**
94 * returns the <svg> header with the given options parsed
95 *
96 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
97 */
98 protected function header():string{
99
100 $header = sprintf(
101 '<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="%2$s" preserveAspectRatio="%3$s">%4$s',
102 $this->options->cssClass,
103 $this->getViewBox(),
104 $this->options->svgPreserveAspectRatio,
105 $this->eol,
106 );
107
108 if($this->options->svgAddXmlHeader){
109 $header = sprintf('<?xml version="1.0" encoding="UTF-8"?>%s%s', $this->eol, $header);
110 }
111
112 return $header;
113 }
114
115 /**
116 * returns one or more SVG <path> elements
117 */
118 protected function paths():string{
119 $paths = $this->collectModules();
120 $svg = [];
121
122 // create the path elements
123 foreach($paths as $M_TYPE => $modules){
124 // limit the total line length
125 $chunks = array_chunk($modules, 100);
126 $chonks = [];
127
128 foreach($chunks as $chunk){
129 $chonks[] = implode(' ', $chunk);
130 }
131
132 $path = implode($this->eol, $chonks);
133
134 if($path === ''){
135 continue;
136 }
137
138 $svg[] = $this->path($path, $M_TYPE);
139 }
140
141 return implode($this->eol, $svg);
142 }
143
144 /**
145 * renders and returns a single <path> element
146 *
147 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
148 */
149 protected function path(string $path, int $M_TYPE):string{
150
151 if($this->options->svgUseFillAttributes){
152 return sprintf(
153 '<path class="%s" fill="%s" d="%s"/>',
154 $this->getCssClass($M_TYPE),
155 $this->getModuleValue($M_TYPE),
156 $path,
157 );
158 }
159
160 return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
161 }
162
163 /**
164 * returns a path segment for a single module
165 *
166 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
167 */
168 protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
169
170 if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
171 return null;
172 }
173
174 if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){
175 // string interpolation: ugly and fast
176 $ix = ($x + 0.5 - $this->circleRadius);
177 $iy = ($y + 0.5);
178
179 // phpcs:ignore
180 return "M$ix $iy a$this->circleRadius $this->circleRadius 0 1 0 $this->circleDiameter 0 a$this->circleRadius $this->circleRadius 0 1 0 -$this->circleDiameter 0Z";
181 }
182
183 // phpcs:ignore
184 return "M$x $y h1 v1 h-1Z";
185 }
186
187 }