Skip to content

Commit 622a0c7

Browse files
committed
Add tests for fractional width in SVGs
1 parent 7e91ca8 commit 622a0c7

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

generate-verified-files.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function getSaveFilename($value) {
88

99
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
1010
file_put_contents('tests/verified-files/081231723897-ean13.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13));
11+
file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75));
1112

1213
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
1314
file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128));

src/BarcodeGeneratorSVG.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ class BarcodeGeneratorSVG extends BarcodeGenerator
1010
* @param $barcode (string) code to print
1111
* @param BarcodeGenerator::Type_* $type (string) type of barcode
1212
* @param $widthFactor (float) Minimum width of a single bar in user units.
13-
* @param $height (int) Height of barcode in user units.
13+
* @param $height (float) Height of barcode in user units.
1414
* @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent).
1515
* @return string SVG code.
1616
* @public
1717
*/
18-
public function getBarcode(string $barcode, $type, float $widthFactor = 2, int $height = 30, string $foregroundColor = 'black')
18+
public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black')
1919
{
2020
$barcodeData = $this->getBarcodeData($barcode, $type);
2121

2222
// replace table for special characters
23-
$repstr = ["\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;'];
23+
$repstr = [
24+
"\0" => '',
25+
'&' => '&amp;',
26+
'<' => '&lt;',
27+
'>' => '&gt;',
28+
];
2429

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

@@ -45,6 +50,7 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, int $
4550

4651
$positionHorizontal += $barWidth;
4752
}
53+
4854
$svg .= "\t</g>" . PHP_EOL;
4955
$svg .= '</svg>' . PHP_EOL;
5056

tests/BarcodeSvgTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public function test_svg_barcode_generator_can_generate_ean_13_barcode()
1111

1212
$this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated);
1313
}
14+
15+
public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width()
16+
{
17+
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
18+
$generated = $generator->getBarcode('081231723897', $generator::TYPE_EAN_13, 0.25, 25.75);
19+
20+
$this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated);
21+
}
1422
}
Lines changed: 37 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)