|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpTui\Tui\Text\LineComposer; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use PhpTui\Tui\Text\LineComposer; |
| 9 | +use PhpTui\Tui\Text\StyledGrapheme; |
| 10 | +use PhpTui\Tui\Widget\HorizontalAlignment; |
| 11 | + |
| 12 | +final class WordWrapper implements LineComposer |
| 13 | +{ |
| 14 | + private const NBSP = "\u{00a0}"; |
| 15 | + |
| 16 | + /** |
| 17 | + * @param list<array{list<StyledGrapheme>,HorizontalAlignment}> $lines |
| 18 | + */ |
| 19 | + public function __construct( |
| 20 | + private readonly array $lines, |
| 21 | + private readonly int $maxLineWidth, |
| 22 | + private readonly bool $trim = false, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function nextLine(): Generator |
| 27 | + { |
| 28 | + if ($this->maxLineWidth === 0) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + $wrappedLines = []; |
| 33 | + foreach ($this->lines as $line) { |
| 34 | + /** |
| 35 | + * @var StyledGrapheme[] $lineSymbols |
| 36 | + * @var HorizontalAlignment $lineAlignment |
| 37 | + */ |
| 38 | + [$lineSymbols, $lineAlignment] = $line; |
| 39 | + [$currentLine, $currentLineWidth] = [[], 0]; |
| 40 | + [$currentWord, $currentWordWidth] = [[], 0]; |
| 41 | + [$whitespaceBuffer, $whitespacesWidth] = [[], 0]; |
| 42 | + $hasSeenNonWhitespace = false; |
| 43 | + |
| 44 | + foreach ($lineSymbols as $symbol) { |
| 45 | + $isWhitespace = $this->isWhitespace($symbol); |
| 46 | + $symbolWidth = $symbol->symbolWidth(); |
| 47 | + if ($symbolWidth > $this->maxLineWidth) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // Append finished word to current line |
| 52 | + if ( |
| 53 | + $hasSeenNonWhitespace && $isWhitespace |
| 54 | + // Append if trimmed (whitespaces removed) word would overflow |
| 55 | + || $this->trim && ($currentWordWidth + $symbolWidth) > $this->maxLineWidth && $currentLine === [] |
| 56 | + // Append if removed whitespace would overflow -> reset whitespace counting to prevent overflow |
| 57 | + || $this->trim && ($whitespacesWidth + $symbolWidth) > $this->maxLineWidth && $currentLine === [] |
| 58 | + // Append if complete word would overflow |
| 59 | + || !$this->trim && ($currentWordWidth + $whitespacesWidth + $symbolWidth) > $this->maxLineWidth && $currentLine === [] |
| 60 | + ) { |
| 61 | + if ($currentLine !== [] || !$this->trim) { |
| 62 | + // Also append whitespaces if not trimming or current line is not empty |
| 63 | + $currentLine = [...$currentLine, ...$whitespaceBuffer]; |
| 64 | + $currentLineWidth += $whitespacesWidth; |
| 65 | + } |
| 66 | + |
| 67 | + // Append trimmed word |
| 68 | + $currentLine = [...$currentLine, ...$currentWord]; |
| 69 | + $currentLineWidth += $currentWordWidth; |
| 70 | + $currentWord = []; |
| 71 | + |
| 72 | + // Clear whitespace buffer |
| 73 | + $whitespaceBuffer = []; |
| 74 | + $whitespacesWidth = 0; |
| 75 | + $currentWordWidth = 0; |
| 76 | + } |
| 77 | + |
| 78 | + if ( |
| 79 | + // Append the unfinished wrapped line to wrapped lines if it is as wide as max line width |
| 80 | + $currentLineWidth >= $this->maxLineWidth |
| 81 | + // or if it would be too long with the current partially processed word added |
| 82 | + || ($currentLineWidth + $whitespacesWidth + $currentWordWidth) >= $this->maxLineWidth && $symbolWidth > 0 |
| 83 | + ) { |
| 84 | + $remainingWidth = max($this->maxLineWidth - $currentLineWidth, 0); |
| 85 | + |
| 86 | + $wrappedLines[] = $this->processLine($currentLine, $lineAlignment); |
| 87 | + $currentLine = []; |
| 88 | + $currentLineWidth = 0; |
| 89 | + |
| 90 | + // Remove all whitespaces till end of just appended wrapped line + next whitespace |
| 91 | + $this->removeWhitespaces($whitespaceBuffer, $whitespacesWidth, $remainingWidth); |
| 92 | + |
| 93 | + // In case all whitespaces have been exhausted, prevent first whitespace to count towards next word |
| 94 | + if ($isWhitespace) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Append symbol to unfinished, partially processed word |
| 100 | + if ($isWhitespace) { |
| 101 | + $whitespaceBuffer[] = $symbol; |
| 102 | + $whitespacesWidth += $symbolWidth; |
| 103 | + } else { |
| 104 | + $currentWord[] = $symbol; |
| 105 | + $currentWordWidth += $symbolWidth; |
| 106 | + } |
| 107 | + |
| 108 | + $hasSeenNonWhitespace = !$isWhitespace; |
| 109 | + } |
| 110 | + |
| 111 | + // Append remaining text parts |
| 112 | + if ($currentWord !== [] || $whitespaceBuffer !== []) { |
| 113 | + if ($currentLine === [] && $currentWord === []) { |
| 114 | + $wrappedLines[] = $this->processLine([], $lineAlignment); |
| 115 | + } elseif(!$this->trim || $currentLine !== []) { |
| 116 | + $currentLine = [...$currentLine, ...$whitespaceBuffer]; |
| 117 | + $whitespaceBuffer = []; |
| 118 | + } |
| 119 | + $currentLine = [...$currentLine, ...$currentWord]; |
| 120 | + } |
| 121 | + |
| 122 | + // Append remaining line |
| 123 | + if ($currentLine !== []) { |
| 124 | + $wrappedLines[] = $this->processLine($currentLine, $lineAlignment); |
| 125 | + } |
| 126 | + |
| 127 | + // Append empty line if there was nothing to wrap in the first place |
| 128 | + if ($wrappedLines === []) { |
| 129 | + $wrappedLines[] = $this->processLine([], $lineAlignment); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + yield from $wrappedLines; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param StyledGrapheme[] $currentLine |
| 138 | + * @return array{list<StyledGrapheme>,int,HorizontalAlignment} |
| 139 | + */ |
| 140 | + private function processLine(array $currentLine, HorizontalAlignment $alignment): array |
| 141 | + { |
| 142 | + $lineWidth = array_reduce($currentLine, function (int $width, StyledGrapheme $grapheme): int { |
| 143 | + return $width + $grapheme->symbolWidth(); |
| 144 | + }, 0); |
| 145 | + |
| 146 | + return [$currentLine, $lineWidth, $alignment]; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param StyledGrapheme[] $whitespaceBuffer |
| 151 | + */ |
| 152 | + private function removeWhitespaces(array &$whitespaceBuffer, int &$whitespacesWidth, int &$remainingWidth): void |
| 153 | + { |
| 154 | + $firstWhitespace = array_shift($whitespaceBuffer); |
| 155 | + while ($firstWhitespace instanceof StyledGrapheme) { |
| 156 | + $symbolWidth = $firstWhitespace->symbolWidth(); |
| 157 | + $whitespacesWidth -= $symbolWidth; |
| 158 | + if ($symbolWidth > $remainingWidth) { |
| 159 | + break; |
| 160 | + } |
| 161 | + $remainingWidth -= $symbolWidth; |
| 162 | + $firstWhitespace = array_shift($whitespaceBuffer); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private function isWhitespace(StyledGrapheme $symbol): bool |
| 167 | + { |
| 168 | + return preg_match('/\p{Z}/u', $symbol->symbol) && $symbol->symbol !== self::NBSP; |
| 169 | + } |
| 170 | +} |
0 commit comments