PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mpdf/classes/directw.php

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