1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 12:18:14 +01:00

Refactor & optimize line splitting

This commit is contained in:
Oliver Vogel 2024-02-03 16:06:37 +01:00
parent 795b794ee8
commit adf3764667
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB

View File

@ -124,10 +124,19 @@ abstract class AbstractFontProcessor implements FontProcessorInterface
$formatedLine = new Line();
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);
} else {
$wrapped[] = $formatedLine;
if ($formatedLine->count()) {
$wrapped[] = $formatedLine;
}
$formatedLine = new Line($word);
}
}