PageRenderTime 41ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/BITM_Project/vendor/mpdf/mpdf/classes/directw.php

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