PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.15
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.15
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-qrcode / src / Output / QREps.php

QREps.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.15, at vendor_prefixed/chillerlan/php-qrcode/src/Output/QREps.php

143 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class QREps
5 *
6 * @created 09.05.2022
7 * @author smiley <smiley@chillerlan.net>
8 * @copyright 2022 smiley
9 * @license MIT
10 */
11 namespace WCPOS\Vendor\chillerlan\QRCode\Output;
12
13 use function array_values, count, date, implode, is_array, is_numeric, max, min, round, sprintf;
14 /**
15 * Encapsulated Postscript (EPS) output
16 *
17 * @see https://github.com/t0k4rt/phpqrcode/blob/bb29e6eb77e0a2a85bb0eb62725e0adc11ff5a90/qrvect.php#L52-L137
18 * @see https://web.archive.org/web/20170818010030/http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/postscript/pdfs/5002.EPSF_Spec.pdf
19 * @see https://web.archive.org/web/20210419003859/https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf
20 * @see https://github.com/chillerlan/php-qrcode/discussions/148
21 */
22 class QREps extends QROutputAbstract
23 {
24 public const MIME_TYPE = 'application/postscript';
25 /**
26 * @inheritDoc
27 */
28 public static function moduleValueIsValid($value) : bool
29 {
30 if (!is_array($value) || count($value) < 3) {
31 return \false;
32 }
33 // check the first values of the array
34 foreach (array_values($value) as $i => $val) {
35 if ($i > 3) {
36 break;
37 }
38 if (!is_numeric($val)) {
39 return \false;
40 }
41 }
42 return \true;
43 }
44 /**
45 * @param array $value
46 *
47 * @inheritDoc
48 */
49 protected function prepareModuleValue($value) : string
50 {
51 $values = [];
52 foreach (array_values($value) as $i => $val) {
53 if ($i > 3) {
54 break;
55 }
56 // clamp value and convert from int 0-255 to float 0-1 RGB/CMYK range
57 $values[] = round(max(0, min(255, \intval($val))) / 255, 6);
58 }
59 return $this->formatColor($values);
60 }
61 /**
62 * @inheritDoc
63 */
64 protected function getDefaultModuleValue(bool $isDark) : string
65 {
66 return $this->formatColor($isDark ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0]);
67 }
68 /**
69 * Set the color format string
70 *
71 * 4 values in the color array will be interpreted as CMYK, 3 as RGB
72 *
73 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
74 */
75 protected function formatColor(array $values) : string
76 {
77 $count = count($values);
78 if ($count < 3) {
79 throw new QRCodeOutputException('invalid color value');
80 }
81 $format = $count === 4 ? '%f %f %f %f C' : '%f %f %f R';
82 return sprintf($format, ...$values);
83 }
84 /**
85 * @inheritDoc
86 */
87 public function dump(?string $file = null) : string
88 {
89 [$width, $height] = $this->getOutputDimensions();
90 $eps = [
91 // main header
92 '%!PS-Adobe-3.0 EPSF-3.0',
93 '%%Creator: php-qrcode (https://github.com/chillerlan/php-qrcode)',
94 '%%Title: QR Code',
95 sprintf('%%%%CreationDate: %1$s', date('c')),
96 '%%DocumentData: Clean7Bit',
97 '%%LanguageLevel: 3',
98 sprintf('%%%%BoundingBox: 0 0 %s %s', $width, $height),
99 '%%EndComments',
100 // function definitions
101 '%%BeginProlog',
102 '/F { rectfill } def',
103 '/R { setrgbcolor } def',
104 '/C { setcmykcolor } def',
105 '%%EndProlog',
106 ];
107 if ($this::moduleValueIsValid($this->options->bgColor)) {
108 $eps[] = $this->prepareModuleValue($this->options->bgColor);
109 $eps[] = sprintf('0 0 %s %s F', $width, $height);
110 }
111 // create the path elements
112 /** @phan-suppress-next-line PhanDeprecatedFunction */
113 $paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE): string => $this->module($x, $y, $M_TYPE));
114 foreach ($paths as $M_TYPE => $path) {
115 if ($path === []) {
116 continue;
117 }
118 $eps[] = $this->getModuleValue($M_TYPE);
119 $eps[] = implode("\n", $path);
120 }
121 // end file
122 $eps[] = '%%EOF';
123 $data = implode("\n", $eps);
124 $this->saveToFile($data, $file);
125 return $data;
126 }
127 /**
128 * Returns a path segment for a single module
129 */
130 protected function module(int $x, int $y, int $M_TYPE) : string
131 {
132 if (!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)) {
133 return '';
134 }
135 $outputX = $x * $this->scale;
136 // Actual size - one block = Topmost y pos.
137 $top = $this->length - $this->scale;
138 // Apparently y-axis is inverted (y0 is at bottom and not top) in EPS, so we have to switch the y-axis here
139 $outputY = $top - $y * $this->scale;
140 return sprintf('%d %d %d %d F', $outputX, $outputY, $this->scale, $this->scale);
141 }
142 }
143