]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | ||
3 | class directw { | |
4 | ||
5 | var $mpdf = null; | |
6 | ||
7 | function directw(&$mpdf) { | |
8 | $this->mpdf = $mpdf; | |
9 | } | |
10 | ||
11 | ||
12 | function Write($h,$txt,$currentx=0,$link='',$directionality='ltr',$align='') { | |
13 | if (!$align) { | |
14 | if ($directionality=='rtl') { $align = 'R'; } | |
15 | else { $align = 'L'; } | |
16 | } | |
17 | if ($h == 0) { $this->mpdf->SetLineHeight(); $h = $this->mpdf->lineheight; } | |
18 | //Output text in flowing mode | |
19 | $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x; | |
20 | ||
21 | $wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR)); | |
22 | $s=str_replace("\r",'',$txt); | |
23 | if ($this->mpdf->usingCoreFont) { $nb=strlen($s); } | |
24 | else { | |
25 | $nb=mb_strlen($s, $this->mpdf->mb_enc ); | |
26 | // handle single space character | |
27 | if(($nb==1) && $s == " ") { | |
28 | $this->mpdf->x += $this->mpdf->GetStringWidth($s); | |
29 | return; | |
30 | } | |
31 | } | |
32 | $sep=-1; | |
33 | $i=0; | |
34 | $j=0; | |
35 | $l=0; | |
36 | $nl=1; | |
37 | if (!$this->mpdf->usingCoreFont) { | |
38 | if (preg_match("/([".$this->mpdf->pregRTLchars."])/u", $txt)) { $this->mpdf->biDirectional = true; } // *RTL* | |
39 | $checkCursive=false; | |
40 | if ($this->mpdf->biDirectional) { $checkCursive=true; } // *RTL* | |
41 | else if (isset($this->mpdf->CurrentFont['indic']) && $this->mpdf->CurrentFont['indic']) { $checkCursive=true; } // *INDIC* | |
42 | while($i<$nb) { | |
43 | //Get next character | |
44 | $c = mb_substr($s,$i,1,$this->mpdf->mb_enc ); | |
45 | if($c == "\n") { | |
46 | // WORD SPACING | |
47 | $this->mpdf->ResetSpacing(); | |
48 | //Explicit line break | |
49 | $tmp = rtrim(mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc)); | |
50 | if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; } // *RTL* | |
51 | $this->mpdf->magic_reverse_dir($tmp, true, $directionality); // *RTL* | |
52 | $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link); | |
53 | $i++; | |
54 | $sep = -1; | |
55 | $j = $i; | |
56 | $l = 0; | |
57 | if($nl == 1) { | |
58 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
59 | else $this->mpdf->x=$this->mpdf->lMargin; | |
60 | $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x; | |
61 | $wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR)); | |
62 | } | |
63 | $nl++; | |
64 | continue; | |
65 | } | |
66 | if($c == " ") { $sep= $i; } | |
67 | $l += $this->mpdf->GetCharWidthNonCore($c); // mPDF 5.3.04 | |
68 | if($l > $wmax) { | |
69 | //Automatic line break (word wrapping) | |
70 | if($sep == -1) { | |
71 | // WORD SPACING | |
72 | $this->mpdf->ResetSpacing(); | |
73 | if($this->mpdf->x > $this->mpdf->lMargin) { | |
74 | //Move to next line | |
75 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
76 | else $this->mpdf->x=$this->mpdf->lMargin; | |
77 | $this->mpdf->y+=$h; | |
78 | $w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x; | |
79 | $wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR)); | |
80 | $i++; | |
81 | $nl++; | |
82 | continue; | |
83 | } | |
84 | if($i==$j) { $i++; } | |
85 | $tmp = rtrim(mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc)); | |
86 | if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; } // *RTL* | |
87 | $this->mpdf->magic_reverse_dir($tmp, true, $directionality); // *RTL* | |
88 | $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link); | |
89 | } | |
90 | else { | |
91 | $tmp = rtrim(mb_substr($s,$j,$sep-$j,$this->mpdf->mb_enc)); | |
92 | if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; } // *RTL* | |
93 | $this->mpdf->magic_reverse_dir($tmp, true, $directionality); // *RTL* | |
94 | ||
95 | if($align=='J') { | |
96 | ////////////////////////////////////////// | |
97 | // JUSTIFY J using Unicode fonts (Word spacing doesn't work) | |
98 | // WORD SPACING | |
99 | // Change NON_BREAKING SPACE to spaces so they are 'spaced' properly | |
100 | $tmp = str_replace(chr(194).chr(160),chr(32),$tmp ); | |
101 | $len_ligne = $this->mpdf->GetStringWidth($tmp ); | |
102 | $nb_carac = mb_strlen( $tmp , $this->mpdf->mb_enc ) ; | |
103 | $nb_spaces = mb_substr_count( $tmp ,' ', $this->mpdf->mb_enc ) ; | |
104 | $inclCursive=false; | |
105 | if ($checkCursive) { | |
106 | if (preg_match("/([".$this->mpdf->pregRTLchars."])/u", $tmp)) { $inclCursive = true; } // *RTL* | |
107 | if (preg_match("/([".$this->mpdf->pregHIchars.$this->mpdf->pregBNchars.$this->mpdf->pregPAchars."])/u", $tmp)) { $inclCursive = true; } // *INDIC* | |
108 | } | |
109 | list($charspacing,$ws) = $this->mpdf->GetJspacing($nb_carac,$nb_spaces,((($w-2) - $len_ligne) * _MPDFK),$inclCursive); | |
110 | $this->mpdf->SetSpacing($charspacing,$ws); | |
111 | ////////////////////////////////////////// | |
112 | } | |
113 | $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link); | |
114 | $i=$sep+1; | |
115 | } | |
116 | $sep = -1; | |
117 | $j = $i; | |
118 | $l = 0; | |
119 | if($nl==1) { | |
120 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
121 | else $this->mpdf->x=$this->mpdf->lMargin; | |
122 | $w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x; | |
123 | $wmax = ($w - ($this->mpdf->cMarginL+$this->mpdf->cMarginR)); | |
124 | } | |
125 | $nl++; | |
126 | } | |
127 | else { $i++; } | |
128 | } | |
129 | //Last chunk | |
130 | // WORD SPACING | |
131 | $this->mpdf->ResetSpacing(); | |
132 | } | |
133 | else { | |
134 | while($i<$nb) { | |
135 | //Get next character | |
136 | $c=$s[$i]; | |
137 | if($c == "\n") { | |
138 | //Explicit line break | |
139 | // WORD SPACING | |
140 | $this->mpdf->ResetSpacing(); | |
141 | $this->mpdf->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, $align, $fill, $link); | |
142 | $i++; | |
143 | $sep = -1; | |
144 | $j = $i; | |
145 | $l = 0; | |
146 | if($nl == 1) { | |
147 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
148 | else $this->mpdf->x=$this->mpdf->lMargin; | |
149 | $w = $this->mpdf->w - $this->mpdf->rMargin - $this->mpdf->x; | |
150 | $wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR); | |
151 | } | |
152 | $nl++; | |
153 | continue; | |
154 | } | |
155 | if($c == " ") { $sep= $i; } | |
156 | $l += $this->mpdf->GetCharWidthCore($c); // mPDF 5.3.04 | |
157 | if($l > $wmax) { | |
158 | //Automatic line break (word wrapping) | |
159 | if($sep == -1) { | |
160 | // WORD SPACING | |
161 | $this->mpdf->ResetSpacing(); | |
162 | if($this->mpdf->x > $this->mpdf->lMargin) { | |
163 | //Move to next line | |
164 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
165 | else $this->mpdf->x=$this->mpdf->lMargin; | |
166 | $this->mpdf->y+=$h; | |
167 | $w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x; | |
168 | $wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR); | |
169 | $i++; | |
170 | $nl++; | |
171 | continue; | |
172 | } | |
173 | if($i==$j) { $i++; } | |
174 | $this->mpdf->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, $align, $fill, $link); | |
175 | } | |
176 | else { | |
177 | $tmp = substr($s, $j, $sep-$j); | |
178 | if($align=='J') { | |
179 | ////////////////////////////////////////// | |
180 | // JUSTIFY J using Unicode fonts (Word spacing doesn't work) | |
181 | // WORD SPACING | |
182 | // Change NON_BREAKING SPACE to spaces so they are 'spaced' properly | |
183 | $tmp = str_replace(chr(160),chr(32),$tmp ); | |
184 | $len_ligne = $this->mpdf->GetStringWidth($tmp ); | |
185 | $nb_carac = strlen( $tmp ) ; | |
186 | $nb_spaces = substr_count( $tmp ,' ' ) ; | |
187 | list($charspacing,$ws) = $this->mpdf->GetJspacing($nb_carac,$nb_spaces,((($w-2) - $len_ligne) * _MPDFK),$false); | |
188 | $this->mpdf->SetSpacing($charspacing,$ws); | |
189 | ////////////////////////////////////////// | |
190 | } | |
191 | $this->mpdf->Cell($w, $h, $tmp, 0, 2, $align, $fill, $link); | |
192 | $i=$sep+1; | |
193 | } | |
194 | $sep = -1; | |
195 | $j = $i; | |
196 | $l = 0; | |
197 | if($nl==1) { | |
198 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
199 | else $this->mpdf->x=$this->mpdf->lMargin; | |
200 | $w=$this->mpdf->w-$this->mpdf->rMargin-$this->mpdf->x; | |
201 | $wmax=$w-($this->mpdf->cMarginL+$this->mpdf->cMarginR); | |
202 | } | |
203 | $nl++; | |
204 | } | |
205 | else { | |
206 | $i++; | |
207 | } | |
208 | } | |
209 | // WORD SPACING | |
210 | $this->mpdf->ResetSpacing(); | |
211 | } | |
212 | //Last chunk | |
213 | if($i!=$j) { | |
214 | if ($currentx != 0) $this->mpdf->x=$currentx; | |
215 | else $this->mpdf->x=$this->mpdf->lMargin; | |
216 | if ($this->mpdf->usingCoreFont) { $tmp = substr($s,$j,$i-$j); } | |
217 | else { | |
218 | $tmp = mb_substr($s,$j,$i-$j,$this->mpdf->mb_enc); | |
219 | if ($directionality == 'rtl' && $align == 'J') { $align = 'R'; } // *RTL* | |
220 | $this->mpdf->magic_reverse_dir($tmp, true, $directionality); // *RTL* | |
221 | } | |
222 | $this->mpdf->Cell($w,$h,$tmp,0,0,$align,$fill,$link); | |
223 | } | |
224 | } | |
225 | ||
226 | ||
227 | function CircularText($x, $y, $r, $text, $align='top', $fontfamily='', $fontsizePt=0, $fontstyle='', $kerning=120, $fontwidth=100, $divider='') { // mPDF 5.5.23 | |
228 | if ($font || $fontstyle || $fontsizePt) $this->mpdf->SetFont($fontfamily,$fontstyle,$fontsizePt); | |
229 | $kerning/=100; | |
230 | $fontwidth/=100; | |
231 | if($kerning==0) $this->mpdf->Error('Please use values unequal to zero for kerning (CircularText)'); | |
232 | if($fontwidth==0) $this->mpdf->Error('Please use values unequal to zero for font width (CircularText)'); | |
233 | $text=str_replace("\r",'',$text); | |
234 | //circumference | |
235 | $u=($r*2)*M_PI; | |
236 | // mPDF 5.5.23 | |
237 | $checking = true; | |
238 | $autoset = false; | |
239 | while($checking) { | |
240 | $t=0; | |
241 | $w = array(); | |
242 | if ($this->mpdf->usingCoreFont) { | |
243 | $nb=strlen($text); | |
244 | for($i=0; $i<$nb; $i++){ | |
245 | $w[$i]=$this->mpdf->GetStringWidth($text[$i]); | |
246 | $w[$i]*=$kerning*$fontwidth; | |
247 | $t+=$w[$i]; | |
248 | } | |
249 | } | |
250 | else { | |
251 | $nb=mb_strlen($text, $this->mpdf->mb_enc ); | |
252 | $lastchar = ''; | |
253 | $unicode = $this->mpdf->UTF8StringToArray($text); | |
254 | for($i=0; $i<$nb; $i++){ | |
255 | $c = mb_substr($text,$i,1,$this->mpdf->mb_enc ); | |
256 | $w[$i]=$this->mpdf->GetStringWidth($c); | |
257 | $w[$i]*=$kerning*$fontwidth; | |
258 | $char = $unicode[$i]; | |
259 | if ($this->mpdf->useKerning && $lastchar) { | |
260 | if (isset($this->mpdf->CurrentFont['kerninfo'][$lastchar][$char])) { | |
261 | $tk = $this->mpdf->CurrentFont['kerninfo'][$lastchar][$char] * ($this->mpdf->FontSize/ 1000) * $kerning * $fontwidth; | |
262 | $w[$i] += $tk/2; | |
263 | $w[$i-1] += $tk/2; | |
264 | $t+=$tk; | |
265 | } | |
266 | } | |
267 | $lastchar = $char; | |
268 | $t+=$w[$i]; | |
269 | } | |
270 | } | |
271 | if ($fontsizePt>=0 || $autoset) { $checking = false; } | |
272 | else { | |
273 | $t+=$this->mpdf->GetStringWidth(' '); | |
274 | if ($divider) | |
275 | $t+=$this->mpdf->GetStringWidth(' '); | |
276 | if ($fontsizePt==-2) | |
277 | $fontsizePt = $this->mpdf->FontSizePt * 0.5 * $u/$t; | |
278 | else | |
279 | $fontsizePt = $this->mpdf->FontSizePt * $u/$t; | |
280 | $this->mpdf->SetFontSize($fontsizePt); | |
281 | $autoset = true; | |
282 | } | |
283 | } | |
284 | ||
285 | //total width of string in degrees | |
286 | $d=($t/$u)*360; | |
287 | ||
288 | $this->mpdf->StartTransform(); | |
289 | // rotate matrix for the first letter to center the text | |
290 | // (half of total degrees) | |
291 | if($align=='top'){ | |
292 | $this->mpdf->transformRotate(-$d/2, $x, $y); | |
293 | } | |
294 | else{ | |
295 | $this->mpdf->transformRotate($d/2, $x, $y); | |
296 | } | |
297 | //run through the string | |
298 | for($i=0; $i<$nb; $i++){ | |
299 | if($align=='top'){ | |
300 | //rotate matrix half of the width of current letter + half of the width of preceding letter | |
301 | if($i==0){ | |
302 | $this->mpdf->transformRotate((($w[$i]/2)/$u)*360, $x, $y); | |
303 | } | |
304 | else{ | |
305 | $this->mpdf->transformRotate((($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y); | |
306 | } | |
307 | if($fontwidth!=1){ | |
308 | $this->mpdf->StartTransform(); | |
309 | $this->mpdf->transformScale($fontwidth*100, 100, $x, $y); | |
310 | } | |
311 | $this->mpdf->SetXY($x-$w[$i]/2, $y-$r); | |
312 | } | |
313 | else{ | |
314 | //rotate matrix half of the width of current letter + half of the width of preceding letter | |
315 | if($i==0){ | |
316 | $this->mpdf->transformRotate(-(($w[$i]/2)/$u)*360, $x, $y); | |
317 | } | |
318 | else{ | |
319 | $this->mpdf->transformRotate(-(($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y); | |
320 | } | |
321 | if($fontwidth!=1){ | |
322 | $this->mpdf->StartTransform(); | |
323 | $this->mpdf->transformScale($fontwidth*100, 100, $x, $y); | |
324 | } | |
325 | $this->mpdf->SetXY($x-$w[$i]/2, $y+$r-($this->mpdf->FontSize)); | |
326 | } | |
327 | if ($this->mpdf->usingCoreFont) { $c=$text[$i]; } | |
328 | else { $c = mb_substr($text,$i,1,$this->mpdf->mb_enc ); } | |
329 | $this->mpdf->Cell(($w[$i]),$this->mpdf->FontSize,$c,0,0,'C'); // mPDF 5.3.53 | |
330 | if($fontwidth!=1){ | |
331 | $this->mpdf->StopTransform(); | |
332 | } | |
333 | } | |
334 | $this->mpdf->StopTransform(); | |
335 | ||
336 | // mPDF 5.5.23 | |
337 | if($align=='top' && $divider!=''){ | |
338 | $wc=$this->mpdf->GetStringWidth($divider); | |
339 | $wc*=$kerning*$fontwidth; | |
340 | ||
341 | $this->mpdf->StartTransform(); | |
342 | $this->mpdf->transformRotate(90, $x, $y); | |
343 | $this->mpdf->SetXY($x-$wc/2, $y-$r); | |
344 | $this->mpdf->Cell(($wc),$this->mpdf->FontSize,$divider,0,0,'C'); | |
345 | $this->mpdf->StopTransform(); | |
346 | ||
347 | $this->mpdf->StartTransform(); | |
348 | $this->mpdf->transformRotate(-90, $x, $y); | |
349 | $this->mpdf->SetXY($x-$wc/2, $y-$r); | |
350 | $this->mpdf->Cell(($wc),$this->mpdf->FontSize,$divider,0,0,'C'); | |
351 | $this->mpdf->StopTransform(); | |
352 | } | |
353 | } | |
354 | ||
355 | function Shaded_box( $text,$font='',$fontstyle='B',$szfont='',$width='70%',$style='DF',$radius=2.5,$fill='#FFFFFF',$color='#000000',$pad=2 ) | |
356 | { | |
357 | // F (shading - no line),S (line, no shading),DF (both) | |
358 | if (!$font) { $font= $this->mpdf->default_font; } | |
359 | if (!$szfont) { $szfont = ($this->mpdf->default_font_size * 1.8); } | |
360 | ||
361 | $text = $this->mpdf->purify_utf8_text($text); | |
362 | if ($this->mpdf->text_input_as_HTML) { | |
363 | $text = $this->mpdf->all_entities_to_utf8($text); | |
364 | } | |
365 | if ($this->mpdf->usingCoreFont) { $text = mb_convert_encoding($text,$this->mpdf->mb_enc,'UTF-8'); } | |
366 | // DIRECTIONALITY | |
367 | $this->mpdf->magic_reverse_dir($text, true, $this->mpdf->directionality); // *RTL* | |
368 | // Font-specific ligature substitution for Indic fonts | |
369 | if (isset($this->mpdf->CurrentFont['indic']) && $this->mpdf->CurrentFont['indic']) $this->mpdf->ConvertIndic($text); // *INDIC* | |
370 | $text = ' '.$text.' '; | |
371 | if (!$width) { $width = $this->mpdf->pgwidth; } else { $width=$this->mpdf->ConvertSize($width,$this->mpdf->pgwidth); } | |
372 | $midpt = $this->mpdf->lMargin+($this->mpdf->pgwidth/2); | |
373 | $r1 = $midpt-($width/2); //($this->mpdf->w / 2) - 40; | |
374 | $r2 = $r1 + $width; //$r1 + 80; | |
375 | $y1 = $this->mpdf->y; | |
376 | ||
377 | ||
378 | $mid = ($r1 + $r2 ) / 2; | |
379 | $loop = 0; | |
380 | ||
381 | while ( $loop == 0 ) | |
382 | { | |
383 | $this->mpdf->SetFont( $font, $fontstyle, $szfont ); | |
384 | $sz = $this->mpdf->GetStringWidth( $text ); | |
385 | if ( ($r1+$sz) > $r2 ) | |
386 | $szfont --; | |
387 | else | |
388 | $loop ++; | |
389 | } | |
390 | ||
391 | $y2 = $this->mpdf->FontSize+($pad*2); | |
392 | ||
393 | $this->mpdf->SetLineWidth(0.1); | |
394 | $fc = $this->mpdf->ConvertColor($fill); | |
395 | $tc = $this->mpdf->ConvertColor($color); | |
396 | $this->mpdf->SetFColor($fc); | |
397 | $this->mpdf->SetTColor($tc); | |
398 | $this->mpdf->RoundedRect($r1, $y1, ($r2 - $r1), $y2, $radius, $style); | |
399 | $this->mpdf->SetX( $r1); | |
400 | $this->mpdf->Cell($r2-$r1, $y2, $text, 0, 1, "C" ); | |
401 | $this->mpdf->SetY($y1+$y2+2); // +2 = mm margin below shaded box | |
402 | $this->mpdf->Reset(); | |
403 | } | |
404 | ||
405 | ||
406 | } | |
407 | ||
408 | ?> |