mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
Refactor & optimize line splitting
This commit is contained in:
parent
795b794ee8
commit
adf3764667
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user