Skip to content

Kernel: AcroForm appearance streams encode field text through WinAnsi, so a value outside WinAnsi throws at save #648

Open

Description

@Tim81

Code references are origin/main 39e5fe0 unless stated. "The #641 branch" is fix/real-tokens-and-form-text at f6fff0b (PR #646, open).

What happens now

A text field value, a choice field's selected option (or its first option when none is selected), and a push button caption are drawn into the widget's appearance stream by AcroFormBuilder, in the Standard-14 Helvetica that the form's /DR declares with /Encoding /WinAnsiEncoding.

  • BuildTextAppearanceContent (AcroFormBuilder.cs:478-507) and BuildPushButtonAppearanceContent (:447-472) pass the string to EscapePdfString (:500, :467). Callers are :124 (text field), :236-239 (choice field, selected or first option) and :385 (push button).
  • EscapePdfString (:656-686) maps each character with WinAnsiEncoding.TryGetByte (WinAnsiEncoding.cs:26-42) and throws ArgumentException for a character it cannot map (:677-681). ParamName is value. The message, read from the code, is "Character U+XXXX ('c') is outside WinAnsiEncoding and cannot be rendered by the Standard-14 Helvetica appearance font. Use an embedded font for this field value."
  • The exception comes from the save, not from AddTextField, AddChoiceField or AddPushButton. AcroFormBuilder.Build runs from Save (PdfDocument.cs:970) and from PrepareForSigning (:1435), and SaveAsync calls Save (:1149). Save sets _written = true at :787, before Build, so after the exception a second Save reports that the document has already been written (read from the code, not run for this issue).
  • The advice in the message cannot be followed today. None of the Add* signatures, and none of the four members of FormFieldOptions (FontSize, ReadOnly, Required, Multiline; FormFieldOptions.cs:13-60), accepts a font.
  • Characters that do not reach the appearance are not refused: a field name, a choice option that is not the selected one, and the check box and radio group (their appearances draw a fixed ZapfDingbats character) all save with any string. The text-string encoding of those is what Kernel: AcroFormBuilder.TextString writes U+0080 to U+00A0 and U+00AD as Latin-1 bytes, writes a leading U+00FE U+00FF as a byte order mark, and can write an unpaired surrogate #641 changes.
  • Measured earlier, for Tracking: defects found while doing the v2.4 work (not yet fixed) #613 item 15, on main 2027421 and on a 2.3.x build: a text field value and a push button caption holding U+D800 both threw ArgumentException ("Character U+D800 ... is outside WinAnsiEncoding") from the save. A later run on the fix(kernel): write whole reals outside int32 as reals, and form text as text strings #646 head, where EscapePdfString is unchanged, gave the same exception for U+0141.
  • The same character in page text is handled differently: PdfCanvas.ShowText writes '?' and records a TextEncodingWarning (PdfCanvas.cs:674-678; ShowTextWinAnsiTests.cs:34-45). The form path has no substitution and no warning.
  • Search scope for "no test covers it": git grep "outside WinAnsi" origin/main -- src tests finds the form message only at AcroFormBuilder.cs:678; the test hits are the canvas tests above. I did not search for a form test that asserts the exception by another phrase.

Relation to #641

#641 changes how /T, /V, /Opt and /MK /CA are encoded: on the #641 branch TextString calls PdfLiteralString.FromTextString (AcroFormBuilder.cs:638-639), so a value above U+00FF is written as UTF-16BE and reaches the file as passed. EscapePdfString is unchanged there (:648-678), so a drawn value with a character outside WinAnsi still throws before /V is written. The AddTextField remarks on that branch (PdfDocument.cs:512-517) and the matching ones on the other methods and on Save, SaveAsync and PrepareForSigning list this exception, so #641 documents the limit and does not remove it.

A value that does pass is therefore encoded twice by two rules: /V as a text string, the appearance as WinAnsi bytes. The two agree for U+0020 to U+007E and U+00A0 to U+00FF and for the WinAnsi punctuation above U+00FF (euro, bullet, curly quotes and so on). They differ for U+0080 to U+009F: TryGetByte returns the identical byte for them (WinAnsiEncoding.cs:28-32), so the appearance draws the glyph WinAnsi gives that byte, U+0080 as the euro sign, where /V holds U+0080. The #641 branch documents that difference (PdfDocument.cs:502-504). Not measured by me: what a viewer draws for the codes WinAnsi leaves undefined.

Related: #592 (a character missing from an embedded font is written as glyph 0 and the save reports nothing). Any option below that embeds a font inherits that behaviour unless it checks coverage first.

What the specification allows

  • ISO 32000-1, 12.7.3.3, "Variable Text" (the freely available Adobe-equivalent text, p. 435): a field's /DA must name a font in the form's /DR; the text says the font "shall match a resource name in the Font entry of the default resource dictionary". Nothing in the passage from the clause heading to the /DA paragraph limits the font to a Standard-14 face or to WinAnsi. The same clause says the reader builds its own appearance at viewing time when none is supplied. I did not read the clause past the /DA paragraph for this issue.
  • ISO 19005-2 (PDF/A-2), read on a licensed copy earlier in the v2.4 work and not re-read for this issue: 6.3.3 requires an appearance dictionary for a widget annotation, and 6.4.1 says a reader renders the appearance and never the field value, with NeedAppearances absent or false. So for PDF/A the appearance stream is the only text a reader draws, and the library writes /NeedAppearances false (AcroFormBuilder.cs:104). Removing the appearance, or setting NeedAppearances true to let a viewer regenerate it, is not available for PDF/A output. 6.2.11.4.1 requires every font used for rendering to be embedded, with no exemption for the 14 standard fonts; a PDF/A-2u document with a single text field, drawn with the unembedded Helvetica, failed veraPDF 1.30.4 with rule 6.2.11.4.1-1 and no other rule, whether the field name was ASCII or not (measured on the fix(kernel): write whole reals outside int32 as reals, and form text as text strings #646 head; filed as an item in Tracking: defects found while doing the v2.4 work (not yet fixed) #613). So the appearance font needs to be embeddable for PDF/A-2 forms regardless of which characters the value holds.

Options (not chosen here)

  1. Draw with an embedded font. Let a field name a font that covers its value, add it to /DR, use it in /DA and in the appearance, and write the glyphs through the embedded-font path. Needs a public member (a font on FormFieldOptions or a per-document form font), subsetting per field, and a decision on a value the font cannot cover. It touches PublicAPI.Unshipped.txt.
  2. Refuse earlier and clearly. Check the value in AddTextField, AddChoiceField and AddPushButton, so the document stays usable, with a message that names the field and does not tell the caller to use a font option that does not exist. Keeps the WinAnsi limit.
  3. Substitute and report, as PdfCanvas.ShowText does: draw '?' for the character and record a warning. Keeps /V exact and the appearance lossy. Needs a way to surface the warning from a PdfDocument; I did not check whether one exists.
  4. Keep the exception and document it, which is the state of the Kernel: AcroFormBuilder.TextString writes U+0080 to U+00A0 and U+00AD as Latin-1 bytes, writes a leading U+00FE U+00FF as a byte order mark, and can write an unpaired surrogate #641 branch.
  5. For non-PDF/A output only: set NeedAppearances true so a viewer regenerates the appearance. Excluded for PDF/A by 6.4.1 above, and it leaves a viewer that does not regenerate showing the stale appearance.

Acceptance criteria

Whichever option is chosen:

  • A text field whose value is "Łódź" (U+0141 U+00F3 U+0064 U+017A) has one pinned outcome on Save, SaveAsync and PrepareForSigning: either all three save a file whose appearance draws each character, or all three refuse with one exception type, one ParamName and one message, each asserted by a test.
  • The same pinned outcome holds for a choice field's selected option, a choice field with no selected option whose first option is the value, and a push button caption.
  • If the document stays usable after a refusal, a test saves it again after correcting the value. If it does not, the Add* and Save remarks say so, in the form FormFieldOptions.FontSize uses.
  • Bytes for values entirely inside WinAnsi are unchanged from main: a test pins the appearance stream and /V bytes of a field holding U+0020 to U+007E, U+00A0 to U+00FF and the WinAnsi punctuation set.
  • The U+0080 to U+009F behaviour is stated in the remarks and pinned by a test, whichever way it goes.
  • If a font is embedded: /DA's font name resolves in /DR /Font (the 12.7.3.3 requirement above), every character of the value has a non-zero glyph id in the appearance (so Kernel: a character missing from an embedded font is written as glyph 0 (.notdef) and the save reports nothing #592's glyph 0 cannot occur), the font's ToUnicode map lists each character, and qpdf --check is clean. A PDF/A-2u document holding such a field passes veraPDF 6.2.11.4.1-1 (it fails it today with the Helvetica appearance), and the test asserts the failed-rule list, not only the exit code.
  • A value with a character the embedded font lacks is refused with the character and the font named.
  • The XML documentation of AddTextField, AddChoiceField, AddPushButton, Save, SaveAsync and PrepareForSigning states the outcome, where it is raised from, and what not to pass, following the boundary pattern of TextStyle.FontSize.

Not measured

The U+0141 case was run on the #646 head only (text field, selected choice option, Save, SaveAsync and PrepareForSigning all threw ArgumentException with ParamName value; an unselected option holding U+0141 saved). The second Save after the exception was not run, and no viewer was used. The statements about them are readings of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions