Skip to content

Commit

Permalink
Fix invalid return type on unknown glyph (#459)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
PrinsFrank and prinsfrank authored Oct 1, 2021
1 parent ba3f039 commit 5ed3040
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,14 +1084,14 @@ public static function getGlyphs(): array
];
}

public static function getCodePoint($glyph)
public static function getCodePoint($glyph): ?int
{
$glyphsMap = static::getGlyphs();

if (isset($glyphsMap[$glyph])) {
return hexdec($glyphsMap[$glyph]);
}

return $glyph;
return null;
}
}
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ 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);

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;
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/EncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Smalot\PdfParser\Unit;

use Smalot\PdfParser\Document;
use Smalot\PdfParser\Encoding;
use Tests\Smalot\PdfParser\TestCase;

class EncodingTest extends TestCase
{
public function testTranslateCharOnUnknownGlyph(): void
{
$encoding = new Encoding(new Document());

$this->assertNull($encoding->translateChar('foo'));
}
}

0 comments on commit 5ed3040

Please sign in to comment.