PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / 1.8.6
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin v1.8.6
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.8.6, at mpdf/classes/directw.php

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