1<?php
2
3namespace Mpdf\Tag;
4
5class BarCode extends Tag
6{
7
8	/**
9	 * @var \Mpdf\Barcode
10	 */
11	protected $barcode;
12
13	public function open($attr, &$ahtml, &$ihtml)
14	{
15		$this->mpdf->ignorefollowingspaces = false;
16		if (!empty($attr['CODE'])) {
17			$objattr = [];
18			$objattr['margin_top'] = 0;
19			$objattr['margin_bottom'] = 0;
20			$objattr['margin_left'] = 0;
21			$objattr['margin_right'] = 0;
22			$objattr['padding_top'] = 0;
23			$objattr['padding_bottom'] = 0;
24			$objattr['padding_left'] = 0;
25			$objattr['padding_right'] = 0;
26			$objattr['width'] = 0;
27			$objattr['height'] = 0;
28			$objattr['border_top']['w'] = 0;
29			$objattr['border_bottom']['w'] = 0;
30			$objattr['border_left']['w'] = 0;
31			$objattr['border_right']['w'] = 0;
32			$objattr['code'] = $attr['CODE'];
33
34			if (isset($attr['TYPE'])) {
35				$objattr['btype'] = strtoupper(trim($attr['TYPE']));
36			} else {
37				$objattr['btype'] = 'EAN13';
38			} // default
39			if (preg_match('/^(EAN13|ISBN|ISSN|EAN8|UPCA|UPCE)P([25])$/', $objattr['btype'], $m)) {
40				$objattr['btype'] = $m[1];
41				$objattr['bsupp'] = $m[2];
42				if (preg_match('/^(\S+)\s+(.*)$/', $objattr['code'], $mm)) {
43					$objattr['code'] = $mm[1];
44					$objattr['bsupp_code'] = $mm[2];
45				}
46			} else {
47				$objattr['bsupp'] = 0;
48			}
49
50			if (isset($attr['TEXT']) && $attr['TEXT'] == 1) {
51				$objattr['showtext'] = 1;
52			} else {
53				$objattr['showtext'] = 0;
54			}
55			if (isset($attr['SIZE']) && $attr['SIZE'] > 0) {
56				$objattr['bsize'] = $attr['SIZE'];
57			} else {
58				$objattr['bsize'] = 1;
59			}
60			if (isset($attr['HEIGHT']) && $attr['HEIGHT'] > 0) {
61				$objattr['bheight'] = $attr['HEIGHT'];
62			} else {
63				$objattr['bheight'] = 1;
64			}
65			if (isset($attr['PR']) && $attr['PR'] > 0) {
66				$objattr['pr_ratio'] = $attr['PR'];
67			} else {
68				$objattr['pr_ratio'] = '';
69			}
70			$properties = $this->cssManager->MergeCSS('', 'BARCODE', $attr);
71			if (isset($properties ['DISPLAY']) && strtolower($properties ['DISPLAY']) === 'none') {
72				return;
73			}
74			if (isset($properties['MARGIN-TOP'])) {
75				$objattr['margin_top'] = $this->sizeConverter->convert(
76					$properties['MARGIN-TOP'],
77					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
78					$this->mpdf->FontSize,
79					false
80				);
81			}
82			if (isset($properties['MARGIN-BOTTOM'])) {
83				$objattr['margin_bottom'] = $this->sizeConverter->convert(
84					$properties['MARGIN-BOTTOM'],
85					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
86					$this->mpdf->FontSize,
87					false
88				);
89			}
90			if (isset($properties['MARGIN-LEFT'])) {
91				$objattr['margin_left'] = $this->sizeConverter->convert(
92					$properties['MARGIN-LEFT'],
93					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
94					$this->mpdf->FontSize,
95					false
96				);
97			}
98			if (isset($properties['MARGIN-RIGHT'])) {
99				$objattr['margin_right'] = $this->sizeConverter->convert(
100					$properties['MARGIN-RIGHT'],
101					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
102					$this->mpdf->FontSize,
103					false
104				);
105			}
106
107			if (isset($properties['PADDING-TOP'])) {
108				$objattr['padding_top'] = $this->sizeConverter->convert(
109					$properties['PADDING-TOP'],
110					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
111					$this->mpdf->FontSize,
112					false
113				);
114			}
115			if (isset($properties['PADDING-BOTTOM'])) {
116				$objattr['padding_bottom'] = $this->sizeConverter->convert(
117					$properties['PADDING-BOTTOM'],
118					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
119					$this->mpdf->FontSize,
120					false
121				);
122			}
123			if (isset($properties['PADDING-LEFT'])) {
124				$objattr['padding_left'] = $this->sizeConverter->convert(
125					$properties['PADDING-LEFT'],
126					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
127					$this->mpdf->FontSize,
128					false
129				);
130			}
131			if (isset($properties['PADDING-RIGHT'])) {
132				$objattr['padding_right'] = $this->sizeConverter->convert(
133					$properties['PADDING-RIGHT'],
134					$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
135					$this->mpdf->FontSize,
136					false
137				);
138			}
139
140			if (isset($properties['BORDER-TOP'])) {
141				$objattr['border_top'] = $this->mpdf->border_details($properties['BORDER-TOP']);
142			}
143			if (isset($properties['BORDER-BOTTOM'])) {
144				$objattr['border_bottom'] = $this->mpdf->border_details($properties['BORDER-BOTTOM']);
145			}
146			if (isset($properties['BORDER-LEFT'])) {
147				$objattr['border_left'] = $this->mpdf->border_details($properties['BORDER-LEFT']);
148			}
149			if (isset($properties['BORDER-RIGHT'])) {
150				$objattr['border_right'] = $this->mpdf->border_details($properties['BORDER-RIGHT']);
151			}
152
153			if (isset($properties['VERTICAL-ALIGN'])) {
154				$objattr['vertical-align'] = $this->getAlign($properties['VERTICAL-ALIGN']);
155			}
156			if (isset($properties['COLOR']) && $properties['COLOR'] != '') {
157				$objattr['color'] = $this->colorConverter->convert($properties['COLOR'], $this->mpdf->PDFAXwarnings);
158			} else {
159				$objattr['color'] = false;
160			}
161			if (isset($properties['BACKGROUND-COLOR']) && $properties['BACKGROUND-COLOR'] != '') {
162				$objattr['bgcolor'] = $this->colorConverter->convert($properties['BACKGROUND-COLOR'], $this->mpdf->PDFAXwarnings);
163			} else {
164				$objattr['bgcolor'] = false;
165			}
166
167			$this->barcode = new \Mpdf\Barcode();
168
169			if (in_array($objattr['btype'], ['EAN13', 'ISBN', 'ISSN', 'UPCA', 'UPCE', 'EAN8'])) {
170
171				$code = preg_replace('/\-/', '', $objattr['code']);
172				$arrcode = $this->barcode->getBarcodeArray($code, $objattr['btype']);
173
174				if ($objattr['bsupp'] == 2 || $objattr['bsupp'] == 5) { // EAN-2 or -5 Supplement
175					$supparrcode = $this->barcode->getBarcodeArray($objattr['bsupp_code'], 'EAN' . $objattr['bsupp']);
176					$w = ($arrcode['maxw'] + $arrcode['lightmL'] + $arrcode['lightmR']
177							+ $supparrcode['maxw'] + $supparrcode['sepM']) * $arrcode['nom-X'] * $objattr['bsize'];
178				} else {
179					$w = ($arrcode['maxw'] + $arrcode['lightmL'] + $arrcode['lightmR']) * $arrcode['nom-X'] * $objattr['bsize'];
180				}
181
182				$h = $arrcode['nom-H'] * $objattr['bsize'] * $objattr['bheight'];
183				// Add height for ISBN string + margin from top of bars
184				if (($objattr['showtext'] && $objattr['btype'] === 'EAN13') || $objattr['btype'] === 'ISBN' || $objattr['btype'] === 'ISSN') {
185					$tisbnm = 1.5 * $objattr['bsize']; // Top margin between TOP TEXT (isbn - if shown) & bars
186					$isbn_fontsize = 2.1 * $objattr['bsize'];
187					$h += $isbn_fontsize + $tisbnm;
188				}
189
190			} elseif ($objattr['btype'] === 'QR') { // QR-code
191				$w = $h = $objattr['bsize'] * 25; // Factor of 25mm (default)
192				$objattr['errorlevel'] = 'L';
193				if (isset($attr['ERROR'])) {
194					$objattr['errorlevel'] = $attr['ERROR'];
195				}
196				$objattr['disableborder'] = false;
197				if (isset($attr['DISABLEBORDER'])) {
198					$objattr['disableborder'] = (bool) $attr['DISABLEBORDER'];
199				}
200
201			} elseif (in_array($objattr['btype'], ['IMB', 'RM4SCC', 'KIX', 'POSTNET', 'PLANET'])) {
202
203				$arrcode = $this->barcode->getBarcodeArray($objattr['code'], $objattr['btype']);
204
205				$w = ($arrcode['maxw'] * $arrcode['nom-X'] * $objattr['bsize']) + $arrcode['quietL'] + $arrcode['quietR'];
206				$h = ($arrcode['nom-H'] * $objattr['bsize']) + (2 * $arrcode['quietTB']);
207
208			} elseif (in_array($objattr['btype'], ['C128A', 'C128B', 'C128C', 'C128RAW', 'EAN128A', 'EAN128B', 'EAN128C',
209				'C39', 'C39+', 'C39E', 'C39E+', 'S25', 'S25+', 'I25', 'I25+', 'I25B',
210				'I25B+', 'C93', 'MSI', 'MSI+', 'CODABAR', 'CODE11'])) {
211
212				$arrcode = $this->barcode->getBarcodeArray($objattr['code'], $objattr['btype'], $objattr['pr_ratio']);
213				$w = ($arrcode['maxw'] + $arrcode['lightmL'] + $arrcode['lightmR']) * $arrcode['nom-X'] * $objattr['bsize'];
214				$h = ((2 * $arrcode['lightTB'] * $arrcode['nom-X']) + $arrcode['nom-H']) * $objattr['bsize'] * $objattr['bheight'];
215
216			} else {
217				return;
218			}
219
220			$extraheight = $objattr['padding_top'] + $objattr['padding_bottom'] + $objattr['margin_top']
221				+ $objattr['margin_bottom'] + $objattr['border_top']['w'] + $objattr['border_bottom']['w'];
222			$extrawidth = $objattr['padding_left'] + $objattr['padding_right'] + $objattr['margin_left']
223				+ $objattr['margin_right'] + $objattr['border_left']['w'] + $objattr['border_right']['w'];
224
225			$objattr['type'] = 'barcode';
226			$objattr['height'] = $h + $extraheight;
227			$objattr['width'] = $w + $extrawidth;
228			$objattr['barcode_height'] = $h;
229			$objattr['barcode_width'] = $w;
230
231			/* -- CSS-IMAGE-FLOAT -- */
232			if (!$this->mpdf->ColActive && !$this->mpdf->tableLevel && !$this->mpdf->listlvl && !$this->mpdf->kwt) {
233				if (isset($properties['FLOAT']) && (strtoupper($properties['FLOAT']) === 'RIGHT' || strtoupper($properties['FLOAT']) === 'LEFT')) {
234					$objattr['float'] = strtoupper(substr($properties['FLOAT'], 0, 1));
235				}
236			}
237			/* -- END CSS-IMAGE-FLOAT -- */
238
239			$e = "\xbb\xa4\xactype=barcode,objattr=" . serialize($objattr) . "\xbb\xa4\xac";
240
241			/* -- TABLES -- */
242			// Output it to buffers
243			if ($this->mpdf->tableLevel) {
244				$this->mpdf->_saveCellTextBuffer($e, $this->mpdf->HREF);
245				$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['s'] += $objattr['width'];
246			} else {
247				/* -- END TABLES -- */
248				$this->mpdf->_saveTextBuffer($e, $this->mpdf->HREF);
249			} // *TABLES*
250		}
251	}
252
253	public function close(&$ahtml, &$ihtml)
254	{
255	}
256}
257