diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e89cef..b2f00ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,21 @@ All notable changes to `dskripchenko/php-pdf` are documented in this file. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.28.0] — 2026-11-14 + +### Added +- `ReaderDocument::text()` extracts the plain text of a PDF: + `Pdf\Reader\Text\TextExtractor` interprets the text operators of each + page and the form XObjects it draws; `FontDecoder` turns codes into text + through `/ToUnicode` maps (`ToUnicodeMap`: code space, `bfchar`, `bfrange` + with arrays), simple-font encodings (`SimpleEncodings`: Standard, + WinAnsi, MacRoman) and `/Differences` named by the Adobe Glyph List + (`GlyphNames`: the Latin set, `uniXXXX`, `uXXXX`, Cyrillic `afii`, + ligatures with `_`); widths from `/Widths` and `/W` place the glyphs, so + lines and word spaces follow the page. Typographic ligatures are written + out. Inline images are skipped; a broken stream gives the text read up to + the break. + ## [1.27.0] — 2026-11-09 ### Added diff --git a/README.md b/README.md index afebd5c..3055c66 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,9 @@ file_put_contents('hello.pdf', $doc->toBytes()); ### Reading and merging - Read existing PDFs (`ReaderDocument`) — classic and stream xref, object streams, corrupt-xref recovery, decryption (RC4 / AES-128 / AES-256). +- Extract plain text (`ReaderDocument::text()`) — ToUnicode maps, simple-font + encodings with `/Differences`, composite fonts, form XObjects; lines and + word spaces from glyph positions. - Merge (`PdfMerger`) — append and reorder whole or selected pages from multiple files; page annotations and bookmarks carried over with internal links and named destinations remapped. diff --git a/docs/de/MERGE.md b/docs/de/MERGE.md index 12e005f..7577376 100644 --- a/docs/de/MERGE.md +++ b/docs/de/MERGE.md @@ -10,6 +10,7 @@ Alles in reinem PHP und MIT-lizenziert — kein FPDI, keine externen Binaries. ## Inhalt - [Ein PDF lesen](#ein-pdf-lesen) +- [Text extrahieren](#text-extrahieren) - [Dokumente anhängen](#dokumente-anhängen) - [Seiten auswählen und umordnen](#seiten-auswählen-und-umordnen) - [Eine Seite einbetten (stempeln)](#eine-seite-einbetten-stempeln) @@ -37,6 +38,23 @@ Ein `ReaderDocument` löst Objekte lazy auf, folgt der Querverweiskette (klassische Tabellen, XRef-Streams und Objekt-Streams) und fällt auf einen vollständigen Datei-Scan zurück, wenn die Querverweise beschädigt sind. +## Text extrahieren + +```php +$doc = ReaderDocument::fromBytes(file_get_contents('report.pdf')); +echo $doc->text(); // alle Seiten, getrennt durch eine Leerzeile +``` + +Der Text jeder Glyphe kommt aus ihrer Schrift: zuerst die `/ToUnicode`-Tabelle, +dann die Kodierung der einfachen Schrift (Standard, WinAnsi, MacRoman) mit +`/Differences` nach den Namen der Adobe Glyph List (einschließlich der +kyrillischen `afii`-Namen und `uniXXXX`). Gelesen wird in Inhaltsreihenfolge; +ein Wechsel in eine andere Zeile beginnt eine neue Zeile, eine Lücke breiter +als ein Fünftel der Schriftgröße wird zum Leerzeichen, Ligaturen werden +ausgeschrieben (`fi` → `fi`). Form-XObjects werden mitgelesen. Ein Scan hat +keinen Text — es gibt keine Texterkennung —, und eine zusammengesetzte +Schrift ohne `/ToUnicode`-Tabelle liefert für ihre Glyphen nichts. + ## Dokumente anhängen `PdfMerger` verkettet Seiten aus einer oder mehreren Quellen zu einem neuen diff --git a/docs/de/README.md b/docs/de/README.md index f4ff5ac..4eb0d7c 100644 --- a/docs/de/README.md +++ b/docs/de/README.md @@ -261,6 +261,9 @@ file_put_contents('hello.pdf', $doc->toBytes()); - Bestehende PDFs lesen (`ReaderDocument`) — klassische und Stream-xref, Objekt-Streams, Wiederherstellung bei beschädigter xref, Entschlüsselung (RC4 / AES-128 / AES-256). +- Text extrahieren (`ReaderDocument::text()`) — ToUnicode-Tabellen, + Kodierungen einfacher Schriften mit `/Differences`, zusammengesetzte + Schriften, Form-XObjects; Zeilen und Wortabstände aus den Glyphpositionen. - Zusammenführen (`PdfMerger`) — ganze oder ausgewählte Seiten aus mehreren Dateien anhängen und umordnen; Seitenannotationen und Lesezeichen werden mit umgemappten internen Links und benannten Zielen übernommen. diff --git a/docs/en/MERGE.md b/docs/en/MERGE.md index 657beda..66c82da 100644 --- a/docs/en/MERGE.md +++ b/docs/en/MERGE.md @@ -9,6 +9,7 @@ All of it is pure PHP and MIT-licensed — no FPDI, no external binaries. ## Contents - [Reading a PDF](#reading-a-pdf) +- [Extracting text](#extracting-text) - [Appending documents](#appending-documents) - [Selecting and reordering pages](#selecting-and-reordering-pages) - [Embedding (stamping) a page](#embedding-stamping-a-page) @@ -36,6 +37,22 @@ A `ReaderDocument` resolves objects lazily, follows the cross-reference chain (classic tables, XRef streams, and object streams), and falls back to a full-file scan when the cross-reference is corrupt. +## Extracting text + +```php +$doc = ReaderDocument::fromBytes(file_get_contents('report.pdf')); +echo $doc->text(); // every page, pages separated by a blank line +``` + +Each glyph's text comes from its font: the `/ToUnicode` map first, then the +simple font's encoding (Standard, WinAnsi, MacRoman) with `/Differences` +named by the Adobe Glyph List (Cyrillic `afii` names, `uniXXXX` included). +Text is read in content order; a move to another line starts a new line, a +gap wider than a fifth of the font size becomes a space, and typographic +ligatures are written out (`fi` → `fi`). Form XObjects are read too. A scan +has no text to give — there is no OCR — and a composite font without a +`/ToUnicode` map gives nothing for its glyphs. + ## Appending documents `PdfMerger` concatenates pages from one or more sources into a new document diff --git a/docs/ru/MERGE.md b/docs/ru/MERGE.md index fd8dd45..a1ef096 100644 --- a/docs/ru/MERGE.md +++ b/docs/ru/MERGE.md @@ -10,6 +10,7 @@ PDF-файлы** и **комбинировать** их: присоединят ## Содержание - [Чтение PDF](#чтение-pdf) +- [Извлечение текста](#извлечение-текста) - [Присоединение документов](#присоединение-документов) - [Выбор и переупорядочивание страниц](#выбор-и-переупорядочивание-страниц) - [Встраивание (штамп) страницы](#встраивание-штамп-страницы) @@ -37,6 +38,22 @@ foreach ($doc->pages() as $page) { (классические таблицы, XRef-потоки и object-потоки) и переходит к полному скану файла, если xref повреждён. +## Извлечение текста + +```php +$doc = ReaderDocument::fromBytes(file_get_contents('report.pdf')); +echo $doc->text(); // все страницы, между страницами — пустая строка +``` + +Текст каждого глифа берётся из его шрифта: сначала карта `/ToUnicode`, затем +кодировка простого шрифта (Standard, WinAnsi, MacRoman) с `/Differences` по +именам Adobe Glyph List (включая кириллические `afii` и `uniXXXX`). Текст +читается в порядке содержимого; переход на другую строку начинает новую +строку, промежуток шире пятой части кегля становится пробелом, лигатуры +раскрываются (`fi` → `fi`). Form XObject тоже читаются. У скана текста нет — +распознавания нет, — а составной шрифт без карты `/ToUnicode` не даёт текста +своих глифов. + ## Присоединение документов `PdfMerger` склеивает страницы из одного или нескольких источников в новый diff --git a/docs/ru/README.md b/docs/ru/README.md index b60422c..55650d3 100644 --- a/docs/ru/README.md +++ b/docs/ru/README.md @@ -258,6 +258,9 @@ file_put_contents('hello.pdf', $doc->toBytes()); ### Чтение и объединение - Чтение готовых PDF (`ReaderDocument`) — классический и потоковый xref, object-потоки, восстановление битого xref, расшифровка (RC4 / AES-128 / AES-256). +- Извлечение текста (`ReaderDocument::text()`) — карты ToUnicode, кодировки + простых шрифтов с `/Differences`, составные шрифты, form XObject; строки и + пробелы — по положению глифов. - Объединение (`PdfMerger`) — склейка и переупорядочивание всех или выбранных страниц из нескольких файлов; аннотации страниц и закладки переносятся с ремапом внутренних ссылок и именованных назначений. diff --git a/docs/zh/MERGE.md b/docs/zh/MERGE.md index 8a0e714..56e1829 100644 --- a/docs/zh/MERGE.md +++ b/docs/zh/MERGE.md @@ -9,6 +9,7 @@ ## 目录 - [读取 PDF](#读取-pdf) +- [提取文本](#提取文本) - [追加文档](#追加文档) - [选择与重排页面](#选择与重排页面) - [嵌入(盖印)页面](#嵌入盖印页面) @@ -35,6 +36,15 @@ foreach ($doc->pages() as $page) { `ReaderDocument` 惰性解析对象,沿交叉引用链(经典表、XRef 流和对象流)遍历, 并在交叉引用损坏时回退到整文件扫描。 +## 提取文本 + +```php +$doc = ReaderDocument::fromBytes(file_get_contents('report.pdf')); +echo $doc->text(); // 所有页面,页面之间以空行分隔 +``` + +每个字形的文本来自其字体:先查 `/ToUnicode` 映射,再按简单字体的编码(Standard、WinAnsi、MacRoman)及按 Adobe Glyph List 命名的 `/Differences`(包括西里尔 `afii` 名称和 `uniXXXX`)。文本按内容顺序读取;换到另一行时开始新行,宽于字号五分之一的间隙变为空格,排版连字会展开(`fi` → `fi`)。表单 XObject 也会读取。扫描件没有文本可提取——不做 OCR;没有 `/ToUnicode` 映射的复合字体不会给出其字形的文本。 + ## 追加文档 `PdfMerger` 将一个或多个来源的页面连接成一个新文档(pdftk 风格): diff --git a/docs/zh/README.md b/docs/zh/README.md index a2c73c4..58c4f50 100644 --- a/docs/zh/README.md +++ b/docs/zh/README.md @@ -241,6 +241,7 @@ file_put_contents('hello.pdf', $doc->toBytes()); ### 读取与合并 - 读取现有 PDF(`ReaderDocument`)—— 经典与流式 xref、对象流、损坏 xref 恢复、 解密(RC4 / AES-128 / AES-256)。 +- 提取纯文本(`ReaderDocument::text()`)—— ToUnicode 映射、带 `/Differences` 的简单字体编码、复合字体、表单 XObject;按字形位置划分行与词间空格。 - 合并(`PdfMerger`)—— 追加并重排来自多个文件的全部或选定页面;页面注释与书签 会被带入,内部链接和命名目标随之重新映射。 - 盖印叠加(水印、信头)与 FPDI 风格的页面导入,通过 diff --git a/src/Pdf/Reader/ReaderDocument.php b/src/Pdf/Reader/ReaderDocument.php index 4b4a533..703c1ea 100644 --- a/src/Pdf/Reader/ReaderDocument.php +++ b/src/Pdf/Reader/ReaderDocument.php @@ -274,6 +274,15 @@ public function streamData(PdfStream $stream): string return (new StreamDecoder($this))->decode($stream); } + /** + * The document's plain text, pages separated by a blank line (§9.10): + * what its fonts say the shown glyphs are, in content order. + */ + public function text(): string + { + return (new Text\TextExtractor($this))->document(); + } + /** * The document catalog (`/Root`). */ diff --git a/src/Pdf/Reader/Text/FontDecoder.php b/src/Pdf/Reader/Text/FontDecoder.php new file mode 100644 index 0000000..14bc122 --- /dev/null +++ b/src/Pdf/Reader/Text/FontDecoder.php @@ -0,0 +1,188 @@ + one-byte code → text, simple fonts */ + private array $encoding = []; + + /** @var array code → width */ + private array $widths = []; + + private float $defaultWidth = 500.0; + + /** Thousandths per glyph space unit: 1 except for Type 3 fonts. */ + private float $scale = 1.0; + + private bool $composite = false; + + public static function fromDictionary(ReaderDocument $doc, PdfDictionary $font): self + { + $decoder = new self(); + $subtype = self::name($doc->deref($font->get('Subtype'))); + $toUnicode = $doc->deref($font->get('ToUnicode')); + if ($toUnicode instanceof PdfStream) { + try { + $map = ToUnicodeMap::parse($doc->streamData($toUnicode)); + $decoder->toUnicode = $map->isEmpty() ? null : $map; + } catch (Throwable) { + $decoder->toUnicode = null; + } + } + if ($subtype === 'Type0') { + $decoder->composite = true; + $decoder->defaultWidth = 1000.0; + $descendants = $doc->deref($font->get('DescendantFonts')); + $cid = is_array($descendants) ? $doc->deref($descendants[0] ?? null) : null; + if ($cid instanceof PdfDictionary) { + $decoder->cidWidths($doc, $cid); + } + + return $decoder; + } + $decoder->simpleEncoding($doc, $font, $subtype); + $decoder->simpleWidths($doc, $font); + if ($subtype === 'Type3') { + $matrix = $doc->deref($font->get('FontMatrix')); + $a = is_array($matrix) ? $doc->deref($matrix[0] ?? null) : null; + $decoder->scale = is_int($a) || is_float($a) ? (float) $a * 1000.0 : 1.0; + } + + return $decoder; + } + + /** + * The codes of a string: each code's text (empty when unknown), width and + * whether it is the single-byte space that word spacing applies to. + * + * @return list + */ + public function decode(string $bytes): array + { + $out = []; + $length = strlen($bytes); + $i = 0; + while ($i < $length) { + $n = $this->toUnicode?->codeLength($bytes, $i) ?? ($this->composite ? 2 : 1); + $code = substr($bytes, $i, $n); + $i += $n; + $number = 0; + foreach (str_split($code) as $byte) { + $number = ($number << 8) | ord($byte); + } + $text = $this->toUnicode?->lookup($code); + if ($text === null && !$this->composite) { + $text = $this->encoding[$number] ?? ''; + } + $out[] = [$text ?? '', ($this->widths[$number] ?? $this->defaultWidth) * $this->scale, $n === 1 && $number === 32]; + } + + return $out; + } + + private function simpleEncoding(ReaderDocument $doc, PdfDictionary $font, ?string $subtype): void + { + $encoding = $doc->deref($font->get('Encoding')); + $base = $subtype === 'TrueType' ? 'WinAnsiEncoding' : 'StandardEncoding'; + $differences = null; + if ($encoding instanceof PdfName) { + $base = $encoding->value; + } elseif ($encoding instanceof PdfDictionary) { + $named = self::name($doc->deref($encoding->get('BaseEncoding'))); + $base = $named ?? $base; + $differences = $doc->deref($encoding->get('Differences')); + } + $this->encoding = SimpleEncodings::map($base); + if (is_array($differences)) { + $code = 0; + foreach ($differences as $item) { + $item = $doc->deref($item); + if (is_int($item)) { + $code = $item; + } elseif ($item instanceof PdfName) { + $text = GlyphNames::toUnicode($item->value); + if ($text !== null) { + $this->encoding[$code] = $text; + } else { + unset($this->encoding[$code]); + } + $code++; + } + } + } + } + + private function simpleWidths(ReaderDocument $doc, PdfDictionary $font): void + { + $first = $doc->deref($font->get('FirstChar')); + $widths = $doc->deref($font->get('Widths')); + if (!is_int($first) || !is_array($widths)) { + return; + } + foreach (array_values($widths) as $i => $w) { + $w = $doc->deref($w); + if (is_int($w) || is_float($w)) { + $this->widths[$first + $i] = (float) $w; + } + } + } + + /** /W: `c [w1 w2 …]` and `cFirst cLast w` entries; /DW the default. */ + private function cidWidths(ReaderDocument $doc, PdfDictionary $cid): void + { + $dw = $doc->deref($cid->get('DW')); + if (is_int($dw) || is_float($dw)) { + $this->defaultWidth = (float) $dw; + } + $w = $doc->deref($cid->get('W')); + if (!is_array($w)) { + return; + } + $items = array_values(array_map(fn (mixed $v): mixed => $doc->deref($v), $w)); + $count = count($items); + for ($i = 0; $i < $count;) { + $start = $items[$i]; + $next = $items[$i + 1] ?? null; + if (is_int($start) && is_array($next)) { + foreach (array_values($next) as $k => $width) { + $width = $doc->deref($width); + if (is_int($width) || is_float($width)) { + $this->widths[$start + $k] = (float) $width; + } + } + $i += 2; + } elseif (is_int($start) && is_int($next) && isset($items[$i + 2]) && (is_int($items[$i + 2]) || is_float($items[$i + 2]))) { + if ($next - $start < 0x10000) { + for ($c = $start; $c <= $next; $c++) { + $this->widths[$c] = (float) $items[$i + 2]; + } + } + $i += 3; + } else { + $i++; + } + } + } + + private static function name(mixed $value): ?string + { + return $value instanceof PdfName ? $value->value : null; + } +} diff --git a/src/Pdf/Reader/Text/GlyphNames.php b/src/Pdf/Reader/Text/GlyphNames.php new file mode 100644 index 0000000..d1d97f9 --- /dev/null +++ b/src/Pdf/Reader/Text/GlyphNames.php @@ -0,0 +1,140 @@ + 0x20, 'exclam' => 0x21, 'quotedbl' => 0x22, 'numbersign' => 0x23, 'dollar' => 0x24, + 'percent' => 0x25, 'ampersand' => 0x26, 'quotesingle' => 0x27, 'parenleft' => 0x28, 'parenright' => 0x29, + 'asterisk' => 0x2A, 'plus' => 0x2B, 'comma' => 0x2C, 'hyphen' => 0x2D, 'period' => 0x2E, 'slash' => 0x2F, + 'zero' => 0x30, 'one' => 0x31, 'two' => 0x32, 'three' => 0x33, 'four' => 0x34, 'five' => 0x35, + 'six' => 0x36, 'seven' => 0x37, 'eight' => 0x38, 'nine' => 0x39, 'colon' => 0x3A, 'semicolon' => 0x3B, + 'less' => 0x3C, 'equal' => 0x3D, 'greater' => 0x3E, 'question' => 0x3F, 'at' => 0x40, + 'bracketleft' => 0x5B, 'backslash' => 0x5C, 'bracketright' => 0x5D, 'asciicircum' => 0x5E, + 'underscore' => 0x5F, 'grave' => 0x60, 'braceleft' => 0x7B, 'bar' => 0x7C, 'braceright' => 0x7D, + 'asciitilde' => 0x7E, 'nbspace' => 0xA0, 'nonbreakingspace' => 0xA0, 'sfthyphen' => 0xAD, 'softhyphen' => 0xAD, + 'Euro' => 0x20AC, 'quotesinglbase' => 0x201A, 'florin' => 0x0192, 'quotedblbase' => 0x201E, + 'ellipsis' => 0x2026, 'dagger' => 0x2020, 'daggerdbl' => 0x2021, 'circumflex' => 0x02C6, + 'perthousand' => 0x2030, 'Scaron' => 0x0160, 'guilsinglleft' => 0x2039, 'OE' => 0x0152, + 'Zcaron' => 0x017D, 'quoteleft' => 0x2018, 'quoteright' => 0x2019, 'quotedblleft' => 0x201C, + 'quotedblright' => 0x201D, 'bullet' => 0x2022, 'endash' => 0x2013, 'emdash' => 0x2014, 'tilde' => 0x02DC, + 'trademark' => 0x2122, 'scaron' => 0x0161, 'guilsinglright' => 0x203A, 'oe' => 0x0153, 'zcaron' => 0x017E, + 'Ydieresis' => 0x0178, 'fi' => 0xFB01, 'fl' => 0xFB02, 'ff' => 0xFB00, 'ffi' => 0xFB03, 'ffl' => 0xFB04, + 'dotlessi' => 0x0131, 'minus' => 0x2212, 'fraction' => 0x2044, 'Lslash' => 0x0141, 'lslash' => 0x0142, + 'caron' => 0x02C7, 'breve' => 0x02D8, 'dotaccent' => 0x02D9, 'ring' => 0x02DA, 'ogonek' => 0x02DB, + 'hungarumlaut' => 0x02DD, 'periodcentered' => 0x00B7, 'numero' => 0x2116, 'afii61352' => 0x2116, + 'arrowright' => 0x2192, 'arrowleft' => 0x2190, 'checkmark' => 0x2713, 'degree' => 0x00B0, + ]; + + /** @var array|null */ + private static ?array $table = null; + + public static function toUnicode(string $name): ?string + { + $base = explode('.', $name, 2)[0]; + if ($base === '') { + return null; + } + if (str_contains($base, '_')) { + $out = ''; + foreach (explode('_', $base) as $part) { + $one = self::single($part); + if ($one === null) { + return null; + } + $out .= $one; + } + + return $out; + } + + return self::single($base); + } + + private static function single(string $name): ?string + { + if (preg_match('/^uni((?:[0-9A-F]{4})+)$/', $name, $m) === 1) { + $out = ''; + foreach (str_split($m[1], 4) as $hex) { + $cp = (int) hexdec($hex); + if ($cp >= 0xD800 && $cp <= 0xDFFF) { + return null; + } + $out .= mb_chr($cp, 'UTF-8'); + } + + return $out; + } + if (preg_match('/^u([0-9A-F]{4,6})$/', $name, $m) === 1) { + $cp = (int) hexdec($m[1]); + + return ($cp <= 0x10FFFF && ($cp < 0xD800 || $cp > 0xDFFF)) ? mb_chr($cp, 'UTF-8') : null; + } + if (preg_match('/^afii(100(1[7-9]|[2-4]\d)|1006[5-9]|100[7-9]\d)$/', $name) === 1) { + return self::cyrillic((int) substr($name, 4)); + } + if (strlen($name) === 1 && ctype_alpha($name)) { + return $name; + } + $cp = self::table()[$name] ?? null; + + return $cp === null ? null : mb_chr($cp, 'UTF-8'); + } + + /** afii10017…10049 — А…Я with Ё; afii10065…10097 — а…я with ё. */ + private static function cyrillic(int $n): ?string + { + $cp = match (true) { + $n === 10023 => 0x0401, + $n >= 10017 && $n <= 10022 => 0x0410 + ($n - 10017), + $n >= 10024 && $n <= 10049 => 0x0416 + ($n - 10024), + $n === 10071 => 0x0451, + $n >= 10065 && $n <= 10070 => 0x0430 + ($n - 10065), + $n >= 10072 && $n <= 10097 => 0x0436 + ($n - 10072), + default => null, + }; + + return $cp === null ? null : mb_chr($cp, 'UTF-8'); + } + + /** @return array */ + private static function table(): array + { + if (self::$table === null) { + $table = self::NAMES; + foreach (self::LATIN1 as $i => $name) { + if ($name !== null) { + $table[$name] ??= 0xA1 + $i; + } + } + self::$table = $table; + } + + return self::$table; + } +} diff --git a/src/Pdf/Reader/Text/SimpleEncodings.php b/src/Pdf/Reader/Text/SimpleEncodings.php new file mode 100644 index 0000000..37ff342 --- /dev/null +++ b/src/Pdf/Reader/Text/SimpleEncodings.php @@ -0,0 +1,60 @@ + 0x2019, 0x60 => 0x2018, 0xA1 => 0x00A1, 0xA2 => 0x00A2, 0xA3 => 0x00A3, 0xA4 => 0x2044, + 0xA5 => 0x00A5, 0xA6 => 0x0192, 0xA7 => 0x00A7, 0xA8 => 0x00A4, 0xA9 => 0x0027, 0xAA => 0x201C, + 0xAB => 0x00AB, 0xAC => 0x2039, 0xAD => 0x203A, 0xAE => 0xFB01, 0xAF => 0xFB02, 0xB1 => 0x2013, + 0xB2 => 0x2020, 0xB3 => 0x2021, 0xB4 => 0x00B7, 0xB6 => 0x00B6, 0xB7 => 0x2022, 0xB8 => 0x201A, + 0xB9 => 0x201E, 0xBA => 0x201D, 0xBB => 0x00BB, 0xBC => 0x2026, 0xBD => 0x2030, 0xBF => 0x00BF, + 0xC1 => 0x0060, 0xC2 => 0x00B4, 0xC3 => 0x02C6, 0xC4 => 0x02DC, 0xC5 => 0x00AF, 0xC6 => 0x02D8, + 0xC7 => 0x02D9, 0xC8 => 0x00A8, 0xCA => 0x02DA, 0xCB => 0x00B8, 0xCD => 0x02DD, 0xCE => 0x02DB, + 0xCF => 0x02C7, 0xD0 => 0x2014, 0xE1 => 0x00C6, 0xE3 => 0x00AA, 0xE8 => 0x0141, 0xE9 => 0x00D8, + 0xEA => 0x0152, 0xEB => 0x00BA, 0xF1 => 0x00E6, 0xF5 => 0x0131, 0xF8 => 0x0142, 0xF9 => 0x00F8, + 0xFA => 0x0153, 0xFB => 0x00DF, + ]; + + /** MacRomanEncoding, codes 0x80…0xFF (Annex D.2). */ + private const MAC_ROMAN_HIGH = [ + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, + 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, + 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, + 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, + 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, + 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, + 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, + 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7, + ]; + + /** + * @return array code → text + */ + public static function map(string $name): array + { + $out = []; + for ($code = 0x20; $code <= 0xFF; $code++) { + $cp = match ($name) { + 'WinAnsiEncoding' => WinAnsi::codepoint($code), + 'MacRomanEncoding' => $code < 0x80 ? $code : self::MAC_ROMAN_HIGH[$code - 0x80], + default => self::STANDARD[$code] ?? ($code < 0x7F ? $code : null), + }; + if ($cp !== null) { + $out[$code] = mb_chr($cp, 'UTF-8'); + } + } + + return $out; + } +} diff --git a/src/Pdf/Reader/Text/TextExtractor.php b/src/Pdf/Reader/Text/TextExtractor.php new file mode 100644 index 0000000..bb2db6b --- /dev/null +++ b/src/Pdf/Reader/Text/TextExtractor.php @@ -0,0 +1,383 @@ + 'ff', "\u{FB01}" => 'fi', "\u{FB02}" => 'fl', "\u{FB03}" => 'ffi', "\u{FB04}" => 'ffl', "\u{FB05}" => 'st', "\u{FB06}" => 'st']; + + /** Form XObjects inside form XObjects deeper than this are not read. */ + private const MAX_DEPTH = 8; + + /** @var array */ + private array $fonts = []; + + private string $out = ''; + + private ?float $lastY = null; + + private ?float $lastEndX = null; + + private float $lastHeight = 0.0; + + public function __construct(private readonly ReaderDocument $doc) + { + } + + /** The text of every page, pages separated by a blank line. */ + public function document(): string + { + $pages = []; + foreach ($this->doc->pages() as $page) { + $pages[] = $this->page($page); + } + + return trim(implode("\n\n", array_filter($pages, static fn (string $t): bool => $t !== ''))); + } + + public function page(ReaderPage $page): string + { + $this->out = ''; + $this->lastY = null; + $this->lastEndX = null; + $contents = $this->doc->deref($page->dict->get('Contents')); + $streams = $contents instanceof PdfStream ? [$contents] : (is_array($contents) ? $contents : []); + $data = ''; + foreach ($streams as $stream) { + $stream = $this->doc->deref($stream); + if ($stream instanceof PdfStream) { + try { + $data .= $this->doc->streamData($stream)."\n"; + } catch (Throwable) { + continue; + } + } + } + $this->run($data, $page->resources, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], 0); + + return self::tidy($this->out); + } + + /** + * @param array{float,float,float,float,float,float} $ctm + */ + private function run(string $data, ?PdfDictionary $resources, array $ctm, int $depth): void + { + $lexer = new Lexer($data); + $state = ['ctm' => $ctm, 'font' => null, 'size' => 0.0, 'tc' => 0.0, 'tw' => 0.0, 'th' => 1.0, 'tl' => 0.0, 'rise' => 0.0]; + $stack = []; + $tm = $tlm = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + $operands = []; + try { + while (($token = $lexer->nextToken())->type !== TokenType::Eof) { + if ($token->type !== TokenType::Keyword || in_array($token->value, ['true', 'false', 'null'], true)) { + $operands[] = $this->operand($lexer, $token); + continue; + } + $op = (string) $token->value; + switch ($op) { + case 'q': + $stack[] = $state; + break; + case 'Q': + $state = array_pop($stack) ?? $state; + break; + case 'cm': + if (count($operands) >= 6) { + $state['ctm'] = self::multiply(self::matrix(array_slice($operands, -6)), $state['ctm']); + } + break; + case 'BT': + $tm = $tlm = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + break; + case 'Tf': + $name = $operands[count($operands) - 2] ?? null; + $state['font'] = $name instanceof PdfName ? $this->font($resources, $name->value) : null; + $state['size'] = self::num($operands[count($operands) - 1] ?? 0); + break; + case 'Tc': + $state['tc'] = self::num(end($operands)); + break; + case 'Tw': + $state['tw'] = self::num(end($operands)); + break; + case 'Tz': + $state['th'] = self::num(end($operands)) / 100.0; + break; + case 'TL': + $state['tl'] = self::num(end($operands)); + break; + case 'Ts': + $state['rise'] = self::num(end($operands)); + break; + case 'Td': + case 'TD': + $tx = self::num($operands[count($operands) - 2] ?? 0); + $ty = self::num($operands[count($operands) - 1] ?? 0); + if ($op === 'TD') { + $state['tl'] = -$ty; + } + $tm = $tlm = self::multiply([1.0, 0.0, 0.0, 1.0, $tx, $ty], $tlm); + break; + case 'Tm': + if (count($operands) >= 6) { + $tm = $tlm = self::matrix(array_slice($operands, -6)); + } + break; + case 'T*': + $tm = $tlm = self::multiply([1.0, 0.0, 0.0, 1.0, 0.0, -$state['tl']], $tlm); + break; + case 'Tj': + $tm = $this->show(end($operands), $state, $tm); + break; + case "'": + $tm = $tlm = self::multiply([1.0, 0.0, 0.0, 1.0, 0.0, -$state['tl']], $tlm); + $tm = $this->show(end($operands), $state, $tm); + break; + case '"': + $state['tw'] = self::num($operands[count($operands) - 3] ?? 0); + $state['tc'] = self::num($operands[count($operands) - 2] ?? 0); + $tm = $tlm = self::multiply([1.0, 0.0, 0.0, 1.0, 0.0, -$state['tl']], $tlm); + $tm = $this->show(end($operands), $state, $tm); + break; + case 'TJ': + $items = end($operands); + foreach (is_array($items) ? $items : [] as $item) { + if (is_int($item) || is_float($item)) { + $shift = -$item / 1000.0 * $state['size'] * $state['th']; + $tm = self::multiply([1.0, 0.0, 0.0, 1.0, $shift, 0.0], $tm); + } else { + $tm = $this->show($item, $state, $tm); + } + } + break; + case 'Do': + $name = end($operands); + if ($name instanceof PdfName && $depth < self::MAX_DEPTH) { + $this->form($resources, $name->value, $state['ctm'], $depth); + } + break; + case 'BI': + self::skipInlineImage($lexer); + break; + } + $operands = []; + } + } catch (Throwable) { + // A broken stream: the text read so far stands. + } + } + + /** + * @param array $state + * @param array{float,float,float,float,float,float} $tm + * @return array{float,float,float,float,float,float} + */ + private function show(mixed $string, array $state, array $tm): array + { + $font = $state['font']; + if (!is_string($string) || !$font instanceof FontDecoder) { + return $tm; + } + $size = (float) $state['size']; + foreach ($font->decode($string) as [$text, $width, $isSpace]) { + $trm = self::multiply(self::multiply([$size * $state['th'], 0.0, 0.0, $size, 0.0, $state['rise']], $tm), $state['ctm']); + $advance = ($width / 1000.0 * $size + $state['tc'] + ($isSpace ? $state['tw'] : 0.0)) * $state['th']; + $next = self::multiply([1.0, 0.0, 0.0, 1.0, $advance, 0.0], $tm); + $end = self::multiply(self::multiply([1.0, 0.0, 0.0, 1.0, 0.0, $state['rise']], $next), $state['ctm']); + $this->emit($text, $trm[4], $trm[5], $end[4], hypot($trm[2], $trm[3])); + $tm = $next; + } + + return $tm; + } + + private function emit(string $text, float $x, float $y, float $endX, float $height): void + { + if ($text === '') { + $this->lastEndX = $endX; + + return; + } + $height = $height > 0.0 ? $height : 10.0; + if ($this->lastY !== null && $this->lastEndX !== null) { + $lineGap = abs($y - $this->lastY); + $reference = max($height, $this->lastHeight); + if ($lineGap > 0.5 * $reference) { + $this->out = rtrim($this->out, ' ').($lineGap > 2.0 * $reference ? "\n\n" : "\n"); + } elseif ($x - $this->lastEndX > 0.2 * $reference && !str_ends_with($this->out, ' ') && !str_starts_with($text, ' ')) { + $this->out .= ' '; + } + } + $this->out .= $text; + $this->lastY = $y; + $this->lastEndX = $endX; + $this->lastHeight = $height; + } + + private function font(?PdfDictionary $resources, string $name): ?FontDecoder + { + $fonts = $resources === null ? null : $this->doc->deref($resources->get('Font')); + if (!$fonts instanceof PdfDictionary) { + return null; + } + $raw = $fonts->get($name); + $key = $raw instanceof PdfReference ? 'r'.$raw->number : 'o'.spl_object_id($fonts).'/'.$name; + if (isset($this->fonts[$key])) { + return $this->fonts[$key]; + } + $dict = $this->doc->deref($raw); + if (!$dict instanceof PdfDictionary) { + return null; + } + + return $this->fonts[$key] = FontDecoder::fromDictionary($this->doc, $dict); + } + + /** + * @param array{float,float,float,float,float,float} $ctm + */ + private function form(?PdfDictionary $resources, string $name, array $ctm, int $depth): void + { + $xobjects = $resources === null ? null : $this->doc->deref($resources->get('XObject')); + $form = $xobjects instanceof PdfDictionary ? $this->doc->deref($xobjects->get($name)) : null; + if (!$form instanceof PdfStream) { + return; + } + $subtype = $this->doc->deref($form->dict->get('Subtype')); + if (!$subtype instanceof PdfName || $subtype->value !== 'Form') { + return; + } + $matrix = $this->doc->deref($form->dict->get('Matrix')); + $own = $this->doc->deref($form->dict->get('Resources')); + try { + $data = $this->doc->streamData($form); + } catch (Throwable) { + return; + } + $m = is_array($matrix) && count($matrix) === 6 ? self::matrix(array_map(fn (mixed $v): mixed => $this->doc->deref($v), array_values($matrix))) : [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + $this->run($data, $own instanceof PdfDictionary ? $own : $resources, self::multiply($m, $ctm), $depth + 1); + } + + /** An operand: a number, a name, a string's bytes, an array of them; a dictionary is skipped. */ + private function operand(Lexer $lexer, Token $token): mixed + { + return match ($token->type) { + TokenType::Number => $token->value, + TokenType::Name => new PdfName((string) $token->value), + TokenType::Str, TokenType::HexStr => (string) $token->value, + TokenType::ArrayOpen => $this->array($lexer), + TokenType::DictOpen => self::skipDictionary($lexer), + default => null, + }; + } + + /** @return list */ + private function array(Lexer $lexer): array + { + $items = []; + while (($token = $lexer->nextToken())->type !== TokenType::ArrayClose) { + if ($token->type === TokenType::Eof) { + break; + } + $items[] = $this->operand($lexer, $token); + } + + return $items; + } + + private static function skipDictionary(Lexer $lexer): null + { + $depth = 1; + while ($depth > 0 && ($token = $lexer->nextToken())->type !== TokenType::Eof) { + $depth += match ($token->type) { + TokenType::DictOpen => 1, + TokenType::DictClose => -1, + default => 0, + }; + } + + return null; + } + + /** `BI … ID EI`: past the image data, which is not PDF syntax. */ + private static function skipInlineImage(Lexer $lexer): void + { + while (($token = $lexer->nextToken())->type !== TokenType::Eof) { + if ($token->isKeyword('ID')) { + break; + } + } + $data = $lexer->data(); + if (preg_match('/\sEI(?=\s|$)/', $data, $m, PREG_OFFSET_CAPTURE, $lexer->position()) === 1) { + $lexer->seek($m[0][1] + strlen($m[0][0])); + } else { + $lexer->seek(strlen($data)); + } + } + + private static function tidy(string $text): string + { + $lines = array_map(static fn (string $l): string => rtrim($l), explode("\n", $text)); + + $text = strtr(implode("\n", $lines), self::LIGATURES); + + return trim((string) preg_replace("/\n{3,}/", "\n\n", $text)); + } + + private static function num(mixed $value): float + { + return is_int($value) || is_float($value) ? (float) $value : 0.0; + } + + /** + * @param list $values + * @return array{float,float,float,float,float,float} + */ + private static function matrix(array $values): array + { + $v = array_map(self::num(...), array_values($values)); + + return [$v[0] ?? 1.0, $v[1] ?? 0.0, $v[2] ?? 0.0, $v[3] ?? 1.0, $v[4] ?? 0.0, $v[5] ?? 0.0]; + } + + /** + * @param array{float,float,float,float,float,float} $m1 + * @param array{float,float,float,float,float,float} $m2 + * @return array{float,float,float,float,float,float} + */ + private static function multiply(array $m1, array $m2): array + { + return [ + $m1[0] * $m2[0] + $m1[1] * $m2[2], + $m1[0] * $m2[1] + $m1[1] * $m2[3], + $m1[2] * $m2[0] + $m1[3] * $m2[2], + $m1[2] * $m2[1] + $m1[3] * $m2[3], + $m1[4] * $m2[0] + $m1[5] * $m2[2] + $m2[4], + $m1[4] * $m2[1] + $m1[5] * $m2[3] + $m2[5], + ]; + } +} diff --git a/src/Pdf/Reader/Text/ToUnicodeMap.php b/src/Pdf/Reader/Text/ToUnicodeMap.php new file mode 100644 index 0000000..59655be --- /dev/null +++ b/src/Pdf/Reader/Text/ToUnicodeMap.php @@ -0,0 +1,178 @@ + code bytes → UTF-8 text */ + private array $map = []; + + /** @var list code space ranges: byte length, low, high */ + private array $spaces = []; + + public static function parse(string $data): self + { + $cmap = new self(); + $lexer = new Lexer($data); + $mode = null; + $operands = []; + while (true) { + $token = $lexer->nextToken(); + if ($token->type === TokenType::Eof) { + break; + } + if ($token->type === TokenType::Keyword) { + $word = (string) $token->value; + if (in_array($word, ['begincodespacerange', 'beginbfchar', 'beginbfrange'], true)) { + $mode = $word; + $operands = []; + } elseif (in_array($word, ['endcodespacerange', 'endbfchar', 'endbfrange'], true)) { + $cmap->apply((string) $mode, $operands); + $mode = null; + $operands = []; + } + continue; + } + if ($mode === null) { + continue; + } + if ($token->type === TokenType::Str || $token->type === TokenType::HexStr) { + $operands[] = (string) $token->value; + } elseif ($token->type === TokenType::ArrayOpen) { + $items = []; + while (($inner = $lexer->nextToken())->type !== TokenType::ArrayClose && $inner->type !== TokenType::Eof) { + if ($inner->type === TokenType::Str || $inner->type === TokenType::HexStr) { + $items[] = (string) $inner->value; + } + } + $operands[] = $items; + } + } + + return $cmap; + } + + public function isEmpty(): bool + { + return $this->map === []; + } + + /** The text of a code, or null when the map does not have it. */ + public function lookup(string $code): ?string + { + return $this->map[$code] ?? null; + } + + /** + * Bytes the next code takes at `$offset`, by the code space ranges; null + * when the map declares none (the caller decides). + */ + public function codeLength(string $bytes, int $offset): ?int + { + if ($this->spaces === []) { + return null; + } + foreach ($this->spaces as [$length, $low, $high]) { + $code = substr($bytes, $offset, $length); + if (strlen($code) === $length && strcmp($code, $low) >= 0 && strcmp($code, $high) <= 0) { + return $length; + } + } + + return null; + } + + /** + * @param list> $operands + */ + private function apply(string $mode, array $operands): void + { + if ($mode === 'begincodespacerange') { + for ($i = 0; $i + 1 < count($operands); $i += 2) { + [$low, $high] = [$operands[$i], $operands[$i + 1]]; + if (is_string($low) && is_string($high) && strlen($low) === strlen($high) && $low !== '') { + $this->spaces[] = [strlen($low), $low, $high]; + } + } + usort($this->spaces, static fn (array $a, array $b): int => $a[0] <=> $b[0]); + } elseif ($mode === 'beginbfchar') { + for ($i = 0; $i + 1 < count($operands); $i += 2) { + if (is_string($operands[$i]) && is_string($operands[$i + 1])) { + $this->map[$operands[$i]] = self::utf16($operands[$i + 1]); + } + } + } elseif ($mode === 'beginbfrange') { + for ($i = 0; $i + 2 < count($operands); $i += 3) { + $this->range($operands[$i], $operands[$i + 1], $operands[$i + 2]); + } + } + } + + /** + * @param string|list $low + * @param string|list $high + * @param string|list $target + */ + private function range(string|array $low, string|array $high, string|array $target): void + { + if (!is_string($low) || !is_string($high) || strlen($low) !== strlen($high) || $low === '') { + return; + } + $from = self::number($low); + $to = self::number($high); + if ($to < $from || $to - $from >= self::MAX_RANGE) { + return; + } + $width = strlen($low); + for ($n = $from; $n <= $to; $n++) { + $code = substr(pack('J', $n), -$width); + if (is_array($target)) { + $item = $target[$n - $from] ?? null; + if (is_string($item)) { + $this->map[$code] = self::utf16($item); + } + continue; + } + // The last UTF-16 unit of the target grows with the code. + $units = array_values(unpack('n*', strlen($target) % 2 === 0 ? $target : "\0".$target) ?: []); + if ($units === []) { + return; + } + $units[count($units) - 1] += $n - $from; + $this->map[$code] = self::utf16(pack('n*', ...array_map(static fn (int $u): int => $u & 0xFFFF, $units))); + } + } + + private static function number(string $bytes): int + { + $n = 0; + foreach (str_split($bytes) as $byte) { + $n = ($n << 8) | ord($byte); + } + + return $n; + } + + private static function utf16(string $bytes): string + { + if (strlen($bytes) % 2 === 1) { + $bytes = "\0".$bytes; + } + $text = mb_convert_encoding($bytes, 'UTF-8', 'UTF-16BE'); + + return is_string($text) ? $text : ''; + } +} diff --git a/tests/Pdf/Reader/TextExtractorTest.php b/tests/Pdf/Reader/TextExtractorTest.php new file mode 100644 index 0000000..42186c4 --- /dev/null +++ b/tests/Pdf/Reader/TextExtractorTest.php @@ -0,0 +1,152 @@ +addPage() + ->showText('Quarterly report', 72, 720, StandardFont::HelveticaBold, 18) + ->showText('Revenue grew', 72, 690, StandardFont::Helvetica, 12) + ->showText('by 12%', 160, 690, StandardFont::Helvetica, 12) + ->showText('Caf'."\u{00E9}".' — fine', 72, 600, StandardFont::TimesRoman, 12); + $doc->addPage()->showText('Second page', 72, 720, StandardFont::Courier, 10); + + $text = ReaderDocument::fromBytes($doc->toBytes())->text(); + + self::assertSame("Quarterly report\nRevenue grew by 12%\n\nCafé — fine\n\nSecond page", $text); + } + + #[Test] + public function reads_cyrillic_of_an_embedded_font_through_its_to_unicode_map(): void + { + $path = __DIR__.'/../../../.cache/fonts/liberation-fonts-ttf-2.1.5/LiberationSans-Regular.ttf'; + if (!is_readable($path)) { + self::markTestSkipped('Liberation Sans not cached.'); + } + $font = new PdfFont(TtfFile::fromFile($path)); + $doc = new PdfDocument(); + $doc->addPage() + ->showEmbeddedText('Техническое задание', 72, 720, $font, 14) + ->showEmbeddedText('Мощность двигателя: 50–500 л. с.', 72, 700, $font, 11); + + self::assertSame("Техническое задание\nМощность двигателя: 50–500 л. с.", ReaderDocument::fromBytes($doc->toBytes())->text()); + } + + /** + * @return array}> + */ + public static function corpus(): array + { + return [ + 'Google Docs' => ['google-doc.pdf', ['Example document', 'Beautiful is better than ugly.', 'Special cases aren\'t special enough to break the rules.']], + 'pdflatex' => ['pdflatex-4pages.pdf', ['Hello, here is some text without a meaning.', '“Huardest gefburn”?']], + 'pdflatex, a chapter' => ['pdflatex-image.pdf', ['1 Your Chapter', 'Lorem ipsum dolor sit amet']], + 'PDF/A with ligatures' => ['crazyones-pdfa.pdf', ['The Crazy Ones', 'The misfits. The rebels.', 'who see things differently']], + 'pdfkit' => ['pdfkit.pdf', ['Header']], + 'annotated' => ['annotated.pdf', ["Line 1\nLine 2"]], + ]; + } + + /** + * @param list $expected + */ + #[Test] + #[DataProvider('corpus')] + public function reads_documents_of_other_producers(string $file, array $expected): void + { + $text = ReaderDocument::fromBytes((string) file_get_contents(self::EXTERNAL.$file))->text(); + + foreach ($expected as $fragment) { + self::assertStringContainsString($fragment, $text); + } + } + + #[Test] + public function a_scan_without_text_gives_nothing(): void + { + self::assertSame('', ReaderDocument::fromBytes((string) file_get_contents(self::EXTERNAL.'imagemagick-lzw.pdf'))->text()); + } + + #[Test] + public function maps_codes_by_char_and_by_range_with_the_code_space(): void + { + $map = ToUnicodeMap::parse(<<<'CMAP' + /CIDInit /ProcSet findresource begin + 12 dict begin + begincmap + 1 begincodespacerange + <0000> + endcodespacerange + 2 beginbfchar + <0003> <0020> + <0010> <0054 0065> + endbfchar + 2 beginbfrange + <0020> <0022> <0410> + <0030> <0031> [<0061> ] + endbfrange + endcmap + CMAP); + + self::assertSame(' ', $map->lookup("\x00\x03")); + self::assertSame('Te', $map->lookup("\x00\x10")); + self::assertSame('А', $map->lookup("\x00\x20")); + self::assertSame('В', $map->lookup("\x00\x22")); + self::assertSame('a', $map->lookup("\x00\x30")); + self::assertSame("\u{1F600}", $map->lookup("\x00\x31")); + self::assertNull($map->lookup("\x00\x23")); + self::assertSame(2, $map->codeLength("\x00\x30\x00\x31", 2)); + self::assertFalse($map->isEmpty()); + self::assertNull(ToUnicodeMap::parse('begincmap endcmap')->codeLength("\x00", 0)); + } + + #[Test] + public function names_glyphs_as_the_glyph_list_does(): void + { + self::assertSame('A', GlyphNames::toUnicode('A')); + self::assertSame('é', GlyphNames::toUnicode('eacute')); + self::assertSame('’', GlyphNames::toUnicode('quoteright')); + self::assertSame('Я', GlyphNames::toUnicode('afii10049')); + self::assertSame('ё', GlyphNames::toUnicode('afii10071')); + self::assertSame('Ё', GlyphNames::toUnicode('afii10023')); + self::assertSame('щ', GlyphNames::toUnicode('afii10091')); + self::assertSame('ЖЗ', GlyphNames::toUnicode('uni04160417')); + self::assertSame("\u{1F600}", GlyphNames::toUnicode('u1F600')); + self::assertSame('fi', GlyphNames::toUnicode('f_i')); + self::assertSame('a', GlyphNames::toUnicode('a.sc')); + self::assertSame('½', GlyphNames::toUnicode('onehalf')); + self::assertNull(GlyphNames::toUnicode('g123')); + self::assertNull(GlyphNames::toUnicode('.notdef')); + } + + #[Test] + public function knows_the_base_encodings(): void + { + self::assertSame('’', SimpleEncodings::map('StandardEncoding')[0x27]); + self::assertSame('fi', SimpleEncodings::map('StandardEncoding')[0xAE]); + self::assertSame('€', SimpleEncodings::map('WinAnsiEncoding')[0x80]); + self::assertSame("'", SimpleEncodings::map('WinAnsiEncoding')[0x27]); + self::assertSame('Ä', SimpleEncodings::map('MacRomanEncoding')[0x80]); + self::assertSame('ˇ', SimpleEncodings::map('MacRomanEncoding')[0xFF]); + } +}