directw.php 13 KB

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