Repository navigation
Shrink a form field's text to fit its field instead of cutting it off (#433) - #441
Merged
jakejackson1 merged 2 commits intoSep 23, 2026
Merged
Conversation
jakejackson1
commented
Sep 23, 2026
Member
Author
|
Do follow ups as part of this PR |
…#433) A text input's value, a combo box's choice and a push button's caption that are too wide for the field are drawn in a smaller font, both into the page and in an active field's appearance. The size scales with the overflow, rounded down to 0.001pt, and goes no lower than half the field's size or 6pt. Past that minimum the text is trimmed to whole characters, found by halving. One rule, fitLine(), serves every case. An active text field or combo box that shrinks is written with /DA 0 Tf so the viewer sizes it too. A push button's /DA names the size its appearance was drawn at. /V, /DV, /Opt and /CA keep the whole text. The static button now shapes its caption through shapeText(), and a static text input measures the value as Cell() draws it, padding shrunk with a shrunk table included. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
force-pushed
the
fix/433-shrink-field-text
branch
from
September 23, 2026 20:15
c0cd7cd to
af06334
Compare
…reports (#451) The baseline ignored "Variable $c in isset() always exists" in Svg.php. PHPStan 2.2.15 stopped reporting it, so the unmatched ignore failed static analysis on gravitypdf and every branch cut from it. $c is always set inside its foreach, so the check goes and the ignore with it, which passes under 2.2.14 and 2.2.15 alike. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
form-fields-static draws its styled field in dark red on pale yellow inside a blue border, and form-fields-active and form-fields-pdfa take that blue as the widget's border colour. The page-break-avoid fixtures draw their fields' grey CSS border. Rasterised against the base branch, nothing else changes; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…lect's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…) (#442) * Draw a static form field in its CSS colour, background and border (#432) A field drawn into the page, with active forms off, always had a near-white fill, black text and a 0.2mm border in the colour already set. The form tags now record the field's background and border, read from the BORDER-TOP shorthand the cascade folds the border into, whether or not forms are active, and Form draws text inputs, text areas, selects, buttons, checkboxes and radio buttons with them, keeping the defaults for what the CSS leaves unset. The disabled and read-only greys still win, and a border of style none or no width is not drawn. An active field's /MK /BC now also follows a border colour given in the border shorthand, which only the border-color longhand reached before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share BorderMerger's border defaults with the form tags, and set a static field's colours in one place Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the static field style helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the form snapshots for a field's CSS colours and border form-fields-static draws its styled field in dark red on pale yellow inside a blue border, and form-fields-active and form-fields-pdfa take that blue as the widget's border colour. The page-break-avoid fixtures draw their fields' grey CSS border. Rasterised against the base branch, nothing else changes; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#445) * Give an active form field the width and style of its CSS border (#431) An active text field, text area, select or button always had a 1pt solid border in /BS, whatever its CSS said. The border width, in points, and style the form tags record now reach /BS and the appearance: solid, dashed, dotted, inset and outset become /S, /D (dashed in the appearance too), /I and /B, and a border of style none has no width and no /BC, which a viewer would draw regardless. A select also takes its CSS border and background colours, which it ignored before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share the active field colour fallback and the no-border rules between the field kinds Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the active border helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the active form snapshots for the CSS border width and the select's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#445) (#469) * Give an active form field the width and style of its CSS border (#431) An active text field, text area, select or button always had a 1pt solid border in /BS, whatever its CSS said. The border width, in points, and style the form tags record now reach /BS and the appearance: solid, dashed, dotted, inset and outset become /S, /D (dashed in the appearance too), /I and /B, and a border of style none has no width and no /BC, which a viewer would draw regardless. A select also takes its CSS border and background colours, which it ignored before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share the active field colour fallback and the no-border rules between the field kinds Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the active border helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the active form snapshots for the CSS border width and the select's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Cherry-picked from c675d3f. PR #445 was merged into fix/432-static-field-css after #442 had already merged that branch into feature/pdfa-2-conformance, so it never reached this branch.
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…#433) (#441) * Shrink a form field's text to fit its field instead of cutting it off (#433) A text input's value, a combo box's choice and a push button's caption that are too wide for the field are drawn in a smaller font, both into the page and in an active field's appearance. The size scales with the overflow, rounded down to 0.001pt, and goes no lower than half the field's size or 6pt. Past that minimum the text is trimmed to whole characters, found by halving. One rule, fitLine(), serves every case. An active text field or combo box that shrinks is written with /DA 0 Tf so the viewer sizes it too. A push button's /DA names the size its appearance was drawn at. /V, /DV, /Opt and /CA keep the whole text. The static button now shapes its caption through shapeText(), and a static text input measures the value as Cell() draws it, padding shrunk with a shrunk table included. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Drop the isset() on a foreach variable that PHPStan 2.2.15 no longer reports (#451) The baseline ignored "Variable $c in isset() always exists" in Svg.php. PHPStan 2.2.15 stopped reporting it, so the unmatched ignore failed static analysis on gravitypdf and every branch cut from it. $c is always set inside its foreach, so the check goes and the ignore with it, which passes under 2.2.14 and 2.2.15 alike. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…) (#442) * Draw a static form field in its CSS colour, background and border (#432) A field drawn into the page, with active forms off, always had a near-white fill, black text and a 0.2mm border in the colour already set. The form tags now record the field's background and border, read from the BORDER-TOP shorthand the cascade folds the border into, whether or not forms are active, and Form draws text inputs, text areas, selects, buttons, checkboxes and radio buttons with them, keeping the defaults for what the CSS leaves unset. The disabled and read-only greys still win, and a border of style none or no width is not drawn. An active field's /MK /BC now also follows a border colour given in the border shorthand, which only the border-color longhand reached before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share BorderMerger's border defaults with the form tags, and set a static field's colours in one place Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the static field style helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the form snapshots for a field's CSS colours and border form-fields-static draws its styled field in dark red on pale yellow inside a blue border, and form-fields-active and form-fields-pdfa take that blue as the widget's border colour. The page-break-avoid fixtures draw their fields' grey CSS border. Rasterised against the base branch, nothing else changes; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#445) (#469) * Give an active form field the width and style of its CSS border (#431) An active text field, text area, select or button always had a 1pt solid border in /BS, whatever its CSS said. The border width, in points, and style the form tags record now reach /BS and the appearance: solid, dashed, dotted, inset and outset become /S, /D (dashed in the appearance too), /I and /B, and a border of style none has no width and no /BC, which a viewer would draw regardless. A select also takes its CSS border and background colours, which it ignored before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share the active field colour fallback and the no-border rules between the field kinds Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the active border helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the active form snapshots for the CSS border width and the select's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Cherry-picked from c675d3f. PR #445 was merged into fix/432-static-field-css after #442 had already merged that branch into feature/pdfa-2-conformance, so it never reached this branch.
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#441 moved SetJSButton()'s SetFormButton() call below the icon, so that the field knows it shows one, and #447 keyed the icon by $this->formCount, which is the button's number only once SetFormButton() has counted it. Merged together, each icon went to the form before it: the widget lost its icon, the object count reserved one that was never written, and SetFormButton() still looked the icon up by name. The icon is now kept under the number the button is about to get, and SetFormButton() looks for it there. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…#433) (#441) * Shrink a form field's text to fit its field instead of cutting it off (#433) A text input's value, a combo box's choice and a push button's caption that are too wide for the field are drawn in a smaller font, both into the page and in an active field's appearance. The size scales with the overflow, rounded down to 0.001pt, and goes no lower than half the field's size or 6pt. Past that minimum the text is trimmed to whole characters, found by halving. One rule, fitLine(), serves every case. An active text field or combo box that shrinks is written with /DA 0 Tf so the viewer sizes it too. A push button's /DA names the size its appearance was drawn at. /V, /DV, /Opt and /CA keep the whole text. The static button now shapes its caption through shapeText(), and a static text input measures the value as Cell() draws it, padding shrunk with a shrunk table included. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Drop the isset() on a foreach variable that PHPStan 2.2.15 no longer reports (#451) The baseline ignored "Variable $c in isset() always exists" in Svg.php. PHPStan 2.2.15 stopped reporting it, so the unmatched ignore failed static analysis on gravitypdf and every branch cut from it. $c is always set inside its foreach, so the check goes and the ignore with it, which passes under 2.2.14 and 2.2.15 alike. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
…) (#442) * Draw a static form field in its CSS colour, background and border (#432) A field drawn into the page, with active forms off, always had a near-white fill, black text and a 0.2mm border in the colour already set. The form tags now record the field's background and border, read from the BORDER-TOP shorthand the cascade folds the border into, whether or not forms are active, and Form draws text inputs, text areas, selects, buttons, checkboxes and radio buttons with them, keeping the defaults for what the CSS leaves unset. The disabled and read-only greys still win, and a border of style none or no width is not drawn. An active field's /MK /BC now also follows a border colour given in the border shorthand, which only the border-color longhand reached before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share BorderMerger's border defaults with the form tags, and set a static field's colours in one place Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the static field style helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the form snapshots for a field's CSS colours and border form-fields-static draws its styled field in dark red on pale yellow inside a blue border, and form-fields-active and form-fields-pdfa take that blue as the widget's border colour. The page-break-avoid fixtures draw their fields' grey CSS border. Rasterised against the base branch, nothing else changes; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#445) (#469) * Give an active form field the width and style of its CSS border (#431) An active text field, text area, select or button always had a 1pt solid border in /BS, whatever its CSS said. The border width, in points, and style the form tags record now reach /BS and the appearance: solid, dashed, dotted, inset and outset become /S, /D (dashed in the appearance too), /I and /B, and a border of style none has no width and no /BC, which a viewer would draw regardless. A select also takes its CSS border and background colours, which it ignored before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share the active field colour fallback and the no-border rules between the field kinds Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the active border helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the active form snapshots for the CSS border width and the select's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Cherry-picked from c675d3f. PR #445 was merged into fix/432-static-field-css after #442 had already merged that branch into feature/pdfa-2-conformance, so it never reached this branch.
jakejackson1
added a commit
that referenced
this pull request
Sep 23, 2026
#441 moved SetJSButton()'s SetFormButton() call below the icon, so that the field knows it shows one, and #447 keyed the icon by $this->formCount, which is the button's number only once SetFormButton() has counted it. Merged together, each icon went to the form before it: the widget lost its icon, the object count reserved one that was never written, and SetFormButton() still looked the icon up by name. The icon is now kept under the number the button is about to get, and SetFormButton() looks for it there. Co-Authored-By: Claude Opus 5.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.
Fixes #433
Some form field text used to be cut off when it was wider than its field. That applied to a text input's value, a combo box's choice and a push button's caption. That text is now drawn in a smaller font, both when it is drawn into the page and in an active field's appearance. Before, the page version cut text inputs to whole characters, and the active version clipped at the field's edge. This is always on. There is no config option.
The rule
One private method,
Form::fitLine(), applies it to every field type and to both the static drawing and the active appearance. The caller passes a closure that measures the text, so the same rule works with shaped page text in mm and with appearance text in points.Tfis written at.Static fields (
useActiveFormsoff)fittedCell(). It shapes the text, fits it to the room inside the padding, sets the fitted size aroundCell()and then restores it. The field's box stays the same size, so the layout does not move.applyOTL()andmagic_reverse_dir()before it is measured, asCell()draws it. The old loop measured and trimmed the raw string, which added unshaped Arabic codepoints to the font subset and could leave a trimmed Arabic value ending in the wrong letter form. Each trimmed candidate is shaped again.print_ob_button()no longer shapes its caption inline. It goes throughshapeText().OtlData::slice()), not shaped a second time. The drop-down arrow keeps the field's size.form_element_spacingis in millimetres. The$kthatprint_ob_text()divides by is the table shrink factor (shrin_k), notMpdf::SCALE, and it is 1 outside a shrunk table. The old loop subtracted the unshrunk padding whileCell()drew the shrunk padding, so in a table shrunk to fit the page, a value that fitted still lost a character. The room is now$wminus twice the paddingCell()is given.Active fields
/DAis written with0 Tfso the viewer auto-sizes it too. Without that, the text jumps back to full size when the field is focused.multipleand without a size of 2 or more). Same as text fields, including/DA … 0 Tfwhen shrunk. Auto size applies to any variable-text field, choice fields included (ISO 32000 12.7.3.3). Acrobat offers "Auto" font size for drop-downs, and pdf.js computes a size for choice widgets whose/DAsize is 0. So once a user picks another option, the viewer fits that option as well. A combo box that fits keeps its fixed size. List boxes are unchanged./DAnames the size the appearance was drawn at, fitted or not. A button has no editable text: a viewer only redraws it from/MK /CAand/DA, for example for the rollover (/RC) or down (/AC) caption. A fixed size matching our appearance keeps that redraw looking the same.0 Tfwould let each viewer choose its own size for a caption the user can never change.fitLine()binary search, measuring withemWidth()./Vand/DV(text),/Opt(combo) and/CA(button) keep the whole text./DAkeeps the field's size. To make this possible,SetJSButton()now registers the icon before it callsSetFormButton().In practice, selects and buttons that come from HTML are sized to their own text by
Tag\SelectandTag\Input, so they rarely overflow. One case where they can is an active button with an emptyvalue, which shows itsname. The rule still applies to them, so a field given a narrower width, or a caption shaped wider than it was measured, is fitted rather than clipped.fitLine()allows 1e-6 of float error: a select or button whose width is its text plus padding would otherwise shrink by 0.001pt when the padding is taken back off.Snapshots
Rasterised with
pdftoppmat 120 DPI and compared against the fixtures onfeature/pdfa-2-conformance. In each, the only pixels that change are in one 37–42 × 14px box around themaxlengthfield. Nothing else on the page moves.form-fields-static: themaxlengthfield shows "12345" at 7.92pt instead of "1234" at 9.9pt. The DejaVu subset no longer carries the six unshaped Arabic codepoints the old trim loop measured, so the embedded font is 776 bytes smaller.form-fields-activeandform-fields-pdfa: themaxlengthfield's/DAis0 Tf, and its appearance draws "12345" at 8.2pt, whole and centred, instead of clipping the last digit at 9.9pt.The fixture combo boxes, buttons and image button all fit already, so their bytes are unchanged.
Verification
FormFieldShrinkTest(22 tests). Courier at 10pt, so the widths are exact.$k./DAsize and appearanceTffor fits (10 / 10.000), overflow (0 / 7.220) and past the minimum (0 / 6.000, appearance "ABCD")./Vand/DVare whole in each./Optis whole./CAis whole./DA … 10 Tfin a 2mm-wide field.TextRecordingMpdfnow also records the font size of eachCell().composer test: 3794 tests pass, with the same 5 skips.--group=snapshot: 89 pass, with no refresh notes.--group=conformance(veraPDF): 29 pass.composer cs: clean.Variable $c in isset()baseline entry forsrc/Image/Svg.php. That entry is unrelated and is being fixed in Drop the isset() on a foreach variable that PHPStan 2.2.15 no longer reports #451.php -lunder PHP 5.6: clean.Limits
emWidth(). A value in a script with its own shaper (Arabic, Indic) can come out a little wider or narrower than its appearance allowed. Such fields are already flagged for the viewer to regenerate (Give every active form widget its own appearance, and ask the viewer to redraw only text it cannot shape (#348, #59) #387).🤖 Generated with Claude Code