Skip to content

Commit

Permalink
New renderer style
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbakker committed Mar 31, 2024
1 parent 132e5eb commit 1c4bc99
Show file tree
Hide file tree
Showing 33 changed files with 137 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.2', '8.3']

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
10 changes: 5 additions & 5 deletions src/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class Barcode
{
protected $barcode;
protected $width = 0;
protected $height = 0;
protected $bars = [];
protected string $barcode;
protected int $width = 0;
protected int $height = 0;
protected array $bars = [];

public function __construct(string $barcode)
{
Expand Down Expand Up @@ -40,4 +40,4 @@ public function getBars(): array
{
return $this->bars;
}
}
}
10 changes: 5 additions & 5 deletions src/BarcodeBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class BarcodeBar
{
protected $width;
protected $height;
protected $positionVertical;
protected $type;
protected int $width;
protected int $height;
protected int $positionVertical;
protected int $type;

const TYPE_BAR = 1;
const TYPE_SPACING = 0;
Expand Down Expand Up @@ -39,4 +39,4 @@ public function isBar(): bool
{
return $this->type === self::TYPE_BAR;
}
}
}
2 changes: 1 addition & 1 deletion src/BarcodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function getBarcodeData(string $code, string $type): Barcode
{
$barcodeDataBuilder = $this->createDataBuilderForType($type);

return $barcodeDataBuilder->getBarcodeData($code);
return $barcodeDataBuilder->getBarcode($code);
}

protected function createDataBuilderForType(string $type)
Expand Down
36 changes: 3 additions & 33 deletions src/BarcodeGeneratorSVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,11 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float
{
$barcodeData = $this->getBarcodeData($barcode, $type);

// replace table for special characters
$repstr = [
"\0" => '',
'&' => '&',
'<' => '&lt;',
'>' => '&gt;',
];

$width = round(($barcodeData->getWidth() * $widthFactor), 3);

$svg = '<?xml version="1.0" standalone="no" ?>' . PHP_EOL;
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . PHP_EOL;
$svg .= '<svg width="' . $width . '" height="' . $height . '" viewBox="0 0 ' . $width . ' ' . $height . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . PHP_EOL;
$svg .= "\t" . '<desc>' . strtr($barcodeData->getBarcode(), $repstr) . '</desc>' . PHP_EOL;
$svg .= "\t" . '<g id="bars" fill="' . $foregroundColor . '" stroke="none">' . PHP_EOL;

// print bars
$positionHorizontal = 0;
/** @var BarcodeBar $bar */
foreach ($barcodeData->getBars() as $bar) {
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
$barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);

if ($bar->isBar() && $barWidth > 0) {
$positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3);
// draw a vertical bar
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . PHP_EOL;
}

$positionHorizontal += $barWidth;
}

$svg .= "\t</g>" . PHP_EOL;
$svg .= '</svg>' . PHP_EOL;
$renderer = new \Picqer\Barcode\Renderers\SvgRenderer();
$renderer->setForegroundColor($foregroundColor);

return $svg;
return $renderer->render($barcodeData, $width, $height);
}
}
57 changes: 57 additions & 0 deletions src/Renderers/SvgRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Picqer\Barcode\Renderers;

use Picqer\Barcode\Barcode;
use Picqer\Barcode\BarcodeBar;

class SvgRenderer
{
protected string $foregroundColor = 'black';

public function render(Barcode $barcode, float $width = 200, float $height = 30): string
{
// replace table for special characters
$repstr = [
"\0" => '',
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
];

// $width = round(($barcode->getWidth() * $widthFactor), 3);
$widthFactor = $width / $barcode->getWidth();

$svg = '<?xml version="1.0" standalone="no" ?>' . PHP_EOL;
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . PHP_EOL;
$svg .= '<svg width="' . $width . '" height="' . $height . '" viewBox="0 0 ' . $width . ' ' . $height . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . PHP_EOL;
$svg .= "\t" . '<desc>' . strtr($barcode->getBarcode(), $repstr) . '</desc>' . PHP_EOL;
$svg .= "\t" . '<g id="bars" fill="' . $this->foregroundColor . '" stroke="none">' . PHP_EOL;

// print bars
$positionHorizontal = 0;
/** @var BarcodeBar $bar */
foreach ($barcode->getBars() as $bar) {
$barWidth = round(($bar->getWidth() * $widthFactor), 3);
$barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3);

if ($bar->isBar() && $barWidth > 0) {
$positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3);
// draw a vertical bar
$svg .= "\t\t" . '<rect x="' . $positionHorizontal . '" y="' . $positionVertical . '" width="' . $barWidth . '" height="' . $barHeight . '" />' . PHP_EOL;
}

$positionHorizontal += $barWidth;
}

$svg .= "\t</g>" . PHP_EOL;
$svg .= '</svg>' . PHP_EOL;

return $svg;
}

public function setForegroundColor(string $color)
{
$this->foregroundColor = $color;
}
}
2 changes: 1 addition & 1 deletion src/Types/TypeCodabar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TypeCodabar implements TypeInterface
'D' => '11122211'
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$barcode = new Barcode($code);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode11.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TypeCode11 implements TypeInterface
'S' => '112211',
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$barcode = new Barcode($code);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode128.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class TypeCode128 implements TypeInterface
'200000' /* END */
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
if (strlen(trim($code)) === 0) {
throw new InvalidLengthException('You should provide a barcode string.');
Expand Down
4 changes: 2 additions & 2 deletions src/Types/TypeCode32.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TypeCode32 extends TypeCode39
'31' => 'Z'
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
// Validate code 32.
$stringLength = strlen($code);
Expand Down Expand Up @@ -85,7 +85,7 @@ public function getBarcodeData(string $code): Barcode
}

// Return barcode data for code 39.
return parent::getBarcodeData($code39);
return parent::getBarcode($code39);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode39.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TypeCode39 implements TypeInterface
'*' => '131131311',
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
if (strlen(trim($code)) === 0) {
throw new InvalidLengthException('You should provide a barcode string.');
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode93.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TypeCode93 implements TypeInterface
42 => '111141', // start-stop
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$encode = [
chr(0) => 'bU',
Expand Down
6 changes: 3 additions & 3 deletions src/Types/TypeEan13.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TypeEan13 extends TypeEanUpcBase
{
protected $length = 13;
protected $upca = false;
protected $upce = false;
protected int $length = 13;
protected bool $upca = false;
protected bool $upce = false;
}
6 changes: 3 additions & 3 deletions src/Types/TypeEan8.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TypeEan8 extends TypeEanUpcBase
{
protected $length = 8;
protected $upca = false;
protected $upce = false;
protected int $length = 8;
protected bool $upca = false;
protected bool $upce = false;
}
10 changes: 5 additions & 5 deletions src/Types/TypeEanUpcBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

abstract class TypeEanUpcBase implements TypeInterface
{
protected $length = 13;
protected $upca = false;
protected $upce = false;
protected int $length = 13;
protected bool $upca = false;
protected bool $upce = false;

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
if (strlen(trim($code)) === 0) {
throw new InvalidLengthException('You should provide a barcode string.');
Expand Down Expand Up @@ -206,7 +206,7 @@ public function getBarcodeData(string $code): Barcode
return $barcode;
}

protected function calculateChecksumDigit(string $code)
protected function calculateChecksumDigit(string $code): int
{
// calculate check digit
$sum_a = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeITF14.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TypeITF14 implements TypeInterface
* @throws InvalidLengthException
* @throws InvalidCharacterException
*/
public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$chr = [];
$chr['0'] = '11221';
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeIntelligentMailBarcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

class TypeIntelligentMailBarcode implements TypeInterface
{
public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$asc_chr = [
4,
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface TypeInterface
{
public function getBarcodeData(string $code): Barcode;
public function getBarcode(string $code): Barcode;
}
2 changes: 1 addition & 1 deletion src/Types/TypeInterleaved25Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TypeInterleaved25Checksum implements TypeInterface
{
public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$chr = [];
$chr['0'] = '11221';
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeMsiChecksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TypeMsiChecksum implements TypeInterface
{
protected $checksum = true;

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$chr['0'] = '100100100100';
$chr['1'] = '100100100110';
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypePharmacode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class TypePharmacode implements TypeInterface
{
public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$code = intval($code);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypePharmacodeTwoCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class TypePharmacodeTwoCode implements TypeInterface
{
public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$code = intval($code);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypePostnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TypePostnet implements TypeInterface
9 => [2, 1, 2, 1, 1]
];

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$code = str_replace(['-', ' '], '', $code);
$len = strlen($code);
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeRms4cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TypeRms4cc implements TypeInterface
{
protected $kix = false;

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
// bar mode
// 1 = pos 1, length 2
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeStandard2of5.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TypeStandard2of5 implements TypeInterface
{
protected $checksum = false;

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
$chr['0'] = '10101110111010';
$chr['1'] = '11101010101110';
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeTelepen.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct($m = 'alpha')
$this->createTelepenConversionTable();
}

public function getBarcodeData(string $code): Barcode
public function getBarcode(string $code): Barcode
{
/* The stream we get from the telepen output gives us the
* width of alternating black/white stripes
Expand Down
6 changes: 3 additions & 3 deletions src/Types/TypeUpcA.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class TypeUpcA extends TypeEanUpcBase
{
protected $length = 12;
protected $upca = true;
protected $upce = false;
protected int $length = 12;
protected bool $upca = true;
protected bool $upce = false;
}
Loading

0 comments on commit 1c4bc99

Please sign in to comment.