Skip to content

Commit 3c41e85

Browse files
authored
Support newlines in rectangles, fixes #286 (#288)
1 parent dae3d77 commit 3c41e85

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/Document/Dictionary/DictionaryValue/Rectangle/Rectangle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Override;
77
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
8+
use PrinsFrank\PdfParser\Exception\RuntimeException;
89

910
/** @api */
1011
class Rectangle implements DictionaryValue {
@@ -22,6 +23,9 @@ public static function fromValue(string $valueString): ?self {
2223
return null;
2324
}
2425

26+
$valueString = str_replace([' ', "\r", "\n"], ' ', $valueString);
27+
$valueString = preg_replace('/\s+/', ' ', $valueString)
28+
?? throw new RuntimeException(preg_last_error_msg());
2529
$coords = explode(' ', trim(rtrim(ltrim($valueString, '['), ']')));
2630
if (count($coords) !== 4) {
2731
return null;

tests/Unit/Document/Dictionary/DictionaryValue/Rectangle/RectangleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@ public function testFromValue(): void {
3030
new Rectangle(42.22, 43.33, 44.44, 45.55),
3131
Rectangle::fromValue('[ 42.22 43.33 44.44 45.55 ]'),
3232
);
33+
static::assertEquals(
34+
new Rectangle(0, 0, 595.2756, 841.8898),
35+
Rectangle::fromValue('[0 0 595.2756' . "\r" . '841.8898]'),
36+
);
37+
static::assertEquals(
38+
new Rectangle(0, 0, 595.2756, 841.8898),
39+
Rectangle::fromValue('[0 0 595.2756' . "\n" . '841.8898]'),
40+
);
3341
}
3442
}

0 commit comments

Comments
 (0)