Skip to content

Commit 9954740

Browse files
jakejackson1claude
andcommitted
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>
1 parent 38f2d3d commit 9954740

2 files changed

Lines changed: 58 additions & 48 deletions

File tree

‎src/Form.php‎

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,18 @@ function print_ob_radio($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir, $x,
658658
}
659659
}
660660

661+
/**
662+
* Whether the field's CSS border style takes its border away
663+
*
664+
* @param mixed[] $objattr
665+
*
666+
* @return bool
667+
*/
668+
private function borderStyleNone(array $objattr)
669+
{
670+
return isset($objattr['border-style']) && in_array($objattr['border-style'], ['none', 'hidden'], true);
671+
}
672+
661673
/**
662674
* The /BS width and style an active field's CSS border sets, for SetFormText(), SetFormChoice() and
663675
* SetFormButton() to take over their defaults. A border of style none or hidden has no width.
@@ -675,7 +687,7 @@ private function activeBorder(array $objattr)
675687
$border['W'] = sprintf('%.3F', $objattr['border-width'] * Mpdf::SCALE);
676688
}
677689
if (isset($objattr['border-style'])) {
678-
if (in_array($objattr['border-style'], ['none', 'hidden'], true)) {
690+
if ($this->borderStyleNone($objattr)) {
679691
$border['W'] = '0';
680692
} else {
681693
// PDF has no double, groove or ridge border
@@ -725,7 +737,7 @@ private function setStaticColors(array $objattr, $greyed)
725737
private function setStaticBorder(array $objattr, $k)
726738
{
727739
$width = isset($objattr['border-width']) ? $objattr['border-width'] : 0.2;
728-
$border = $width > 0 && !(isset($objattr['border-style']) && in_array($objattr['border-style'], ['none', 'hidden'], true));
740+
$border = $width > 0 && !$this->borderStyleNone($objattr);
729741

730742
$this->mpdf->SetLineWidth(($border ? $width : 0.2) / $k);
731743
if (isset($objattr['border-col'])) {
@@ -1013,16 +1025,6 @@ function SetFormText($w, $h, $name, $value = '', $default = '', $title = '', $fl
10131025
}
10141026
$title = $this->writer->utf8ToUtf16BigEndian($title);
10151027
}
1016-
if ($background_col) {
1017-
$bg_c = $this->mpdf->SetColor($background_col, 'CodeOnly');
1018-
} else {
1019-
$bg_c = $this->form_background_color;
1020-
}
1021-
if ($border_col) {
1022-
$bc_c = $this->mpdf->SetColor($border_col, 'CodeOnly');
1023-
} else {
1024-
$bc_c = $this->form_border_color;
1025-
}
10261028

10271029
$f = [
10281030
'n' => $this->formCount,
@@ -1042,8 +1044,8 @@ function SetFormText($w, $h, $name, $value = '', $default = '', $title = '', $fl
10421044
'maxlen' => $maxlen,
10431045
'BS_W' => $border['W'],
10441046
'BS_S' => $border['S'],
1045-
'BC_C' => $bc_c,
1046-
'BG_C' => $bg_c,
1047+
'BC_C' => $this->activeColor($border_col, $this->form_border_color),
1048+
'BG_C' => $this->activeColor($background_col, $this->form_background_color),
10471049
'style' => [
10481050
'font' => $this->mpdf->FontFamily,
10491051
'fontsize' => $this->mpdf->FontSizePt,
@@ -1128,8 +1130,8 @@ function SetFormChoice($w, $h, $name, $flags, $array, $align = 'L', $js = '', $b
11281130
'Q' => $align,
11291131
'BS_W' => $border['W'],
11301132
'BS_S' => $border['S'],
1131-
'BC_C' => $border_col ? $this->mpdf->SetColor($border_col, 'CodeOnly') : $this->form_border_color,
1132-
'BG_C' => $background_col ? $this->mpdf->SetColor($background_col, 'CodeOnly') : $this->form_background_color,
1133+
'BC_C' => $this->activeColor($border_col, $this->form_border_color),
1134+
'BG_C' => $this->activeColor($background_col, $this->form_background_color),
11331135
'style' => [
11341136
'font' => $this->mpdf->FontFamily,
11351137
'fontsize' => $this->mpdf->FontSizePt,
@@ -1309,16 +1311,6 @@ function SetFormButton($bb, $hh, $name, $value, $type, $title = '', $flags = [],
13091311
} else {
13101312
$activ = 0;
13111313
}
1312-
if ($background_col) {
1313-
$bg_c = $this->mpdf->SetColor($background_col, 'CodeOnly');
1314-
} else {
1315-
$bg_c = $this->form_button_background_color;
1316-
}
1317-
if ($border_col) {
1318-
$bc_c = $this->mpdf->SetColor($border_col, 'CodeOnly');
1319-
} else {
1320-
$bc_c = $this->form_button_border_color;
1321-
}
13221314
$f = ['n' => $this->formCount,
13231315
'typ' => 'Bt',
13241316
'page' => $this->mpdf->page,
@@ -1337,8 +1329,8 @@ function SetFormButton($bb, $hh, $name, $value, $type, $title = '', $flags = [],
13371329
'AC' => $this->form_button_text_click,
13381330
'BS_W' => $border['W'],
13391331
'BS_S' => $border['S'],
1340-
'BC_C' => $bc_c,
1341-
'BG_C' => $bg_c,
1332+
'BC_C' => $this->activeColor($border_col, $this->form_button_border_color),
1333+
'BG_C' => $this->activeColor($background_col, $this->form_button_background_color),
13421334
'activ' => $activ,
13431335
'disabled' => $disabled,
13441336
'noprint' => $noprint,
@@ -1625,9 +1617,6 @@ private function writeAppearance($form)
16251617
// A dashed border's style carries its dash array, as /S /D /D [3]
16261618
$dash = preg_match('/\/D (\[[^]]*\])/', $form['BS_S'], $m) ? ' ' . $m[1] . ' 0 d' : '';
16271619
$s .= sprintf(' %s %.3F w%s %.3F %.3F %.3F %.3F re S', $this->appearanceColor($form['BC_C'], 'RG'), $border, $dash, $border / 2, $border / 2, $width - $border, $height - $border);
1628-
if ($dash) {
1629-
$s .= ' [] 0 d';
1630-
}
16311620
}
16321621

16331622
if ($form['AP']['highlights']) {
@@ -1651,6 +1640,34 @@ private function writeAppearance($form)
16511640
$this->writeAppearanceStream($s, [$width, $height]);
16521641
}
16531642

1643+
/**
1644+
* An active field's colour as /MK gives it: from its CSS, or else a default
1645+
*
1646+
* @param mixed $color a colour the field's CSS sets, or false
1647+
* @param string $default e.g. '0.6 0.6 0.72'
1648+
*
1649+
* @return string
1650+
*/
1651+
private function activeColor($color, $default)
1652+
{
1653+
return $color ? $this->mpdf->SetColor($color, 'CodeOnly') : $default;
1654+
}
1655+
1656+
/**
1657+
* The border and background colours of a text or choice field's /MK. A viewer that redraws the field draws a border
1658+
* wherever /BC is given, whatever the width, so a field without a border has none.
1659+
*
1660+
* @param mixed[] $form
1661+
*
1662+
* @return string
1663+
*/
1664+
private function markColors($form)
1665+
{
1666+
$colors = (float) $form['BS_W'] > 0 ? '/BC [ ' . $form['BC_C'] . ' ] ' : '';
1667+
1668+
return $colors . '/BG [ ' . $form['BG_C'] . ' ] ';
1669+
}
1670+
16541671
/**
16551672
* Writes a stream a widget shows
16561673
*
@@ -2034,13 +2051,7 @@ function _putform_ch($form, $hPt)
20342051
$temp .= '/S /' . $form['BS_S'] . ' ';
20352052
$this->writer->write("/BS << $temp >>");
20362053

2037-
$temp = '';
2038-
// A viewer that redraws the field draws a border wherever /BC is given, whatever its width
2039-
if ((float) $form['BS_W'] > 0) {
2040-
$temp .= '/BC [ ' . $form['BC_C'] . ' ] ';
2041-
}
2042-
$temp .= '/BG [ ' . $form['BG_C'] . ' ] ';
2043-
$this->writer->write('/MK << ' . $temp . ' >>');
2054+
$this->writer->write('/MK << ' . $this->markColors($form) . ' >>');
20442055

20452056
$this->writer->write('/NM ' . $this->writer->string(sprintf('%04u-%04u', $n, 6000 + $form['n'])));
20462057
$this->writer->write('/M ' . $this->writer->dateString());
@@ -2132,13 +2143,7 @@ function _putform_tx($form, $hPt)
21322143
$temp .= '/S /' . $form['BS_S'] . ' ';
21332144
$this->writer->write("/BS << $temp >>");
21342145

2135-
$temp = '';
2136-
// A viewer that redraws the field draws a border wherever /BC is given, whatever its width
2137-
if ((float) $form['BS_W'] > 0) {
2138-
$temp .= '/BC [ ' . $form['BC_C'] . ' ] ';
2139-
}
2140-
$temp .= '/BG [ ' . $form['BG_C'] . ' ] ';
2141-
$this->writer->write('/MK <<' . $temp . ' >>');
2146+
$this->writer->write('/MK <<' . $this->markColors($form) . ' >>');
21422147

21432148
$this->writer->write('/T ' . $this->writer->string($form['T']));
21442149
$this->writer->write('/TU ' . $this->writer->string($form['TU']));

‎tests/Mpdf/ActiveFieldBorderTest.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,14 @@ public function testTheBorderColourFollowsTheOutputIntent()
179179
*/
180180
private function widget($pdf, $name)
181181
{
182-
$this->assertSame(1, preg_match('/<<\s*\/Type \/Annot (?:(?!endobj).)*?\/T \(' . $name . '\)(?:(?!endobj).)*/s', $pdf, $match), 'The field should have a widget');
183-
184-
return $match[0];
182+
foreach ($this->annotationRefs($pdf)[0] as $ref) {
183+
$annotation = $this->object($pdf, $ref);
184+
if (strpos($annotation, '/T (' . $name . ')') !== false) {
185+
return $annotation;
186+
}
187+
}
188+
189+
$this->fail('The field ' . $name . ' should have a widget');
185190
}
186191

187192
}

0 commit comments

Comments
 (0)