Skip to content
Merged
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
23 changes: 15 additions & 8 deletions src/Document/CMap/ToUnicode/ToUnicodeCMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use PrinsFrank\PdfParser\Exception\InvalidArgumentException;
use PrinsFrank\PdfParser\Exception\PdfParserException;

readonly class ToUnicodeCMap {
class ToUnicodeCMap {
/** @var list<BFRange|BFChar> */
private array $bfCharRangeInfo;
private readonly array $bfCharRangeInfo;

/** @var array<int, string|null> */
private array $charCache = [];

/**
* @no-named-arguments
Expand All @@ -17,8 +20,8 @@
* @throws InvalidArgumentException
*/
public function __construct(
public array $codeSpaceRanges,
public int $byteSize,
public readonly array $codeSpaceRanges,
public readonly int $byteSize,
BFRange|BFChar ...$bfCharRangeInfo,
) {
$this->bfCharRangeInfo = $bfCharRangeInfo;
Expand All @@ -41,25 +44,29 @@ public function textToUnicode(string $characterGroup): string {

/** @throws PdfParserException */
protected function charToUnicode(int $characterCode): ?string {
if (array_key_exists($characterCode, $this->charCache)) {
return $this->charCache[$characterCode];
}

$char = null;
foreach ($this->bfCharRangeInfo as $bfCharRangeInfo) {
if (!$bfCharRangeInfo->containsCharacterCode($characterCode)) {
continue;
}

if (($char = $bfCharRangeInfo->toUnicode($characterCode)) !== "\0") { // Some characters map to NULL in one BFRange and to an actual character in another
return $char;
return $this->charCache[$characterCode] = $char;
}
}

if ($char === "\0") {
return $char; // Only return NULL when it is the only character this is mapped to
return $this->charCache[$characterCode] = $char; // Only return NULL when it is the only character this is mapped to
}

if ($characterCode === 0) {
return '';
return $this->charCache[$characterCode] = '';
}

return null;
return $this->charCache[$characterCode] = null;
}
}
Loading