Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Mcustiel\Tests;
- use setasign\Fpdi\Fpdi;
- class MyFpdi extends Fpdi
- {
- const NEW_LINE = "\n";
- const CARRIAGE_RETURN = "\r";
- const SPACE = ' ';
- /**
- * @var float
- */
- public $angle = 0;
- /**
- * @param float $c
- * @param float $m
- * @param float $y
- * @param float $k
- */
- public function SetTextColorCMYK($c, $m, $y, $k)
- {
- // Set color for text
- $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $c / 100, $m / 100, $y / 100, $k / 100);
- $this->ColorFlag = ($this->FillColor != $this->TextColor);
- }
- /**
- * @param float $angle
- */
- public function Rotate($angle)
- {
- if ($this->angle != 0) {
- $this->_out('Q');
- }
- $this->angle = $angle;
- if ($angle != 0) {
- $angle = deg2rad($angle);
- $cos = cos($angle);
- $sin = sin($angle);
- $horizontalTranslation = $this->x * $this->k;
- $verticalTranslation = ($this->h - $this->y) * $this->k;
- $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));
- }
- }
- public function _endpage()
- {
- if ($this->angle != 0) {
- $this->angle = 0;
- $this->_out('Q');
- }
- parent::_endpage();
- }
- /**
- * @param string $text
- * @param float $width
- * @param float $height
- * @param string $align
- * @param string $valign
- * @param float $lineHeightScale
- */
- public function drawTextBox($text, $width, $height, $align = 'L', $valign = 'T', $lineHeightScale = 1.0)
- {
- $cellMargin = $this->cMargin;
- $this->cMargin = 0;
- $xPos = $this->GetX();
- $yPos = $this->GetY();
- $rowHeight = $this->FontSize * $lineHeightScale;
- $textrows = $this->drawRows($width, $rowHeight, $text, $align, 0, 0, 0);
- $maxrows = floor($height / $this->FontSize);
- $rows = min($textrows, $maxrows);
- $yDelta = 0;
- if (strtoupper($valign) == 'M') {
- $yDelta = ($height - $rows * $rowHeight) / 2;
- }
- if (strtoupper($valign) == 'B') {
- $yDelta = $height - $rows * $rowHeight;
- }
- $this->SetY($yPos + $yDelta);
- $this->SetX($xPos);
- $this->drawRows($width, $rowHeight, $text, $align, false, $rows, 1);
- $this->cMargin = $cellMargin;
- }
- /**
- * @param float $width
- * @param float $height
- * @param string $text
- * @param string $align
- * @param boolean $fill
- * @param int $maxline
- * @param int $prn
- * @return string|int
- */
- public function drawRows($width, $height, $text, $align = 'J', $fill = false, $maxline = 0, $prn = 0)
- {
- $cw = &$this->CurrentFont['cw'];
- if ($width == 0) {
- $width = $this->w - $this->rMargin - $this->x;
- }
- $maxWidth = ($width - 2 * $this->cMargin) * 1000 / $this->FontSize;
- $string = str_replace(self::CARRIAGE_RETURN, '', $text);
- $stringLength = strlen($string);
- if ($stringLength > 0 && $string[$stringLength - 1] == self::NEW_LINE) {
- $stringLength --;
- }
- $sep = - 1;
- $i = 0;
- $j = 0;
- $length = 0;
- $spacesCounter = 0;
- $newLinesCounter = 1;
- $ls = 0;
- while ($i < $stringLength) {
- // Get next character
- $currentCharacter = $string[$i];
- if ($currentCharacter == self::NEW_LINE) {
- // Explicit line break
- if ($this->ws > 0) {
- $this->ws = 0;
- if ($prn == 1) {
- $this->_out('0 Tw');
- }
- }
- if ($prn == 1) {
- $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
- }
- $i ++;
- $sep = - 1;
- $j = $i;
- $length = 0;
- $spacesCounter = 0;
- $newLinesCounter ++;
- if ($maxline && $newLinesCounter > $maxline) {
- return substr($string, $i);
- }
- continue;
- }
- if ($currentCharacter == self::SPACE) {
- $sep = $i;
- $ls = $length;
- $spacesCounter ++;
- }
- $length += $cw[$currentCharacter];
- if ($length > $maxWidth) {
- // Automatic line break
- if ($sep == - 1) {
- if ($i == $j) {
- $i ++;
- }
- if ($this->ws > 0) {
- $this->ws = 0;
- if ($prn == 1) {
- $this->_out('0 Tw');
- }
- }
- if ($prn == 1) {
- $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
- }
- } else {
- if ($align == 'J') {
- $this->ws = ($spacesCounter > 1) ? ($maxWidth - $ls) / 1000 * $this->FontSize / ($spacesCounter - 1) : 0;
- if ($prn == 1) {
- $this->_out(sprintf('%.3F Tw', $this->ws * $this->k));
- }
- }
- if ($prn == 1) {
- $this->Cell($width, $height, substr($string, $j, $sep - $j), 0, 2, $align, $fill);
- }
- $i = $sep + 1;
- }
- $sep = - 1;
- $j = $i;
- $length = 0;
- $spacesCounter = 0;
- $newLinesCounter ++;
- if ($maxline && $newLinesCounter > $maxline) {
- return substr($string, $i);
- }
- } else {
- $i ++;
- }
- }
- // Last chunk
- if ($this->ws > 0) {
- $this->ws = 0;
- if ($prn == 1) {
- $this->_out('0 Tw');
- }
- }
- if ($prn == 1) {
- $this->Cell($width, $height, substr($string, $j, $i - $j), 0, 2, $align, $fill);
- }
- $this->x = $this->lMargin;
- return $newLinesCounter;
- }
- }
Add Comment
Please, Sign In to add comment