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