| 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\ColorInterface; |
| 9 |
use BaconQrCode\Renderer\Path\Close; |
| 10 |
use BaconQrCode\Renderer\Path\Curve; |
| 11 |
use BaconQrCode\Renderer\Path\EllipticArc; |
| 12 |
use BaconQrCode\Renderer\Path\Line; |
| 13 |
use BaconQrCode\Renderer\Path\Move; |
| 14 |
use BaconQrCode\Renderer\Path\Path; |
| 15 |
use BaconQrCode\Renderer\RendererStyle\Gradient; |
| 16 |
use BaconQrCode\Renderer\RendererStyle\GradientType; |
| 17 |
use XMLWriter; |
| 18 |
|
| 19 |
final class SvgImageBackEnd implements ImageBackEndInterface |
| 20 |
{ |
| 21 |
private const PRECISION = 3; |
| 22 |
|
| 23 |
private ?XMLWriter $xmlWriter; |
| 24 |
|
| 25 |
private ?array $stack; |
| 26 |
|
| 27 |
private ?int $currentStack; |
| 28 |
|
| 29 |
private ?int $gradientCount; |
| 30 |
|
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
if (! class_exists(XMLWriter::class)) { |
| 34 |
throw new RuntimeException( |
| 35 |
'You need to install the libxml extension and enable the xmlwriter extension to use this back end' |
| 36 |
); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function new(int $size, ColorInterface $backgroundColor) : void |
| 41 |
{ |
| 42 |
$this->xmlWriter = new XMLWriter(); |
| 43 |
$this->xmlWriter->openMemory(); |
| 44 |
|
| 45 |
$this->xmlWriter->startDocument('1.0', 'UTF-8'); |
| 46 |
$this->xmlWriter->startElement('svg'); |
| 47 |
$this->xmlWriter->writeAttribute('xmlns', 'http://www.w3.org/2000/svg'); |
| 48 |
$this->xmlWriter->writeAttribute('version', '1.1'); |
| 49 |
$this->xmlWriter->writeAttribute('width', (string) $size); |
| 50 |
$this->xmlWriter->writeAttribute('height', (string) $size); |
| 51 |
$this->xmlWriter->writeAttribute('viewBox', '0 0 '. $size . ' ' . $size); |
| 52 |
|
| 53 |
$this->gradientCount = 0; |
| 54 |
$this->currentStack = 0; |
| 55 |
$this->stack[0] = 0; |
| 56 |
|
| 57 |
$alpha = 1; |
| 58 |
|
| 59 |
if ($backgroundColor instanceof Alpha) { |
| 60 |
$alpha = $backgroundColor->getAlpha() / 100; |
| 61 |
} |
| 62 |
|
| 63 |
if (0 === $alpha) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$this->xmlWriter->startElement('rect'); |
| 68 |
$this->xmlWriter->writeAttribute('x', '0'); |
| 69 |
$this->xmlWriter->writeAttribute('y', '0'); |
| 70 |
$this->xmlWriter->writeAttribute('width', (string) $size); |
| 71 |
$this->xmlWriter->writeAttribute('height', (string) $size); |
| 72 |
$this->xmlWriter->writeAttribute('fill', $this->getColorString($backgroundColor)); |
| 73 |
|
| 74 |
if ($alpha < 1) { |
| 75 |
$this->xmlWriter->writeAttribute('fill-opacity', (string) $alpha); |
| 76 |
} |
| 77 |
|
| 78 |
$this->xmlWriter->endElement(); |
| 79 |
} |
| 80 |
|
| 81 |
public function scale(float $size) : void |
| 82 |
{ |
| 83 |
if (null === $this->xmlWriter) { |
| 84 |
throw new RuntimeException('No image has been started'); |
| 85 |
} |
| 86 |
|
| 87 |
$this->xmlWriter->startElement('g'); |
| 88 |
$this->xmlWriter->writeAttribute( |
| 89 |
'transform', |
| 90 |
sprintf('scale(%s)', round($size, self::PRECISION)) |
| 91 |
); |
| 92 |
++$this->stack[$this->currentStack]; |
| 93 |
} |
| 94 |
|
| 95 |
public function translate(float $x, float $y) : void |
| 96 |
{ |
| 97 |
if (null === $this->xmlWriter) { |
| 98 |
throw new RuntimeException('No image has been started'); |
| 99 |
} |
| 100 |
|
| 101 |
$this->xmlWriter->startElement('g'); |
| 102 |
$this->xmlWriter->writeAttribute( |
| 103 |
'transform', |
| 104 |
sprintf('translate(%s,%s)', round($x, self::PRECISION), round($y, self::PRECISION)) |
| 105 |
); |
| 106 |
++$this->stack[$this->currentStack]; |
| 107 |
} |
| 108 |
|
| 109 |
public function rotate(int $degrees) : void |
| 110 |
{ |
| 111 |
if (null === $this->xmlWriter) { |
| 112 |
throw new RuntimeException('No image has been started'); |
| 113 |
} |
| 114 |
|
| 115 |
$this->xmlWriter->startElement('g'); |
| 116 |
$this->xmlWriter->writeAttribute('transform', sprintf('rotate(%d)', $degrees)); |
| 117 |
++$this->stack[$this->currentStack]; |
| 118 |
} |
| 119 |
|
| 120 |
public function push() : void |
| 121 |
{ |
| 122 |
if (null === $this->xmlWriter) { |
| 123 |
throw new RuntimeException('No image has been started'); |
| 124 |
} |
| 125 |
|
| 126 |
$this->xmlWriter->startElement('g'); |
| 127 |
$this->stack[] = 1; |
| 128 |
++$this->currentStack; |
| 129 |
} |
| 130 |
|
| 131 |
public function pop() : void |
| 132 |
{ |
| 133 |
if (null === $this->xmlWriter) { |
| 134 |
throw new RuntimeException('No image has been started'); |
| 135 |
} |
| 136 |
|
| 137 |
for ($i = 0; $i < $this->stack[$this->currentStack]; ++$i) { |
| 138 |
$this->xmlWriter->endElement(); |
| 139 |
} |
| 140 |
|
| 141 |
array_pop($this->stack); |
| 142 |
--$this->currentStack; |
| 143 |
} |
| 144 |
|
| 145 |
public function drawPathWithColor(Path $path, ColorInterface $color) : void |
| 146 |
{ |
| 147 |
if (null === $this->xmlWriter) { |
| 148 |
throw new RuntimeException('No image has been started'); |
| 149 |
} |
| 150 |
|
| 151 |
$alpha = 1; |
| 152 |
|
| 153 |
if ($color instanceof Alpha) { |
| 154 |
$alpha = $color->getAlpha() / 100; |
| 155 |
} |
| 156 |
|
| 157 |
$this->startPathElement($path); |
| 158 |
$this->xmlWriter->writeAttribute('fill', $this->getColorString($color)); |
| 159 |
|
| 160 |
if ($alpha < 1) { |
| 161 |
$this->xmlWriter->writeAttribute('fill-opacity', (string) $alpha); |
| 162 |
} |
| 163 |
|
| 164 |
$this->xmlWriter->endElement(); |
| 165 |
} |
| 166 |
|
| 167 |
public function drawPathWithGradient( |
| 168 |
Path $path, |
| 169 |
Gradient $gradient, |
| 170 |
float $x, |
| 171 |
float $y, |
| 172 |
float $width, |
| 173 |
float $height |
| 174 |
) : void { |
| 175 |
if (null === $this->xmlWriter) { |
| 176 |
throw new RuntimeException('No image has been started'); |
| 177 |
} |
| 178 |
|
| 179 |
$gradientId = $this->createGradientFill($gradient, $x, $y, $width, $height); |
| 180 |
$this->startPathElement($path); |
| 181 |
$this->xmlWriter->writeAttribute('fill', 'url(#' . $gradientId . ')'); |
| 182 |
$this->xmlWriter->endElement(); |
| 183 |
} |
| 184 |
|
| 185 |
public function done() : string |
| 186 |
{ |
| 187 |
if (null === $this->xmlWriter) { |
| 188 |
throw new RuntimeException('No image has been started'); |
| 189 |
} |
| 190 |
|
| 191 |
foreach ($this->stack as $openElements) { |
| 192 |
for ($i = $openElements; $i > 0; --$i) { |
| 193 |
$this->xmlWriter->endElement(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
$this->xmlWriter->endDocument(); |
| 198 |
$blob = $this->xmlWriter->outputMemory(true); |
| 199 |
$this->xmlWriter = null; |
| 200 |
$this->stack = null; |
| 201 |
$this->currentStack = null; |
| 202 |
$this->gradientCount = null; |
| 203 |
|
| 204 |
return $blob; |
| 205 |
} |
| 206 |
|
| 207 |
private function startPathElement(Path $path) : void |
| 208 |
{ |
| 209 |
$pathData = []; |
| 210 |
|
| 211 |
foreach ($path as $op) { |
| 212 |
switch (true) { |
| 213 |
case $op instanceof Move: |
| 214 |
$pathData[] = sprintf( |
| 215 |
'M%s %s', |
| 216 |
round($op->getX(), self::PRECISION), |
| 217 |
round($op->getY(), self::PRECISION) |
| 218 |
); |
| 219 |
break; |
| 220 |
|
| 221 |
case $op instanceof Line: |
| 222 |
$pathData[] = sprintf( |
| 223 |
'L%s %s', |
| 224 |
round($op->getX(), self::PRECISION), |
| 225 |
round($op->getY(), self::PRECISION) |
| 226 |
); |
| 227 |
break; |
| 228 |
|
| 229 |
case $op instanceof EllipticArc: |
| 230 |
$pathData[] = sprintf( |
| 231 |
'A%s %s %s %u %u %s %s', |
| 232 |
round($op->getXRadius(), self::PRECISION), |
| 233 |
round($op->getYRadius(), self::PRECISION), |
| 234 |
round($op->getXAxisAngle(), self::PRECISION), |
| 235 |
$op->isLargeArc(), |
| 236 |
$op->isSweep(), |
| 237 |
round($op->getX(), self::PRECISION), |
| 238 |
round($op->getY(), self::PRECISION) |
| 239 |
); |
| 240 |
break; |
| 241 |
|
| 242 |
case $op instanceof Curve: |
| 243 |
$pathData[] = sprintf( |
| 244 |
'C%s %s %s %s %s %s', |
| 245 |
round($op->getX1(), self::PRECISION), |
| 246 |
round($op->getY1(), self::PRECISION), |
| 247 |
round($op->getX2(), self::PRECISION), |
| 248 |
round($op->getY2(), self::PRECISION), |
| 249 |
round($op->getX3(), self::PRECISION), |
| 250 |
round($op->getY3(), self::PRECISION) |
| 251 |
); |
| 252 |
break; |
| 253 |
|
| 254 |
case $op instanceof Close: |
| 255 |
$pathData[] = 'Z'; |
| 256 |
break; |
| 257 |
|
| 258 |
default: |
| 259 |
throw new RuntimeException('Unexpected draw operation: ' . get_class($op)); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
$this->xmlWriter->startElement('path'); |
| 264 |
$this->xmlWriter->writeAttribute('fill-rule', 'evenodd'); |
| 265 |
$this->xmlWriter->writeAttribute('d', implode('', $pathData)); |
| 266 |
} |
| 267 |
|
| 268 |
private function createGradientFill(Gradient $gradient, float $x, float $y, float $width, float $height) : string |
| 269 |
{ |
| 270 |
$this->xmlWriter->startElement('defs'); |
| 271 |
|
| 272 |
$startColor = $gradient->getStartColor(); |
| 273 |
$endColor = $gradient->getEndColor(); |
| 274 |
|
| 275 |
if ($gradient->getType() === GradientType::RADIAL()) { |
| 276 |
$this->xmlWriter->startElement('radialGradient'); |
| 277 |
} else { |
| 278 |
$this->xmlWriter->startElement('linearGradient'); |
| 279 |
} |
| 280 |
|
| 281 |
$this->xmlWriter->writeAttribute('gradientUnits', 'userSpaceOnUse'); |
| 282 |
|
| 283 |
switch ($gradient->getType()) { |
| 284 |
case GradientType::HORIZONTAL(): |
| 285 |
$this->xmlWriter->writeAttribute('x1', (string) round($x, self::PRECISION)); |
| 286 |
$this->xmlWriter->writeAttribute('y1', (string) round($y, self::PRECISION)); |
| 287 |
$this->xmlWriter->writeAttribute('x2', (string) round($x + $width, self::PRECISION)); |
| 288 |
$this->xmlWriter->writeAttribute('y2', (string) round($y, self::PRECISION)); |
| 289 |
break; |
| 290 |
|
| 291 |
case GradientType::VERTICAL(): |
| 292 |
$this->xmlWriter->writeAttribute('x1', (string) round($x, self::PRECISION)); |
| 293 |
$this->xmlWriter->writeAttribute('y1', (string) round($y, self::PRECISION)); |
| 294 |
$this->xmlWriter->writeAttribute('x2', (string) round($x, self::PRECISION)); |
| 295 |
$this->xmlWriter->writeAttribute('y2', (string) round($y + $height, self::PRECISION)); |
| 296 |
break; |
| 297 |
|
| 298 |
case GradientType::DIAGONAL(): |
| 299 |
$this->xmlWriter->writeAttribute('x1', (string) round($x, self::PRECISION)); |
| 300 |
$this->xmlWriter->writeAttribute('y1', (string) round($y, self::PRECISION)); |
| 301 |
$this->xmlWriter->writeAttribute('x2', (string) round($x + $width, self::PRECISION)); |
| 302 |
$this->xmlWriter->writeAttribute('y2', (string) round($y + $height, self::PRECISION)); |
| 303 |
break; |
| 304 |
|
| 305 |
case GradientType::INVERSE_DIAGONAL(): |
| 306 |
$this->xmlWriter->writeAttribute('x1', (string) round($x, self::PRECISION)); |
| 307 |
$this->xmlWriter->writeAttribute('y1', (string) round($y + $height, self::PRECISION)); |
| 308 |
$this->xmlWriter->writeAttribute('x2', (string) round($x + $width, self::PRECISION)); |
| 309 |
$this->xmlWriter->writeAttribute('y2', (string) round($y, self::PRECISION)); |
| 310 |
break; |
| 311 |
|
| 312 |
case GradientType::RADIAL(): |
| 313 |
$this->xmlWriter->writeAttribute('cx', (string) round(($x + $width) / 2, self::PRECISION)); |
| 314 |
$this->xmlWriter->writeAttribute('cy', (string) round(($y + $height) / 2, self::PRECISION)); |
| 315 |
$this->xmlWriter->writeAttribute('r', (string) round(max($width, $height) / 2, self::PRECISION)); |
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
$toBeHashed = $this->getColorString($startColor) . $this->getColorString($endColor) . $gradient->getType(); |
| 320 |
if ($startColor instanceof Alpha) { |
| 321 |
$toBeHashed .= (string) $startColor->getAlpha(); |
| 322 |
} |
| 323 |
$id = sprintf('g%d-%s', ++$this->gradientCount, hash('xxh3', $toBeHashed)); |
| 324 |
$this->xmlWriter->writeAttribute('id', $id); |
| 325 |
|
| 326 |
$this->xmlWriter->startElement('stop'); |
| 327 |
$this->xmlWriter->writeAttribute('offset', '0%'); |
| 328 |
$this->xmlWriter->writeAttribute('stop-color', $this->getColorString($startColor)); |
| 329 |
|
| 330 |
if ($startColor instanceof Alpha) { |
| 331 |
$this->xmlWriter->writeAttribute('stop-opacity', (string) $startColor->getAlpha()); |
| 332 |
} |
| 333 |
|
| 334 |
$this->xmlWriter->endElement(); |
| 335 |
|
| 336 |
$this->xmlWriter->startElement('stop'); |
| 337 |
$this->xmlWriter->writeAttribute('offset', '100%'); |
| 338 |
$this->xmlWriter->writeAttribute('stop-color', $this->getColorString($endColor)); |
| 339 |
|
| 340 |
if ($endColor instanceof Alpha) { |
| 341 |
$this->xmlWriter->writeAttribute('stop-opacity', (string) $endColor->getAlpha()); |
| 342 |
} |
| 343 |
|
| 344 |
$this->xmlWriter->endElement(); |
| 345 |
|
| 346 |
$this->xmlWriter->endElement(); |
| 347 |
$this->xmlWriter->endElement(); |
| 348 |
|
| 349 |
return $id; |
| 350 |
} |
| 351 |
|
| 352 |
private function getColorString(ColorInterface $color) : string |
| 353 |
{ |
| 354 |
$color = $color->toRgb(); |
| 355 |
|
| 356 |
return sprintf( |
| 357 |
'#%02x%02x%02x', |
| 358 |
$color->getRed(), |
| 359 |
$color->getGreen(), |
| 360 |
$color->getBlue() |
| 361 |
); |
| 362 |
} |
| 363 |
} |