Skip to content

Commit 1058144

Browse files
committed
test: harden text layouter mutation paths
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent c0f2a69 commit 1058144

2 files changed

Lines changed: 414 additions & 23 deletions

File tree

src/Layout/TextBoxLayouter.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ private function resolveSettings(StyleMap $style): array
8787
$this->styleResolver->styleValue($style, 'font-family', 'helvetica'),
8888
$this->styleResolver->styleValue($style, 'font-weight', 'normal'),
8989
),
90-
'align' => strtolower(trim($this->styleResolver->styleValue($style, 'text-align', 'left'))),
91-
'overflow' => strtolower(trim($this->styleResolver->styleValue($style, 'overflow', 'visible'))),
92-
'textOverflow' => strtolower(trim($this->styleResolver->styleValue($style, 'text-overflow', 'clip'))),
93-
'hyphens' => strtolower(trim($this->styleResolver->styleValue($style, 'hyphens', 'none'))),
94-
'whiteSpace' => strtolower(trim($this->styleResolver->styleValue($style, 'white-space', 'normal'))),
90+
'align' => strtolower($this->styleResolver->styleValue($style, 'text-align', 'left')),
91+
'overflow' => strtolower($this->styleResolver->styleValue($style, 'overflow', 'visible')),
92+
'textOverflow' => strtolower($this->styleResolver->styleValue($style, 'text-overflow', 'clip')),
93+
'hyphens' => strtolower($this->styleResolver->styleValue($style, 'hyphens', 'none')),
94+
'whiteSpace' => strtolower($this->styleResolver->styleValue($style, 'white-space', 'normal')),
9595
'color' => $this->styleResolver->styleValue($style, 'color', '#000000'),
9696
];
9797
}
@@ -129,13 +129,8 @@ private function applyOverflowConstraints(
129129

130130
$effectiveClipBox = $clipBox ?? $box;
131131
$maxVisibleLines = $this->resolveMaxVisibleLines($box['height'], $settings['lineHeight']);
132-
if ($maxVisibleLines === 0) {
133-
return ['lines' => [], 'truncated' => true, 'clipBox' => $effectiveClipBox];
134-
}
135-
136-
$truncated = false;
137-
if (count($lines) > $maxVisibleLines) {
138-
$truncated = true;
132+
$truncated = count($lines) > $maxVisibleLines;
133+
if ($truncated) {
139134
$lines = array_slice($lines, 0, $maxVisibleLines);
140135
if ($settings['textOverflow'] === 'ellipsis' && $lines !== []) {
141136
$lastIndex = count($lines) - 1;
@@ -146,9 +141,7 @@ private function applyOverflowConstraints(
146141
$settings['fontSize'],
147142
);
148143
}
149-
}
150-
151-
if ($lines !== [] && $this->shouldApplyEllipsis($lines, $truncated, $box['width'], $settings)) {
144+
} elseif ($lines !== [] && $this->shouldApplyEllipsis($lines, $box['width'], $settings)) {
152145
$lastIndex = count($lines) - 1;
153146
$lines[$lastIndex] = $this->overflowTruncator->truncateWithEllipsis(
154147
$lines[$lastIndex],
@@ -178,18 +171,13 @@ private function applyOverflowConstraints(
178171
*/
179172
private function shouldApplyEllipsis(
180173
array $lines,
181-
bool $truncated,
182174
float $boxWidth,
183175
array $settings,
184176
): bool {
185177
if ($settings['textOverflow'] !== 'ellipsis' || $lines === []) {
186178
return false;
187179
}
188180

189-
if ($truncated) {
190-
return true;
191-
}
192-
193181
$lastLine = $lines[count($lines) - 1];
194182

195183
return $this->fontMetrics->measureString($settings['fontAlias'], $settings['fontSize'], $lastLine) > $boxWidth;
@@ -290,7 +278,7 @@ private function buildLayoutLine(
290278

291279
private function resolveMaxVisibleLines(float $boxHeight, float $lineHeight): int
292280
{
293-
return max((int) ceil($boxHeight / max($lineHeight, 0.0001)), 0);
281+
return (int) ceil($boxHeight / max($lineHeight, 0.0001));
294282
}
295283

296284
private function resolveWordSpacing(
@@ -329,11 +317,11 @@ private function toPdfClipBox(?array $clipBox, float $canvasHeight): ?array
329317
private function calculateWordSpacing(string $text, float $lineWidth, float $boxWidth): float
330318
{
331319
$spaceCount = substr_count($text, ' ');
332-
if ($spaceCount === 0 || $boxWidth <= $lineWidth) {
320+
if ($spaceCount === 0) {
333321
return 0.0;
334322
}
335323

336-
return ($boxWidth - $lineWidth) / $spaceCount;
324+
return max($boxWidth - $lineWidth, 0.0) / $spaceCount;
337325
}
338326

339327
/**

0 commit comments

Comments
 (0)