Skip to content

Commit 85279b5

Browse files
authored
Array values can be multiple resource names not seperated by space (#196)
* Array values can be multiple resource names not seperated by space * /On dictionary value can be a reference value array * Parse Dictionary value for /Nums as TextStringValue for now * /OpenAction gets parsed correctly as array value now, update test
1 parent 38e0ad1 commit 85279b5

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/Document/Dictionary/DictionaryKey/DictionaryKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,15 +1007,15 @@ public function getValueTypes(): array {
10071007
self::NR => [BooleanValue::class],
10081008
self::NU => [DictionaryArrayValue::class],
10091009
self::NUM_COPIES => [IntegerValue::class],
1010-
self::NUMS => [ArrayValue::class],
1010+
self::NUMS => [TextStringValue::class],
10111011
self::O => [TextStringValue::class, IntegerValue::class, FloatValue::class, Dictionary::class, ArrayValue::class, BooleanValue::class],
10121012
self::OB => [TextStringValue::class],
10131013
self::OC => [Dictionary::class],
10141014
self::OCGS => [ReferenceValueArray::class, DictionaryArrayValue::class],
10151015
self::OCPROPERTIES => [Dictionary::class],
10161016
self::OFF => [ArrayValue::class],
10171017
self::OID => [ArrayValue::class],
1018-
self::ON => [ArrayValue::class],
1018+
self::ON => [ReferenceValueArray::class, ArrayValue::class],
10191019
self::ON_INSTANTIATE => [TextStringValue::class],
10201020
self::OP => [BooleanValue::class, IntegerValue::class],
10211021
self::OP_L => [TextStringValue::class],

src/Document/Dictionary/DictionaryValue/Array/ArrayValue.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public static function fromValue(string $valueString): null|self|ReferenceValueA
2626

2727
$valueString = preg_replace('/(<[^>]*>)(?=<[^>]*>)/', '$1 $2', $valueString)
2828
?? throw new RuntimeException('An error occurred while sanitizing array value');
29-
$values = explode(' ', str_replace("\n", ' ', rtrim(ltrim($valueString, '[ '), ' ]')));
29+
$valueString = str_replace(['/', "\n"], [' /', ' '], rtrim(ltrim($valueString, '[ '), ' ]'));
30+
$valueString = preg_replace('/\s+/', ' ', $valueString)
31+
?? throw new RuntimeException('An error occurred while removing duplicate spaces from array value');
32+
$values = explode(' ', $valueString);
3033
if (count($values) % 3 === 0 && array_key_exists(2, $values) && $values[2] === 'R') {
3134
return ReferenceValueArray::fromValue($valueString);
3235
}

tests/Unit/Document/Dictionary/DictionaryParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ public function testHandlesNumsNumberTree(): void {
247247
);
248248
static::assertEquals(
249249
new Dictionary(
250-
new DictionaryEntry(DictionaryKey::OPEN_ACTION, new ArrayValue([3, 0, 'R/Fit'])),
250+
new DictionaryEntry(DictionaryKey::OPEN_ACTION, new ArrayValue([3, 0, 'R', '/Fit'])),
251251
new DictionaryEntry(DictionaryKey::PAGE_MODE, PageModeNameValue::USE_OUTLINES),
252252
new DictionaryEntry(DictionaryKey::PAGE_LABELS, new Dictionary(
253-
new DictionaryEntry(DictionaryKey::NUMS, new ArrayValue(['0<</S/r>>12<</S/D>>'])),
253+
new DictionaryEntry(DictionaryKey::NUMS, new TextStringValue('[0<</S/r>>12<</S/D>>]')),
254254
)),
255255
new DictionaryEntry(DictionaryKey::NAMES, new ReferenceValue(13164, 0)),
256256
new DictionaryEntry(DictionaryKey::OUTLINES, new ReferenceValue(13165, 0)),

tests/Unit/Document/Dictionary/DictionaryValue/Array/ArrayValueTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public function testFromValue(): void {
2727
new ArrayValue(['foo', 'bar']),
2828
ArrayValue::fromValue('[foo bar]'),
2929
);
30+
static::assertEquals(
31+
new ArrayValue(['/foo', '/bar']),
32+
ArrayValue::fromValue('[/foo/bar]'),
33+
);
34+
static::assertEquals(
35+
new ArrayValue([3, 0, 'R', '/FitH', 'null']),
36+
ArrayValue::fromValue('[3 0 R /FitH null]'),
37+
);
3038
static::assertEquals(
3139
new ArrayValue([42, 43, 44]),
3240
ArrayValue::fromValue(

0 commit comments

Comments
 (0)