1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-08 06:46:33 +02:00

Refactor & optimize line splitting

This commit is contained in:
Oliver Vogel
2024-02-03 16:06:37 +01:00
parent 795b794ee8
commit adf3764667

View File

@@ -124,10 +124,19 @@ abstract class AbstractFontProcessor implements FontProcessorInterface
$formatedLine = new Line(); $formatedLine = new Line();
foreach ($line as $word) { foreach ($line as $word) {
if ($font->wrapWidth() >= $this->boxSize((string) $formatedLine . ' ' . $word, $font)->width()) { // calculate width of newly formated line
$lineWidth = $this->boxSize(match ($formatedLine->count()) {
0 => $word,
default => (string) $formatedLine . ' ' . $word,
}, $font)->width();
// decide if word fits on current line or a new line must be created
if ($line->count() === 1 || $lineWidth <= $font->wrapWidth()) {
$formatedLine->add($word); $formatedLine->add($word);
} else { } else {
$wrapped[] = $formatedLine; if ($formatedLine->count()) {
$wrapped[] = $formatedLine;
}
$formatedLine = new Line($word); $formatedLine = new Line($word);
} }
} }