aboutsummaryrefslogblamecommitdiffhomepage
path: root/inc/3rdparty/libraries/mpdf/classes/directw.php
blob: dc317d26c1bff25c9ae89358378efed0c70361ba (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408























































































































































































































































































































































































































                                                                                                                                                                                                          
<?php

class directw {

var $mpdf = null;

function directw(&$mpdf) {
	$this->mpdf = $mpdf;
}


function Write($h,$txt,$currentx=0,$link='',$directionality='ltr',$align='') {
	if (!$align) { 
		if ($directionality=='rtl') { $align = 'R'; }
		else { $align = 'L'; }
	}
	if ($h == 0) { $this->mpdf->SetLineHeight(); $h = $this->mpdf->lineheight; }
	//Output text in flowing mode
	$w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x; 

	$wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR));
	$s=str_replace("\r",'',$txt);
	if ($this->mpdf->usingCoreFont)  { $nb=strlen($s); }
	else {
		$nb=mb_strlen($s, $this->mpdf->mb_enc );
		// handle single space character
		if(($nb==1) && $s == " ") {
			$this->mpdf->x += $this->mpdf->GetStringWidth($s);
			return;
		}
	}
	$sep=-1;
	$i=0;
	$j=0;
	$l=0;
	$nl=1;
	if (!$this->mpdf->usingCoreFont) {
		if (preg_match("/([".$this->mpdf->pregRTLchars."])/u", $txt)) { $this->mpdf->biDirectional = true; }	// *RTL*
		$checkCursive=false;
		if ($this->mpdf->biDirectional) {  $checkCursive=true; }	// *RTL*
		else if (isset($this->mpdf->CurrentFont['indic']) && $this->mpdf->CurrentFont['indic']) {  $checkCursive=true; }	// *INDIC*
		while($i<$nb) {
			//Get next character
			$c = mb_substr($s,$i,1,$this->mpdf->mb_enc );
			if($c == "\n") {
				// WORD SPACING
				$this->mpdf->ResetSpacing();
				//Explicit line break
				$tmp = rtrim(mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc));
				if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; }	// *RTL*
				$this->mpdf->magic_reverse_dir($tmp, true, $directionality);	// *RTL*
				$this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
				$i++;
				$sep = -1;
				$j = $i;
				$l = 0;
				if($nl == 1) {
					if ($currentx != 0) $this->mpdf->x=$currentx;
					else $this->mpdf->x=$this->mpdf->lMargin;
					$w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
					$wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR));
				}
				$nl++;
				continue;
			}
			if($c == " ") { $sep= $i; }
			$l += $this->mpdf->GetCharWidthNonCore($c);	// mPDF 5.3.04
			if($l > $wmax) {
				//Automatic line break (word wrapping)
				if($sep == -1) {
					// WORD SPACING
					$this->mpdf->ResetSpacing();
					if($this->mpdf->x > $this->mpdf->lMargin) {
						//Move to next line
						if ($currentx != 0) $this->mpdf->x=$currentx;
						else $this->mpdf->x=$this->mpdf->lMargin;
						$this->mpdf->y+=$h;
						$w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x;
						$wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR));
						$i++;
						$nl++;
						continue;
					}
					if($i==$j) { $i++; }
					$tmp = rtrim(mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc));
					if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; }	// *RTL*
					$this->mpdf->magic_reverse_dir($tmp, true, $directionality);	// *RTL*
					$this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
				}
				else {
					$tmp = rtrim(mb_substr($s,$j,$sep-$j,$this->mpdf->mb_enc));
					if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; }	// *RTL*
					$this->mpdf->magic_reverse_dir($tmp, true, $directionality);	// *RTL*

					if($align=='J') {
						//////////////////////////////////////////
						// JUSTIFY J using Unicode fonts (Word spacing doesn't work)
						// WORD SPACING
						// Change NON_BREAKING SPACE to spaces so they are 'spaced' properly
					      $tmp = str_replace(chr(194).chr(160),chr(32),$tmp ); 
						$len_ligne = $this->mpdf->GetStringWidth($tmp );
						$nb_carac = mb_strlen( $tmp , $this->mpdf->mb_enc ) ;  
						$nb_spaces = mb_substr_count( $tmp ,' ', $this->mpdf->mb_enc ) ;  
						$inclCursive=false;
						if ($checkCursive) {
							if (preg_match("/([".$this->mpdf->pregRTLchars."])/u", $tmp)) { $inclCursive = true; }	// *RTL*
							if (preg_match("/([".$this->mpdf->pregHIchars.$this->mpdf->pregBNchars.$this->mpdf->pregPAchars."])/u", $tmp)) { $inclCursive = true; }	// *INDIC*
						}
						list($charspacing,$ws) = $this->mpdf->GetJspacing($nb_carac,$nb_spaces,((($w-2) - $len_ligne) * _MPDFK),$inclCursive);
						$this->mpdf->SetSpacing($charspacing,$ws);
						//////////////////////////////////////////
					}
					$this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
					$i=$sep+1;
				}
				$sep = -1;
				$j = $i;
				$l = 0;
				if($nl==1) {
					if ($currentx != 0) $this->mpdf->x=$currentx;
					else $this->mpdf->x=$this->mpdf->lMargin;
					$w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x;
					$wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR));
				}
				$nl++;
			}
			else { $i++; }
		}
		//Last chunk
		// WORD SPACING
		$this->mpdf->ResetSpacing();
	}
	else {
		while($i<$nb) {
			//Get next character
			$c=$s[$i];
			if($c == "\n") {
				//Explicit line break
				// WORD SPACING
				$this->mpdf->ResetSpacing();
				$this->mpdf->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, $align, $fill, $link);
				$i++;
				$sep = -1;
				$j = $i;
				$l = 0;
				if($nl == 1) {
					if ($currentx != 0) $this->mpdf->x=$currentx;
					else $this->mpdf->x=$this->mpdf->lMargin;
					$w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x;
					$wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR);
				}
				$nl++;
				continue;
			}
			if($c == " ") { $sep= $i; }
			$l += $this->mpdf->GetCharWidthCore($c);	// mPDF 5.3.04
			if($l > $wmax) {
				//Automatic line break (word wrapping)
				if($sep == -1) {
					// WORD SPACING
					$this->mpdf->ResetSpacing();
					if($this->mpdf->x > $this->mpdf->lMargin) {
						//Move to next line
						if ($currentx != 0) $this->mpdf->x=$currentx;
						else $this->mpdf->x=$this->mpdf->lMargin;
						$this->mpdf->y+=$h;
						$w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x;
						$wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR);
						$i++;
						$nl++;
						continue;
					}
					if($i==$j) { $i++; }
					$this->mpdf->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, $align, $fill, $link);
				}
				else {
					$tmp = substr($s, $j, $sep-$j);
					if($align=='J') {
						//////////////////////////////////////////
						// JUSTIFY J using Unicode fonts (Word spacing doesn't work)
						// WORD SPACING
						// Change NON_BREAKING SPACE to spaces so they are 'spaced' properly
					      $tmp = str_replace(chr(160),chr(32),$tmp );
						$len_ligne = $this->mpdf->GetStringWidth($tmp );
						$nb_carac = strlen( $tmp ) ;  
						$nb_spaces = substr_count( $tmp ,' ' ) ;  
						list($charspacing,$ws) = $this->mpdf->GetJspacing($nb_carac,$nb_spaces,((($w-2) - $len_ligne) * _MPDFK),$false);
						$this->mpdf->SetSpacing($charspacing,$ws);
						//////////////////////////////////////////
					}
					$this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link);
					$i=$sep+1;
				}
				$sep = -1;
				$j = $i;
				$l = 0;
				if($nl==1) {
					if ($currentx != 0) $this->mpdf->x=$currentx;
					else $this->mpdf->x=$this->mpdf->lMargin;
					$w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x;
					$wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR);
				}
				$nl++;
			}
			else {
				$i++;
			}
		}
		// WORD SPACING
		$this->mpdf->ResetSpacing();
	}
	//Last chunk
	if($i!=$j) {
		if ($currentx != 0) $this->mpdf->x=$currentx;
		else $this->mpdf->x=$this->mpdf->lMargin;	
		if ($this->mpdf->usingCoreFont) { $tmp = substr($s,$j,$i-$j); }
		else {
			$tmp = mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc);
			if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; }	// *RTL*
			$this->mpdf->magic_reverse_dir($tmp, true, $directionality);	// *RTL*
		}
		$this->mpdf->Cell($w,$h,$tmp,0,0,$align,$fill,$link);
	}
}


function CircularText($x, $y, $r, $text, $align='top', $fontfamily='', $fontsizePt=0, $fontstyle='', $kerning=120, $fontwidth=100, $divider='') {	// mPDF 5.5.23
	if ($font || $fontstyle || $fontsizePt) $this->mpdf->SetFont($fontfamily,$fontstyle,$fontsizePt);
	$kerning/=100;
	$fontwidth/=100;        
	if($kerning==0) $this->mpdf->Error('Please use values unequal to zero for kerning (CircularText)');
	if($fontwidth==0) $this->mpdf->Error('Please use values unequal to zero for font width (CircularText)');
	$text=str_replace("\r",'',$text);
	//circumference
	$u=($r*2)*M_PI;
	// mPDF 5.5.23
	$checking = true;
	$autoset = false;
	while($checking) {
	   $t=0;
	   $w = array();
	   if ($this->mpdf->usingCoreFont)  {
		$nb=strlen($text); 
		for($i=0; $i<$nb; $i++){
			$w[$i]=$this->mpdf->GetStringWidth($text[$i]);
			$w[$i]*=$kerning*$fontwidth;
			$t+=$w[$i];
		}
	   }
	   else { 
		$nb=mb_strlen($text, $this->mpdf->mb_enc ); 
		$lastchar = '';
		$unicode = $this->mpdf->UTF8StringToArray($text);
		for($i=0; $i<$nb; $i++){
			$c = mb_substr($text,$i,1,$this->mpdf->mb_enc );
			$w[$i]=$this->mpdf->GetStringWidth($c);
			$w[$i]*=$kerning*$fontwidth;
			$char = $unicode[$i];
			if ($this->mpdf->useKerning && $lastchar) {
				if (isset($this->mpdf->CurrentFont['kerninfo'][$lastchar][$char])) { 
					$tk = $this->mpdf->CurrentFont['kerninfo'][$lastchar][$char] * ($this->mpdf->FontSize/ 1000) * $kerning * $fontwidth; 
					$w[$i] += $tk/2;
					$w[$i-1] += $tk/2;
					$t+=$tk;
				}
			}
			$lastchar = $char;
			$t+=$w[$i];
		}
	   }
	   if ($fontsizePt>=0 || $autoset) { $checking = false; }
	   else {
		$t+=$this->mpdf->GetStringWidth('  ');
		if ($divider)
			$t+=$this->mpdf->GetStringWidth('  ');
		if ($fontsizePt==-2)
			$fontsizePt = $this->mpdf->FontSizePt * 0.5  *  $u/$t;
		else 
			$fontsizePt = $this->mpdf->FontSizePt * $u/$t;
		$this->mpdf->SetFontSize($fontsizePt);
		$autoset = true;
	   }
	}

	//total width of string in degrees
	$d=($t/$u)*360;

	$this->mpdf->StartTransform();
	// rotate matrix for the first letter to center the text
	// (half of total degrees)
	if($align=='top'){
		$this->mpdf->transformRotate(-$d/2, $x, $y);
	}
	else{
		$this->mpdf->transformRotate($d/2, $x, $y);
	}
	//run through the string
	for($i=0; $i<$nb; $i++){
		if($align=='top'){
			//rotate matrix half of the width of current letter + half of the width of preceding letter
			if($i==0){
				$this->mpdf->transformRotate((($w[$i]/2)/$u)*360, $x, $y);
			}
			else{
				$this->mpdf->transformRotate((($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
			}
			if($fontwidth!=1){
				$this->mpdf->StartTransform();
				$this->mpdf->transformScale($fontwidth*100, 100, $x, $y);
			}
			$this->mpdf->SetXY($x-$w[$i]/2, $y-$r);
		}
		else{
			//rotate matrix half of the width of current letter + half of the width of preceding letter
			if($i==0){
				$this->mpdf->transformRotate(-(($w[$i]/2)/$u)*360, $x, $y);
			}
			else{
				$this->mpdf->transformRotate(-(($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
			}
			if($fontwidth!=1){
				$this->mpdf->StartTransform();
				$this->mpdf->transformScale($fontwidth*100, 100, $x, $y);
			}
			$this->mpdf->SetXY($x-$w[$i]/2, $y+$r-($this->mpdf->FontSize));
		}
		if ($this->mpdf->usingCoreFont) { $c=$text[$i]; }
		else { $c = mb_substr($text,$i,1,$this->mpdf->mb_enc ); }
		$this->mpdf->Cell(($w[$i]),$this->mpdf->FontSize,$c,0,0,'C');	// mPDF 5.3.53
		if($fontwidth!=1){
			$this->mpdf->StopTransform();
		}
	}
	$this->mpdf->StopTransform();

	// mPDF 5.5.23
	if($align=='top' && $divider!=''){
		$wc=$this->mpdf->GetStringWidth($divider);
		$wc*=$kerning*$fontwidth;

		$this->mpdf->StartTransform();
		$this->mpdf->transformRotate(90, $x, $y);
		$this->mpdf->SetXY($x-$wc/2, $y-$r);
		$this->mpdf->Cell(($wc),$this->mpdf->FontSize,$divider,0,0,'C');
		$this->mpdf->StopTransform();

		$this->mpdf->StartTransform();
		$this->mpdf->transformRotate(-90, $x, $y);
		$this->mpdf->SetXY($x-$wc/2, $y-$r);
		$this->mpdf->Cell(($wc),$this->mpdf->FontSize,$divider,0,0,'C');
		$this->mpdf->StopTransform();
	}
}

function Shaded_box( $text,$font='',$fontstyle='B',$szfont='',$width='70%',$style='DF',$radius=2.5,$fill='#FFFFFF',$color='#000000',$pad=2 )
{
// F (shading - no line),S (line, no shading),DF (both)
	if (!$font) { $font= $this->mpdf->default_font; }
	if (!$szfont) { $szfont = ($this->mpdf->default_font_size * 1.8); }

	$text = $this->mpdf->purify_utf8_text($text);
	if ($this->mpdf->text_input_as_HTML) {
		$text = $this->mpdf->all_entities_to_utf8($text);
	}
	if ($this->mpdf->usingCoreFont) { $text = mb_convert_encoding($text,$this->mpdf->mb_enc,'UTF-8'); }
	// DIRECTIONALITY
	$this->mpdf->magic_reverse_dir($text, true, $this->mpdf->directionality);	// *RTL*
	// Font-specific ligature substitution for Indic fonts
	if (isset($this->mpdf->CurrentFont['indic']) && $this->mpdf->CurrentFont['indic']) $this->mpdf->ConvertIndic($text);	// *INDIC*
	$text = ' '.$text.' ';
	if (!$width) { $width = $this->mpdf->pgwidth; } else { $width=$this->mpdf->ConvertSize($width,$this->mpdf->pgwidth); }
	$midpt = $this->mpdf->lMargin+($this->mpdf->pgwidth/2);
	$r1  = $midpt-($width/2);	//($this->mpdf->w / 2) - 40;
	$r2  = $r1 + $width; 		//$r1 + 80;
	$y1  = $this->mpdf->y;


	$mid = ($r1 + $r2 ) / 2;
	$loop   = 0;
    
	while ( $loop == 0 )
	{
		$this->mpdf->SetFont( $font, $fontstyle, $szfont );
		$sz = $this->mpdf->GetStringWidth( $text );
		if ( ($r1+$sz) > $r2 )
			$szfont --;
		else
			$loop ++;
	}

	$y2  = $this->mpdf->FontSize+($pad*2);

	$this->mpdf->SetLineWidth(0.1);
	$fc = $this->mpdf->ConvertColor($fill);
	$tc = $this->mpdf->ConvertColor($color);
	$this->mpdf->SetFColor($fc);
	$this->mpdf->SetTColor($tc);
	$this->mpdf->RoundedRect($r1, $y1, ($r2 - $r1), $y2, $radius, $style);
	$this->mpdf->SetX( $r1);
	$this->mpdf->Cell($r2-$r1, $y2, $text, 0, 1, "C" );
	$this->mpdf->SetY($y1+$y2+2);	// +2 = mm margin below shaded box
	$this->mpdf->Reset();
}


}

?>