Skip to content

Commit

Permalink
Don't throw an exception if Encoding doesn't have BaseEncoding (#433)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update tests/Integration/FontTest.php

Co-authored-by: Konrad Abicht <[email protected]>

* Move EncodingNotFoundException to src/Smalot/PdfParser/Exception

Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
LucianoHanna and k00ni authored Jun 17, 2021
1 parent b32bb7a commit 35c8812
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Smalot/PdfParser/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Exception;
use Smalot\PdfParser\Element\ElementNumeric;
use Smalot\PdfParser\Encoding\PostScriptGlyphs;
use Smalot\PdfParser\Exception\EncodingNotFoundException;

/**
* Class Encoding
Expand Down Expand Up @@ -148,7 +149,7 @@ public function __toString()
/**
* @return string
*
* @throws \Exception
* @throws EncodingNotFoundException
*/
protected function getEncodingClass()
{
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Exception/EncodingNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Smalot\PdfParser\Exception;

class EncodingNotFoundException extends \Exception
{
}
11 changes: 9 additions & 2 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace Smalot\PdfParser;

use Smalot\PdfParser\Encoding\WinAnsiEncoding;
use Smalot\PdfParser\Exception\EncodingNotFoundException;

/**
* Class Font
Expand Down Expand Up @@ -107,9 +108,15 @@ public function translateChar($char, $use_default = true)
\strlen($char) < 2
&& $this->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;
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')]);
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 35c8812

Please sign in to comment.