| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCode\Renderer\Image; |
| 5 |
|
| 6 |
use BaconQrCode\Exception\RuntimeException; |
| 7 |
use BaconQrCode\Renderer\Color\Alpha; |
| 8 |
use BaconQrCode\Renderer\Color\Cmyk; |
| 9 |
use BaconQrCode\Renderer\Color\ColorInterface; |
| 10 |
use BaconQrCode\Renderer\Color\Gray; |
| 11 |
use BaconQrCode\Renderer\Color\Rgb; |
| 12 |
use BaconQrCode\Renderer\Path\Close; |
| 13 |
use BaconQrCode\Renderer\Path\Curve; |
| 14 |
use BaconQrCode\Renderer\Path\EllipticArc; |
| 15 |
use BaconQrCode\Renderer\Path\Line; |
| 16 |
use BaconQrCode\Renderer\Path\Move; |
| 17 |
use BaconQrCode\Renderer\Path\Path; |
| 18 |
use BaconQrCode\Renderer\RendererStyle\Gradient; |
| 19 |
use BaconQrCode\Renderer\RendererStyle\GradientType; |
| 20 |
|
| 21 |
final class EpsImageBackEnd implements ImageBackEndInterface |
| 22 |
{ |
| 23 |
private const PRECISION = 3; |
| 24 |
|
| 25 |
private ?string $eps; |
| 26 |
|
| 27 |
public function new(int $size, ColorInterface $backgroundColor) : void |
| 28 |
{ |
| 29 |
$this->eps = "%!PS-Adobe-3.0 EPSF-3.0\n" |
| 30 |
. "%%Creator: BaconQrCode\n" |
| 31 |
. sprintf("%%%%BoundingBox: 0 0 %d %d \n", $size, $size) |
| 32 |
. "%%BeginProlog\n" |
| 33 |
. "save\n" |
| 34 |
. "50 dict begin\n" |
| 35 |
. "/q { gsave } bind def\n" |
| 36 |
. "/Q { grestore } bind def\n" |
| 37 |
. "/s { scale } bind def\n" |
| 38 |
. "/t { translate } bind def\n" |
| 39 |
. "/r { rotate } bind def\n" |
| 40 |
. "/n { newpath } bind def\n" |
| 41 |
. "/m { moveto } bind def\n" |
| 42 |
. "/l { lineto } bind def\n" |
| 43 |
. "/c { curveto } bind def\n" |
| 44 |
. "/z { closepath } bind def\n" |
| 45 |
. "/f { eofill } bind def\n" |
| 46 |
. "/rgb { setrgbcolor } bind def\n" |
| 47 |
. "/cmyk { setcmykcolor } bind def\n" |
| 48 |
. "/gray { setgray } bind def\n" |
| 49 |
. "%%EndProlog\n" |
| 50 |
. "1 -1 s\n" |
| 51 |
. sprintf("0 -%d t\n", $size); |
| 52 |
|
| 53 |
if ($backgroundColor instanceof Alpha && 0 === $backgroundColor->getAlpha()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$this->eps .= wordwrap( |
| 58 |
'0 0 m' |
| 59 |
. sprintf(' %s 0 l', (string) $size) |
| 60 |
. sprintf(' %s %s l', (string) $size, (string) $size) |
| 61 |
. sprintf(' 0 %s l', (string) $size) |
| 62 |
. ' z' |
| 63 |
. ' ' .$this->getColorSetString($backgroundColor) . " f\n", |
| 64 |
75, |
| 65 |
"\n " |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
public function scale(float $size) : void |
| 70 |
{ |
| 71 |
if (null === $this->eps) { |
| 72 |
throw new RuntimeException('No image has been started'); |
| 73 |
} |
| 74 |
|
| 75 |
$this->eps .= sprintf("%1\$s %1\$s s\n", round($size, self::PRECISION)); |
| 76 |
} |
| 77 |
|
| 78 |
public function translate(float $x, float $y) : void |
| 79 |
{ |
| 80 |
if (null === $this->eps) { |
| 81 |
throw new RuntimeException('No image has been started'); |
| 82 |
} |
| 83 |
|
| 84 |
$this->eps .= sprintf("%s %s t\n", round($x, self::PRECISION), round($y, self::PRECISION)); |
| 85 |
} |
| 86 |
|
| 87 |
public function rotate(int $degrees) : void |
| 88 |
{ |
| 89 |
if (null === $this->eps) { |
| 90 |
throw new RuntimeException('No image has been started'); |
| 91 |
} |
| 92 |
|
| 93 |
$this->eps .= sprintf("%d r\n", $degrees); |
| 94 |
} |
| 95 |
|
| 96 |
public function push() : void |
| 97 |
{ |
| 98 |
if (null === $this->eps) { |
| 99 |
throw new RuntimeException('No image has been started'); |
| 100 |
} |
| 101 |
|
| 102 |
$this->eps .= "q\n"; |
| 103 |
} |
| 104 |
|
| 105 |
public function pop() : void |
| 106 |
{ |
| 107 |
if (null === $this->eps) { |
| 108 |
throw new RuntimeException('No image has been started'); |
| 109 |
} |
| 110 |
|
| 111 |
$this->eps .= "Q\n"; |
| 112 |
} |
| 113 |
|
| 114 |
public function drawPathWithColor(Path $path, ColorInterface $color) : void |
| 115 |
{ |
| 116 |
if (null === $this->eps) { |
| 117 |
throw new RuntimeException('No image has been started'); |
| 118 |
} |
| 119 |
|
| 120 |
$fromX = 0; |
| 121 |
$fromY = 0; |
| 122 |
$this->eps .= wordwrap( |
| 123 |
'n ' |
| 124 |
. $this->drawPathOperations($path, $fromX, $fromY) |
| 125 |
. ' ' . $this->getColorSetString($color) . " f\n", |
| 126 |
75, |
| 127 |
"\n " |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
public function drawPathWithGradient( |
| 132 |
Path $path, |
| 133 |
Gradient $gradient, |
| 134 |
float $x, |
| 135 |
float $y, |
| 136 |
float $width, |
| 137 |
float $height |
| 138 |
) : void { |
| 139 |
if (null === $this->eps) { |
| 140 |
throw new RuntimeException('No image has been started'); |
| 141 |
} |
| 142 |
|
| 143 |
$fromX = 0; |
| 144 |
$fromY = 0; |
| 145 |
$this->eps .= wordwrap( |
| 146 |
'q n ' . $this->drawPathOperations($path, $fromX, $fromY) . "\n", |
| 147 |
75, |
| 148 |
"\n " |
| 149 |
); |
| 150 |
|
| 151 |
$this->createGradientFill($gradient, $x, $y, $width, $height); |
| 152 |
} |
| 153 |
|
| 154 |
public function done() : string |
| 155 |
{ |
| 156 |
if (null === $this->eps) { |
| 157 |
throw new RuntimeException('No image has been started'); |
| 158 |
} |
| 159 |
|
| 160 |
$this->eps .= "%%TRAILER\nend restore\n%%EOF"; |
| 161 |
$blob = $this->eps; |
| 162 |
$this->eps = null; |
| 163 |
|
| 164 |
return $blob; |
| 165 |
} |
| 166 |
|
| 167 |
private function drawPathOperations(Iterable $ops, &$fromX, &$fromY) : string |
| 168 |
{ |
| 169 |
$pathData = []; |
| 170 |
|
| 171 |
foreach ($ops as $op) { |
| 172 |
switch (true) { |
| 173 |
case $op instanceof Move: |
| 174 |
$fromX = $toX = round($op->getX(), self::PRECISION); |
| 175 |
$fromY = $toY = round($op->getY(), self::PRECISION); |
| 176 |
$pathData[] = sprintf('%s %s m', $toX, $toY); |
| 177 |
break; |
| 178 |
|
| 179 |
case $op instanceof Line: |
| 180 |
$fromX = $toX = round($op->getX(), self::PRECISION); |
| 181 |
$fromY = $toY = round($op->getY(), self::PRECISION); |
| 182 |
$pathData[] = sprintf('%s %s l', $toX, $toY); |
| 183 |
break; |
| 184 |
|
| 185 |
case $op instanceof EllipticArc: |
| 186 |
$pathData[] = $this->drawPathOperations($op->toCurves($fromX, $fromY), $fromX, $fromY); |
| 187 |
break; |
| 188 |
|
| 189 |
case $op instanceof Curve: |
| 190 |
$x1 = round($op->getX1(), self::PRECISION); |
| 191 |
$y1 = round($op->getY1(), self::PRECISION); |
| 192 |
$x2 = round($op->getX2(), self::PRECISION); |
| 193 |
$y2 = round($op->getY2(), self::PRECISION); |
| 194 |
$fromX = $x3 = round($op->getX3(), self::PRECISION); |
| 195 |
$fromY = $y3 = round($op->getY3(), self::PRECISION); |
| 196 |
$pathData[] = sprintf('%s %s %s %s %s %s c', $x1, $y1, $x2, $y2, $x3, $y3); |
| 197 |
break; |
| 198 |
|
| 199 |
case $op instanceof Close: |
| 200 |
$pathData[] = 'z'; |
| 201 |
break; |
| 202 |
|
| 203 |
default: |
| 204 |
throw new RuntimeException('Unexpected draw operation: ' . get_class($op)); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return implode(' ', $pathData); |
| 209 |
} |
| 210 |
|
| 211 |
private function createGradientFill(Gradient $gradient, float $x, float $y, float $width, float $height) : void |
| 212 |
{ |
| 213 |
$startColor = $gradient->getStartColor(); |
| 214 |
$endColor = $gradient->getEndColor(); |
| 215 |
|
| 216 |
if ($startColor instanceof Alpha) { |
| 217 |
$startColor = $startColor->getBaseColor(); |
| 218 |
} |
| 219 |
|
| 220 |
$startColorType = get_class($startColor); |
| 221 |
|
| 222 |
if (! in_array($startColorType, [Rgb::class, Cmyk::class, Gray::class])) { |
| 223 |
$startColorType = Cmyk::class; |
| 224 |
$startColor = $startColor->toCmyk(); |
| 225 |
} |
| 226 |
|
| 227 |
if (get_class($endColor) !== $startColorType) { |
| 228 |
switch ($startColorType) { |
| 229 |
case Cmyk::class: |
| 230 |
$endColor = $endColor->toCmyk(); |
| 231 |
break; |
| 232 |
|
| 233 |
case Rgb::class: |
| 234 |
$endColor = $endColor->toRgb(); |
| 235 |
break; |
| 236 |
|
| 237 |
case Gray::class: |
| 238 |
$endColor = $endColor->toGray(); |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
$this->eps .= "eoclip\n<<\n"; |
| 244 |
|
| 245 |
if ($gradient->getType() === GradientType::RADIAL()) { |
| 246 |
$this->eps .= " /ShadingType 3\n"; |
| 247 |
} else { |
| 248 |
$this->eps .= " /ShadingType 2\n"; |
| 249 |
} |
| 250 |
|
| 251 |
$this->eps .= " /Extend [ true true ]\n" |
| 252 |
. " /AntiAlias true\n"; |
| 253 |
|
| 254 |
switch ($startColorType) { |
| 255 |
case Cmyk::class: |
| 256 |
$this->eps .= " /ColorSpace /DeviceCMYK\n"; |
| 257 |
break; |
| 258 |
|
| 259 |
case Rgb::class: |
| 260 |
$this->eps .= " /ColorSpace /DeviceRGB\n"; |
| 261 |
break; |
| 262 |
|
| 263 |
case Gray::class: |
| 264 |
$this->eps .= " /ColorSpace /DeviceGray\n"; |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
switch ($gradient->getType()) { |
| 269 |
case GradientType::HORIZONTAL(): |
| 270 |
$this->eps .= sprintf( |
| 271 |
" /Coords [ %s %s %s %s ]\n", |
| 272 |
round($x, self::PRECISION), |
| 273 |
round($y, self::PRECISION), |
| 274 |
round($x + $width, self::PRECISION), |
| 275 |
round($y, self::PRECISION) |
| 276 |
); |
| 277 |
break; |
| 278 |
|
| 279 |
case GradientType::VERTICAL(): |
| 280 |
$this->eps .= sprintf( |
| 281 |
" /Coords [ %s %s %s %s ]\n", |
| 282 |
round($x, self::PRECISION), |
| 283 |
round($y, self::PRECISION), |
| 284 |
round($x, self::PRECISION), |
| 285 |
round($y + $height, self::PRECISION) |
| 286 |
); |
| 287 |
break; |
| 288 |
|
| 289 |
case GradientType::DIAGONAL(): |
| 290 |
$this->eps .= sprintf( |
| 291 |
" /Coords [ %s %s %s %s ]\n", |
| 292 |
round($x, self::PRECISION), |
| 293 |
round($y, self::PRECISION), |
| 294 |
round($x + $width, self::PRECISION), |
| 295 |
round($y + $height, self::PRECISION) |
| 296 |
); |
| 297 |
break; |
| 298 |
|
| 299 |
case GradientType::INVERSE_DIAGONAL(): |
| 300 |
$this->eps .= sprintf( |
| 301 |
" /Coords [ %s %s %s %s ]\n", |
| 302 |
round($x, self::PRECISION), |
| 303 |
round($y + $height, self::PRECISION), |
| 304 |
round($x + $width, self::PRECISION), |
| 305 |
round($y, self::PRECISION) |
| 306 |
); |
| 307 |
break; |
| 308 |
|
| 309 |
case GradientType::RADIAL(): |
| 310 |
$centerX = ($x + $width) / 2; |
| 311 |
$centerY = ($y + $height) / 2; |
| 312 |
|
| 313 |
$this->eps .= sprintf( |
| 314 |
" /Coords [ %s %s 0 %s %s %s ]\n", |
| 315 |
round($centerX, self::PRECISION), |
| 316 |
round($centerY, self::PRECISION), |
| 317 |
round($centerX, self::PRECISION), |
| 318 |
round($centerY, self::PRECISION), |
| 319 |
round(max($width, $height) / 2, self::PRECISION) |
| 320 |
); |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
$this->eps .= " /Function\n" |
| 325 |
. " <<\n" |
| 326 |
. " /FunctionType 2\n" |
| 327 |
. " /Domain [ 0 1 ]\n" |
| 328 |
. sprintf(" /C0 [ %s ]\n", $this->getColorString($startColor)) |
| 329 |
. sprintf(" /C1 [ %s ]\n", $this->getColorString($endColor)) |
| 330 |
. " /N 1\n" |
| 331 |
. " >>\n>>\nshfill\nQ\n"; |
| 332 |
} |
| 333 |
|
| 334 |
private function getColorSetString(ColorInterface $color) : string |
| 335 |
{ |
| 336 |
if ($color instanceof Rgb) { |
| 337 |
return $this->getColorString($color) . ' rgb'; |
| 338 |
} |
| 339 |
|
| 340 |
if ($color instanceof Cmyk) { |
| 341 |
return $this->getColorString($color) . ' cmyk'; |
| 342 |
} |
| 343 |
|
| 344 |
if ($color instanceof Gray) { |
| 345 |
return $this->getColorString($color) . ' gray'; |
| 346 |
} |
| 347 |
|
| 348 |
return $this->getColorSetString($color->toCmyk()); |
| 349 |
} |
| 350 |
|
| 351 |
private function getColorString(ColorInterface $color) : string |
| 352 |
{ |
| 353 |
if ($color instanceof Rgb) { |
| 354 |
return sprintf('%s %s %s', $color->getRed() / 255, $color->getGreen() / 255, $color->getBlue() / 255); |
| 355 |
} |
| 356 |
|
| 357 |
if ($color instanceof Cmyk) { |
| 358 |
return sprintf( |
| 359 |
'%s %s %s %s', |
| 360 |
$color->getCyan() / 100, |
| 361 |
$color->getMagenta() / 100, |
| 362 |
$color->getYellow() / 100, |
| 363 |
$color->getBlack() / 100 |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
if ($color instanceof Gray) { |
| 368 |
return sprintf('%s', $color->getGray() / 100); |
| 369 |
} |
| 370 |
|
| 371 |
return $this->getColorString($color->toCmyk()); |
| 372 |
} |
| 373 |
} |