1<?php
2
3namespace Mpdf\Barcode;
4
5class Rm4Scc extends \Mpdf\Barcode\AbstractBarcode implements \Mpdf\Barcode\BarcodeInterface
6{
7
8	/**
9	 * @param string $code
10	 * @param float $xDim
11	 * @param float $gapWidth
12	 * @param int[] $daft
13	 * @param bool $kix
14	 */
15	public function __construct($code, $xDim, $gapWidth, $daft, $kix = false)
16	{
17		$this->init($code, $gapWidth, $daft, $kix);
18
19		$this->data['nom-X'] = $xDim;
20		$this->data['nom-H'] = 5.0; // Nominal value for Height of Full bar in mm (spec.)
21		$this->data['quietL'] = 2; // LEFT Quiet margin =  mm (spec.)
22		$this->data['quietR'] = 2; // RIGHT Quiet margin =  mm (spec.)
23		$this->data['quietTB'] = 2; // TOP/BOTTOM Quiet margin =  mm (spec?)
24	}
25
26	/**
27	 * @param string $code
28	 * @param float $gapWidth
29	 * @param int[] $daft
30	 * @param bool $kix
31	 */
32	private function init($code, $gapWidth, $daft, $kix)
33	{
34		$notkix = !$kix;
35
36		// bar mode
37		// 1 = pos 1, length 2
38		// 2 = pos 1, length 3
39		// 3 = pos 2, length 1
40		// 4 = pos 2, length 2
41
42		$barmode = [
43			'0' => [3, 3, 2, 2],
44			'1' => [3, 4, 1, 2],
45			'2' => [3, 4, 2, 1],
46			'3' => [4, 3, 1, 2],
47			'4' => [4, 3, 2, 1],
48			'5' => [4, 4, 1, 1],
49			'6' => [3, 1, 4, 2],
50			'7' => [3, 2, 3, 2],
51			'8' => [3, 2, 4, 1],
52			'9' => [4, 1, 3, 2],
53			'A' => [4, 1, 4, 1],
54			'B' => [4, 2, 3, 1],
55			'C' => [3, 1, 2, 4],
56			'D' => [3, 2, 1, 4],
57			'E' => [3, 2, 2, 3],
58			'F' => [4, 1, 1, 4],
59			'G' => [4, 1, 2, 3],
60			'H' => [4, 2, 1, 3],
61			'I' => [1, 3, 4, 2],
62			'J' => [1, 4, 3, 2],
63			'K' => [1, 4, 4, 1],
64			'L' => [2, 3, 3, 2],
65			'M' => [2, 3, 4, 1],
66			'N' => [2, 4, 3, 1],
67			'O' => [1, 3, 2, 4],
68			'P' => [1, 4, 1, 4],
69			'Q' => [1, 4, 2, 3],
70			'R' => [2, 3, 1, 4],
71			'S' => [2, 3, 2, 3],
72			'T' => [2, 4, 1, 3],
73			'U' => [1, 1, 4, 4],
74			'V' => [1, 2, 3, 4],
75			'W' => [1, 2, 4, 3],
76			'X' => [2, 1, 3, 4],
77			'Y' => [2, 1, 4, 3],
78			'Z' => [2, 2, 3, 3]
79		];
80
81		$code = strtoupper($code);
82		$len = strlen($code);
83
84		$bararray = ['code' => $code, 'maxw' => 0, 'maxh' => $daft['F'], 'bcode' => []];
85
86		if ($notkix) {
87			// table for checksum calculation (row,col)
88			$checktable = [
89				'0' => [1, 1],
90				'1' => [1, 2],
91				'2' => [1, 3],
92				'3' => [1, 4],
93				'4' => [1, 5],
94				'5' => [1, 0],
95				'6' => [2, 1],
96				'7' => [2, 2],
97				'8' => [2, 3],
98				'9' => [2, 4],
99				'A' => [2, 5],
100				'B' => [2, 0],
101				'C' => [3, 1],
102				'D' => [3, 2],
103				'E' => [3, 3],
104				'F' => [3, 4],
105				'G' => [3, 5],
106				'H' => [3, 0],
107				'I' => [4, 1],
108				'J' => [4, 2],
109				'K' => [4, 3],
110				'L' => [4, 4],
111				'M' => [4, 5],
112				'N' => [4, 0],
113				'O' => [5, 1],
114				'P' => [5, 2],
115				'Q' => [5, 3],
116				'R' => [5, 4],
117				'S' => [5, 5],
118				'T' => [5, 0],
119				'U' => [0, 1],
120				'V' => [0, 2],
121				'W' => [0, 3],
122				'X' => [0, 4],
123				'Y' => [0, 5],
124				'Z' => [0, 0]
125			];
126			$row = 0;
127			$col = 0;
128			for ($i = 0; $i < $len; ++$i) {
129				$row += $checktable[$code[$i]][0];
130				$col += $checktable[$code[$i]][1];
131			}
132			$row %= 6;
133			$col %= 6;
134			$chk = array_keys($checktable, [$row, $col]);
135			$code .= $chk[0];
136			$bararray['checkdigit'] = $chk[0];
137			++$len;
138		}
139
140		$k = 0;
141		if ($notkix) {
142			// start bar
143			$bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => $daft['A'], 'p' => 0];
144			$bararray['bcode'][$k++] = ['t' => 0, 'w' => $gapWidth, 'h' => $daft['A'], 'p' => 0];
145			$bararray['maxw'] += (1 + $gapWidth);
146		}
147
148		for ($i = 0; $i < $len; ++$i) {
149
150			for ($j = 0; $j < 4; ++$j) {
151
152				switch ($barmode[$code[$i]][$j]) {
153					case 1:
154						// ascender (A)
155						$p = 0;
156						$h = $daft['A'];
157						break;
158					case 2:
159						// full bar (F)
160						$p = 0;
161						$h = $daft['F'];
162						break;
163					case 3:
164						// tracker (T)
165						$p = ($daft['F'] - $daft['T']) / 2;
166						$h = $daft['T'];
167						break;
168					case 4:
169						// descender (D)
170						$p = $daft['F'] - $daft['D'];
171						$h = $daft['D'];
172						break;
173				}
174
175				$bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => $h, 'p' => $p];
176				$bararray['bcode'][$k++] = ['t' => 0, 'w' => $gapWidth, 'h' => 2, 'p' => 0];
177				$bararray['maxw'] += (1 + $gapWidth);
178
179			}
180		}
181
182		if ($notkix) {
183			// stop bar
184			$bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => $daft['F'], 'p' => 0];
185			$bararray['maxw'] += 1;
186		}
187
188		$this->data = $bararray;
189	}
190
191	/**
192	 * @inheritdoc
193	 */
194	public function getType()
195	{
196		return 'RM4SCC';
197	}
198
199}
200