1<?php
2
3namespace Mpdf\Tag;
4
5use Mpdf\Mpdf;
6use Mpdf\Utils\UtfString;
7
8class TextCircle extends Tag
9{
10
11	public function open($attr, &$ahtml, &$ihtml)
12	{
13		$objattr = [];
14		$objattr['margin_top'] = 0;
15		$objattr['margin_bottom'] = 0;
16		$objattr['margin_left'] = 0;
17		$objattr['margin_right'] = 0;
18		$objattr['padding_top'] = 0;
19		$objattr['padding_bottom'] = 0;
20		$objattr['padding_left'] = 0;
21		$objattr['padding_right'] = 0;
22		$objattr['width'] = 0;
23		$objattr['height'] = 0;
24		$objattr['border_top']['w'] = 0;
25		$objattr['border_bottom']['w'] = 0;
26		$objattr['border_left']['w'] = 0;
27		$objattr['border_right']['w'] = 0;
28		$objattr['top-text'] = '';
29		$objattr['bottom-text'] = '';
30		$objattr['r'] = 20; // radius (default value here for safety)
31		$objattr['space-width'] = 120;
32		$objattr['char-width'] = 100;
33
34		$this->mpdf->InlineProperties['TEXTCIRCLE'] = $this->mpdf->saveInlineProperties();
35		$properties = $this->cssManager->MergeCSS('INLINE', 'TEXTCIRCLE', $attr);
36
37		if (isset($properties ['DISPLAY']) && strtolower($properties ['DISPLAY']) === 'none') {
38			return;
39		}
40		if (isset($attr['R'])) {
41			$objattr['r'] = $this->sizeConverter->convert(
42				$attr['R'],
43				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
44				$this->mpdf->FontSize,
45				false
46			);
47		}
48		if (isset($attr['TOP-TEXT'])) {
49			$objattr['top-text'] = UtfString::strcode2utf($attr['TOP-TEXT']);
50			$objattr['top-text'] = $this->mpdf->lesser_entity_decode($objattr['top-text']);
51			if ($this->mpdf->onlyCoreFonts) {
52				$objattr['top-text'] = mb_convert_encoding($objattr['top-text'], $this->mpdf->mb_enc, 'UTF-8');
53			}
54		}
55		if (isset($attr['BOTTOM-TEXT'])) {
56			$objattr['bottom-text'] = UtfString::strcode2utf($attr['BOTTOM-TEXT']);
57			$objattr['bottom-text'] = $this->mpdf->lesser_entity_decode($objattr['bottom-text']);
58			if ($this->mpdf->onlyCoreFonts) {
59				$objattr['bottom-text'] = mb_convert_encoding($objattr['bottom-text'], $this->mpdf->mb_enc, 'UTF-8');
60			}
61		}
62		if (!empty($attr['SPACE-WIDTH'])) {
63			$objattr['space-width'] = $attr['SPACE-WIDTH'];
64		}
65		if (!empty($attr['CHAR-WIDTH'])) {
66			$objattr['char-width'] = $attr['CHAR-WIDTH'];
67		}
68
69		// VISIBILITY
70		$objattr['visibility'] = 'visible';
71		if (isset($properties['VISIBILITY'])) {
72			$v = strtolower($properties['VISIBILITY']);
73			if (($v === 'hidden' || $v === 'printonly' || $v === 'screenonly') && $this->mpdf->visibility === 'visible') {
74				$objattr['visibility'] = $v;
75			}
76		}
77		if (isset($properties['FONT-SIZE'])) {
78			if (strtolower($properties['FONT-SIZE']) === 'auto') {
79				if ($objattr['top-text'] && $objattr['bottom-text']) {
80					$objattr['fontsize'] = -2;
81				} else {
82					$objattr['fontsize'] = -1;
83				}
84			} else {
85				$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
86				$this->mpdf->SetFontSize($mmsize * Mpdf::SCALE, false);
87				$objattr['fontsize'] = $this->mpdf->FontSizePt;
88			}
89		}
90		if (isset($attr['DIVIDER'])) {
91			$objattr['divider'] = UtfString::strcode2utf($attr['DIVIDER']);
92			$objattr['divider'] = $this->mpdf->lesser_entity_decode($objattr['divider']);
93			if ($this->mpdf->onlyCoreFonts) {
94				$objattr['divider'] = mb_convert_encoding($objattr['divider'], $this->mpdf->mb_enc, 'UTF-8');
95			}
96		}
97
98		if (isset($properties['COLOR'])) {
99			$objattr['color'] = $this->colorConverter->convert($properties['COLOR'], $this->mpdf->PDFAXwarnings);
100		}
101
102		$objattr['fontstyle'] = '';
103		if (isset($properties['FONT-WEIGHT'])) {
104			if (strtoupper($properties['FONT-WEIGHT']) === 'BOLD') {
105				$objattr['fontstyle'] .= 'B';
106			}
107		}
108		if (isset($properties['FONT-STYLE'])) {
109			if (strtoupper($properties['FONT-STYLE']) === 'ITALIC') {
110				$objattr['fontstyle'] .= 'I';
111			}
112		}
113
114		if (isset($properties['FONT-FAMILY'])) {
115			$this->mpdf->SetFont($properties['FONT-FAMILY'], $this->mpdf->FontStyle, 0, false);
116		}
117		$objattr['fontfamily'] = $this->mpdf->FontFamily;
118
119		// VSPACE and HSPACE converted to margins in MergeCSS
120		if (isset($properties['MARGIN-TOP'])) {
121			$objattr['margin_top'] = $this->sizeConverter->convert(
122				$properties['MARGIN-TOP'],
123				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
124				$this->mpdf->FontSize,
125				false
126			);
127		}
128		if (isset($properties['MARGIN-BOTTOM'])) {
129			$objattr['margin_bottom'] = $this->sizeConverter->convert(
130				$properties['MARGIN-BOTTOM'],
131				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
132				$this->mpdf->FontSize,
133				false
134			);
135		}
136		if (isset($properties['MARGIN-LEFT'])) {
137			$objattr['margin_left'] = $this->sizeConverter->convert(
138				$properties['MARGIN-LEFT'],
139				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
140				$this->mpdf->FontSize,
141				false
142			);
143		}
144		if (isset($properties['MARGIN-RIGHT'])) {
145			$objattr['margin_right'] = $this->sizeConverter->convert(
146				$properties['MARGIN-RIGHT'],
147				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
148				$this->mpdf->FontSize,
149				false
150			);
151		}
152
153		if (isset($properties['PADDING-TOP'])) {
154			$objattr['padding_top'] = $this->sizeConverter->convert(
155				$properties['PADDING-TOP'],
156				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
157				$this->mpdf->FontSize,
158				false
159			);
160		}
161		if (isset($properties['PADDING-BOTTOM'])) {
162			$objattr['padding_bottom'] = $this->sizeConverter->convert(
163				$properties['PADDING-BOTTOM'],
164				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
165				$this->mpdf->FontSize,
166				false
167			);
168		}
169		if (isset($properties['PADDING-LEFT'])) {
170			$objattr['padding_left'] = $this->sizeConverter->convert(
171				$properties['PADDING-LEFT'],
172				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
173				$this->mpdf->FontSize,
174				false
175			);
176		}
177		if (isset($properties['PADDING-RIGHT'])) {
178			$objattr['padding_right'] = $this->sizeConverter->convert(
179				$properties['PADDING-RIGHT'],
180				$this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
181				$this->mpdf->FontSize,
182				false
183			);
184		}
185
186		if (isset($properties['BORDER-TOP'])) {
187			$objattr['border_top'] = $this->mpdf->border_details($properties['BORDER-TOP']);
188		}
189		if (isset($properties['BORDER-BOTTOM'])) {
190			$objattr['border_bottom'] = $this->mpdf->border_details($properties['BORDER-BOTTOM']);
191		}
192		if (isset($properties['BORDER-LEFT'])) {
193			$objattr['border_left'] = $this->mpdf->border_details($properties['BORDER-LEFT']);
194		}
195		if (isset($properties['BORDER-RIGHT'])) {
196			$objattr['border_right'] = $this->mpdf->border_details($properties['BORDER-RIGHT']);
197		}
198
199		if (isset($properties['OPACITY']) && $properties['OPACITY'] > 0 && $properties['OPACITY'] <= 1) {
200			$objattr['opacity'] = $properties['OPACITY'];
201		}
202		if (isset($properties['BACKGROUND-COLOR']) && $properties['BACKGROUND-COLOR'] != '') {
203			$objattr['bgcolor'] = $this->colorConverter->convert($properties['BACKGROUND-COLOR'], $this->mpdf->PDFAXwarnings);
204		} else {
205			$objattr['bgcolor'] = false;
206		}
207		if ($this->mpdf->HREF) {
208			if (strpos($this->mpdf->HREF, '.') === false && strpos($this->mpdf->HREF, '@') !== 0) {
209				$href = $this->mpdf->HREF;
210				while (array_key_exists($href, $this->mpdf->internallink)) {
211					$href = '#' . $href;
212				}
213				$this->mpdf->internallink[$href] = $this->mpdf->AddLink();
214				$objattr['link'] = $this->mpdf->internallink[$href];
215			} else {
216				$objattr['link'] = $this->mpdf->HREF;
217			}
218		}
219		$extraheight = $objattr['padding_top'] + $objattr['padding_bottom'] + $objattr['margin_top'] + $objattr['margin_bottom'] + $objattr['border_top']['w'] + $objattr['border_bottom']['w'];
220		$extrawidth = $objattr['padding_left'] + $objattr['padding_right'] + $objattr['margin_left'] + $objattr['margin_right'] + $objattr['border_left']['w'] + $objattr['border_right']['w'];
221
222
223		$w = $objattr['r'] * 2;
224		$h = $w;
225		$objattr['height'] = $h + $extraheight;
226		$objattr['width'] = $w + $extrawidth;
227		$objattr['type'] = 'textcircle';
228
229		$e = "\xbb\xa4\xactype=image,objattr=" . serialize($objattr) . "\xbb\xa4\xac";
230
231		/* -- TABLES -- */
232		// Output it to buffers
233		if ($this->mpdf->tableLevel) {
234			$this->mpdf->_saveCellTextBuffer($e, $this->mpdf->HREF);
235			$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['s'] += $objattr['width'];
236		} else {
237			/* -- END TABLES -- */
238			$this->mpdf->_saveTextBuffer($e, $this->mpdf->HREF);
239		} // *TABLES*
240
241		if ($this->mpdf->InlineProperties['TEXTCIRCLE']) {
242			$this->mpdf->restoreInlineProperties($this->mpdf->InlineProperties['TEXTCIRCLE']);
243		}
244		unset($this->mpdf->InlineProperties['TEXTCIRCLE']);
245	}
246
247	public function close(&$ahtml, &$ihtml)
248	{
249	}
250}
251