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
|
|
|
|
11
|
|
|
namespace chillerlan\QRCode\Output; |
12
|
|
|
|
13
|
|
|
use chillerlan\QRCode\Data\QRMatrix; |
14
|
|
|
use function array_chunk, implode, sprintf; |
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/Element/svg |
21
|
|
|
* @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/ |
22
|
|
|
* @see http://apex.infogridpacific.com/SVG/svg-tutorial-contents.html |
23
|
|
|
*/ |
24
|
|
|
class QRMarkupSVG extends QRMarkup{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
protected function createMarkup(bool $saveToFile):string{ |
30
|
|
|
$svg = $this->header(); |
31
|
|
|
|
32
|
|
|
if(!empty($this->options->svgDefs)){ |
33
|
|
|
$svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->options->eol); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$svg .= $this->paths(); |
37
|
|
|
|
38
|
|
|
// close svg |
39
|
|
|
$svg .= sprintf('%1$s</svg>%1$s', $this->options->eol); |
40
|
|
|
|
41
|
|
|
// transform to data URI only when not saving to file |
42
|
|
|
if(!$saveToFile && $this->options->imageBase64){ |
43
|
|
|
$svg = $this->toBase64DataURI($svg, 'image/svg+xml'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $svg; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* returns the <svg> header with the given options parsed |
51
|
|
|
*/ |
52
|
|
|
protected function header():string{ |
53
|
|
|
$width = $this->options->svgWidth !== null ? sprintf(' width="%s"', $this->options->svgWidth) : ''; |
54
|
|
|
$height = $this->options->svgHeight !== null ? sprintf(' height="%s"', $this->options->svgHeight) : ''; |
55
|
|
|
|
56
|
|
|
/** @noinspection HtmlUnknownAttribute */ |
57
|
|
|
return sprintf( |
58
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>%6$s'. |
59
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="0 0 %2$s %2$s" preserveAspectRatio="%3$s"%4$s%5$s>%6$s', |
60
|
|
|
$this->options->cssClass, |
61
|
|
|
$this->options->svgViewBoxSize ?? $this->moduleCount, |
62
|
|
|
$this->options->svgPreserveAspectRatio, |
63
|
|
|
$width, |
64
|
|
|
$height, |
65
|
|
|
$this->options->eol |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* returns one or more SVG <path> elements |
71
|
|
|
*/ |
72
|
|
|
protected function paths():string{ |
73
|
|
|
$paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE)); |
74
|
|
|
$svg = []; |
75
|
|
|
|
76
|
|
|
// create the path elements |
77
|
|
|
foreach($paths as $M_TYPE => $modules){ |
78
|
|
|
// limit the total line length |
79
|
|
|
$chunks = array_chunk($modules, 100); |
80
|
|
|
$chonks = []; |
81
|
|
|
|
82
|
|
|
foreach($chunks as $chunk){ |
83
|
|
|
$chonks[] = implode(' ', $chunk); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$path = implode($this->options->eol, $chonks); |
|
|
|
|
87
|
|
|
|
88
|
|
|
if(empty($path)){ |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$svg[] = $this->path($path, $M_TYPE); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return implode($this->options->eol, $svg); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* renders and returns a single <path> element |
100
|
|
|
* |
101
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path |
102
|
|
|
*/ |
103
|
|
|
protected function path(string $path, int $M_TYPE):string{ |
104
|
|
|
// ignore non-existent module values |
105
|
|
|
$format = !isset($this->moduleValues[$M_TYPE]) || empty($this->moduleValues[$M_TYPE]) |
106
|
|
|
? '<path class="%1$s" d="%2$s"/>' |
107
|
|
|
: '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>'; |
108
|
|
|
|
109
|
|
|
return sprintf( |
110
|
|
|
$format, |
111
|
|
|
$this->getCssClass($M_TYPE), |
112
|
|
|
$path, |
113
|
|
|
$this->moduleValues[$M_TYPE] ?? '', // value may or may not exist |
114
|
|
|
$this->options->svgOpacity |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
protected function getCssClass(int $M_TYPE):string{ |
122
|
|
|
return implode(' ', [ |
123
|
|
|
'qr-'.$M_TYPE, |
124
|
|
|
($M_TYPE & QRMatrix::IS_DARK) === QRMatrix::IS_DARK ? 'dark' : 'light', |
125
|
|
|
$this->options->cssClass, |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* returns a path segment for a single module |
131
|
|
|
* |
132
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d |
133
|
|
|
*/ |
134
|
|
|
protected function module(int $x, int $y, int $M_TYPE):string{ |
|
|
|
|
135
|
|
|
|
136
|
|
|
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){ |
137
|
|
|
return ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if($this->options->drawCircularModules && $this->matrix->checkTypeNotIn($x, $y, $this->options->keepAsSquare)){ |
|
|
|
|
141
|
|
|
$r = $this->options->circleRadius; |
142
|
|
|
|
143
|
|
|
return sprintf( |
144
|
|
|
'M%1$s %2$s a%3$s %3$s 0 1 0 %4$s 0 a%3$s %3$s 0 1 0 -%4$s 0Z', |
145
|
|
|
($x + 0.5 - $r), |
146
|
|
|
($y + 0.5), |
147
|
|
|
$r, |
148
|
|
|
($r * 2) |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return sprintf('M%1$s %2$s h1 v1 h-1Z', $x, $y); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|