From 5ed3040ae40ee09225492628af3673ab0c514b46 Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Fri, 1 Oct 2021 12:31:21 +0200 Subject: [PATCH] Fix invalid return type on unknown glyph (#459) * Fix invalid return type on unknown glyphs * Fix invalid return type on unknown glyphs * Fix invalid return type on unknown glyphs * Fix invalid return type on unknown glyphs * Don't call assert statically as per the current project standard * Remove coverage annotation as it is currently not used in this project Co-authored-by: prinsfrank --- src/Smalot/PdfParser/Encoding.php | 2 +- .../PdfParser/Encoding/PostScriptGlyphs.php | 4 ++-- src/Smalot/PdfParser/Font.php | 4 ++-- tests/Unit/EncodingTest.php | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/EncodingTest.php diff --git a/src/Smalot/PdfParser/Encoding.php b/src/Smalot/PdfParser/Encoding.php index 55ff9ee8..4e1f1e50 100644 --- a/src/Smalot/PdfParser/Encoding.php +++ b/src/Smalot/PdfParser/Encoding.php @@ -110,7 +110,7 @@ public function getDetails(bool $deep = true): array return $details; } - public function translateChar($dec): int + public function translateChar($dec): ?int { if (isset($this->mapping[$dec])) { $dec = $this->mapping[$dec]; diff --git a/src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php b/src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php index 3cc36f0b..ad546c3d 100644 --- a/src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php +++ b/src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php @@ -1084,7 +1084,7 @@ public static function getGlyphs(): array ]; } - public static function getCodePoint($glyph) + public static function getCodePoint($glyph): ?int { $glyphsMap = static::getGlyphs(); @@ -1092,6 +1092,6 @@ public static function getCodePoint($glyph) return hexdec($glyphsMap[$glyph]); } - return $glyph; + return null; } } diff --git a/src/Smalot/PdfParser/Font.php b/src/Smalot/PdfParser/Font.php index ae959a19..6c22b809 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -428,7 +428,7 @@ public function decodeContent(string $text, ?bool &$unicode = null): string foreach ($chars as $char) { $dec_av = hexdec(bin2hex($char)); $dec_ap = $encoding->translateChar($dec_av); - $result .= self::uchr($dec_ap); + $result .= self::uchr($dec_ap ?? $dec_av); } } else { $length = \strlen($text); @@ -436,7 +436,7 @@ public function decodeContent(string $text, ?bool &$unicode = null): string for ($i = 0; $i < $length; ++$i) { $dec_av = hexdec(bin2hex($text[$i])); $dec_ap = $encoding->translateChar($dec_av); - $result .= self::uchr($dec_ap); + $result .= self::uchr($dec_ap ?? $dec_av); } } $text = $result; diff --git a/tests/Unit/EncodingTest.php b/tests/Unit/EncodingTest.php new file mode 100644 index 00000000..36566a2a --- /dev/null +++ b/tests/Unit/EncodingTest.php @@ -0,0 +1,19 @@ +assertNull($encoding->translateChar('foo')); + } +}