|
| 1 | +<?php |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 LibreSign |
| 4 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace LibreSign\XObjectTemplate\Tests\Unit\Layout; |
| 9 | + |
| 10 | +use LibreSign\XObjectTemplate\Layout\TextLineBreaker; |
| 11 | +use LibreSign\XObjectTemplate\Pdf\StandardFontMetrics; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +final class TextLineBreakerTest extends TestCase |
| 15 | +{ |
| 16 | + public function testWrapReturnsOriginalTextForNowrapAndNonPositiveWidths(): void |
| 17 | + { |
| 18 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 19 | + |
| 20 | + self::assertSame(['alpha beta'], $breaker->wrap('alpha beta', 0.0, 'F1', 10.0, 'auto', 'normal')); |
| 21 | + self::assertSame(['alpha beta'], $breaker->wrap('alpha beta', 100.0, 'F1', 10.0, 'auto', 'nowrap')); |
| 22 | + } |
| 23 | + |
| 24 | + public function testWrapPreservesWhitespaceOnlyInput(): void |
| 25 | + { |
| 26 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 27 | + |
| 28 | + self::assertSame([" \t "], $breaker->wrap(" \t ", 20.0, 'F1', 10.0, 'auto', 'normal')); |
| 29 | + } |
| 30 | + |
| 31 | + public function testWrapKeepsWordsOnTheSameLineWhenTheyStillFit(): void |
| 32 | + { |
| 33 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 34 | + |
| 35 | + self::assertSame(['a b'], $breaker->wrap('a b', 20.0, 'F5', 10.0, 'none', 'normal')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testWrapKeepsAppendingAfterManualHyphenBreaks(): void |
| 39 | + { |
| 40 | + $metrics = new StandardFontMetrics(); |
| 41 | + $breaker = new TextLineBreaker($metrics); |
| 42 | + $maxWidth = max( |
| 43 | + $metrics->measureString('F1', 10.0, 'hyphen-'), |
| 44 | + $metrics->measureString('F1', 10.0, 'ation test'), |
| 45 | + ); |
| 46 | + |
| 47 | + self::assertSame( |
| 48 | + ['hyphen-', 'ation test'], |
| 49 | + $breaker->wrap("hyphen\u{00AD}ation test", $maxWidth, 'F1', 10.0, 'manual', 'normal'), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWrapContinuesUsingTheLastBrokenSegmentAsTheCurrentLine(): void |
| 54 | + { |
| 55 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 56 | + |
| 57 | + self::assertSame( |
| 58 | + ['abcd-', 'ef gh'], |
| 59 | + $breaker->wrap('abcdef gh', 30.0, 'F5', 10.0, 'auto', 'normal'), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function testWrapAutomaticallyHyphenatesFixedWidthWords(): void |
| 64 | + { |
| 65 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 66 | + |
| 67 | + self::assertSame( |
| 68 | + ['abc-', 'def'], |
| 69 | + $breaker->wrap('abcdef', 24.0, 'F5', 10.0, 'auto', 'normal'), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testWrapFallsBackToSingleCharactersWhenTheFirstAutoSegmentDoesNotFit(): void |
| 74 | + { |
| 75 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 76 | + |
| 77 | + self::assertSame( |
| 78 | + ['a-', 'b'], |
| 79 | + $breaker->wrap('ab', 5.0, 'F1', 10.0, 'auto', 'normal'), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function testWrapReturnsInvalidUtf8WordsUnchangedWhenCharacterSplittingFails(): void |
| 84 | + { |
| 85 | + $breaker = new TextLineBreaker(new StandardFontMetrics()); |
| 86 | + $invalidUtf8 = "\xc3\x28"; |
| 87 | + |
| 88 | + self::assertSame([$invalidUtf8], $breaker->wrap($invalidUtf8, 1.0, 'F1', 10.0, 'auto', 'normal')); |
| 89 | + } |
| 90 | +} |
0 commit comments