| 1 |
<?php |
| 2 |
|
| 3 |
namespace WCPOS\Vendor\Picqer\Barcode; |
| 4 |
|
| 5 |
class BarcodeGeneratorSVG extends BarcodeGenerator |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Return a SVG string representation of barcode. |
| 9 |
* |
| 10 |
* @param $barcode (string) code to print |
| 11 |
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode |
| 12 |
* @param $widthFactor (float) Minimum width of a single bar in user units. |
| 13 |
* @param $height (float) Height of barcode in user units. |
| 14 |
* @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). |
| 15 |
* @return string SVG code. |
| 16 |
* @public |
| 17 |
*/ |
| 18 |
public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black') : string |
| 19 |
{ |
| 20 |
$barcodeData = $this->getBarcodeData($barcode, $type); |
| 21 |
// replace table for special characters |
| 22 |
$repstr = ["\x00" => '', '&' => '&', '<' => '<', '>' => '>']; |
| 23 |
$width = \round($barcodeData->getWidth() * $widthFactor, 3); |
| 24 |
$svg = '<?xml version="1.0" standalone="no" ?>' . \PHP_EOL; |
| 25 |
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . \PHP_EOL; |
| 26 |
$svg .= '<svg width="' . $width . '" height="' . $height . '" viewBox="0 0 ' . $width . ' ' . $height . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . \PHP_EOL; |
| 27 |
$svg .= "\t" . '<desc>' . \strtr($barcodeData->getBarcode(), $repstr) . '</desc>' . \PHP_EOL; |
| 28 |
$svg .= "\t" . '<g id="bars" fill="' . $foregroundColor . '" stroke="none">' . \PHP_EOL; |
| 29 |
// print bars |
| 30 |
$positionHorizontal = 0; |
| 31 |
/** @var BarcodeBar $bar */ |
| 32 |
foreach ($barcodeData->getBars() as $bar) { |
| 33 |
$barWidth = \round($bar->getWidth() * $widthFactor, 3); |
| 34 |
$barHeight = \round($bar->getHeight() * $height / $barcodeData->getHeight(), 3); |
| 35 |
if ($bar->isBar() && $barWidth > 0) { |
| 36 |
$positionVertical = \round($bar->getPositionVertical() * $height / $barcodeData->getHeight(), 3); |
| 37 |
// draw a vertical bar |
| 38 |
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . \PHP_EOL; |
| 39 |
} |
| 40 |
$positionHorizontal += $barWidth; |
| 41 |
} |
| 42 |
$svg .= "\t</g>" . \PHP_EOL; |
| 43 |
$svg .= '</svg>' . \PHP_EOL; |
| 44 |
return $svg; |
| 45 |
} |
| 46 |
} |
| 47 |
|