Add text: watermarks, page numbers, Bates stamps - #3
Merged
Conversation
The other half of what people do to a PDF they have: write on it. One
primitive — a line of text, a face, a size, a colour, a transparency, an angle
and one of nine places — with the three things people actually ask for built on
it. The text may name what it is drawn on: {page}, {pages}, and {n} for a
counter that advances once per page, which is what a serial numbering scheme
needs.
Nothing is embedded. The four faces every viewer is required to have are enough
for a watermark or a page number, so a stamp costs about a kilobyte and works
everywhere. Their widths are Adobe's own, exact through printable ASCII, with a
Latin-1 letter taking the width of the letter it is built on — which is true of
these faces. Text is encoded as WinAnsi, and a character that encoding has no
room for is dropped rather than drawn as something else.
A stamped page keeps everything it had. Its own content streams are referenced
unchanged, wrapped in a save and a restore so whatever state they leave behind
cannot reach the text, and the resources are rebuilt as a dictionary of this
file's own — because the page may be sharing them with pages that are not being
stamped. The text goes in the page's own box, so on a page carrying a rotation
it turns with the page, as the page's own layout does. A page used as a tile is
wrapped first, so its text lands in the space the page is displayed in.
Verified across the corpus: for each of the 118 833 files, numbering every page
has to leave the page's own content stream in the file byte for byte, draw the
number, name a face the file carries, and put the text inside the page. All
118 833 hold — after the campaign found two real defects. A /Contents written
as a reference to an array, which 2 194 files do, lost the page's content
entirely; and 950 pages are smaller than the default margin, which put their
text off the page. Both are fixed, and the earlier 118 833-file and 1 959-file
checks still pass.
What this cannot yet check is what the text looks like. That needs a
rasteriser, and when there is one these stamps should be checked in pixels.
Exact 100% statement coverage over the package and the command, go vet clean,
nine cross-compilation targets.
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The other half of what people do to a PDF they have: write on it. One
primitive — a line of text, a face, a size, a colour, a transparency, an angle
and one of nine places — with the three things people actually ask for built on
it. The text may name what it is drawn on: {page}, {pages}, and {n} for a
counter that advances once per page, which is what a serial numbering scheme
needs.
Nothing is embedded. The four faces every viewer is required to have are enough
for a watermark or a page number, so a stamp costs about a kilobyte and works
everywhere. Their widths are Adobe's own, exact through printable ASCII, with a
Latin-1 letter taking the width of the letter it is built on — which is true of
these faces. Text is encoded as WinAnsi, and a character that encoding has no
room for is dropped rather than drawn as something else.
A stamped page keeps everything it had. Its own content streams are referenced
unchanged, wrapped in a save and a restore so whatever state they leave behind
cannot reach the text, and the resources are rebuilt as a dictionary of this
file's own — because the page may be sharing them with pages that are not being
stamped. The text goes in the page's own box, so on a page carrying a rotation
it turns with the page, as the page's own layout does. A page used as a tile is
wrapped first, so its text lands in the space the page is displayed in.
Verified across the corpus: for each of the 118 833 files, numbering every page
has to leave the page's own content stream in the file byte for byte, draw the
number, name a face the file carries, and put the text inside the page. All
118 833 hold — after the campaign found two real defects. A /Contents written
as a reference to an array, which 2 194 files do, lost the page's content
entirely; and 950 pages are smaller than the default margin, which put their
text off the page. Both are fixed, and the earlier 118 833-file and 1 959-file
checks still pass.
What this cannot yet check is what the text looks like. That needs a
rasteriser, and when there is one these stamps should be checked in pixels.
Exact 100% statement coverage over the package and the command, go vet clean,
nine cross-compilation targets.