Guest User

Untitled

a guest
Nov 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. <?php
  2. namespace Mcustiel\Tests;
  3.  
  4. use setasign\Fpdi\Fpdi;
  5.  
  6. class MyFpdi extends Fpdi
  7. {
  8. const NEW_LINE = "\n";
  9. const CARRIAGE_RETURN = "\r";
  10. const SPACE = ' ';
  11. /**
  12. * @var float
  13. */
  14. public $angle = 0;
  15. /**
  16. * @param float $c
  17. * @param float $m
  18. * @param float $y
  19. * @param float $k
  20. */
  21. public function SetTextColorCMYK($c, $m, $y, $k)
  22. {
  23. // Set color for text
  24. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $c / 100, $m / 100, $y / 100, $k / 100);
  25. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  26. }
  27. /**
  28. * @param float $angle
  29. */
  30. public function Rotate($angle)
  31. {
  32. if ($this->angle != 0) {
  33. $this->_out('Q');
  34. }
  35. $this->angle = $angle;
  36. if ($angle != 0) {
  37. $angle = deg2rad($angle);
  38. $cos = cos($angle);
  39. $sin = sin($angle);
  40. $horizontalTranslation = $this->x * $this->k;
  41. $verticalTranslation = ($this->h - $this->y) * $this->k;
  42. $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $cos, $sin, - $sin, $cos, $horizontalTranslation, $verticalTranslation, - $horizontalTranslation, - $verticalTranslation));
  43. }
  44. }
  45. public function _endpage()
  46. {
  47. if ($this->angle != 0) {
  48. $this->angle = 0;
  49. $this->_out('Q');
  50. }
  51. parent::_endpage();
  52. }
  53. /**
  54. * @param string $text
  55. * @param float $width
  56. * @param float $height
  57. * @param string $align
  58. * @param string $valign
  59. * @param float $lineHeightScale
  60. */
  61. public function drawTextBox($text, $width, $height, $align = 'L', $valign = 'T', $lineHeightScale = 1.0)
  62. {
  63. $cellMargin = $this->cMargin;
  64. $this->cMargin = 0;
  65. $xPos = $this->GetX();
  66. $yPos = $this->GetY();
  67. $rowHeight = $this->FontSize * $lineHeightScale;
  68. $textrows = $this->drawRows($width, $rowHeight, $text, $align, 0, 0, 0);
  69. $maxrows = floor($height / $this->FontSize);
  70. $rows = min($textrows, $maxrows);
  71. $yDelta = 0;
  72. if (strtoupper($valign) == 'M') {
  73. $yDelta = ($height - $rows * $rowHeight) / 2;
  74. }
  75. if (strtoupper($valign) == 'B') {
  76. $yDelta = $height - $rows * $rowHeight;
  77. }
  78. $this->SetY($yPos + $yDelta);
  79. $this->SetX($xPos);
  80. $this->drawRows($width, $rowHeight, $text, $align, false, $rows, 1);
  81. $this->cMargin = $cellMargin;
  82. }
  83. /**
  84. * @param float $width
  85. * @param float $height
  86. * @param string $text
  87. * @param string $align
  88. * @param boolean $fill
  89. * @param int $maxline
  90. * @param int $prn
  91. * @return string|int
  92. */
  93. public function drawRows($width, $height, $text, $align = 'J', $fill = false, $maxline = 0, $prn = 0)
  94. {
  95. $cw = &$this->CurrentFont['cw'];
  96. if ($width == 0) {
  97. $width = $this->w - $this->rMargin - $this->x;
  98. }
  99. $maxWidth = ($width - 2 * $this->cMargin) * 1000 / $this->FontSize;
  100. $string = str_replace(self::CARRIAGE_RETURN, '', $text);
  101. $stringLength = strlen($string);
  102. if ($stringLength > 0 && $string[$stringLength - 1] == self::NEW_LINE) {
  103. $stringLength --;
  104. }
  105. $sep = - 1;
  106. $i = 0;
  107. $j = 0;
  108. $length = 0;
  109. $spacesCounter = 0;
  110. $newLinesCounter = 1;
  111. $ls = 0;
  112. while ($i < $stringLength) {
  113. // Get next character
  114. $currentCharacter = $string[$i];
  115. if ($currentCharacter == self::NEW_LINE) {
  116. // Explicit line break
  117. if ($this->ws > 0) {
  118. $this->ws = 0;
  119. if ($prn == 1) {
  120. $this->_out('0 Tw');
  121. }
  122. }
  123. if ($prn == 1) {
  124. $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
  125. }
  126. $i ++;
  127. $sep = - 1;
  128. $j = $i;
  129. $length = 0;
  130. $spacesCounter = 0;
  131. $newLinesCounter ++;
  132. if ($maxline && $newLinesCounter > $maxline) {
  133. return substr($string, $i);
  134. }
  135. continue;
  136. }
  137. if ($currentCharacter == self::SPACE) {
  138. $sep = $i;
  139. $ls = $length;
  140. $spacesCounter ++;
  141. }
  142. $length += $cw[$currentCharacter];
  143. if ($length > $maxWidth) {
  144. // Automatic line break
  145. if ($sep == - 1) {
  146. if ($i == $j) {
  147. $i ++;
  148. }
  149. if ($this->ws > 0) {
  150. $this->ws = 0;
  151. if ($prn == 1) {
  152. $this->_out('0 Tw');
  153. }
  154. }
  155. if ($prn == 1) {
  156. $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
  157. }
  158. } else {
  159. if ($align == 'J') {
  160. $this->ws = ($spacesCounter > 1) ? ($maxWidth - $ls) / 1000 * $this->FontSize / ($spacesCounter - 1) : 0;
  161. if ($prn == 1) {
  162. $this->_out(sprintf('%.3F Tw', $this->ws * $this->k));
  163. }
  164. }
  165. if ($prn == 1) {
  166. $this->Cell($width, $height, substr($string, $j, $sep - $j), 0, 2, $align, $fill);
  167. }
  168. $i = $sep + 1;
  169. }
  170. $sep = - 1;
  171. $j = $i;
  172. $length = 0;
  173. $spacesCounter = 0;
  174. $newLinesCounter ++;
  175. if ($maxline && $newLinesCounter > $maxline) {
  176. return substr($string, $i);
  177. }
  178. } else {
  179. $i ++;
  180. }
  181. }
  182. // Last chunk
  183. if ($this->ws > 0) {
  184. $this->ws = 0;
  185. if ($prn == 1) {
  186. $this->_out('0 Tw');
  187. }
  188. }
  189. if ($prn == 1) {
  190. $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
  191. }
  192. $this->x = $this->lMargin;
  193. return $newLinesCounter;
  194. }
  195. }
Add Comment
Please, Sign In to add comment