Skip to content

Commit 8fdf289

Browse files
committed
Add a RendererInterface
1 parent 5fd1162 commit 8fdf289

52 files changed

Lines changed: 308 additions & 291 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

generate-verified-files.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75));
1616

1717
$svgRendererRed = new Picqer\Barcode\Renderers\SvgRenderer();
18-
$svgRendererRed->setBackgroundColor('red');
18+
$svgRendererRed->setBackgroundColor([255, 0, 0]);
1919
file_put_contents('tests/verified-files/081231723897-ean13-red-background.svg', $svgRendererRed->render($barcode, $barcode->getWidth() * 2));
2020

2121
$barcode = $typeEncoderCode128->getBarcode('081231723897');
2222
file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2));
2323
$htmlRendererRed = new Picqer\Barcode\Renderers\HtmlRenderer();
24-
$htmlRendererRed->setBackgroundColor('red');
24+
$htmlRendererRed->setBackgroundColor([255, 0, 0]);
2525
file_put_contents('tests/verified-files/081231723897-code128-red-background.html', $htmlRendererRed->render($barcode, $barcode->getWidth() * 2));
2626

2727
$barcode = $typeEncoderIMB->getBarcode('12345678903');

src/Renderers/DynamicHtmlRenderer.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
use Picqer\Barcode\Barcode;
66
use Picqer\Barcode\BarcodeBar;
77

8-
class DynamicHtmlRenderer
8+
class DynamicHtmlRenderer implements RendererInterface
99
{
1010
protected const WIDTH_PRECISION = 6;
1111

12-
protected string $foregroundColor = 'black';
13-
protected ?string $backgroundColor = null;
12+
protected array $foregroundColor = [0, 0, 0];
13+
protected ?array $backgroundColor = null;
1414

15-
public function render(Barcode $barcode): string
15+
// Width and height are ignored in this renderer
16+
public function render(Barcode $barcode, float $width = 200, float $height = 30): string
1617
{
17-
$html = '<div style="font-size:0;position:relative;width:100%;height:100%' . ($this->backgroundColor ? ';background-color:' . $this->backgroundColor : '') . '">' . PHP_EOL;
18+
$html = '<div style="font-size:0;position:relative;width:100%;height:100%' . ($this->backgroundColor ? ';background-color:rgb(' . implode(',', $this->backgroundColor) . ')' : '') . '">' . PHP_EOL;
1819

1920
$positionHorizontal = 0;
2021
/** @var BarcodeBar $bar */
@@ -26,7 +27,7 @@ public function render(Barcode $barcode): string
2627
$positionVertical = round(($bar->getPositionVertical() / $barcode->getHeight() * 100), 3);
2728

2829
// draw a vertical bar
29-
$html .= '<div style="background-color:' . $this->foregroundColor . ';width:' . round($barWidth, self::WIDTH_PRECISION) . '%;height:' . $barHeight . '%;position:absolute;left:' . round($positionHorizontal, self::WIDTH_PRECISION) . '%;top:' . $positionVertical . (($positionVertical > 0) ? '%' : '') . '">&nbsp;</div>' . PHP_EOL;
30+
$html .= '<div style="background-color:rgb(' . implode(',', $this->foregroundColor) . ');width:' . round($barWidth, self::WIDTH_PRECISION) . '%;height:' . $barHeight . '%;position:absolute;left:' . round($positionHorizontal, self::WIDTH_PRECISION) . '%;top:' . $positionVertical . (($positionVertical > 0) ? '%' : '') . '">&nbsp;</div>' . PHP_EOL;
3031
}
3132

3233
$positionHorizontal += $barWidth;
@@ -37,15 +38,13 @@ public function render(Barcode $barcode): string
3738
return $html;
3839
}
3940

40-
// Use HTML color definitions, like 'red' or '#ff0000'
41-
public function setForegroundColor(string $color): self
41+
public function setForegroundColor(array $color): self
4242
{
4343
$this->foregroundColor = $color;
4444
return $this;
4545
}
4646

47-
// Use HTML color definitions, like 'red' or '#ff0000'
48-
public function setBackgroundColor(?string $color): self
47+
public function setBackgroundColor(?array $color): self
4948
{
5049
$this->backgroundColor = $color;
5150
return $this;

src/Renderers/HtmlRenderer.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
use Picqer\Barcode\Barcode;
66
use Picqer\Barcode\BarcodeBar;
77

8-
class HtmlRenderer
8+
class HtmlRenderer implements RendererInterface
99
{
10-
protected string $foregroundColor = 'black';
11-
protected ?string $backgroundColor = null;
10+
protected array $foregroundColor = [0, 0, 0];
11+
protected ?array $backgroundColor = null;
1212

1313
public function render(Barcode $barcode, float $width = 200, float $height = 30): string
1414
{
1515
$widthFactor = $width / $barcode->getWidth();
1616

17-
$html = '<div style="font-size:0;position:relative;width:' . $width . 'px;height:' . ($height) . 'px;' . ($this->backgroundColor ? 'background-color:' . $this->backgroundColor . ';' : '') . '">' . PHP_EOL;
17+
$html = '<div style="font-size:0;position:relative;width:' . $width . 'px;height:' . ($height) . 'px;' . ($this->backgroundColor ? 'background-color:rgb(' . implode(',', $this->backgroundColor) . ');' : '') . '">' . PHP_EOL;
1818

1919
$positionHorizontal = 0;
2020
/** @var BarcodeBar $bar */
@@ -26,7 +26,7 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30)
2626
$positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3);
2727

2828
// draw a vertical bar
29-
$html .= '<div style="background-color:' . $this->foregroundColor . ';width:' . $barWidth . 'px;height:' . $barHeight . 'px;position:absolute;left:' . $positionHorizontal . 'px;top:' . $positionVertical . (($positionVertical > 0) ? 'px' : '') . '">&nbsp;</div>' . PHP_EOL;
29+
$html .= '<div style="background-color:rgb(' . implode(',', $this->foregroundColor) . ');width:' . $barWidth . 'px;height:' . $barHeight . 'px;position:absolute;left:' . $positionHorizontal . 'px;top:' . $positionVertical . (($positionVertical > 0) ? 'px' : '') . '">&nbsp;</div>' . PHP_EOL;
3030
}
3131

3232
$positionHorizontal += $barWidth;
@@ -37,15 +37,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30)
3737
return $html;
3838
}
3939

40-
// Use HTML color definitions, like 'red' or '#ff0000'
41-
public function setForegroundColor(string $color): self
40+
public function setForegroundColor(array $color): self
4241
{
4342
$this->foregroundColor = $color;
4443
return $this;
4544
}
4645

47-
// Use HTML color definitions, like 'red' or '#ff0000'
48-
public function setBackgroundColor(?string $color): self
46+
public function setBackgroundColor(?array $color): self
4947
{
5048
$this->backgroundColor = $color;
5149
return $this;

src/Renderers/PngRenderer.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Picqer\Barcode\BarcodeBar;
1010
use Picqer\Barcode\Exceptions\BarcodeException;
1111

12-
class PngRenderer
12+
class PngRenderer implements RendererInterface
1313
{
1414
protected array $foregroundColor = [0, 0, 0];
1515
protected ?array $backgroundColor = null;
@@ -49,9 +49,15 @@ public function useGd(): self
4949
return $this;
5050
}
5151

52-
public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string
52+
// Floats in width and height will be rounded to integers
53+
// For best (and valid) result, use a width as a factor of the width of the Barcode object
54+
// Example: $width = $barcode->getWidth() * 3
55+
public function render(Barcode $barcode, float $width = 200, float $height = 30): string
5356
{
54-
$width = (int)round($barcode->getWidth() * $widthFactor);
57+
$width = round($width);
58+
$height = round($height);
59+
60+
$widthFactor = $width / $barcode->getWidth();
5561

5662
if ($this->useImagick) {
5763
$image = $this->createImagickImageObject($width, $height);
@@ -66,17 +72,17 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30)
6672
$positionHorizontal = 0;
6773
/** @var BarcodeBar $bar */
6874
foreach ($barcode->getBars() as $bar) {
69-
$barWidth = (int)round(($bar->getWidth() * $widthFactor));
75+
$barWidth = $bar->getWidth() * $widthFactor;
7076

7177
if ($bar->isBar() && $barWidth > 0) {
7278
$y = (int)round(($bar->getPositionVertical() * $height / $barcode->getHeight()));
7379
$barHeight = (int)round(($bar->getHeight() * $height / $barcode->getHeight()));
7480

7581
// draw a vertical bar
7682
if ($this->useImagick) {
77-
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
83+
$imagickBarsShape->rectangle(round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight));
7884
} else {
79-
\imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
85+
\imagefilledrectangle($image, round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
8086
}
8187
}
8288
$positionHorizontal += $barWidth;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Renderers;
4+
5+
use Picqer\Barcode\Barcode;
6+
7+
interface RendererInterface
8+
{
9+
public function render(Barcode $barcode, float $width = 200, float $height = 30): string;
10+
11+
public function setForegroundColor(array $color): self;
12+
13+
public function setBackgroundColor(?array $color): self;
14+
}

src/Renderers/SvgRenderer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Picqer\Barcode\BarcodeBar;
77
use Picqer\Barcode\Exceptions\InvalidOptionException;
88

9-
class SvgRenderer
9+
class SvgRenderer implements RendererInterface
1010
{
11-
protected string $foregroundColor = 'black';
12-
protected ?string $backgroundColor = null;
11+
protected array $foregroundColor = [0, 0, 0];
12+
protected ?array $backgroundColor = null;
1313
protected string $svgType = self::TYPE_SVG_STANDALONE;
1414

1515
public const TYPE_SVG_STANDALONE = 'standalone';
@@ -29,10 +29,10 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30)
2929

3030
// Add background rectangle if backgroundColor is set
3131
if ($this->backgroundColor !== null) {
32-
$svg .= "\t" . '<rect id="background" width="100%" height="100%" fill="' . $this->backgroundColor . '"/>' . PHP_EOL;
32+
$svg .= "\t" . '<rect id="background" width="100%" height="100%" fill="rgb(' . implode(',', $this->backgroundColor) . ')"/>' . PHP_EOL;
3333
}
3434

35-
$svg .= "\t" . '<g id="bars" fill="' . $this->foregroundColor . '" stroke="none">' . PHP_EOL;
35+
$svg .= "\t" . '<g id="bars" fill="rgb(' . implode(',', $this->foregroundColor) . ')" stroke="none">' . PHP_EOL;
3636

3737
// print bars
3838
$positionHorizontal = 0;
@@ -56,13 +56,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30)
5656
return $svg;
5757
}
5858

59-
public function setForegroundColor(string $color): self
59+
public function setForegroundColor(array $color): self
6060
{
6161
$this->foregroundColor = $color;
6262
return $this;
6363
}
6464

65-
public function setBackgroundColor(?string $color): self
65+
public function setBackgroundColor(?array $color): self
6666
{
6767
$this->backgroundColor = $color;
6868
return $this;

tests/verified-files/0049000004632-ean13.svg

Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
<div style="font-size:0;position:relative;width:202px;height:30px;background-color:red;">
2-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:0px;top:0">&nbsp;</div>
3-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:6px;top:0">&nbsp;</div>
4-
<div style="background-color:black;width:6px;height:30px;position:absolute;left:12px;top:0">&nbsp;</div>
5-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:22px;top:0">&nbsp;</div>
6-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:30px;top:0">&nbsp;</div>
7-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:38px;top:0">&nbsp;</div>
8-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:44px;top:0">&nbsp;</div>
9-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:48px;top:0">&nbsp;</div>
10-
<div style="background-color:black;width:6px;height:30px;position:absolute;left:56px;top:0">&nbsp;</div>
11-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:66px;top:0">&nbsp;</div>
12-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:72px;top:0">&nbsp;</div>
13-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:82px;top:0">&nbsp;</div>
14-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:88px;top:0">&nbsp;</div>
15-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:94px;top:0">&nbsp;</div>
16-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:106px;top:0">&nbsp;</div>
17-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:110px;top:0">&nbsp;</div>
18-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:118px;top:0">&nbsp;</div>
19-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:128px;top:0">&nbsp;</div>
20-
<div style="background-color:black;width:8px;height:30px;position:absolute;left:132px;top:0">&nbsp;</div>
21-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:142px;top:0">&nbsp;</div>
22-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:146px;top:0">&nbsp;</div>
23-
<div style="background-color:black;width:6px;height:30px;position:absolute;left:154px;top:0">&nbsp;</div>
24-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:162px;top:0">&nbsp;</div>
25-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:166px;top:0">&nbsp;</div>
26-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:176px;top:0">&nbsp;</div>
27-
<div style="background-color:black;width:6px;height:30px;position:absolute;left:186px;top:0">&nbsp;</div>
28-
<div style="background-color:black;width:2px;height:30px;position:absolute;left:194px;top:0">&nbsp;</div>
29-
<div style="background-color:black;width:4px;height:30px;position:absolute;left:198px;top:0">&nbsp;</div>
1+
<div style="font-size:0;position:relative;width:202px;height:30px;background-color:rgb(255,0,0);">
2+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:0px;top:0">&nbsp;</div>
3+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:6px;top:0">&nbsp;</div>
4+
<div style="background-color:rgb(0,0,0);width:6px;height:30px;position:absolute;left:12px;top:0">&nbsp;</div>
5+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:22px;top:0">&nbsp;</div>
6+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:30px;top:0">&nbsp;</div>
7+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:38px;top:0">&nbsp;</div>
8+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:44px;top:0">&nbsp;</div>
9+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:48px;top:0">&nbsp;</div>
10+
<div style="background-color:rgb(0,0,0);width:6px;height:30px;position:absolute;left:56px;top:0">&nbsp;</div>
11+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:66px;top:0">&nbsp;</div>
12+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:72px;top:0">&nbsp;</div>
13+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:82px;top:0">&nbsp;</div>
14+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:88px;top:0">&nbsp;</div>
15+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:94px;top:0">&nbsp;</div>
16+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:106px;top:0">&nbsp;</div>
17+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:110px;top:0">&nbsp;</div>
18+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:118px;top:0">&nbsp;</div>
19+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:128px;top:0">&nbsp;</div>
20+
<div style="background-color:rgb(0,0,0);width:8px;height:30px;position:absolute;left:132px;top:0">&nbsp;</div>
21+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:142px;top:0">&nbsp;</div>
22+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:146px;top:0">&nbsp;</div>
23+
<div style="background-color:rgb(0,0,0);width:6px;height:30px;position:absolute;left:154px;top:0">&nbsp;</div>
24+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:162px;top:0">&nbsp;</div>
25+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:166px;top:0">&nbsp;</div>
26+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:176px;top:0">&nbsp;</div>
27+
<div style="background-color:rgb(0,0,0);width:6px;height:30px;position:absolute;left:186px;top:0">&nbsp;</div>
28+
<div style="background-color:rgb(0,0,0);width:2px;height:30px;position:absolute;left:194px;top:0">&nbsp;</div>
29+
<div style="background-color:rgb(0,0,0);width:4px;height:30px;position:absolute;left:198px;top:0">&nbsp;</div>
3030
</div>

0 commit comments

Comments
 (0)