Skip to content

Commit 5ed3040

Browse files
PrinsFrankprinsfrank
andauthored
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 <[email protected]>
1 parent ba3f039 commit 5ed3040

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/Smalot/PdfParser/Encoding.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getDetails(bool $deep = true): array
110110
return $details;
111111
}
112112

113-
public function translateChar($dec): int
113+
public function translateChar($dec): ?int
114114
{
115115
if (isset($this->mapping[$dec])) {
116116
$dec = $this->mapping[$dec];

src/Smalot/PdfParser/Encoding/PostScriptGlyphs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,14 +1084,14 @@ public static function getGlyphs(): array
10841084
];
10851085
}
10861086

1087-
public static function getCodePoint($glyph)
1087+
public static function getCodePoint($glyph): ?int
10881088
{
10891089
$glyphsMap = static::getGlyphs();
10901090

10911091
if (isset($glyphsMap[$glyph])) {
10921092
return hexdec($glyphsMap[$glyph]);
10931093
}
10941094

1095-
return $glyph;
1095+
return null;
10961096
}
10971097
}

src/Smalot/PdfParser/Font.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,15 @@ public function decodeContent(string $text, ?bool &$unicode = null): string
428428
foreach ($chars as $char) {
429429
$dec_av = hexdec(bin2hex($char));
430430
$dec_ap = $encoding->translateChar($dec_av);
431-
$result .= self::uchr($dec_ap);
431+
$result .= self::uchr($dec_ap ?? $dec_av);
432432
}
433433
} else {
434434
$length = \strlen($text);
435435

436436
for ($i = 0; $i < $length; ++$i) {
437437
$dec_av = hexdec(bin2hex($text[$i]));
438438
$dec_ap = $encoding->translateChar($dec_av);
439-
$result .= self::uchr($dec_ap);
439+
$result .= self::uchr($dec_ap ?? $dec_av);
440440
}
441441
}
442442
$text = $result;

tests/Unit/EncodingTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Smalot\PdfParser\Unit;
6+
7+
use Smalot\PdfParser\Document;
8+
use Smalot\PdfParser\Encoding;
9+
use Tests\Smalot\PdfParser\TestCase;
10+
11+
class EncodingTest extends TestCase
12+
{
13+
public function testTranslateCharOnUnknownGlyph(): void
14+
{
15+
$encoding = new Encoding(new Document());
16+
17+
$this->assertNull($encoding->translateChar('foo'));
18+
}
19+
}

0 commit comments

Comments
 (0)