interplane.Get Omega
Read in your language

omega-pdf

Omega PDF: read and write PDF files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-pdf / README.md

100 lines · 8919 bytes Raw Blame History Source

omega-pdf

opdf reads PDF files and copies pages between them. It is written only in seeded Omega and built by the pinned compiler (see PIN). It is written from the public standard ISO 32000-1:2008 (PDF 1.7); each rule's clause is cited in the source. No vendor program was disassembled or copied.

opdf info FILE.pdf                    version, page count, encryption, cross-reference and object streams, Info strings
opdf pages FILE.pdf                   per page: index, MediaBox (inherited, 7.7.3.4), Rotate, number of content streams
opdf text FILE.pdf [FIRST LAST]       the text of the pages, in content-stream order
opdf split FILE.pdf FIRST LAST OUT.pdf
opdf merge OUT.pdf A.pdf [B.pdf ...]

text prints text in the order the content streams draw it. This is the reading order as written in the file, not layout analysis: no columns, no sorting, no dehyphenation.

Unicode comes from the font's ToUnicode CMap (bfchar, bfrange in both forms, UTF-16 surrogates, multi-character destinations) when present (9.10.3). Otherwise it comes from the simple font's Encoding: StandardEncoding, WinAnsiEncoding, MacRomanEncoding and Differences with the Latin glyph names. The table is ISO 32000-1 Annex D.2/D.3, kept as data in src/text.om.

split and merge write a new file. It holds the selected pages and every object they reach: resources, fonts, and content and image streams copied byte for byte with no re-encoding. It also has a fresh page tree and catalog, the effective MediaBox/CropBox/Resources/Rotate written on each page, a classic cross-reference table and a trailer.

Build

cat src/head.om vendor/omega-zip/vendor/omega-mv/seed/text/text.om vendor/omega-zip/src/io.om vendor/omega-zip/src/crc32.om vendor/omega-zip/src/inflate.om vendor/omega-zip/src/zlibd.om src/core.om src/text.om src/writer.om src/main.om | OMC > opdf && chmod +x opdf

OMC is omega-mv 82e536e1 seed/out/omc, sha256 6d1b1cf5a44803389a831b67f750ea4cd1c9f53edfde9d6d9241d7352887f1f8. FlateDecode uses omega-zip's inflate at 28e91f99, vendored with S2's one-line sink.patch (vendor/omega-zip/PIN in omega-office records the hashes). src/core.om defines the wout sink.

Gate

OMC < tests/gate.om > build/gate-bin && chmod +x build/gate-bin && build/gate-bin OMC

The gate prints OPDFGATE PASS cases=N mutants-killed=K/K robust-runs=R, exit 0. It prints NOT_RUN and exits 3 when the compiler is not the pin. It runs installed oracles as test-only tools, never shipped: pdfinfo and pdftotext 24.02.0 (their outputs are recorded in corpus/expect), Ghostscript 10.02.1 (-dPDFSTOPONERROR -dPDFSTOPONWARNING -sDEVICE=nullpage, run live on every split and merge output as the structure checker), and pdftotext live on those outputs. qpdf and mutool are not installed. The corpus is described in corpus/SOURCES.txt, made by LibreOffice 24.2.7.2, pdfTeX 1.40.25 and tests/mkcases.om, with oracle verdicts in corpus/ORACLE.txt.

Before the cases the gate builds tests/docs.om with the same compiler and runs it: publication checks of the repository's own text. Its rules (tests/docrules.txt) refuse local home paths, a source or README note citing a file outside the repository or missing from it, a note leaving a question open, a missing licence, and a sentence saying an exit code has no case when tests/cases.txt has one. It prints DOCS PASS ... mutants-killed=K/K (its own mutants, tests/docmutants.txt, each putting back a defect bay 7 found at 9b4164be); any other result fails the gate.

Text comparison normalisation:

Difference classes against the oracles:

  1. Line structure and trailing blanks. pdftotext places line breaks by its own layout rules. On the rotated pdfTeX page it breaks after every word, and it drops trailing spaces. These are removed by the whitespace normalisation.
  2. Ligatures. Annex D maps the glyph names fi and fl to U+FB01 and U+FB02. pdftotext expands them. Removed by the ligature expansion.
  3. Info strings that are not ASCII. info prints strings raw, with bytes outside printable ASCII as \xHH; pdfinfo decodes UTF-16 and PDFDocEncoding. These values are not compared and are counted as info-strings-not-compared.
  4. Glyph names outside Annex D (for example ff, ffi, dotlessj in pdfTeX T1 fonts). pdftotext resolves them through the Adobe Glyph List; opdf prints U+FFFD. No corpus page shows such a code.
  5. Symbolic fonts without Encoding or ToUnicode. Only Differences codes are mapped; other shown codes print U+FFFD, because the font program's built-in encoding is not read.
  6. Standard-14 fonts without Widths. Spacing uses MissingWidth or 500/1000 em, so spaces may differ (none in the corpus).
  7. Repairs. pdftotext and Ghostscript repair damaged files: bad startxref, xref offsets, Prev cycles, missing endstream. opdf refuses them, because reconstruction is not in this slice.

Exit codes

CodeMeaning
0done
1usage, or page range outside the document
2cannot open the input
5cannot create the output (it must not exist already; exclusive creation)
3read error inside the vendored inflate input (pread)
10..21DEFLATE stream errors (omega-zip S1 codes)
30bad zlib header
31zlib preset dictionary
32Adler-32 mismatch
60malformed PDF: startxref, cross-reference data, object header, stream Length/endstream, page tree. No reconstruction
61encrypted (trailer Encrypt). Not supported in this slice
62unsupported filter or predictor for that stream: LZW, RunLength, DCT, JBIG2, CCITT, JPX, Crypt, external (F) stream data, TIFF predictor. Only jobs that need that stream are refused
63a bound or structural cycle (page-tree cycle, Prev cycle, reference chains, nesting, sizes, work)
64I/O error (read, seek, or stdout write)
65text: unsupported font or CMap (CID font without ToUnicode, any CID Encoding other than Identity-H, vertical writing, form XObjects and inline images carrying text)
66text: malformed content stream, font or CMap
67split/merge: an object that cannot be copied (a reference to an ObjStm or XRef stream itself, external stream data, a duplicate page, a direct stream)
68split/merge: write error on the output

Refusal cases in the gate cover 1, 2, 5, 10..21, 30..32, 60, 61, 62, 63, 64 (a directory as input), 65, 66 and 67. Codes 3 and 68 have no case: a regular file cannot make pread or pwrite fail without fault injection.

Bounds (each refused with its code, never a trap)

Static buffers only: there is no heap.

Not supported in this slice

Encryption; LZW, RunLength and image-filter decoding (DCT, JBIG2, CCITT, JPX), though image streams are copied untouched by split and merge; CID fonts without ToUnicode; vertical writing; layout analysis; forms (AcroForm) and text inside form XObjects; annotations (pages are copied with their Annots, and references to pages that are not copied become null); incremental-update writing; linearization on output; damaged-file reconstruction.

Next slice

A real writer for new PDFs: content-stream generation, fonts (embedding a subset with Widths and ToUnicode), compression on output (needs a deflate compressor, which does not exist in Omega yet), plus the Annex D Symbol/ZapfDingbats sets and reading font programs' built-in encodings.

Caveat

If an input changes between the writer's preflight pass and its copy pass and its zlib data then fails, a partial OUT can remain. The exit status is non-zero (10..32).