PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 4.0.0
WP 2FA – Two-factor authentication for WordPress v4.0.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / bacon / bacon-qr-code / src / Renderer / Image / EpsImageBackEnd.php
wp-2fa / includes / classes / bacon / bacon-qr-code / src / Renderer / Image Last commit date
EpsImageBackEnd.php 1 week ago ImageBackEndInterface.php 1 week ago ImagickImageBackEnd.php 1 week ago SvgImageBackEnd.php 1 week ago TransformationMatrix.php 1 week ago index.php 1 week ago
EpsImageBackEnd.php
226 lines
1 <?php
2
3 declare (strict_types=1);
4 namespace WP2FA_Vendor\BaconQrCode\Renderer\Image;
5
6 use WP2FA_Vendor\BaconQrCode\Exception\RuntimeException;
7 use WP2FA_Vendor\BaconQrCode\Renderer\Color\Alpha;
8 use WP2FA_Vendor\BaconQrCode\Renderer\Color\Cmyk;
9 use WP2FA_Vendor\BaconQrCode\Renderer\Color\ColorInterface;
10 use WP2FA_Vendor\BaconQrCode\Renderer\Color\Gray;
11 use WP2FA_Vendor\BaconQrCode\Renderer\Color\Rgb;
12 use WP2FA_Vendor\BaconQrCode\Renderer\Path\Close;
13 use WP2FA_Vendor\BaconQrCode\Renderer\Path\Curve;
14 use WP2FA_Vendor\BaconQrCode\Renderer\Path\EllipticArc;
15 use WP2FA_Vendor\BaconQrCode\Renderer\Path\Line;
16 use WP2FA_Vendor\BaconQrCode\Renderer\Path\Move;
17 use WP2FA_Vendor\BaconQrCode\Renderer\Path\Path;
18 use WP2FA_Vendor\BaconQrCode\Renderer\RendererStyle\Gradient;
19 use WP2FA_Vendor\BaconQrCode\Renderer\RendererStyle\GradientType;
20 final class EpsImageBackEnd implements ImageBackEndInterface
21 {
22 private const PRECISION = 3;
23 /**
24 * @var string|null
25 */
26 private $eps;
27 public function new(int $size, ColorInterface $backgroundColor) : void
28 {
29 $this->eps = "%!PS-Adobe-3.0 EPSF-3.0\n" . "%%Creator: BaconQrCode\n" . \sprintf("%%%%BoundingBox: 0 0 %d %d \n", $size, $size) . "%%BeginProlog\n" . "save\n" . "50 dict begin\n" . "/q { gsave } bind def\n" . "/Q { grestore } bind def\n" . "/s { scale } bind def\n" . "/t { translate } bind def\n" . "/r { rotate } bind def\n" . "/n { newpath } bind def\n" . "/m { moveto } bind def\n" . "/l { lineto } bind def\n" . "/c { curveto } bind def\n" . "/z { closepath } bind def\n" . "/f { eofill } bind def\n" . "/rgb { setrgbcolor } bind def\n" . "/cmyk { setcmykcolor } bind def\n" . "/gray { setgray } bind def\n" . "%%EndProlog\n" . "1 -1 s\n" . \sprintf("0 -%d t\n", $size);
30 if ($backgroundColor instanceof Alpha && 0 === $backgroundColor->getAlpha()) {
31 return;
32 }
33 $this->eps .= \wordwrap('0 0 m' . \sprintf(' %s 0 l', (string) $size) . \sprintf(' %s %s l', (string) $size, (string) $size) . \sprintf(' 0 %s l', (string) $size) . ' z' . ' ' . $this->getColorSetString($backgroundColor) . " f\n", 75, "\n ");
34 }
35 public function scale(float $size) : void
36 {
37 if (null === $this->eps) {
38 throw new RuntimeException('No image has been started');
39 }
40 $this->eps .= \sprintf("%1\$s %1\$s s\n", \round($size, self::PRECISION));
41 }
42 public function translate(float $x, float $y) : void
43 {
44 if (null === $this->eps) {
45 throw new RuntimeException('No image has been started');
46 }
47 $this->eps .= \sprintf("%s %s t\n", \round($x, self::PRECISION), \round($y, self::PRECISION));
48 }
49 public function rotate(int $degrees) : void
50 {
51 if (null === $this->eps) {
52 throw new RuntimeException('No image has been started');
53 }
54 $this->eps .= \sprintf("%d r\n", $degrees);
55 }
56 public function push() : void
57 {
58 if (null === $this->eps) {
59 throw new RuntimeException('No image has been started');
60 }
61 $this->eps .= "q\n";
62 }
63 public function pop() : void
64 {
65 if (null === $this->eps) {
66 throw new RuntimeException('No image has been started');
67 }
68 $this->eps .= "Q\n";
69 }
70 public function drawPathWithColor(Path $path, ColorInterface $color) : void
71 {
72 if (null === $this->eps) {
73 throw new RuntimeException('No image has been started');
74 }
75 $fromX = 0;
76 $fromY = 0;
77 $this->eps .= \wordwrap('n ' . $this->drawPathOperations($path, $fromX, $fromY) . ' ' . $this->getColorSetString($color) . " f\n", 75, "\n ");
78 }
79 public function drawPathWithGradient(Path $path, Gradient $gradient, float $x, float $y, float $width, float $height) : void
80 {
81 if (null === $this->eps) {
82 throw new RuntimeException('No image has been started');
83 }
84 $fromX = 0;
85 $fromY = 0;
86 $this->eps .= \wordwrap('q n ' . $this->drawPathOperations($path, $fromX, $fromY) . "\n", 75, "\n ");
87 $this->createGradientFill($gradient, $x, $y, $width, $height);
88 }
89 public function done() : string
90 {
91 if (null === $this->eps) {
92 throw new RuntimeException('No image has been started');
93 }
94 $this->eps .= "%%TRAILER\nend restore\n%%EOF";
95 $blob = $this->eps;
96 $this->eps = null;
97 return $blob;
98 }
99 private function drawPathOperations(iterable $ops, &$fromX, &$fromY) : string
100 {
101 $pathData = [];
102 foreach ($ops as $op) {
103 switch (\true) {
104 case $op instanceof Move:
105 $fromX = $toX = \round($op->getX(), self::PRECISION);
106 $fromY = $toY = \round($op->getY(), self::PRECISION);
107 $pathData[] = \sprintf('%s %s m', $toX, $toY);
108 break;
109 case $op instanceof Line:
110 $fromX = $toX = \round($op->getX(), self::PRECISION);
111 $fromY = $toY = \round($op->getY(), self::PRECISION);
112 $pathData[] = \sprintf('%s %s l', $toX, $toY);
113 break;
114 case $op instanceof EllipticArc:
115 $pathData[] = $this->drawPathOperations($op->toCurves($fromX, $fromY), $fromX, $fromY);
116 break;
117 case $op instanceof Curve:
118 $x1 = \round($op->getX1(), self::PRECISION);
119 $y1 = \round($op->getY1(), self::PRECISION);
120 $x2 = \round($op->getX2(), self::PRECISION);
121 $y2 = \round($op->getY2(), self::PRECISION);
122 $fromX = $x3 = \round($op->getX3(), self::PRECISION);
123 $fromY = $y3 = \round($op->getY3(), self::PRECISION);
124 $pathData[] = \sprintf('%s %s %s %s %s %s c', $x1, $y1, $x2, $y2, $x3, $y3);
125 break;
126 case $op instanceof Close:
127 $pathData[] = 'z';
128 break;
129 default:
130 throw new RuntimeException('Unexpected draw operation: ' . \get_class($op));
131 }
132 }
133 return \implode(' ', $pathData);
134 }
135 private function createGradientFill(Gradient $gradient, float $x, float $y, float $width, float $height) : void
136 {
137 $startColor = $gradient->getStartColor();
138 $endColor = $gradient->getEndColor();
139 if ($startColor instanceof Alpha) {
140 $startColor = $startColor->getBaseColor();
141 }
142 $startColorType = \get_class($startColor);
143 if (!\in_array($startColorType, [Rgb::class, Cmyk::class, Gray::class])) {
144 $startColorType = Cmyk::class;
145 $startColor = $startColor->toCmyk();
146 }
147 if (\get_class($endColor) !== $startColorType) {
148 switch ($startColorType) {
149 case Cmyk::class:
150 $endColor = $endColor->toCmyk();
151 break;
152 case Rgb::class:
153 $endColor = $endColor->toRgb();
154 break;
155 case Gray::class:
156 $endColor = $endColor->toGray();
157 break;
158 }
159 }
160 $this->eps .= "eoclip\n<<\n";
161 if ($gradient->getType() === GradientType::RADIAL()) {
162 $this->eps .= " /ShadingType 3\n";
163 } else {
164 $this->eps .= " /ShadingType 2\n";
165 }
166 $this->eps .= " /Extend [ true true ]\n" . " /AntiAlias true\n";
167 switch ($startColorType) {
168 case Cmyk::class:
169 $this->eps .= " /ColorSpace /DeviceCMYK\n";
170 break;
171 case Rgb::class:
172 $this->eps .= " /ColorSpace /DeviceRGB\n";
173 break;
174 case Gray::class:
175 $this->eps .= " /ColorSpace /DeviceGray\n";
176 break;
177 }
178 switch ($gradient->getType()) {
179 case GradientType::HORIZONTAL():
180 $this->eps .= \sprintf(" /Coords [ %s %s %s %s ]\n", \round($x, self::PRECISION), \round($y, self::PRECISION), \round($x + $width, self::PRECISION), \round($y, self::PRECISION));
181 break;
182 case GradientType::VERTICAL():
183 $this->eps .= \sprintf(" /Coords [ %s %s %s %s ]\n", \round($x, self::PRECISION), \round($y, self::PRECISION), \round($x, self::PRECISION), \round($y + $height, self::PRECISION));
184 break;
185 case GradientType::DIAGONAL():
186 $this->eps .= \sprintf(" /Coords [ %s %s %s %s ]\n", \round($x, self::PRECISION), \round($y, self::PRECISION), \round($x + $width, self::PRECISION), \round($y + $height, self::PRECISION));
187 break;
188 case GradientType::INVERSE_DIAGONAL():
189 $this->eps .= \sprintf(" /Coords [ %s %s %s %s ]\n", \round($x, self::PRECISION), \round($y + $height, self::PRECISION), \round($x + $width, self::PRECISION), \round($y, self::PRECISION));
190 break;
191 case GradientType::RADIAL():
192 $centerX = ($x + $width) / 2;
193 $centerY = ($y + $height) / 2;
194 $this->eps .= \sprintf(" /Coords [ %s %s 0 %s %s %s ]\n", \round($centerX, self::PRECISION), \round($centerY, self::PRECISION), \round($centerX, self::PRECISION), \round($centerY, self::PRECISION), \round(\max($width, $height) / 2, self::PRECISION));
195 break;
196 }
197 $this->eps .= " /Function\n" . " <<\n" . " /FunctionType 2\n" . " /Domain [ 0 1 ]\n" . \sprintf(" /C0 [ %s ]\n", $this->getColorString($startColor)) . \sprintf(" /C1 [ %s ]\n", $this->getColorString($endColor)) . " /N 1\n" . " >>\n>>\nshfill\nQ\n";
198 }
199 private function getColorSetString(ColorInterface $color) : string
200 {
201 if ($color instanceof Rgb) {
202 return $this->getColorString($color) . ' rgb';
203 }
204 if ($color instanceof Cmyk) {
205 return $this->getColorString($color) . ' cmyk';
206 }
207 if ($color instanceof Gray) {
208 return $this->getColorString($color) . ' gray';
209 }
210 return $this->getColorSetString($color->toCmyk());
211 }
212 private function getColorString(ColorInterface $color) : string
213 {
214 if ($color instanceof Rgb) {
215 return \sprintf('%s %s %s', $color->getRed() / 255, $color->getGreen() / 255, $color->getBlue() / 255);
216 }
217 if ($color instanceof Cmyk) {
218 return \sprintf('%s %s %s %s', $color->getCyan() / 100, $color->getMagenta() / 100, $color->getYellow() / 100, $color->getBlack() / 100);
219 }
220 if ($color instanceof Gray) {
221 return \sprintf('%s', $color->getGray() / 100);
222 }
223 return $this->getColorString($color->toCmyk());
224 }
225 }
226