1<?php
2
3namespace Mpdf;
4
5class Barcode
6{
7
8	public function getBarcodeArray($code, $type, $pr = '', $quiet_zone_left = null, $quiet_zone_right = null)
9	{
10		$barcode = $this->getBarcode($code, $type, $pr, $quiet_zone_left, $quiet_zone_right);
11		return $barcode ? $barcode->getData() : false;
12	}
13
14	public function getChecksum($code, $type)
15	{
16		$barcode = $this->getBarcode($code, $type);
17		return $barcode ? $barcode->getChecksum() : false;
18	}
19
20	/**
21	 * @param string $code
22	 * @param string $type
23	 * @param float $pr
24	 *
25	 * @return \Mpdf\Barcode\BarcodeInterface
26	 */
27	public function getBarcode($code, $type, $pr = 0.0, $quiet_zone_left = null, $quiet_zone_right = null)
28	{
29		switch (strtoupper($type)) {
30			case 'ISBN':
31			case 'ISSN':
32			case 'EAN13': // EAN 13
33				return new Barcode\EanUpc($code, 13, 11, 7, 0.33, 25.93);
34
35			case 'UPCA': // UPC-A
36				return new Barcode\EanUpc($code, 12, 9, 9, 0.33, 25.91);
37
38			case 'UPCE': // UPC-E
39				return new Barcode\EanUpc($code, 6, 9, 7, 0.33, 25.93);
40
41			case 'EAN8': // EAN 8
42				return new Barcode\EanUpc($code, 8, 7, 7, 0.33, 21.64);
43
44			case 'EAN2': // 2-Digits UPC-Based Extention
45				return new Barcode\EanExt($code, 2, 7, 7, 0.33, 20, 9);
46
47			case 'EAN5': // 5-Digits UPC-Based Extention
48				return new Barcode\EanExt($code, 5, 7, 7, 0.33, 20, 9);
49
50			case 'IMB': // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
51				$xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
52				$bpi = 22; // Bars per inch
53				return new Barcode\Imb($code, $xdim, ((25.4 / $bpi) - $xdim) / $xdim, ['D' => 2, 'A' => 2, 'F' => 3, 'T' => 1]);
54
55			case 'RM4SCC': // RM4SCC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
56				$xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
57				$bpi = 22; // Bars per inch
58				return new Barcode\Rm4Scc($code, $xdim, ((25.4 / $bpi) - $xdim) / $xdim, ['D' => 5, 'A' => 5, 'F' => 8, 'T' => 2]);
59
60			case 'KIX': // KIX (Klant index - Customer index)
61				$xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
62				$bpi = 22; // Bars per inch
63				return new Barcode\Rm4Scc($code, $xdim, ((25.4 / $bpi) - $xdim) / $xdim, ['D' => 5, 'A' => 5, 'F' => 8, 'T' => 2], true);
64
65			case 'POSTNET': // POSTNET
66				$xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
67				$bpi = 22; // Bars per inch
68				return new Barcode\Postnet($code, $xdim, ((25.4 / $bpi) - $xdim) / $xdim, false);
69
70			case 'PLANET': // PLANET
71				$xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
72				$bpi = 22; // Bars per inch
73				return new Barcode\Postnet($code, $xdim, ((25.4 / $bpi) - $xdim) / $xdim, true);
74
75			case 'C93': // CODE 93 - USS-93
76				return new Barcode\Code93($code, $quiet_zone_left, $quiet_zone_right);
77
78			case 'CODE11': // CODE 11
79				return new Barcode\Code11($code, ($pr > 0) ? $pr : 3, $quiet_zone_left, $quiet_zone_right);
80
81			case 'MSI':  // MSI (Variation of Plessey code)
82				return new Barcode\Msi($code, false, $quiet_zone_left, $quiet_zone_right);
83
84			case 'MSI+': // MSI + CHECKSUM (modulo 11)
85				return new Barcode\Msi($code, true, $quiet_zone_left, $quiet_zone_right);
86
87			case 'CODABAR': // CODABAR
88				return new Barcode\Codabar($code, ($pr > 0) ? $pr : 2.5, $quiet_zone_left, $quiet_zone_right);
89
90			case 'C128A': // CODE 128 A
91				return new Barcode\Code128($code, 'A', false, $quiet_zone_left, $quiet_zone_right);
92
93			case 'C128B': // CODE 128 B
94				return new Barcode\Code128($code, 'B', false, $quiet_zone_left, $quiet_zone_right);
95
96			case 'C128C':  // CODE 128 C
97				return new Barcode\Code128($code, 'C', false, $quiet_zone_left, $quiet_zone_right);
98
99			case 'C128RAW':  // CODE 128 RAW -- code is a space separated list of codes with startcode but without checkdigit,stop,end ex: "105 12 34"
100				return new Barcode\Code128($code, 'RAW', false, $quiet_zone_left, $quiet_zone_right);
101
102			case 'EAN128A':  // EAN 128 A
103				return new Barcode\Code128($code, 'A', true, $quiet_zone_left, $quiet_zone_right);
104
105			case 'EAN128B':  // EAN 128 B
106				return new Barcode\Code128($code, 'B', true, $quiet_zone_left, $quiet_zone_right);
107
108			case 'EAN128C': // EAN 128 C
109				return new Barcode\Code128($code, 'C', true, $quiet_zone_left, $quiet_zone_right);
110
111			case 'C39':  // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
112				return new Barcode\Code39($this->sanitizeCode($code), ($pr > 0) ? $pr : 2.5, false, false, $quiet_zone_left, $quiet_zone_right);
113
114			case 'C39+': // CODE 39 with checksum
115				return new Barcode\Code39($this->sanitizeCode($code), ($pr > 0) ? $pr : 2.5, false, true, $quiet_zone_left, $quiet_zone_right);
116
117			case 'C39E': // CODE 39 EXTENDED
118				return new Barcode\Code39($this->sanitizeCode($code), ($pr > 0) ? $pr : 2.5, true, false, $quiet_zone_left, $quiet_zone_right);
119
120			case 'C39E+': // CODE 39 EXTENDED + CHECKSUM
121				return new Barcode\Code39($this->sanitizeCode($code), ($pr > 0) ? $pr : 2.5, true, true, $quiet_zone_left, $quiet_zone_right);
122
123			case 'S25':  // Standard 2 of 5
124				return new Barcode\S25($code, false, $quiet_zone_left, $quiet_zone_right);
125
126			case 'S25+': // Standard 2 of 5 + CHECKSUM
127				return new Barcode\S25($code, true, $quiet_zone_left, $quiet_zone_right);
128
129			case 'I25':  // Interleaved 2 of 5
130				return new Barcode\I25($code, 0, ($pr > 0) ? $pr : 2.5, false, $quiet_zone_left, $quiet_zone_right);
131
132			case 'I25+': // Interleaved 2 of 5 + CHECKSUM
133				return new Barcode\I25($code, 0, ($pr > 0) ? $pr : 2.5, true, $quiet_zone_left, $quiet_zone_right);
134
135			case 'I25B':  // Interleaved 2 of 5 + Bearer bars
136				return new Barcode\I25($code, 2, ($pr > 0) ? $pr : 2.5, false, $quiet_zone_left, $quiet_zone_right);
137
138			case 'I25B+': // Interleaved 2 of 5 + CHECKSUM + Bearer bars
139				return new Barcode\I25($code, 2, ($pr > 0) ? $pr : 2.5, true, $quiet_zone_left, $quiet_zone_right);
140		}
141
142		return false;
143	}
144
145	private function sanitizeCode($code)
146	{
147		$code = str_replace(chr(194) . chr(160), ' ', $code); // mPDF 5.3.95  (for utf-8 encoded)
148		$code = str_replace(chr(160), ' ', $code); // mPDF 5.3.95	(for win-1252)
149
150		return $code;
151	}
152
153}
154