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