refactor(pdf): R4.8 the TypeScript PDF engine leaves: packages/pdf, pdf-lib, the TS CLI and import path, the oracle generators deleted; test read-back on an own reader of the writer's files (pdfFile.ts), view goldens from the session's SVGs, import helper on the module's reader, N-1 scanner exported by core-wasm/n-1; app no longer depends on @faunosteo/domain; r4.census suite
This commit is contained in:
1 parent
77e11a53e4
commit
0dc1ba27c7
96 files changed
+743
-15769
No files matched your search
@@ -1,20 +1,13 @@
|
||||
import {
|
||||
decodePDFRawStream,
|
||||
PDFArray,
|
||||
PDFDict,
|
||||
PDFDocument,
|
||||
PDFName,
|
||||
PDFRawStream,
|
||||
type PDFPage,
|
||||
} from 'pdf-lib'
|
||||
import { PdfFile, type PdfObject } from './pdfFile'
|
||||
import { round2, type ImageEntry, type TextEntry } from './goldens'
|
||||
|
||||
/**
|
||||
* The drawing facts of a saved PDF, read back from its content streams (R4.6: the core
|
||||
* writes the file, so nothing is recorded at draw time any more; pdf-lib still reads the
|
||||
* file in the tests until R4.8). The streams are the operator sequences pdf-lib emitted
|
||||
* and the core reproduces: one operator per line, `q BT rg Tf TL Tm (Tj T*)+ ET Q` per
|
||||
* text call, `q cm cm cm cm Do Q` per image. Positions are rounded like the recorder did.
|
||||
* writes the file, so nothing is recorded at draw time any more; R4.8: the file is read by
|
||||
* `pdfFile.ts`, pdf-lib having left the tests). The streams are the operator sequences
|
||||
* pdf-lib emitted and the core reproduces: one operator per line, `q BT rg Tf TL Tm (Tj
|
||||
* T*)+ ET Q` per text call, `q cm cm cm cm Do Q` per image. Positions are rounded like the
|
||||
* recorder did.
|
||||
*/
|
||||
|
||||
/** WinAnsi is Windows-1252 for every glyph the engine prints. */
|
||||
@@ -29,43 +22,13 @@ function hexBytes(token: string): Uint8Array {
|
||||
return out
|
||||
}
|
||||
|
||||
function contentOf(page: PDFPage): string {
|
||||
const contents = page.node.Contents()
|
||||
const streams: PDFRawStream[] = []
|
||||
if (contents instanceof PDFRawStream) streams.push(contents)
|
||||
else if (contents instanceof PDFArray) {
|
||||
for (let i = 0; i < contents.size(); i += 1) {
|
||||
streams.push(contents.lookup(i, PDFRawStream))
|
||||
}
|
||||
}
|
||||
return streams
|
||||
.map((stream) =>
|
||||
new TextDecoder('latin1').decode(decodePDFRawStream(stream).decode()),
|
||||
)
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
/** `/F1` → `Helvetica`, through the page's font resources. */
|
||||
function fontNames(page: PDFPage): Map<string, string> {
|
||||
const names = new Map<string, string>()
|
||||
const resources = page.node.lookup(PDFName.of('Resources'), PDFDict)
|
||||
const fonts = resources.lookupMaybe(PDFName.of('Font'), PDFDict)
|
||||
if (!fonts) return names
|
||||
for (const [key, ref] of fonts.entries()) {
|
||||
const font = fonts.context.lookup(ref, PDFDict)
|
||||
const base = font.lookup(PDFName.of('BaseFont'))
|
||||
names.set(key.decodeText(), String(base).replace(/^\//, ''))
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
interface PageFacts {
|
||||
texts: TextEntry[]
|
||||
images: ImageEntry[]
|
||||
}
|
||||
|
||||
function readPage(page: PDFPage, index: number): PageFacts {
|
||||
const fonts = fontNames(page)
|
||||
function readPage(file: PdfFile, page: PdfObject, index: number): PageFacts {
|
||||
const fonts = file.fontNames(page)
|
||||
const texts: TextEntry[] = []
|
||||
const images: ImageEntry[] = []
|
||||
let font: string | null = null
|
||||
@@ -74,7 +37,7 @@ function readPage(page: PDFPage, index: number): PageFacts {
|
||||
let y: number | null = null
|
||||
let lines: string[] = []
|
||||
let matrices: number[][] = []
|
||||
for (const line of contentOf(page).split('\n')) {
|
||||
for (const line of file.content(page).split('\n')) {
|
||||
const tokens = line.trim().split(' ')
|
||||
const op = tokens[tokens.length - 1]
|
||||
const operands = tokens.slice(0, -1)
|
||||
@@ -123,11 +86,11 @@ function readPage(page: PDFPage, index: number): PageFacts {
|
||||
|
||||
/** Every text and image call of the file, page by page, in stream order. */
|
||||
export async function readDrawing(bytes: Uint8Array): Promise<PageFacts> {
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const file = PdfFile.parse(bytes)
|
||||
const texts: TextEntry[] = []
|
||||
const images: ImageEntry[] = []
|
||||
doc.getPages().forEach((page, index) => {
|
||||
const facts = readPage(page, index)
|
||||
file.pages().forEach((page, index) => {
|
||||
const facts = readPage(file, page, index)
|
||||
texts.push(...facts.texts)
|
||||
images.push(...facts.images)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user