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 @@ -21,8 +21,7 @@ public function __construct(
public function getText(Font $font): string {
$differences = $font->getDifferences();
$encoding = $font->getEncoding();
$toUnicodeCMap = $font->getToUnicodeCMap()
?? $font->getToUnicodeCMapDescendantFont();
$toUnicodeCMap = $font->getToUnicodeCMap();

$text = '';
$previousOffset = null;
Expand Down
17 changes: 12 additions & 5 deletions src/Document/Font/FontWidths.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

namespace PrinsFrank\PdfParser\Document\Font;

readonly class FontWidths {
class FontWidths {
/** @var array<int, float|null> */
private array $widthCache = [];

/** @param list<float> $widths */
public function __construct(
public int $firstChar,
public array $widths,
public readonly int $firstChar,
public readonly array $widths,
) {}

public function getWidthForCharacter(int $characterCode): ?float {
if (isset($this->widthCache[$characterCode])) {
return $this->widthCache[$characterCode];
}

$width = $this->widths[$characterCode - $this->firstChar] ?? null;
if ($width === null) {
return null;
return $this->widthCache[$characterCode] = null;
}

return $width / 1000;
return $this->widthCache[$characterCode] = $width / 1000;
}
}
53 changes: 36 additions & 17 deletions src/Document/Object/Decorator/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Font extends DecoratedObject {
private readonly array $descendantFontsCache;
private readonly ToUnicodeCMap|false $toUnicodeCMap;
private readonly CIDFontWidths|FontWidths|false $widths;
private readonly DifferencesArrayValue|false $differencesCache;
private readonly EncodingNameValue|false $encodingCache;

/** @throws PdfParserException */
public function getBaseFont(): ?string {
Expand All @@ -53,48 +55,65 @@ public function getEncodingDictionary(): ?Dictionary {

/** @throws PdfParserException */
public function getEncoding(): ?EncodingNameValue {
$encodingType = $this->getDictionary()->getTypeForKey(DictionaryKey::ENCODING);
if ($encodingType === null) {
return null;
if (isset($this->encodingCache)) {
return $this->encodingCache === false ? null : $this->encodingCache;
}

if ($this->getDictionary()->getTypeForKey(DictionaryKey::ENCODING) === EncodingNameValue::class) {
$encoding = $this->getDictionary()->getValueForKey($this->document, DictionaryKey::ENCODING, EncodingNameValue::class);
} else {
$encoding = $this->getEncodingDictionary()
?->getValueForKey($this->document, DictionaryKey::BASE_ENCODING, EncodingNameValue::class);
}

if ($encodingType === EncodingNameValue::class) {
return $this->getDictionary()->getValueForKey($this->document, DictionaryKey::ENCODING, EncodingNameValue::class);
if ($encoding === null) {
$this->encodingCache = false;
return null;
}

return $this->getEncodingDictionary()
?->getValueForKey($this->document, DictionaryKey::BASE_ENCODING, EncodingNameValue::class);
return $this->encodingCache = $encoding;
}

public function getDifferences(): ?DifferencesArrayValue {
return $this->getEncodingDictionary()
if (isset($this->differencesCache)) {
return $this->differencesCache === false ? null : $this->differencesCache;
}

$differences = $this->getEncodingDictionary()
?->getValueForKey($this->document, DictionaryKey::DIFFERENCES, DifferencesArrayValue::class);
if ($differences === null) {
$this->differencesCache = false;
return null;
}

return $this->differencesCache = $differences;
}

/** @throws PdfParserException */
public function getToUnicodeCMap(): ?ToUnicodeCMap {
if (isset($this->toUnicodeCMap)) {
if ($this->toUnicodeCMap === false) {
return null;
}

return $this->toUnicodeCMap;
return $this->toUnicodeCMap === false
? null
: $this->toUnicodeCMap;
}

if ($this->getDictionary()->getTypeForKey(DictionaryKey::TO_UNICODE) === ToUnicodeCMapNameValue::class) {
$toUnicodeCMapNameValue = $this->getDictionary()
->getValueForKey($this->document, DictionaryKey::TO_UNICODE, ToUnicodeCMapNameValue::class)
?? throw new ParseFailureException();

return $this->toUnicodeCMap = $toUnicodeCMapNameValue
->getToUnicodeCMap();
return $this->toUnicodeCMap = $toUnicodeCMapNameValue->getToUnicodeCMap();
}

$toUnicodeObject = $this->getDictionary()
->getObjectForReference($this->document, DictionaryKey::TO_UNICODE);
if ($toUnicodeObject === null) {
$this->toUnicodeCMap = false;
$descendantUnicodeCMap = $this->getToUnicodeCMapDescendantFont();
if ($descendantUnicodeCMap !== null) {
return $this->toUnicodeCMap = $descendantUnicodeCMap;
}

$this->toUnicodeCMap = false;
return null;
}

Expand All @@ -106,7 +125,7 @@ public function getToUnicodeCMap(): ?ToUnicodeCMap {
return $this->toUnicodeCMap = ToUnicodeCMapParser::parse($stream, 0, $stream->getSizeInBytes());
}

public function getToUnicodeCMapDescendantFont(): ?ToUnicodeCMap {
private function getToUnicodeCMapDescendantFont(): ?ToUnicodeCMap {
foreach ($this->getDescendantFonts() as $descendantFont) {
$fontDictionary = $descendantFont instanceof Dictionary ? $descendantFont : $descendantFont->getDictionary();

Expand Down
Loading