Repository: PrinsFrank/pdfparser (php)

Issue:
Comment operator (%) in string are still treated as comments
This snippet produces an invalid dictionary:
```
<<
    /Foo (%A)
    /Bar (B)
>>
```

Pull request:
Fix comment state in dictionary parsing
Fixes #57 

Patch:
diff --git a/src/Document/Dictionary/DictionaryParser.php b/src/Document/Dictionary/DictionaryParser.php
index 895063c..a36377e 100644
--- a/src/Document/Dictionary/DictionaryParser.php
+++ b/src/Document/Dictionary/DictionaryParser.php
@@ -48,7 +48,7 @@ class DictionaryParser {
                 } elseif ($nestingContext->getContext() === DictionaryParseContext::KEY || $nestingContext->getContext() === DictionaryParseContext::KEY_VALUE_SEPARATOR) {
                     $nestingContext->setContext(DictionaryParseContext::VALUE);
                 }
-            } elseif ($char === LiteralStringEscapeCharacter::LINE_FEED->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS) {
+            } elseif ($char === WhitespaceCharacter::LINE_FEED->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS) {
                 if ($nestingContext->getContext() === DictionaryParseContext::KEY) {
                     $nestingContext->setContext(DictionaryParseContext::VALUE);
                 } elseif ($nestingContext->getContext() === DictionaryParseContext::VALUE) {
@@ -72,7 +72,7 @@ class DictionaryParser {
                 }
             } elseif (trim($char) !== '' && $nestingContext->getContext() === DictionaryParseContext::KEY_VALUE_SEPARATOR) {
                 $nestingContext->setContext(DictionaryParseContext::VALUE);
-            } elseif ($char === DelimiterCharacter::PERCENT_SIGN->value && $rollingCharBuffer->getPreviousCharacter() !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value) {
+            } elseif ($char === DelimiterCharacter::PERCENT_SIGN->value && $rollingCharBuffer->getPreviousCharacter() !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $nestingContext->getContext() !== DictionaryParseContext::VALUE_IN_PARENTHESES) {
                 $nestingContext->setContext(DictionaryParseContext::COMMENT);
             }
 


Test patch:
diff --git a/tests/Unit/Document/Dictionary/DictionaryParserTest.php b/tests/Unit/Document/Dictionary/DictionaryParserTest.php
index 070095d..531818b 100644
--- a/tests/Unit/Document/Dictionary/DictionaryParserTest.php
+++ b/tests/Unit/Document/Dictionary/DictionaryParserTest.php
@@ -216,12 +216,16 @@ class DictionaryParserTest extends TestCase {
             /Producer (pdfTeX-1.40.18)
             %/Creator (TeX)
             %  /Creator (TeX)
+            /Foo (Bar)
+            /Bar (% this is not a comment but a string literal)
             >>
             EOD,
         );
         static::assertEquals(
             new Dictionary(
                 new DictionaryEntry(DictionaryKey::PRODUCER, new TextStringValue('(pdfTeX-1.40.18)')),
+                new DictionaryEntry(new ExtendedDictionaryKey('Foo'), new TextStringValue('(Bar)')),
+                new DictionaryEntry(new ExtendedDictionaryKey('Bar'), new TextStringValue('(% this is not a comment but a string literal)')),
             ),
             DictionaryParser::parse($stream, 0, $stream->getSizeInBytes())
         );