Skip to content

Give every redrawn form field a new appearance stream - #135

Merged
bobsingor merged 4 commits into
embedpdf/mainfrom
embedpdf/fix/form-appearance-new-stream
Nov 29, 2026
Merged

bobsingor merged 4 commits into
embedpdf/mainfrom
embedpdf/fix/form-appearance-new-stream

Conversation

@bobsingor

@bobsingor bobsingor commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Cause

The engine undoes a form write by putting the field's and its widgets' dictionaries back with EPDFDoc_ImportDictRaw(), from a capture taken before the write (EPDFDoc_ExportDictRawToOwnedBuffer() with the deep keys AP MK). An import puts an object back only where the layer holds no version of its number (InstallObjects, guarded by EPDFCaptureEmbedderTest.ImportNeverReplacesAVersionTheLayerHolds). So a widget gets the appearance it had back only when the write left the stream it named as it read.

GenerateFormAPToTarget drew a text field, a combo box and a list box into the stream the widget already named. A push button, a check box, a radio button and a box got a new stream on every redraw.

So after a value written over an appearance, a reset, an appearance text (EPDFForm_SetFieldAppearanceText()) or a restyle, the import put /V and the widget's keys back, and the widget still named the same stream, which drew what the undone write had drawn. The page showed the undone value, and a saved file held /V (hello) under a stream that draws (hello world). One fill and its undo is enough. It has been so since captures exist.

Two more things followed from writing in place:

  • A stream the file holds was copied into the layer and written there. A capture never holds the file's originals, so nothing could bring it back.
  • A stream, or an indirect /AP, that two widgets name was written into for both.

Change

  • GenerateFormAPToTarget makes a new stream for every kind and names it in an /AP of the widget's own (OwnAppearanceDict). The stream the widget had is not opened for writing, so nothing of it is copied into a layer, and a save writes only what the document still reaches.
  • What the old appearance said beyond its drawing is said of the new one. One helper, NameNormalAppearance, names the new appearance for every form drawer (text, combo box, list box, both push button drawers, the box, check box, radio button):
    • A new stream shows as the stream it replaces showed. It takes that stream's layer (/OC), its transparency group (/Group) and the default colour spaces in its resources (/DefaultGray, /DefaultRGB, /DefaultCMYK). For a check box or a radio button each state's new stream takes them from that state's old one.
    • A reference under /R or /D that named a replaced stream names the new one. Each of /N, /R and /D is one stream or a dictionary of states, whatever the others are, so every reference under the entry is followed: the entry itself or a state of it. Where two states were one stream, a state follows its own new stream. A rollover or pressed appearance made apart stays as it is. A dictionary of states that is an indirect object is copied onto the widget first, as /AP is.
  • Nothing that is about the old drawing is carried over. The rest of the old stream's dictionary and resources (its pictures, patterns, other colour spaces, metadata) is left, so what it named is in no file saved in full afterwards. The new stream holds what a first drawing's does (for a text field /Type, /Subtype, /Matrix, /BBox and /Resources with the fonts) and the three things above. What is drawn does not change.
  • The ledger's own-appearance-generator rule and the comment of EPDFAnnot_GenerateFormFieldAP() state the rule for every kind.

The second point is the second and the last commit, after two reviews: a redraw into the old stream had kept all of it for a text field, a combo box and a list box, and the first commit lost it. The kinds that got new streams before (push button, box, check box, radio button) had lost it already, so for those it is a change from the released runtime. /Group is in neither review. It is of the same kind as the other two, since it says how the stream is composited, so it is carried by the same rule.

Tests

  • EPDFCaptureEmbedderTest.RedrawnFieldComesBackWithItsAppearance: a field filled in twice, with a capture before the second fill. The second fill names a new stream and leaves the first as it read; the import brings the widget back naming the first, with its bytes, also after a save and reopen dropped it.
  • EPDFCaptureEmbedderTest.RedrawnFieldLeavesTheFilesAppearanceAlone: a widget that names a stream of the file, which a square names too. After a fill the layer holds no version of it, the square still names it, and the import brings the widget back naming it.
  • EPDFCaptureEmbedderTest.RedrawnFieldComesBackWithItsSameAppearances: a rollover appearance that is the normal one follows the redraw to the new stream, and the import brings both back.
  • EPDFFormEmbedderTest.RedrawIsANewStreamEveryTime: a text field, a combo box and a list box each get another stream on a second redraw, and the first still draws what it drew.
  • EPDFFormEmbedderTest.RedrawLeavesASharedAppearanceAlone: two widgets naming one indirect /AP and one stream each get an /AP and a stream of their own, and the shared two stay as they were.
  • EPDFWidgetDrawingEmbedderTest.ARedrawKeepsHowItsAppearanceShows: a widget of every kind (text, combo box, list box, push button with a caption and without, signature box, check box, radio button) whose appearance is in a layer that is off, has a transparency group and default colour spaces, and whose /R and /D are its /N. After a redraw each new stream has the layer, the group and the default colour spaces, and neither the colour space nor the picture that the old drawing alone named. /R and /D name the new stream, per state for the two with states. Three more shapes: a text field with one stream under /N and dictionaries of states under /R and /D, a check box whose /D is the stream of one of its /N states, and a check box whose two states were one stream. A rollover appearance made apart stays, and so does a dictionary of states that is an object of its own.
  • Three of them are listed as layer-mode writers. 22 tests that redraw a text or choice field leave one to six more objects in a layer, one for each redraw; their counts in the list are updated.

Checked

On the branch at a7de2feed9, which has main (0585b319eb) merged in:

  • Without the first commit's change its four tests fail. Without the second's its two fail, the every-kind one for every kind. Without the last commit's the every-kind test fails for every kind and for the first two of the three new shapes.
  • macOS: unit 1412 passed. Embedder 1897 passed and 12 failed, the 12 golden failures an unchanged build has, by name.
  • Linux x64, in the release image: unit 1419 passed, embedder 1911 passed, none failed.
  • The whole suite in layer mode, on macOS and on Linux: no error. On Linux 598 writers, each with the count the list gives.
  • Pixel runner on macOS: the same failures and surprises as on the base build.
  • The saved-file checker over the macOS embedder run: 49 saved PDFs pass.
  • check-fork-tags.py --strict, check-c-api.py (604 functions), check-globals.py on a macOS library built from this branch.
  • One test failed off and on in the later runs, while the machine was busy with other work: EPDFSlicedLoadEmbedderTest.SlicesKeepToTheirBudget, which measures the wall-clock time of a page load's steps and touches no form. It passed in both systems' suite runs above. It failed in the first Linux layer-mode run, which was run again (no error, the test failing in that run's plain half instead), and in the macOS run that fed the saved-file checker. Alone it passed 7 times of 8 on Linux and 1 of 3 on the loaded Mac.
  • Both reviews' cases, saved through the engine and rendered in Ghostscript 10.08:
    • A text field whose appearance stream holds /OC for a layer that is off: the released runtime keeps the entry; the first commit lost it and Ghostscript drew the new value; now the new stream holds it and Ghostscript draws nothing.
    • A text field whose /N, /R and /D are one stream: after a fill all three name the new stream and draw the new value, where the first commit left /R and /D on the old one.
    • A check box restyled: its new streams keep /OC and /D follows them. The released runtime loses both.
    • A text field with one stream under /N and /R << /Shown >>, /D << /Shown >> naming it: after a fill all three name the new stream and draw the new value, where the second commit left the two states on the old one.
    • A text field whose appearance draws device red through a calibrated /DefaultRGB that makes it blue: the released runtime keeps the entry; the first two commits lost it and Ghostscript drew the field red; now the new stream holds it and Ghostscript draws blue.
    • A text field whose old stream holds every key a form XObject may hold and every kind of resource: the new stream's dictionary is /BBox /Group /Matrix /Resources /Subtype /Type, its resources /ColorSpace /Font, its colour spaces the three defaults. The old stream's picture, pattern and metadata are not in the file saved in full, where the released runtime, which wrote into the old stream, kept all of them.
  • WebAssembly and a macOS library built from this branch, in the engine at the current pin:
    • Five undo round trips over a widget that holds a drawing (a second value, an appearance text, a reset, a restyle, a combo box's second choice) fail on the released runtime and pass on this one, in the local engine and through the server.
    • The local engine's suite passes (2279 tests), and the cloud engine's, which runs through the server (1395).
    • A text field's drawing after a fill and after a restyle, and a combo box's, a check box's and a radio button's after a restyle, are the same pixels as with the released runtime.
    • In a browser, on the first commit's build: fill, fill, undo, undo, redo each draw the right value, and the saved file's /V and stream agree.

Left

  • Release, and the pin in embed-pdf-viewer. Its tests for this are on the branch fix/undo-restores-field-appearance there and fail until the pin.
  • Draw form widgets' borders, button layouts and automatic sizes as Acrobat does #133 is open on the same ground: merged with this branch it conflicts in the ledger's own-appearance-generator paragraph, and its new tests' counts in the writers list need taking again.
  • Not run: the Linux library build with check-globals.py, and Acrobat, on none of these cases and not on a file filled in after a signature. The engine's signature tests pass on this runtime.
  • PDFium's own render goes by neither /OC on an appearance stream nor a default colour space for device colours, with or without this change, so the engine's pictures of those two cases are the same throughout. What this change keeps is what the saved file says, for a reader that goes by it.
  • Not part of this change: a placed drawing (SetDetachedNormalAppearance in epdf_annot.cpp, a stamp's or a mark's picture set as the appearance) names its new stream without carrying any of this or following /R and /D.

An undo of a form write puts the field's and its widgets' dictionaries
back from a capture taken before the write. An import never replaces an
object the layer holds a version of, since that version may be newer. So
a widget gets the appearance it had back only when the write left that
stream as it read.

A text field, a combo box and a list box were drawn into the stream the
widget already named. After a second value, a reset, a shown text or a
restyle, the undo put /V and the widget's keys back, and the widget still
named the same stream, which drew what the write had drawn: the page and
a saved file showed the undone value. The stream was also written into
when the file held it, or when another widget named it too.

GenerateFormAPToTarget now makes a new stream for every kind, as it did
for a push button and as the check box, radio button and box drawers do,
under an /AP of the widget's own (OwnAppearanceDict). The old stream is
not opened for writing, so nothing of it is copied into a layer, and a
save writes only what the document still reaches.

Tests: EPDFCaptureEmbedderTest.RedrawnFieldComesBackWithItsAppearance and
RedrawnFieldLeavesTheFilesAppearanceAlone,
EPDFFormEmbedderTest.RedrawIsANewStreamEveryTime and
RedrawLeavesASharedAppearanceAlone. All four fail without the change. The
two form tests are listed as layer-mode writers, and 21 tests that redraw
a text or choice field leave one to six more objects in a layer, one for
each redraw.
A review of pull request 135 found two things a new stream lost, which a
redraw into the old stream had kept for a text field, a combo box and a
list box. The kinds that got new streams before had lost them already.

A stream may say which layer it shows in (/OC). The new stream said
nothing, so a field whose appearance is in a layer that is off showed its
value after a fill, in a reader that goes by the entry.

A widget's rollover and pressed appearances (/R, /D) may be its normal
one. They went on naming the old stream, with the old value.

NameNormalAppearance now names the new appearance for every drawer. A new
stream takes the /OC of the stream it replaces, the one of its state for
a check box or a radio button. An /R or a /D that named the replaced
stream, for the widget or for a state, names the new one, and one made
apart stays as it is. A dictionary of states that is an indirect object
is copied onto the widget first, as /AP is: another widget may name it.

A capture taken before the redraw still brings everything back, since it
puts the widget's /AP back whole.

Tests:
EPDFWidgetDrawingEmbedderTest.ARedrawKeepsTheLayerAndTheSameAppearances,
over a text field, a combo box, a list box, a push button with a caption
and one without, a signature box, a check box and a radio button, and
EPDFCaptureEmbedderTest.RedrawnFieldComesBackWithItsSameAppearances. Both
fail without the change, the first for every kind. The first is listed as
a layer-mode writer.
…/form-appearance-new-stream

Main brought the inline form font fix (#134), the const read of a
drawing rect (#136) and redactions through the active layer (#137).

Two conflicts, each where both sides added to the same place. The
ledger's own-appearance-generator paragraph keeps both additions. The
list of layer-mode writers keeps both sides' tests, and
EPDFFormEmbedderTest.AFieldWithoutWidgetsGetsNoDrawing, new on main,
leaves 8 objects in a layer where main lists 7: one more for the new
stream of its redraw.
A second review of pull request 135 found two more things a redraw into
the old stream had kept and a new stream lost.

/N, /R and /D are each one stream or a dictionary of states, whatever
the others are. FollowReplacedStream handled /R and /D only in the shape
/N had, so a text field's /R << /Shown ... >> naming its one normal
stream went on naming the old one. FollowReplacedStreams now goes
through every reference under the entry, the entry itself or a state of
it, and makes each that named a replaced stream name the one standing
for it. Where two states were one stream, a state follows its own.

A stream's resources may hold default colour spaces, which its device
colours are read in. The new stream held only its fonts, so a field
drawn through a calibrated /DefaultRGB changed colour in a reader that
goes by it. CarryHowADrawingShows gives the new stream what the old one
says of any drawing in it: /OC as before, /DefaultGray, /DefaultRGB and
/DefaultCMYK, and /Group, which the review did not name but is of the
same kind: it says how the stream is composited. The rest of the old
dictionary and resources is about the old drawing and is left, so what
it named is no longer kept in the file.

Test: EPDFWidgetDrawingEmbedderTest.ARedrawKeepsHowItsAppearanceShows,
the test over every kind under a new name. Every stream now has a group,
default colour spaces, and a colour space and a picture of its own that
must not come along. It adds a text field with states under /R and /D, a
check box with one stream under /D, and one whose two states were one
stream. Without the change it fails for every kind and for the first two
of those.
@bobsingor
bobsingor merged commit f5961d7 into embedpdf/main Nov 29, 2026
1 check passed
@bobsingor
bobsingor deleted the embedpdf/fix/form-appearance-new-stream branch November 29, 2026 23:04
bobsingor added a commit to embedpdf/embed-pdf-viewer that referenced this pull request Nov 30, 2026
The release has the runtime changes merged since 7f399f838:

- A redrawn form field gets a new appearance stream, where a text field,
  a dropdown and a list box were drawn into the one they had
  (embedpdf/runtime#135). An undo puts back the stream the widget named,
  so it brings the drawing back. The undo round trips this branch adds
  failed on the old pin and pass on both engines on this one.
- Asking where an annotation without a stored appearance draws reads its
  dictionary and no longer copies it into the document's changes
  (embedpdf/runtime#136).
- A font the form holds inline is made an object of its own before an
  appearance names it (embedpdf/runtime#134), and a redaction is applied
  at the place its mark has in a layer (embedpdf/runtime#137).
- The upstream sync to 31e80351b (Nov 25 2026, embedpdf/runtime#138).

The bindings generated from the release's headers don't change.
runtime-src moves to the pin.
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