Debian Codegraph @

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

raw
1 <?php
2 /**
3 * Class QREps
4 *
5 * @created 09.05.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_values, count, date, implode, is_array, is_numeric, max, min, round, sprintf;
15
16 /**
17 * Encapsulated Postscript (EPS) output
18 *
19 * @see https://github.com/t0k4rt/phpqrcode/blob/bb29e6eb77e0a2a85bb0eb62725e0adc11ff5a90/qrvect.php#L52-L137
20 * @see https://web.archive.org/web/20170818010030/http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/postscript/pdfs/5002.EPSF_Spec.pdf
21 * @see https://web.archive.org/web/20210419003859/https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf
22 * @see https://github.com/chillerlan/php-qrcode/discussions/148
23 */
24 class QREps extends QROutputAbstract{
25
26 final public const MIME_TYPE = 'application/postscript';
27
28 public static function moduleValueIsValid(mixed $value):bool{
29
30 if(!is_array($value) || count($value) < 3){
31 return false;
32 }
33
34 // check the first values of the array
35 foreach(array_values($value) as $i => $val){
36
37 if($i > 3){
38 break;
39 }
40
41 if(!is_numeric($val)){
42 return false;
43 }
44
45 }
46
47 return true;
48 }
49
50 protected function prepareModuleValue(mixed $value):string{
51 $values = [];
52
53 foreach(array_values($value) as $i => $val){
54
55 if($i > 3){
56 break;
57 }
58
59 // clamp value and convert from int 0-255 to float 0-1 RGB/CMYK range
60 $values[] = round((max(0, min(255, intval($val))) / 255), 6);
61 }
62
63 return $this->formatColor($values);
64 }
65
66 protected function getDefaultModuleValue(bool $isDark):string{
67 return $this->formatColor(($isDark) ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0]);
68 }
69
70 /**
71 * Set the color format string
72 *
73 * 4 values in the color array will be interpreted as CMYK, 3 as RGB
74 *
75 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
76 *
77 * @param float[] $values
78 */
79 protected function formatColor(array $values):string{
80 $count = count($values);
81
82 if($count < 3){
83 throw new QRCodeOutputException('invalid color value');
84 }
85
86 // the EPS functions "C" and "R" are defined in the header below
87 $format = ($count === 4)
88 ? '%f %f %f %f C' // CMYK
89 : '%f %f %f R'; // RGB
90
91 return sprintf($format, ...$values);
92 }
93
94 public function dump(string|null $file = null):string{
95
96 $eps = implode("\n", [
97 // initialize header
98 $this->header(),
99 // create the path elements
100 $this->paths(),
101 // end file
102 '%%EOF',
103 ]);
104
105 $this->saveToFile($eps, $file);
106
107 return $eps;
108 }
109
110 /**
111 * Returns the main header for the EPS file, including function definitions and background
112 */
113 protected function header():string{
114 [$width, $height] = $this->getOutputDimensions();
115
116 $header = [
117 // main header
118 '%!PS-Adobe-3.0 EPSF-3.0',
119 '%%Creator: php-qrcode (https://github.com/chillerlan/php-qrcode)',
120 '%%Title: QR Code',
121 sprintf('%%%%CreationDate: %1$s', date('c')),
122 '%%DocumentData: Clean7Bit',
123 '%%LanguageLevel: 3',
124 sprintf('%%%%BoundingBox: 0 0 %s %s', $width, $height),
125 '%%EndComments',
126 // function definitions
127 '%%BeginProlog',
128 '/F { rectfill } def',
129 '/R { setrgbcolor } def',
130 '/C { setcmykcolor } def',
131 '%%EndProlog',
132 ];
133
134 if($this::moduleValueIsValid($this->options->bgColor)){
135 $header[] = $this->prepareModuleValue($this->options->bgColor);
136 $header[] = sprintf('0 0 %s %s F', $width, $height);
137 }
138
139 return implode("\n", $header);
140 }
141
142 /**
143 * returns one or more EPS path blocks
144 */
145 protected function paths():string{
146 $paths = $this->collectModules();
147 $eps = [];
148
149 foreach($paths as $M_TYPE => $path){
150
151 if($path === []){
152 continue;
153 }
154
155 $eps[] = $this->getModuleValue($M_TYPE);
156 $eps[] = implode("\n", $path);
157 }
158
159 return implode("\n", $eps);
160 }
161
162 /**
163 * Returns a path segment for a single module
164 */
165 protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
166
167 if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
168 return null;
169 }
170
171 $outputX = ($x * $this->scale);
172 // Actual size - one block = Topmost y pos.
173 $top = ($this->length - $this->scale);
174 // Apparently y-axis is inverted (y0 is at bottom and not top) in EPS, so we have to switch the y-axis here
175 $outputY = ($top - ($y * $this->scale));
176
177 return sprintf('%d %d %d %d F', $outputX, $outputY, $this->scale, $this->scale);
178 }
179
180 }