Skip to content

Commit c10ec3e

Browse files
jakejackson1claude
andcommitted
Write push buttons that share a name as one field with each widget as a kid (#447)
* Write push buttons that share a name as one field with each widget as a kid Same-named submit, reset, script and image buttons were written as separate fields with the same /T, and their script and icon were looked up by name, so the second button's script replaced the first's and was then used up. Each group is now a parent field (/FT /Btn, push-button flag, /T, /Kids) listed in /AcroForm /Fields but in no page's /Annots, and its widgets keep their own action, caption, icon and appearance under a /Parent. Scripts and icons are keyed by the button's form number. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Read the reference lists in the shared button name tests with one helper, and write the push-button flag as the widgets do Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what a push button is where the shared button name code asks Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 79a5af6 commit c10ec3e

6 files changed

Lines changed: 327 additions & 38 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ parameters:
5151
-
5252
message: '#^Variable \$js in isset\(\) always exists and is not nullable\.$#'
5353
identifier: isset.variable
54-
count: 2
54+
count: 1
5555
path: src/Form.php
5656

5757
-

‎src/Form.php‎

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class Form
9292
var $formAction;
9393
var $form_fonts;
9494
var $form_radio_groups;
95+
96+
/**
97+
* @var array[] by name, the push buttons that share it, which are written as one field with the widgets as kids
98+
*/
99+
private $buttonGroups = [];
100+
95101
var $form_checkboxes;
96102
var $pdf_acro_array;
97103
var $pdf_array_co;
@@ -833,12 +839,12 @@ private function getCountItems($form)
833839
}
834840

835841
if ($form['typ'] === 'Bt') {
836-
if (isset($this->array_form_button_js[$form['T']])) {
842+
if (isset($this->array_form_button_js[$form['n']])) {
837843
$total++;
838844
}
839-
if (isset($this->form_button_icon[$form['T']])) {
845+
if (isset($this->form_button_icon[$form['n']])) {
840846
$total++;
841-
if ($this->form_button_icon[$form['T']]['Indexed']) {
847+
if ($this->form_button_icon[$form['n']]['Indexed']) {
842848
$total++;
843849
}
844850
}
@@ -874,6 +880,61 @@ function addFormIds($n, array &$annots, &$id)
874880
}
875881
}
876882

883+
/**
884+
* Gathers the push buttons that share a name and numbers the field each group is written as, from $id. A button
885+
* with a name of its own stays a single field and widget.
886+
*
887+
* @param int $id the first group's object number, left at the next free one
888+
*/
889+
function addButtonGroupIds(&$id)
890+
{
891+
$names = [];
892+
foreach ($this->forms as $form) {
893+
if ($this->isPushButton($form)) {
894+
$names[$form['T']][] = $form['n'];
895+
}
896+
}
897+
898+
$this->buttonGroups = [];
899+
foreach ($names as $name => $kids) {
900+
if (count($kids) > 1) {
901+
$this->buttonGroups[$name] = ['obj_id' => $id++, 'kids' => $kids];
902+
}
903+
}
904+
}
905+
906+
/**
907+
* Whether a field is a submit, reset, script or image button, which is pressed rather than switched on and off
908+
*
909+
* @param mixed[] $form
910+
*
911+
* @return bool
912+
*/
913+
private function isPushButton($form)
914+
{
915+
return $form['typ'] === 'Bt' && $form['subtype'] !== 'radio' && $form['subtype'] !== 'checkbox';
916+
}
917+
918+
/**
919+
* Writes each group of same-named push buttons as a field holding the name and the push-button flag, with the
920+
* widgets as its kids. It is not an annotation, so no page lists it.
921+
*/
922+
function putButtonGroups()
923+
{
924+
foreach ($this->buttonGroups as $name => $group) {
925+
$this->writer->object();
926+
$this->pdf_acro_array .= $this->mpdf->n . ' 0 R ';
927+
928+
$kids = '';
929+
foreach ($group['kids'] as $kid) {
930+
$kids .= $this->forms[$kid]['obj'] . ' 0 R ';
931+
}
932+
933+
$this->writer->write('<< /FT /Btn /Ff ' . $this->_setflag([17]) . ' /T ' . $this->writer->string($name) . ' /Kids [ ' . $kids . '] >>');
934+
$this->writer->write('endobj');
935+
}
936+
}
937+
877938
// In _putannots
878939
function _putFormItems($n, $hPt)
879940
{
@@ -986,17 +1047,20 @@ private function actionAllowed()
9861047
return false;
9871048
}
9881049

989-
function SetFormButtonJS($name, $js)
1050+
/**
1051+
* Gives a button its script, by the button's number rather than its name, which other buttons may share
1052+
*
1053+
* @param int $n the button's number, its key in $forms
1054+
* @param string $js
1055+
*/
1056+
function SetFormButtonJS($n, $js)
9901057
{
9911058
if (!$this->actionAllowed()) {
9921059
return;
9931060
}
994-
$js = str_replace("\t", ' ', trim($js));
995-
if (isset($name) && isset($js)) {
996-
$this->array_form_button_js[$this->writer->escape($name)] = [
997-
'js' => $js
998-
];
999-
}
1061+
$this->array_form_button_js[$n] = [
1062+
'js' => str_replace("\t", ' ', trim($js))
1063+
];
10001064
}
10011065

10021066
function SetFormChoiceJS($name, $js)
@@ -1244,15 +1308,15 @@ function SetJSButton($w, $h, $name, $value, $js, $image_id = 0, $title = '', $fl
12441308
}
12451309
// pos => 1 = no caption, icon only; 0 = caption only
12461310
if ($image_id) {
1247-
$this->form_button_icon[$this->writer->escape($name)] = [
1311+
$this->form_button_icon[$this->formCount] = [
12481312
'pos' => 1,
12491313
'image_id' => $image_id,
12501314
'Indexed' => $indexed,
12511315
];
12521316
}
12531317
$this->SetFormButton($w, $h, $name, $value, 'js_button', $title, $flags, false, false, $background_col, $border_col, $noprint, $border);
12541318
if ($js) {
1255-
$this->SetFormButtonJS($name, $js);
1319+
$this->SetFormButtonJS($this->formCount, $js);
12561320
}
12571321
$this->mpdf->x += $w;
12581322
}
@@ -1809,8 +1873,8 @@ private function writeAppearance($form)
18091873
}
18101874
}
18111875

1812-
if (isset($this->form_button_icon[$form['T']])) {
1813-
$s .= sprintf(' q %.3F 0 0 %.3F 0 0 cm /I%d Do Q', $width, $height, $this->form_button_icon[$form['T']]['image_id']);
1876+
if (isset($this->form_button_icon[$form['n']])) {
1877+
$s .= sprintf(' q %.3F 0 0 %.3F 0 0 cm /I%d Do Q', $width, $height, $this->form_button_icon[$form['n']]['image_id']);
18141878
} elseif ($form['AP']['lines']) {
18151879
$s .= sprintf(' /Tx BMC q %.3F %.3F %.3F %.3F re W n BT', $border, $border, $width - 2 * $border, $height - 2 * $border);
18161880
$s .= sprintf(' /F%d %.3F Tf %s', $this->mpdf->fonts[$form['style']['font']]['i'], $form['AP']['size'], $form['style']['fontcolor']);
@@ -1979,7 +2043,9 @@ function _putform_bt($form, $hPt)
19792043
$this->writer->object();
19802044
$n = $this->mpdf->n;
19812045

1982-
if ($form['subtype'] !== 'radio') {
2046+
// A radio button or a button that shares its name is listed through the field it is a kid of
2047+
$group = $this->isPushButton($form) && isset($this->buttonGroups[$form['T']]);
2048+
if ($form['subtype'] !== 'radio' && !$group) {
19832049
$this->pdf_acro_array .= $n . ' 0 R '; // Add to /Field element
19842050
}
19852051

@@ -1999,13 +2065,15 @@ function _putform_bt($form, $hPt)
19992065
$this->writer->write('/FT /Btn ');
20002066
$this->writer->write('/H /P ');
20012067

2002-
if ($form['subtype'] !== 'radio') { // mPDF 5.3.23
2068+
if ($group) {
2069+
$this->writer->write('/Parent ' . $this->buttonGroups[$form['T']]['obj_id'] . ' 0 R ');
2070+
} elseif ($form['subtype'] !== 'radio') { // mPDF 5.3.23
20032071
$this->writer->write('/T ' . $this->writer->string($form['T']));
20042072
}
20052073

20062074
$this->writer->write('/TU ' . $this->writer->string($form['TU']));
20072075

2008-
if (isset($this->form_button_icon[$form['T']])) {
2076+
if (isset($this->form_button_icon[$form['n']])) {
20092077
$form['BS_W'] = 0;
20102078
}
20112079

@@ -2137,14 +2205,14 @@ function _putform_bt($form, $hPt)
21372205

21382206
if ($form['subtype'] === 'js_button') {
21392207
// Icon / image
2140-
if (isset($this->form_button_icon[$form['T']])) {
2208+
if (isset($this->form_button_icon[$form['n']])) {
21412209
$cc++;
2142-
$temp .= '/TP ' . $this->form_button_icon[$form['T']]['pos'] . ' ';
2210+
$temp .= '/TP ' . $this->form_button_icon[$form['n']]['pos'] . ' ';
21432211
$temp .= '/I ' . ($cc + $this->mpdf->n) . ' 0 R '; // Normal icon
21442212
$temp .= '/RI ' . ($cc + $this->mpdf->n) . ' 0 R '; // onMouseOver
21452213
$temp .= '/IX ' . ($cc + $this->mpdf->n) . ' 0 R '; // onClick / onMouseDown
21462214
$temp .= '/IF << /SW /A /S /A /A [0.0 0.0] >> '; // Icon fit dictionary
2147-
if ($this->form_button_icon[$form['T']]['Indexed']) {
2215+
if ($this->form_button_icon[$form['n']]['Indexed']) {
21482216
$cc++;
21492217
}
21502218
$put_icon = 1;
@@ -2158,7 +2226,7 @@ function _putform_bt($form, $hPt)
21582226
$form['FF'][] = 17;
21592227
$this->writer->write('/Ff ' . $this->_setflag($form['FF']));
21602228
// Javascript
2161-
if (isset($this->array_form_button_js[$form['T']])) {
2229+
if (isset($this->array_form_button_js[$form['n']])) {
21622230
$cc++;
21632231
$this->writer->write('/AA << /D ' . ($cc + $this->mpdf->n) . ' 0 R >>');
21642232
$put_js = 1;
@@ -2172,13 +2240,12 @@ function _putform_bt($form, $hPt)
21722240
// additional objects
21732241
// obj icon
21742242
if ($put_icon === 1) {
2175-
$this->_put_button_icon($this->form_button_icon[$form['T']], $form['w'], $form['h']);
2243+
$this->_put_button_icon($this->form_button_icon[$form['n']], $form['w'], $form['h']);
21762244
$put_icon = null;
21772245
}
21782246
// obj + 1
21792247
if ($put_js === 1) {
2180-
$this->mpdf->_set_object_javascript($this->array_form_button_js[$form['T']]['js']);
2181-
unset($this->array_form_button_js[$form['T']]);
2248+
$this->mpdf->_set_object_javascript($this->array_form_button_js[$form['n']]['js']);
21822249
$put_js = null;
21832250
}
21842251

‎src/Writer/MetadataWriter.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,8 @@ public function writeAnnotations() // _putannots
10171017
$this->form->_putRadioItems($n);
10181018
}
10191019

1020+
$this->form->putButtonGroups();
1021+
10201022
$this->annotationFiles = [];
10211023
}
10221024

@@ -1025,7 +1027,7 @@ public function writeAnnotations() // _putannots
10251027
*
10261028
* Each page gets its links, then its annotations, then its form widgets. An annotation's ids name the annotation
10271029
* itself, its embedded file (under PDF/A-3 its file specification, then the file) or else its popup, and its
1028-
* appearance under PDF/A. The radio groups come last.
1030+
* appearance under PDF/A. The radio groups come last, then the groups of push buttons that share a name.
10291031
*
10301032
* @param int $id the object number after the last page's
10311033
*
@@ -1076,6 +1078,8 @@ public function numberAnnotations($id)
10761078
$this->form->form_radio_groups[$name]['obj_id'] = $id++;
10771079
}
10781080

1081+
$this->form->addButtonGroupIds($id);
1082+
10791083
return $annots;
10801084
}
10811085

‎tests/Mpdf/AnnotationObjectNumbersTest.php‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class AnnotationObjectNumbersTest extends \Yoast\PHPUnitPolyfills\TestCases\Test
1111
use PageStreams;
1212

1313
/**
14-
* Every /Annots entry is an annotation of its page, and every /Popup, /Parent, /EF, /AP and radio /Parent
15-
* reference names an object of the kind it should
14+
* Every /Annots entry is an annotation of its page, and every /Popup, /Parent, /EF, /AP and radio or button /Parent
15+
* reference names an object of the kind it should. A field that gathers widgets is listed by /Fields, not /Annots.
1616
*
1717
* @dataProvider documents
1818
*
@@ -30,11 +30,14 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)
3030
$this->assertEveryObjectWritten($pdf);
3131

3232
$subtypes = [];
33+
$listed = [];
34+
$parents = [];
3335
foreach ($this->pageObjects($pdf) as $i => $page) {
3436
$refs = $this->annotsOf($pdf, $page);
3537
$subtypes[$i] = [];
3638

3739
foreach ($refs as $ref) {
40+
$listed[] = $ref;
3841
$annot = $this->object($pdf, $ref);
3942
$this->assertStringContainsString('/Type /Annot', $annot);
4043
preg_match('/\/Subtype \/(\w+)/', $annot, $subtype);
@@ -72,11 +75,24 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)
7275

7376
if ($subtype[1] === 'Widget' && preg_match('/\/Parent (\d+) 0 R/', $annot, $group)) {
7477
$this->assertMatchesRegularExpression('/\/Kids \[[^\]]*\b' . $ref . ' 0 R/', $this->object($pdf, $group[1]));
78+
$this->assertStringNotContainsString('/T (', $annot, 'A kid takes its name from its field');
79+
$parents[] = $group[1];
7580
}
7681
}
7782
}
7883

7984
$this->assertSame($expected, $subtypes);
85+
86+
if ($forms) {
87+
preg_match('/\/Fields \[([^\]]*)\]/', $pdf, $list);
88+
preg_match_all('/(\d+) 0 R/', $list[1], $fields);
89+
$parents = array_unique($parents);
90+
$this->assertCount(4, $parents, 'Two radio groups and two groups of buttons');
91+
foreach ($parents as $parent) {
92+
$this->assertNotContains($parent, $listed, 'A field is not an annotation');
93+
$this->assertContains($parent, $fields[1]);
94+
}
95+
}
8096
}
8197

8298
/**
@@ -87,24 +103,25 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)
87103
public function documents()
88104
{
89105
$notes = ['Link', 'Text', 'Popup', 'Text', 'Text', 'Text', 'Popup'];
90-
$widgets = ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'];
106+
$widgets = ['Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget'];
91107
$second = ['Link', 'Text', 'Popup'];
92108

93109
$withFile = ['Link', 'Text', 'Popup', 'FileAttachment', 'Text', 'Text', 'Popup'];
94110

95111
return [
96-
'file not allowed, forms' => [['mode' => 'c', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
97-
'embedded fonts, forms' => [['useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
98-
'file allowed, forms' => [['mode' => 'c', 'useActiveForms' => true, 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], true, [array_merge($withFile, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
112+
'file not allowed, forms' => [['mode' => 'c', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
113+
'embedded fonts, forms' => [['useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
114+
'file allowed, forms' => [['mode' => 'c', 'useActiveForms' => true, 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], true, [array_merge($withFile, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
99115
'PDF/A-2 appearances, file not a PDF/A' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], false, [$notes, $second]],
100-
'PDF/A-2 appearances, forms' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
116+
'PDF/A-2 appearances, forms' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
101117
'PDF/A-3 appearances and file' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '3-B', 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], false, [$withFile, $second]],
102118
];
103119
}
104120

105121
/**
106122
* Two pages of links, notes with and without popups, a note with a file and, when asked, form widgets with a
107-
* radio group on each page
123+
* radio group on each page, two buttons named go on the first and one on the second, and buttons named b and
124+
* lone
108125
*
109126
* @param bool $forms
110127
*
@@ -118,13 +135,16 @@ private function html($forms)
118135
if ($forms) {
119136
$html .= '<form><input type="text" name="t1" value="x" onchange="alert(1)" /> <input type="checkbox" name="c1" value="1" checked="checked" />'
120137
. ' <input type="radio" name="r1" value="a" checked="checked" /> <input type="radio" name="r1" value="b" />'
121-
. ' <select name="s1" onchange="x()"><option value="1">1</option></select></form>';
138+
. ' <select name="s1" onchange="x()"><option value="1">1</option></select>'
139+
. ' <input type="button" name="go" value="A" onclick="a()" /> <input type="submit" name="go" value="B" />'
140+
. ' <input type="image" name="b" src="' . __DIR__ . '/../data/img/ratio-16x9.png" onclick="b()" /> <input type="reset" name="lone" /></form>';
122141
}
123142

124143
$html .= '<pagebreak /><p id="b"><a href="https://example.com">ext</a> <annotation content="Second" popup="true" /></p>';
125144

126145
if ($forms) {
127-
$html .= '<form><input type="radio" name="r2" value="c" /> <input type="radio" name="r2" value="d" checked="checked" /> <input type="text" name="t2" /></form>';
146+
$html .= '<form><input type="radio" name="r2" value="c" /> <input type="radio" name="r2" value="d" checked="checked" /> <input type="text" name="t2" />'
147+
. ' <input type="button" name="go" value="C" onclick="c()" /> <input type="image" name="b" src="' . __DIR__ . '/../data/img/ratio-9x16.png" /></form>';
128148
}
129149

130150
return $html;

‎tests/Mpdf/PdfaConformanceTest.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public function testVisibilityConforms($version)
130130

131131
/**
132132
* An active form conforms: checkboxes and radio buttons are drawn without ZapfDingbats, every widget carries its
133-
* appearance, and the JavaScript, submit and reset actions PDF/A forbids are left out (#348)
133+
* appearance, and the JavaScript, submit and reset actions PDF/A forbids are left out (#348). Two of the buttons
134+
* share a name and are written as kids of one field (#443).
134135
*
135136
* @dataProvider documents
136137
*/
@@ -147,7 +148,8 @@ public function testActiveFormConforms($version, $config)
147148
. '<p><select name="s" onchange="app.alert(2)"><option value="1">One</option><option value="2" selected="selected">Two</option></select>'
148149
. ' <select name="m" size="3" multiple="multiple"><option value="1">One</option><option value="2" selected="selected">Two</option></select></p>'
149150
. '<p><input type="submit" name="go" value="Send" /> <input type="reset" name="rs" value="Reset" />'
150-
. ' <input type="button" name="b" value="Push" onclick="app.alert(3)" noprint="noprint" /></p>'
151+
. ' <input type="button" name="b" value="Push" onclick="app.alert(3)" noprint="noprint" />'
152+
. ' <input type="button" name="b" value="Again" onclick="app.alert(4)" /></p>'
151153
. '</form>'
152154
);
153155

0 commit comments

Comments
 (0)