Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Document/Encoding/PDFDocEncoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace PrinsFrank\PdfParser\Document\Encoding;

use Override;

/** @see Annex D.3 */
class PDFDocEncoding implements Encoding {
private const CHAR_MAP = [
128 => "\u{2022}",
129 => "\u{2020}",
130 => "\u{2021}",
131 => "\u{2026}",
132 => "\u{2014}",
133 => "\u{2013}",
134 => "\u{0192}",
135 => "\u{2044}",
136 => "\u{2039}",
137 => "\u{203A}",
138 => "\u{2212}",
139 => "\u{2030}",
140 => "\u{201E}",
141 => "\u{201C}",
142 => "\u{201D}",
143 => "\u{2018}",
144 => "\u{2019}",
145 => "\u{201A}",
146 => "\u{2122}",
147 => "\u{FB01}",
148 => "\u{FB02}",
149 => "\u{0141}",
150 => "\u{0152}",
151 => "\u{0160}",
152 => "\u{0178}",
153 => "\u{017D}",
154 => "\u{0131}",
155 => "\u{0142}",
156 => "\u{0153}",
157 => "\u{0161}",
158 => "\u{017E}",
159 => "\u{FFFD}",
160 => "\u{20AC}",
];

#[Override]
public static function textToUnicode(string $string): string {
$result = '';
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$byte = ord($string[$i]);
if ($byte < 128) {
$result .= chr($byte);
} elseif ($byte >= 161) {
$result .= mb_convert_encoding(chr($byte), 'UTF-8', 'ISO-8859-1');
} else {
$result .= self::CHAR_MAP[$byte];
}
}

return $result;
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Document/Encoding/PDFDocEncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace PrinsFrank\PdfParser\Tests\Unit\Document\Encoding;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use PrinsFrank\PdfParser\Document\Encoding\PDFDocEncoding;

#[CoversClass(PDFDocEncoding::class)]
class PDFDocEncodingTest extends TestCase {
public function testTextToUnicode(): void {
static::assertSame('a', PDFDocEncoding::textToUnicode('a')); // ASCII
static::assertSame('•', PDFDocEncoding::textToUnicode("\x80")); // Start of custom range
static::assertSame('�', PDFDocEncoding::textToUnicode("\x9f")); // Undefined in custom range
static::assertSame('€', PDFDocEncoding::textToUnicode("\xa0")); // addition in PDF 1.3, Table D.2
static::assertSame('¡', PDFDocEncoding::textToUnicode("\xa1")); // after custom range in ISO 8591-1
static::assertSame('ÿ', PDFDocEncoding::textToUnicode("\xff")); // end of ISO 8591-1
}
}
Loading