Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
46 / 46 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Alignment | |
100.00% |
46 / 46 |
|
100.00% |
5 / 5 |
15 | |
100.00% |
1 / 1 |
| createLeft | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRight | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createCenter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStrings | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
11 | |||
| isCenter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Pdf\Document\Page\Text; |
| 16 | |
| 17 | use Pop\Pdf\Document\Exception; |
| 18 | use Pop\Pdf\Document\Page; |
| 19 | use Pop\Pdf\Document\Font; |
| 20 | |
| 21 | /** |
| 22 | * Pdf page text alignment class |
| 23 | * |
| 24 | * @category Pop |
| 25 | * @package Pop\Pdf |
| 26 | * @author Nick Sagona, III <nick@popphp.org> |
| 27 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 28 | * @license https://www.popphp.org/license New BSD License |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | class Alignment extends AbstractAlignment |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * Alignment constants |
| 36 | */ |
| 37 | const CENTER = 'CENTER'; |
| 38 | |
| 39 | /** |
| 40 | * Create LEFT alignment object |
| 41 | * |
| 42 | * @param int $leftX |
| 43 | * @param int $rightX |
| 44 | * @param int $leading |
| 45 | * @return Alignment |
| 46 | */ |
| 47 | public static function createLeft(int $leftX = 0, int $rightX = 0, int $leading = 0): Alignment |
| 48 | { |
| 49 | return new self(self::LEFT, $leftX, $rightX, $leading); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Create RIGHT alignment object |
| 54 | * |
| 55 | * @param int $leftX |
| 56 | * @param int $rightX |
| 57 | * @param int $leading |
| 58 | * @return Alignment |
| 59 | */ |
| 60 | public static function createRight(int $leftX = 0, int $rightX = 0, int $leading = 0): Alignment |
| 61 | { |
| 62 | return new self(self::RIGHT, $leftX, $rightX, $leading); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Create CENTER alignment object |
| 67 | * |
| 68 | * @param int $leftX |
| 69 | * @param int $rightX |
| 70 | * @param int $leading |
| 71 | * @return Alignment |
| 72 | */ |
| 73 | public static function createCenter(int $leftX = 0, int $rightX = 0, int $leading = 0): Alignment |
| 74 | { |
| 75 | return new self(self::CENTER, $leftX, $rightX, $leading); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get strings |
| 80 | * |
| 81 | * @param Page\Text $text |
| 82 | * @param Font $font |
| 83 | * @param int $startY |
| 84 | * @throws Exception |
| 85 | * @return array |
| 86 | */ |
| 87 | public function getStrings(Page\Text $text, Font $font, int $startY): array |
| 88 | { |
| 89 | $strings = []; |
| 90 | $curString = ''; |
| 91 | $curWidth = 0; |
| 92 | $words = explode(' ', $text->getString()); |
| 93 | $wrapLength = abs($this->rightX - $this->leftX); |
| 94 | $startX = $this->leftX; |
| 95 | $size = $text->getSize(); |
| 96 | $spaceWidth = $font->getStringWidth(' ', $size); |
| 97 | |
| 98 | if ((int)$this->leading == 0) { |
| 99 | $this->leading = $size; |
| 100 | } |
| 101 | |
| 102 | foreach ($words as $word) { |
| 103 | $newString = ($curString != '') ? $curString . ' ' . $word : $word; |
| 104 | $wordWidth = $font->getStringWidth($word, $size); |
| 105 | $newWidth = ($curString != '') ? ($curWidth + $spaceWidth + $wordWidth) : $wordWidth; |
| 106 | |
| 107 | if ($newWidth <= $wrapLength) { |
| 108 | $curString = $newString; |
| 109 | $curWidth = $newWidth; |
| 110 | } else { |
| 111 | if ($this->isRight()) { |
| 112 | $x = $this->leftX + ($wrapLength - $curWidth); |
| 113 | } else if ($this->isCenter()) { |
| 114 | $x = $this->leftX + (($wrapLength - $curWidth) / 2); |
| 115 | } else { |
| 116 | $x = $startX; |
| 117 | } |
| 118 | |
| 119 | $strings[] = [ |
| 120 | 'string' => $curString, |
| 121 | 'x' => $x, |
| 122 | 'y' => $startY |
| 123 | ]; |
| 124 | $curString = $word; |
| 125 | $curWidth = $wordWidth; |
| 126 | $startY -= $this->leading; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!empty($curString)) { |
| 131 | if ($this->isRight()) { |
| 132 | $x = $this->leftX + ($wrapLength - $curWidth); |
| 133 | } else if ($this->isCenter()) { |
| 134 | $x = $this->leftX + (($wrapLength - $curWidth) / 2); |
| 135 | } else { |
| 136 | $x = $startX; |
| 137 | } |
| 138 | $strings[] = [ |
| 139 | 'string' => $curString, |
| 140 | 'x' => $x, |
| 141 | 'y' => $startY |
| 142 | ]; |
| 143 | } |
| 144 | |
| 145 | return $strings; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Is CENTER alignment |
| 150 | * |
| 151 | * @return bool |
| 152 | */ |
| 153 | public function isCenter(): bool |
| 154 | { |
| 155 | return ($this->alignment == self::CENTER); |
| 156 | } |
| 157 | |
| 158 | } |