From 35c88120b7a6edebb54037a3bb15c5de3a5c8a24 Mon Sep 17 00:00:00 2001 From: Luciano Hanna El Adji Date: Thu, 17 Jun 2021 04:28:42 -0300 Subject: [PATCH] Don't throw an exception if Encoding doesn't have BaseEncoding (#433) * Dont crash when a Encoding doesn't have BaseEncoding * Create a specific Exception for encoding not found * Fix phpdoc and change exception in catch * Improve test * Create a test for Encoding without BaseEncoding * Update src/Smalot/PdfParser/Font.php Co-authored-by: Konrad Abicht * Update tests/Integration/FontTest.php Co-authored-by: Konrad Abicht * Move EncodingNotFoundException to src/Smalot/PdfParser/Exception Co-authored-by: Konrad Abicht --- src/Smalot/PdfParser/Encoding.php | 5 +++-- .../Exception/EncodingNotFoundException.php | 7 +++++++ src/Smalot/PdfParser/Font.php | 11 +++++++++-- tests/Integration/EncodingTest.php | 3 ++- tests/Integration/FontTest.php | 13 +++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 src/Smalot/PdfParser/Exception/EncodingNotFoundException.php diff --git a/src/Smalot/PdfParser/Encoding.php b/src/Smalot/PdfParser/Encoding.php index 369d3af6..b6537f34 100644 --- a/src/Smalot/PdfParser/Encoding.php +++ b/src/Smalot/PdfParser/Encoding.php @@ -33,6 +33,7 @@ use Exception; use Smalot\PdfParser\Element\ElementNumeric; use Smalot\PdfParser\Encoding\PostScriptGlyphs; +use Smalot\PdfParser\Exception\EncodingNotFoundException; /** * Class Encoding @@ -148,7 +149,7 @@ public function __toString() /** * @return string * - * @throws \Exception + * @throws EncodingNotFoundException */ protected function getEncodingClass() { @@ -157,7 +158,7 @@ protected function getEncodingClass() $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding; if (!class_exists($className)) { - throw new Exception('Missing encoding data for: "'.$baseEncoding.'".'); + throw new EncodingNotFoundException('Missing encoding data for: "'.$baseEncoding.'".'); } return $className; diff --git a/src/Smalot/PdfParser/Exception/EncodingNotFoundException.php b/src/Smalot/PdfParser/Exception/EncodingNotFoundException.php new file mode 100644 index 00000000..c43a66c1 --- /dev/null +++ b/src/Smalot/PdfParser/Exception/EncodingNotFoundException.php @@ -0,0 +1,7 @@ +has('Encoding') && $this->get('Encoding') instanceof Encoding - && WinAnsiEncoding::class === $this->get('Encoding')->__toString() ) { - $fallbackDecoded = self::uchr($dec); + try { + if (WinAnsiEncoding::class === $this->get('Encoding')->__toString()) { + $fallbackDecoded = self::uchr($dec); + } + } catch (EncodingNotFoundException $e) { + // Encoding->getEncodingClass() throws EncodingNotFoundException when BaseEncoding doesn't exists + // See table 5.11 on PDF 1.5 specs for more info + } } return $use_default ? self::MISSING : $fallbackDecoded; diff --git a/tests/Integration/EncodingTest.php b/tests/Integration/EncodingTest.php index f6ef6464..9dc9edc3 100644 --- a/tests/Integration/EncodingTest.php +++ b/tests/Integration/EncodingTest.php @@ -37,6 +37,7 @@ use Smalot\PdfParser\Element; use Smalot\PdfParser\Encoding; use Smalot\PdfParser\Encoding\StandardEncoding; +use Smalot\PdfParser\Exception\EncodingNotFoundException; use Smalot\PdfParser\Header; use Tests\Smalot\PdfParser\TestCase; @@ -62,7 +63,7 @@ public function testGetEncodingClass() */ public function testInitGetEncodingClassMissingClassException() { - $this->expectException(Exception::class); + $this->expectException(EncodingNotFoundException::class); $this->expectExceptionMessage('Missing encoding data for: "invalid"'); $header = new Header(['BaseEncoding' => new Element('invalid')]); diff --git a/tests/Integration/FontTest.php b/tests/Integration/FontTest.php index c4c1ce94..4175ed04 100644 --- a/tests/Integration/FontTest.php +++ b/tests/Integration/FontTest.php @@ -379,4 +379,17 @@ public function testXmlContent() $this->assertEquals('Example PDF', $text); } + + /** + * Create an instance of Header containing an instance of Encoding that doesn't have a BaseEncoding. + * Test if the Font won't raise a exception because Encoding don't have BaseEncoding. + */ + public function testEncodingWithoutBaseEncoding() + { + $document = new Document(); + $header = new Header(['Encoding' => new Encoding($document)]); + $font = new Font(new Document(), $header); + $font->setTable([]); + $this->assertEquals('?', $font->translateChar('a')); + } }