1<?php
2
3namespace Mpdf\Barcode;
4
5/**
6 * CODABAR barcodes.
7 * Older code often used in library systems, sometimes in blood banks
8 */
9class Codabar extends \Mpdf\Barcode\AbstractBarcode implements \Mpdf\Barcode\BarcodeInterface
10{
11
12	/**
13	 * @param string $code
14	 * @param float $printRatio
15	 */
16	public function __construct($code, $printRatio)
17	{
18		$this->init($code, $printRatio);
19
20		$this->data['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
21		$this->data['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
22		$this->data['lightmL'] = 10; // LEFT light margin =  x X-dim (spec.)
23		$this->data['lightmR'] = 10; // RIGHT light margin =  x X-dim (spec.)
24		$this->data['lightTB'] = 0; // TOP/BOTTOM light margin =  x X-dim (non-spec.)
25	}
26
27	/**
28	 * @param string $code
29	 * @param float $printRatio
30	 */
31	private function init($code, $printRatio)
32	{
33		$chr = [
34			'0' => '11111221',
35			'1' => '11112211',
36			'2' => '11121121',
37			'3' => '22111111',
38			'4' => '11211211',
39			'5' => '21111211',
40			'6' => '12111121',
41			'7' => '12112111',
42			'8' => '12211111',
43			'9' => '21121111',
44			'-' => '11122111',
45			'$' => '11221111',
46			':' => '21112121',
47			'/' => '21211121',
48			'.' => '21212111',
49			'+' => '11222221',
50			'A' => '11221211',
51			'B' => '12121121',
52			'C' => '11121221',
53			'D' => '11122211'
54		];
55
56		$bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []];
57		$k = 0;
58
59		$code = strtoupper($code);
60		$len = strlen($code);
61
62		for ($i = 0; $i < $len; ++$i) {
63			if (!isset($chr[$code[$i]])) {
64				throw new \Mpdf\Barcode\BarcodeException(sprintf('Invalid character "%s" CODABAR barcode value', $code[$i]));
65			}
66			$seq = $chr[$code[$i]];
67			for ($j = 0; $j < 8; ++$j) {
68				if (($j % 2) == 0) {
69					$t = true; // bar
70				} else {
71					$t = false; // space
72				}
73				$x = $seq[$j];
74				if ($x == 2) {
75					$w = $printRatio;
76				} else {
77					$w = 1;
78				}
79				$bararray['bcode'][$k] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0];
80				$bararray['maxw'] += $w;
81				++$k;
82			}
83		}
84
85		$this->data = $bararray;
86	}
87
88	public function getType()
89	{
90		return 'CODABAR';
91	}
92
93}
94