Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PrinsFrank\PdfParser\Document\ContentStream\Command\Operator\State\Interaction\InteractsWithTextState;
use PrinsFrank\PdfParser\Document\ContentStream\PositionedText\TextState;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;
use PrinsFrank\PdfParser\Exception\InvalidArgumentException;
use PrinsFrank\PdfParser\Exception\ParseFailureException;

Expand Down Expand Up @@ -51,7 +51,7 @@ public function applyToTextState(string $operands, TextState $textState): TextSt
}

return $textState->withFont(
DictionaryKey::tryFrom($matches['fontReference']) ?? new ExtendedDictionaryKey($matches['fontReference']),
DictionaryKey::tryFrom($matches['fontReference']) ?? new NameObjectStringValue($matches['fontReference']),
(float) $matches['FontSize'],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Override;
use PrinsFrank\PdfParser\Document\ContentStream\Command\Operator\State\Interaction\IncludesXObjects;
use PrinsFrank\PdfParser\Document\ContentStream\PositionedText\TransformationMatrix;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;
use PrinsFrank\PdfParser\Document\Object\Decorator\Page;
use PrinsFrank\PdfParser\Document\Object\Decorator\XObject;
use PrinsFrank\PdfParser\Exception\ParseFailureException;

/**
* @internal
Expand All @@ -22,7 +23,7 @@ enum XObjectOperator: string implements IncludesXObjects {
#[Override]
public function getPositionedTextElements(string $operands, TransformationMatrix $transformationMatrix, Page|XObject $context, array $visitedObjectIds): array {
$reference = $context->getXObjectsDictionary()
?->getValueForKey($context->document, ExtendedDictionaryKey::fromKeyString($operands), ReferenceValue::class);
?->getValueForKey($context->document, NameObjectStringValue::fromValue($operands) ?? throw new ParseFailureException(), ReferenceValue::class);
if ($reference === null) {
return [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Document/ContentStream/PositionedText/TextState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PrinsFrank\PdfParser\Document\ContentStream\PositionedText;

use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;

readonly class TextState {
public function __construct(
public DictionaryKey|ExtendedDictionaryKey|null $fontName, // Tf
public DictionaryKey|NameObjectStringValue|null $fontName, // Tf
public ?float $fontSize, // Tfs
public float $charSpace = 0, // Tc
public float $wordSpace = 0, // Tw
Expand All @@ -17,7 +17,7 @@ public function __construct(
public float $rise = 0, // Trise
) {}

public function withFont(DictionaryKey|ExtendedDictionaryKey|null $fontName, ?float $fontSize): self {
public function withFont(DictionaryKey|NameObjectStringValue|null $fontName, ?float $fontSize): self {
return new TextState(
$fontName,
$fontSize,
Expand Down
13 changes: 6 additions & 7 deletions src/Document/Dictionary/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use PrinsFrank\PdfParser\Document\Dictionary\DictionaryEntry\DictionaryEntry;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\ArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\DictionaryArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
Expand Down Expand Up @@ -41,10 +40,10 @@ public function __construct(
* @param class-string<T> $expectedValueType
* @return T
*/
public function getValueForKey(?Document $document, DictionaryKey|ExtendedDictionaryKey $dictionaryKey, string $expectedValueType): DictionaryValue|Dictionary|NameValue|null {
public function getValueForKey(?Document $document, DictionaryKey|NameObjectStringValue $dictionaryKey, string $expectedValueType): DictionaryValue|Dictionary|NameValue|null {
foreach ($this->dictionaryEntries as $dictionaryEntry) {
if (($dictionaryKey instanceof DictionaryKey && $dictionaryEntry->key === $dictionaryKey) === false
&& ($dictionaryKey instanceof ExtendedDictionaryKey && $dictionaryEntry->key instanceof ExtendedDictionaryKey && $dictionaryEntry->key->value === $dictionaryKey->value) === false) {
&& ($dictionaryKey instanceof NameObjectStringValue && $dictionaryEntry->key instanceof NameObjectStringValue && $dictionaryEntry->key->value === $dictionaryKey->value) === false) {
continue;
}

Expand Down Expand Up @@ -102,7 +101,7 @@ public function getValueForKey(?Document $document, DictionaryKey|ExtendedDictio
return null;
}

public function getStringValue(?Document $document, DictionaryKey|ExtendedDictionaryKey $dictionaryKey): HexadecimalStringValue|LiteralStringValue|NameObjectStringValue|null {
public function getStringValue(?Document $document, DictionaryKey|NameObjectStringValue $dictionaryKey): HexadecimalStringValue|LiteralStringValue|NameObjectStringValue|null {
return match ($this->getTypeForKey($dictionaryKey)) {
HexadecimalStringValue::class => $this->getValueForKey($document, $dictionaryKey, HexadecimalStringValue::class),
LiteralStringValue::class => $this->getValueForKey($document, $dictionaryKey, LiteralStringValue::class),
Expand All @@ -112,7 +111,7 @@ public function getStringValue(?Document $document, DictionaryKey|ExtendedDictio
}

/** @return class-string<DictionaryValue|NameValue|Dictionary> */
public function getTypeForKey(DictionaryKey|ExtendedDictionaryKey $dictionaryKey): ?string {
public function getTypeForKey(DictionaryKey|NameObjectStringValue $dictionaryKey): ?string {
foreach ($this->dictionaryEntries as $dictionaryEntry) {
if ($dictionaryEntry->key === $dictionaryKey) {
return $dictionaryEntry->value::class;
Expand Down Expand Up @@ -153,7 +152,7 @@ public function getSubDictionary(?Document $document, DictionaryKey $dictionaryK
* @param class-string<T>|null $expectedDecoratorFQN
* @return ($expectedDecoratorFQN is null ? DecoratedObject : T)
*/
public function getObjectForReference(Document $document, DictionaryKey|ExtendedDictionaryKey $dictionaryKey, ?string $expectedDecoratorFQN = null): ?DecoratedObject {
public function getObjectForReference(Document $document, DictionaryKey|NameObjectStringValue $dictionaryKey, ?string $expectedDecoratorFQN = null): ?DecoratedObject {
$reference = $this->getValueForKey($document, $dictionaryKey, ReferenceValue::class);
if ($reference === null) {
return null;
Expand All @@ -168,7 +167,7 @@ public function getObjectForReference(Document $document, DictionaryKey|Extended
* @param class-string<T>|null $expectedDecoratorFQN
* @return ($expectedDecoratorFQN is null ? list<DecoratedObject> : list<T>)
*/
public function getObjectsForReference(Document $document, DictionaryKey|ExtendedDictionaryKey $dictionaryKey, ?string $expectedDecoratorFQN = null): array {
public function getObjectsForReference(Document $document, DictionaryKey|NameObjectStringValue $dictionaryKey, ?string $expectedDecoratorFQN = null): array {
$references = $this->getValueForKey($document, $dictionaryKey, ReferenceValueArray::class);
if ($references === null) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/Document/Dictionary/DictionaryEntry/DictionaryEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

use PrinsFrank\PdfParser\Document\Dictionary\Dictionary;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\NameValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;

/** @api */
readonly class DictionaryEntry {
public function __construct(
public DictionaryKey|ExtendedDictionaryKey $key,
public DictionaryKey|NameObjectStringValue $key,
public DictionaryValue|Dictionary|NameValue $value,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use PrinsFrank\PdfParser\Document\Dictionary\Dictionary;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryFactory;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\ArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\NameValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValueArray;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;
use PrinsFrank\PdfParser\Document\Dictionary\Normalization\NameValueNormalizer;
use PrinsFrank\PdfParser\Document\Encryption\RC4;
use PrinsFrank\PdfParser\Document\Security\EncryptionContext;
Expand All @@ -27,7 +27,8 @@ class DictionaryEntryFactory {
*/
public static function fromKeyValuePair(?EncryptionContext $encryptionContext, string $keyString, string|array $dictionaryValue): ?DictionaryEntry {
$dictionaryKey = DictionaryKey::tryFromKeyString($keyString)
?? ExtendedDictionaryKey::fromKeyString($keyString);
?? NameObjectStringValue::fromValue($keyString)
?? throw new ParseFailureException();

return new DictionaryEntry($dictionaryKey, self::getValue($encryptionContext, $dictionaryKey, $dictionaryValue));
}
Expand All @@ -36,7 +37,7 @@ public static function fromKeyValuePair(?EncryptionContext $encryptionContext, s
* @param string|array<string, mixed> $value
* @throws PdfParserException
*/
protected static function getValue(?EncryptionContext $encryptionContext, DictionaryKey|ExtendedDictionaryKey $dictionaryKey, string|array $value): Dictionary|DictionaryValue|NameValue {
protected static function getValue(?EncryptionContext $encryptionContext, DictionaryKey|NameObjectStringValue $dictionaryKey, string|array $value): Dictionary|DictionaryValue|NameValue {
if ($encryptionContext !== null && is_string($value)) {
if (str_starts_with($value, '<') && str_ends_with($value, '>') && ($binaryValue = hex2bin(substr($value, 1, -1))) !== false) {
$value = '<' . bin2hex(RC4::crypt($encryptionContext->getObjectEncryptionKey(), $binaryValue)) . '>';
Expand Down Expand Up @@ -81,8 +82,8 @@ protected static function getValue(?EncryptionContext $encryptionContext, Dictio
return $referenceValueArray;
}

if (in_array(ExtendedDictionaryKey::class, $allowedValueTypes, true) && is_string($value) && ($extendedDictionaryKey = ExtendedDictionaryKey::fromValue($value)) !== null) {
return $extendedDictionaryKey;
if (in_array(NameObjectStringValue::class, $allowedValueTypes, true) && is_string($value) && ($nameObjectStringValue = NameObjectStringValue::fromValue($value)) !== null) {
return $nameObjectStringValue;
}

throw new ParseFailureException(sprintf('Value "%s" for dictionary key %s could not be parsed to a valid value type', is_array($value) ? 'array()' : $value, $dictionaryKey->value));
Expand Down
2 changes: 1 addition & 1 deletion src/Document/Dictionary/DictionaryKey/DictionaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public function getValueTypes(): array {
self::EMBEDDED_FILES => [Dictionary::class],
self::ENCODE => [ArrayValue::class],
self::ENCODED_BYTE_ALIGN => [BooleanValue::class],
self::ENCODING => [EncodingNameValue::class, Dictionary::class, ExtendedDictionaryKey::class],
self::ENCODING => [EncodingNameValue::class, Dictionary::class, NameObjectStringValue::class],
self::ENCRYPT => [Dictionary::class],
self::ENCRYPT_METADATA => [BooleanValue::class],
self::ENCRYPTION_REVISION => [IntegerValue::class],
Expand Down
60 changes: 0 additions & 60 deletions src/Document/Dictionary/DictionaryKey/ExtendedDictionaryKey.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
namespace PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString;

use Override;
use PrinsFrank\PdfParser\Document\Dictionary\Dictionary;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKeyInterface;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\ArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\DictionaryArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Boolean\BooleanValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Date\DateValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Float\FloatValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Integer\IntegerValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Rectangle\Rectangle;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValueArray;
use PrinsFrank\PdfParser\Exception\ParseFailureException;

readonly class NameObjectStringValue implements DictionaryValue {
readonly class NameObjectStringValue implements DictionaryKeyInterface, DictionaryValue {
public function __construct(
public string $value,
) {}
Expand All @@ -19,13 +30,33 @@ public function getText(): string {
) ?? throw new ParseFailureException();
}

/** @api */
#[Override]
public function getValueTypes(): array {
return [
Dictionary::class,
ArrayValue::class,
DictionaryArrayValue::class,
BooleanValue::class,
DateValue::class,
FloatValue::class,
IntegerValue::class,
Rectangle::class,
ReferenceValue::class,
ReferenceValueArray::class,
HexadecimalStringValue::class,
LiteralStringValue::class,
NameObjectStringValue::class,
];
}

#[Override]
public static function fromValue(string $valueString): ?self {
$valueString = trim($valueString);
if (str_starts_with($valueString, '/') === false) {
return null;
}

return new self($valueString);
return new self(substr($valueString, 1));
}
}
4 changes: 2 additions & 2 deletions src/Document/Object/Decorator/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PrinsFrank\PdfParser\Document\ContentStream\PositionedText\TransformationMatrix;
use PrinsFrank\PdfParser\Document\Dictionary\Dictionary;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\ExtendedDictionaryKey;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\ArrayValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\CIDFontWidths;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\CrossReferenceStreamByteSizes;
Expand All @@ -23,6 +22,7 @@
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\ToUnicodeCMapNameValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValue;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValueArray;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\TextString\NameObjectStringValue;
use PrinsFrank\PdfParser\Document\Font\FontWidths;
use PrinsFrank\PdfParser\Document\Object\Item\UncompressedObject\UncompressedObject;
use PrinsFrank\PdfParser\Exception\InvalidArgumentException;
Expand All @@ -45,7 +45,7 @@ public function getBaseFont(): ?string {
}

public function getEncodingDictionary(): ?Dictionary {
if (in_array($this->getDictionary()->getTypeForKey(DictionaryKey::ENCODING), [null, EncodingNameValue::class, ExtendedDictionaryKey::class], true)) {
if (in_array($this->getDictionary()->getTypeForKey(DictionaryKey::ENCODING), [null, EncodingNameValue::class, NameObjectStringValue::class], true)) {
return null;
}

Expand Down
Loading
Loading