PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / 1.5
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin v1.5
trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 All 71 releases
pdf-print / mpdf / classes / directw.php

directw.php in PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin 1.5, at mpdf/classes/directw.php

371 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) {
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 $t=0;
234 $text=str_replace("\r",'',$text);
235 if ($this->mpdf->usingCoreFont) {
236 $nb=strlen($text);
237 for($i=0; $i<$nb; $i++){
238 $w[$i]=$this->mpdf->GetStringWidth($text[$i]);
239 $w[$i]*=$kerning*$fontwidth;
240 $t+=$w[$i];
241 }
242 }
243 else {
244 $nb=mb_strlen($text, $this->mpdf->mb_enc );
245 $lastchar = '';
246 $unicode = $this->mpdf->UTF8StringToArray($text);
247 for($i=0; $i<$nb; $i++){
248 $c = mb_substr($text,$i,1,$this->mpdf->mb_enc );
249 $w[$i]=$this->mpdf->GetStringWidth($c);
250 $w[$i]*=$kerning*$fontwidth;
251 $char = $unicode[$i];
252 if ($this->mpdf->useKerning && $lastchar) {
253 if (isset($this->mpdf->CurrentFont['kerninfo'][$lastchar][$char])) {
254 $tk = $this->mpdf->CurrentFont['kerninfo'][$lastchar][$char] * ($this->mpdf->FontSize/ 1000) * $kerning * $fontwidth;
255 $w[$i] += $tk/2;
256 $w[$i-1] += $tk/2;
257 $t+=$tk;
258 }
259 }
260 $lastchar = $char;
261 $t+=$w[$i];
262 }
263 }
264 //circumference
265 $u=($r*2)*M_PI;
266 //total width of string in degrees
267 $d=($t/$u)*360;
268
269 $this->mpdf->StartTransform();
270 // rotate matrix for the first letter to center the text
271 // (half of total degrees)
272 if($align=='top'){
273 $this->mpdf->transformRotate(-$d/2, $x, $y);
274 }
275 else{
276 $this->mpdf->transformRotate($d/2, $x, $y);
277 }
278 //run through the string
279 for($i=0; $i<$nb; $i++){
280 if($align=='top'){
281 //rotate matrix half of the width of current letter + half of the width of preceding letter
282 if($i==0){
283 $this->mpdf->transformRotate((($w[$i]/2)/$u)*360, $x, $y);
284 }
285 else{
286 $this->mpdf->transformRotate((($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
287 }
288 if($fontwidth!=1){
289 $this->mpdf->StartTransform();
290 $this->mpdf->transformScale($fontwidth*100, 100, $x, $y);
291 }
292 $this->mpdf->SetXY($x-$w[$i]/2, $y-$r);
293 }
294 else{
295 //rotate matrix half of the width of current letter + half of the width of preceding letter
296 if($i==0){
297 $this->mpdf->transformRotate(-(($w[$i]/2)/$u)*360, $x, $y);
298 }
299 else{
300 $this->mpdf->transformRotate(-(($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
301 }
302 if($fontwidth!=1){
303 $this->mpdf->StartTransform();
304 $this->mpdf->transformScale($fontwidth*100, 100, $x, $y);
305 }
306 $this->mpdf->SetXY($x-$w[$i]/2, $y+$r-($this->mpdf->FontSize));
307 }
308 if ($this->mpdf->usingCoreFont) { $c=$text[$i]; }
309 else { $c = mb_substr($text,$i,1,$this->mpdf->mb_enc ); }
310 $this->mpdf->Cell(($w[$i]),$this->mpdf->FontSize,$c,0,0,'C'); // mPDF 5.3.53
311 if($fontwidth!=1){
312 $this->mpdf->StopTransform();
313 }
314 }
315 $this->mpdf->StopTransform();
316 }
317
318 function Shaded_box( $text,$font='',$fontstyle='B',$szfont='',$width='70%',$style='DF',$radius=2.5,$fill='#FFFFFF',$color='#000000',$pad=2 )
319 {
320 // F (shading - no line),S (line, no shading),DF (both)
321 if (!$font) { $font= $this->mpdf->default_font; }
322 if (!$szfont) { $szfont = ($this->mpdf->default_font_size * 1.8); }
323
324 $text = $this->mpdf->purify_utf8_text($text);
325 if ($this->mpdf->text_input_as_HTML) {
326 $text = $this->mpdf->all_entities_to_utf8($text);
327 }
328 if ($this->mpdf->usingCoreFont) { $text = mb_convert_encoding($text,$this->mpdf->mb_enc,'UTF-8'); }
329 // DIRECTIONALITY
330 $this->mpdf->magic_reverse_dir($text, true, $this->mpdf->directionality); // *RTL*
331 // Font-specific ligature substitution for Indic fonts
332 if (isset($this->mpdf->CurrentFont['indic']) && $this->mpdf->CurrentFont['indic']) $this->mpdf->ConvertIndic($text); // *INDIC*
333 $text = ' '.$text.' ';
334 if (!$width) { $width = $this->mpdf->pgwidth; } else { $width=$this->mpdf->ConvertSize($width,$this->mpdf->pgwidth); }
335 $midpt = $this->mpdf->lMargin+($this->mpdf->pgwidth/2);
336 $r1 = $midpt-($width/2); //($this->mpdf->w / 2) - 40;
337 $r2 = $r1 + $width; //$r1 + 80;
338 $y1 = $this->mpdf->y;
339
340
341 $mid = ($r1 + $r2 ) / 2;
342 $loop = 0;
343
344 while ( $loop == 0 )
345 {
346 $this->mpdf->SetFont( $font, $fontstyle, $szfont );
347 $sz = $this->mpdf->GetStringWidth( $text );
348 if ( ($r1+$sz) > $r2 )
349 $szfont --;
350 else
351 $loop ++;
352 }
353
354 $y2 = $this->mpdf->FontSize+($pad*2);
355
356 $this->mpdf->SetLineWidth(0.1);
357 $fc = $this->mpdf->ConvertColor($fill);
358 $tc = $this->mpdf->ConvertColor($color);
359 $this->mpdf->SetFColor($fc);
360 $this->mpdf->SetTColor($tc);
361 $this->mpdf->RoundedRect($r1, $y1, ($r2 - $r1), $y2, $radius, $style);
362 $this->mpdf->SetX( $r1);
363 $this->mpdf->Cell($r2-$r1, $y2, $text, 0, 1, "C" );
364 $this->mpdf->SetY($y1+$y2+2); // +2 = mm margin below shaded box
365 $this->mpdf->Reset();
366 }
367
368
369 }
370
371 ?>