Skip to content

Add the serialiser: objects, a Writer, and graph copying - #5

Merged
tannevaled merged 1 commit into
mainfrom
writer
Aug 25, 2026
Merged

Add the serialiser: objects, a Writer, and graph copying#5
tannevaled merged 1 commit into
mainfrom
writer

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

The exact inverse of the parser, and the thing every operation on an existing
file needs. It lives beside the object model rather than in a module of its
own, because splitting a model from its serialiser is how the two drift apart.

AppendObject renders any object in PDF syntax with dictionary keys in order, so
the same object always produces the same bytes and two files can be compared.
Writer hands out object numbers, writes objects under them, and lays down a
cross-reference table and a trailer. Copy walks an object graph out of a
document into the file being built, renumbering as it goes and remembering what
it has already copied, so shared objects stay shared, a /Parent pointing back
at its own tree node terminates, and graphs from several documents can be
merged without their numbers colliding.

Verified by rewriting the corpus rather than by assertion: each of the 118 833
files that open is copied into a new file, re-read, and compared on page count,
media boxes and the bytes of every content stream. All 118 833 match — 35.5 GB
in, 35.2 GB out, twenty-eight seconds.

That comparison found the one real defect: a producer that writes -0 for a
coordinate. Negative zero is legal, meaningless, and rendered by strconv as
"-0", which reads back as the integer zero — so a rewrite was not a fixpoint.
Zero is now written as zero whatever its sign. It was forty-three files out of
118 833, and no amount of unit testing would have suggested looking.

Exact 100% statement coverage, go vet clean, nine cross-compilation targets.

The exact inverse of the parser, and the thing every operation on an existing
file needs. It lives beside the object model rather than in a module of its
own, because splitting a model from its serialiser is how the two drift apart.

AppendObject renders any object in PDF syntax with dictionary keys in order, so
the same object always produces the same bytes and two files can be compared.
Writer hands out object numbers, writes objects under them, and lays down a
cross-reference table and a trailer. Copy walks an object graph out of a
document into the file being built, renumbering as it goes and remembering what
it has already copied, so shared objects stay shared, a /Parent pointing back
at its own tree node terminates, and graphs from several documents can be
merged without their numbers colliding.

Verified by rewriting the corpus rather than by assertion: each of the 118 833
files that open is copied into a new file, re-read, and compared on page count,
media boxes and the bytes of every content stream. All 118 833 match — 35.5 GB
in, 35.2 GB out, twenty-eight seconds.

That comparison found the one real defect: a producer that writes -0 for a
coordinate. Negative zero is legal, meaningless, and rendered by strconv as
"-0", which reads back as the integer zero — so a rewrite was not a fixpoint.
Zero is now written as zero whatever its sign. It was forty-three files out of
118 833, and no amount of unit testing would have suggested looking.

Exact 100% statement coverage, go vet clean, nine cross-compilation targets.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@tannevaled
tannevaled merged commit a83037e into main Aug 25, 2026
1 check passed
@tannevaled
tannevaled deleted the writer branch August 25, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant