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

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