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, $quiet_zone_left = null, $quiet_zone_right = null)
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'] = ($quiet_zone_left !== null ? $quiet_zone_left : 10); // LEFT light margin =  x X-dim (spec.)
23		$this->data['lightmR'] = ($quiet_zone_right !== null ? $quiet_zone_right : 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
64			if (!isset($chr[$code[$i]])) {
65				throw new \Mpdf\Barcode\BarcodeException(sprintf('Invalid character "%s" in CODABAR barcode value "%s"', $code[$i], $code));
66			}
67
68			$seq = $chr[$code[$i]];
69
70			for ($j = 0; $j < 8; ++$j) {
71				if (($j % 2) == 0) {
72					$t = true; // bar
73				} else {
74					$t = false; // space
75				}
76				$x = $seq[$j];
77				if ($x == 2) {
78					$w = $printRatio;
79				} else {
80					$w = 1;
81				}
82				$bararray['bcode'][$k] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0];
83				$bararray['maxw'] += $w;
84				++$k;
85			}
86		}
87
88		$this->data = $bararray;
89	}
90
91	public function getType()
92	{
93		return 'CODABAR';
94	}
95
96}
97