Skip to content

Commit 1c4bc99

Browse files
committed
New renderer style
1 parent 132e5eb commit 1c4bc99

33 files changed

Lines changed: 137 additions & 84 deletions

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php-versions: ['8.1', '8.2', '8.3']
15+
php-versions: ['8.2', '8.3']
1616

1717
steps:
1818
- name: Checkout code

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^8.1",
21+
"php": "^8.2",
2222
"ext-mbstring": "*"
2323
},
2424
"require-dev": {

src/Barcode.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class Barcode
66
{
7-
protected $barcode;
8-
protected $width = 0;
9-
protected $height = 0;
10-
protected $bars = [];
7+
protected string $barcode;
8+
protected int $width = 0;
9+
protected int $height = 0;
10+
protected array $bars = [];
1111

1212
public function __construct(string $barcode)
1313
{
@@ -40,4 +40,4 @@ public function getBars(): array
4040
{
4141
return $this->bars;
4242
}
43-
}
43+
}

src/BarcodeBar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class BarcodeBar
66
{
7-
protected $width;
8-
protected $height;
9-
protected $positionVertical;
10-
protected $type;
7+
protected int $width;
8+
protected int $height;
9+
protected int $positionVertical;
10+
protected int $type;
1111

1212
const TYPE_BAR = 1;
1313
const TYPE_SPACING = 0;
@@ -39,4 +39,4 @@ public function isBar(): bool
3939
{
4040
return $this->type === self::TYPE_BAR;
4141
}
42-
}
42+
}

src/BarcodeGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected function getBarcodeData(string $code, string $type): Barcode
105105
{
106106
$barcodeDataBuilder = $this->createDataBuilderForType($type);
107107

108-
return $barcodeDataBuilder->getBarcodeData($code);
108+
return $barcodeDataBuilder->getBarcode($code);
109109
}
110110

111111
protected function createDataBuilderForType(string $type)

src/BarcodeGeneratorSVG.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,11 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float
1919
{
2020
$barcodeData = $this->getBarcodeData($barcode, $type);
2121

22-
// replace table for special characters
23-
$repstr = [
24-
"\0" => '',
25-
'&' => '&',
26-
'<' => '&lt;',
27-
'>' => '&gt;',
28-
];
29-
3022
$width = round(($barcodeData->getWidth() * $widthFactor), 3);
3123

32-
$svg = '<?xml version="1.0" standalone="no" ?>' . PHP_EOL;
33-
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . PHP_EOL;
34-
$svg .= '<svg width="' . $width . '" height="' . $height . '" viewBox="0 0 ' . $width . ' ' . $height . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . PHP_EOL;
35-
$svg .= "\t" . '<desc>' . strtr($barcodeData->getBarcode(), $repstr) . '</desc>' . PHP_EOL;
36-
$svg .= "\t" . '<g id="bars" fill="' . $foregroundColor . '" stroke="none">' . PHP_EOL;
37-
38-
// print bars
39-
$positionHorizontal = 0;
40-
/** @var BarcodeBar $bar */
41-
foreach ($barcodeData->getBars() as $bar) {
42-
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
43-
$barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
44-
45-
if ($bar->isBar() && $barWidth > 0) {
46-
$positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3);
47-
// draw a vertical bar
48-
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . PHP_EOL;
49-
}
50-
51-
$positionHorizontal += $barWidth;
52-
}
53-
54-
$svg .= "\t</g>" . PHP_EOL;
55-
$svg .= '</svg>' . PHP_EOL;
24+
$renderer = new \Picqer\Barcode\Renderers\SvgRenderer();
25+
$renderer->setForegroundColor($foregroundColor);
5626

57-
return $svg;
27+
return $renderer->render($barcodeData, $width, $height);
5828
}
5929
}

src/Renderers/SvgRenderer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Renderers;
4+
5+
use Picqer\Barcode\Barcode;
6+
use Picqer\Barcode\BarcodeBar;
7+
8+
class SvgRenderer
9+
{
10+
protected string $foregroundColor = 'black';
11+
12+
public function render(Barcode $barcode, float $width = 200, float $height = 30): string
13+
{
14+
// replace table for special characters
15+
$repstr = [
16+
"\0" => '',
17+
'&' => '&amp;',
18+
'<' => '&lt;',
19+
'>' => '&gt;',
20+
];
21+
22+
// $width = round(($barcode->getWidth() * $widthFactor), 3);
23+
$widthFactor = $width / $barcode->getWidth();
24+
25+
$svg = '<?xml version="1.0" standalone="no" ?>' . PHP_EOL;
26+
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . PHP_EOL;
27+
$svg .= '<svg width="' . $width . '" height="' . $height . '" viewBox="0 0 ' . $width . ' ' . $height . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . PHP_EOL;
28+
$svg .= "\t" . '<desc>' . strtr($barcode->getBarcode(), $repstr) . '</desc>' . PHP_EOL;
29+
$svg .= "\t" . '<g id="bars" fill="' . $this->foregroundColor . '" stroke="none">' . PHP_EOL;
30+
31+
// print bars
32+
$positionHorizontal = 0;
33+
/** @var BarcodeBar $bar */
34+
foreach ($barcode->getBars() as $bar) {
35+
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
36+
$barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3);
37+
38+
if ($bar->isBar() && $barWidth > 0) {
39+
$positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3);
40+
// draw a vertical bar
41+
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . PHP_EOL;
42+
}
43+
44+
$positionHorizontal += $barWidth;
45+
}
46+
47+
$svg .= "\t</g>" . PHP_EOL;
48+
$svg .= '</svg>' . PHP_EOL;
49+
50+
return $svg;
51+
}
52+
53+
public function setForegroundColor(string $color)
54+
{
55+
$this->foregroundColor = $color;
56+
}
57+
}

src/Types/TypeCodabar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TypeCodabar implements TypeInterface
3636
'D' => '11122211'
3737
];
3838

39-
public function getBarcodeData(string $code): Barcode
39+
public function getBarcode(string $code): Barcode
4040
{
4141
$barcode = new Barcode($code);
4242

src/Types/TypeCode11.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TypeCode11 implements TypeInterface
2828
'S' => '112211',
2929
];
3030

31-
public function getBarcodeData(string $code): Barcode
31+
public function getBarcode(string $code): Barcode
3232
{
3333
$barcode = new Barcode($code);
3434

src/Types/TypeCode128.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TypeCode128 implements TypeInterface
131131
'200000' /* END */
132132
];
133133

134-
public function getBarcodeData(string $code): Barcode
134+
public function getBarcode(string $code): Barcode
135135
{
136136
if (strlen(trim($code)) === 0) {
137137
throw new InvalidLengthException('You should provide a barcode string.');

0 commit comments

Comments
 (0)