Skip to content

Commit 52319dd

Browse files
jakejackson1claude
andauthored
Scale the appearance of a form field in a fixed-position block that shrinks to fit (#457) (#487)
* Scale the appearance of a form field in a fixed-position block that shrinks to fit WriteFixedPosHTML() scaled a field's rectangle and font size when it shrank a block to fit its box, but not the appearance laid out when the field was placed, so the appearance drew unscaled text in the scaled box. The appearance is now drawn at its layout's size under a scale matrix, the border width /BS names is scaled with the rest, and both it and the /DA font size are rounded to three decimals. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Add a snapshot of form fields in fixed-position blocks that shrink to fit Each field is drawn in a box it fits and beside it in a box too short for it, so the shrunk appearances can be compared with the full-size ones. 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 101bf4a commit 52319dd

5 files changed

Lines changed: 189 additions & 6 deletions

File tree

‎src/Form.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private function appearanceText($w, $h, $border, array $lines, $align, $flow, ar
17341734
$top = min($selected);
17351735
}
17361736

1737-
$layout = ['size' => $size, 'lines' => [], 'highlights' => []];
1737+
$layout = ['size' => $size, 'lines' => [], 'highlights' => [], 'scale' => 1];
17381738
foreach (array_slice($lines, $top, $flow === 'list' ? $rows : null, true) as $i => $line) {
17391739
if ($flow === 'list') {
17401740
$rowBottom = $height - $border - ($i - $top + 1) * $rowHeight;
@@ -1888,9 +1888,12 @@ private function writeAppearance($form)
18881888
return;
18891889
}
18901890

1891-
$width = $form['w'] * Mpdf::SCALE;
1892-
$height = $form['h'] * Mpdf::SCALE;
1893-
$border = (float) $form['BS_W'];
1891+
// A widget scaled after it was laid out, as WriteFixedPosHTML() shrinks a block to fit, is drawn at its layout's
1892+
// size and scaled to the widget
1893+
$scale = $form['AP']['scale'];
1894+
$width = $form['w'] * Mpdf::SCALE / $scale;
1895+
$height = $form['h'] * Mpdf::SCALE / $scale;
1896+
$border = (float) $form['BS_W'] / $scale;
18941897

18951898
$s = sprintf('%s 0 0 %.3F %.3F re f', $this->appearanceColor($form['BG_C'], 'rg'), $width, $height);
18961899
if ($border > 0) {
@@ -1915,7 +1918,11 @@ private function writeAppearance($form)
19151918
$s .= ' ' . implode(' ', $form['AP']['lines']) . ' Q EMC';
19161919
}
19171920

1918-
$this->writeAppearanceStream($s, [$width, $height]);
1921+
if ($scale != 1) {
1922+
$s = sprintf('q %.5F 0 0 %.5F 0 0 cm %s Q', $scale, $scale, $s);
1923+
}
1924+
1925+
$this->writeAppearanceStream($s, [$width * $scale, $height * $scale]);
19191926
}
19201927

19211928
/**

‎src/Mpdf.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15849,7 +15849,12 @@ function WriteFixedPosHTML($html, $x, $y, $w, $h, $overflow = 'visible', $boundi
1584915849
$f['y'] = $y + (($f['y'] - $y) * $shrink_f);
1585015850
$f['w'] *= $shrink_f;
1585115851
$f['h'] *= $shrink_f;
15852-
$f['style']['fontsize'] *= $shrink_f;
15852+
$f['style']['fontsize'] = round($f['style']['fontsize'] * $shrink_f, 3);
15853+
$f['BS_W'] = round($f['BS_W'] * $shrink_f, 3);
15854+
if (isset($f['AP'])) {
15855+
// The appearance was laid out at the unscaled size
15856+
$f['AP']['scale'] = $shrink_f;
15857+
}
1585315858
}
1585415859
$this->form->forms[$f['n']] = $f;
1585515860
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* An active field in a block WriteFixedPosHTML() shrinks to fit its box keeps an appearance that matches the shrunk
7+
* widget (#457)
8+
*/
9+
class ShrunkFixedPosFormTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
use PageStreams;
13+
14+
/**
15+
* The appearance is drawn at the size /DA names, with each line of text and each highlight inside its box, and the
16+
* sizes /DA and /BS name are written to three decimals
17+
*
18+
* @dataProvider fields
19+
*
20+
* @param string $field
21+
*/
22+
public function testAppearanceIsScaledWithTheWidget($field)
23+
{
24+
$mpdf = $this->mpdf(['useActiveForms' => true]);
25+
$filler = str_repeat('<p>Filler line that makes the block taller than its box.</p>', 12);
26+
$mpdf->WriteFixedPosHTML('<form>' . $field . '</form>' . $filler, 20, 40, 120, 40, 'auto');
27+
$pdf = $this->output($mpdf);
28+
29+
$widget = $this->object($pdf, $this->annotationRefs($pdf)[0][0]);
30+
$this->assertSame(1, preg_match('/\/DA \(\/F(\d+) (\d+(?:\.\d{1,3})?) Tf /', $widget, $da));
31+
$this->assertSame(1, preg_match('/\/BS << \/W \d+(?:\.\d{1,3})? /', $widget));
32+
$this->assertSame(1, preg_match('/\/AP << \/N (?:<< \/Push )?(\d+) 0 R/', $widget, $ref));
33+
34+
$appearance = $this->object($pdf, $ref[1]);
35+
$this->assertSame(1, preg_match('/\/BBox \[0 0 ([\d.]+) ([\d.]+)\]/', $appearance, $box));
36+
$this->assertSame(1, preg_match('/stream\n(.*)\nendstream/s', $appearance, $stream));
37+
$this->assertSame(1, preg_match('/^q ([\d.]+) 0 0 \1 0 0 cm /', $stream[1], $matrix));
38+
$scale = (float) $matrix[1];
39+
$this->assertLessThan(1, $scale);
40+
41+
$this->assertSame(1, preg_match('/BT \/F' . $da[1] . ' ([\d.]+) Tf ET/', $stream[1], $tf));
42+
$size = $tf[1] * $scale;
43+
$this->assertEqualsWithDelta((float) $da[2], $size, 0.001);
44+
45+
$desc = $this->fontDescriptor($mpdf, $da[1]);
46+
preg_match_all('/BT -?[\d.]+ (-?[\d.]+) Td/', $stream[1], $baselines);
47+
$this->assertNotEmpty($baselines[1]);
48+
foreach ($baselines[1] as $baseline) {
49+
$this->assertGreaterThanOrEqual(0, $baseline * $scale + $desc['Descent'] / 1000 * $size);
50+
$this->assertLessThanOrEqual((float) $box[2], $baseline * $scale + $desc['Ascent'] / 1000 * $size);
51+
}
52+
53+
preg_match_all('/([\d.]+) ([\d.]+) ([\d.]+) ([\d.]+) re f/', $stream[1], $rects, PREG_SET_ORDER);
54+
foreach ($rects as $rect) {
55+
$this->assertLessThanOrEqual($box[1] + 0.002, ($rect[1] + $rect[3]) * $scale);
56+
$this->assertLessThanOrEqual($box[2] + 0.002, ($rect[2] + $rect[4]) * $scale);
57+
}
58+
}
59+
60+
/**
61+
* A text field, a list box with a highlighted option, and a push button
62+
*
63+
* @return string[][]
64+
*/
65+
public function fields()
66+
{
67+
return [
68+
'text field' => ['<input type="text" name="t" value="Typed value" size="30" />'],
69+
'list box' => ['<select name="s" size="3"><option value="1">One</option><option value="2" selected="selected">Two</option><option value="3">Three</option></select>'],
70+
'push button' => ['<input type="button" name="b" value="Press" />'],
71+
];
72+
}
73+
74+
/**
75+
* A field in a block that fits its box keeps the appearance it was laid out with
76+
*/
77+
public function testAppearanceOfAFieldThatFitsIsNotScaled()
78+
{
79+
$mpdf = $this->mpdf(['useActiveForms' => true]);
80+
$mpdf->WriteFixedPosHTML('<form><input type="text" name="t" value="Typed value" /></form>', 20, 40, 120, 40, 'auto');
81+
$pdf = $this->output($mpdf);
82+
83+
$widget = $this->object($pdf, $this->annotationRefs($pdf)[0][0]);
84+
$this->assertSame(1, preg_match('/\/AP << \/N (\d+) 0 R/', $widget, $ref));
85+
$this->assertStringNotContainsString(' cm ', $this->object($pdf, $ref[1]));
86+
}
87+
88+
/**
89+
* The descriptor of the font a PDF names /F$number
90+
*
91+
* @param \Mpdf\Mpdf $mpdf
92+
* @param string $number
93+
*
94+
* @return mixed[]
95+
*/
96+
private function fontDescriptor(Mpdf $mpdf, $number)
97+
{
98+
foreach ($mpdf->fonts as $font) {
99+
if ($font['i'] == $number) {
100+
return $font['desc'];
101+
}
102+
}
103+
104+
$this->fail('No font is written as /F' . $number);
105+
}
106+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* Active form fields in fixed-position blocks that shrink to fit their box, each beside the same block at full size.
7+
* The shrunk fields draw their appearance at the widget's scaled size (#457).
8+
*
9+
* @group snapshot
10+
*/
11+
class ShrunkFixedPosFormSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getId()
18+
{
19+
return 'shrunk-fixed-pos-form';
20+
}
21+
22+
/**
23+
* Each block twice: in a box it fits, and in a box too short for it, which shrinks it
24+
*/
25+
public function generatePdf()
26+
{
27+
$this->mpdf = $this->createMpdf(['useActiveForms' => true]);
28+
29+
$fields = [
30+
'Text field' => '<input type="text" name="text" value="Typed value" size="30" />',
31+
'Text area' => '<textarea name="area" rows="3" cols="30">A value that wraps over more than one line of the text area.</textarea>',
32+
'List box' => '<select name="list" size="3"><option value="1">One</option><option value="2" selected="selected">Two, selected</option>'
33+
. '<option value="3">Three</option><option value="4">Four</option></select>',
34+
'Combo box' => '<select name="combo"><option value="1">First</option><option value="2" selected="selected">Second, selected</option></select>',
35+
'Buttons' => '<input type="checkbox" name="check" value="yes" checked="checked" /> Checked'
36+
. ' <input type="button" name="button" value="Push button" />',
37+
];
38+
39+
$y = 20;
40+
foreach ($fields as $label => $field) {
41+
$html = '<p style="font-weight: bold; margin: 0 0 1mm 0">' . $label . '</p><form>' . $field . '</form>';
42+
43+
$this->mpdf->WriteFixedPosHTML($this->rename($html, 'full'), 15, $y, 85, 40, 'auto');
44+
$this->mpdf->WriteFixedPosHTML($this->rename($html, 'shrunk'), 110, $y, 85, 9, 'auto');
45+
$this->mpdf->Rect(15, $y, 85, 40);
46+
$this->mpdf->Rect(110, $y, 85, 9);
47+
48+
$y += 50;
49+
}
50+
}
51+
52+
/**
53+
* The block's fields with the suffix added to their names, as a form has one field per name
54+
*
55+
* @param string $html
56+
* @param string $suffix
57+
*
58+
* @return string
59+
*/
60+
private function rename($html, $suffix)
61+
{
62+
return preg_replace('/name="(\w+)"/', 'name="$1_' . $suffix . '"', $html);
63+
}
64+
65+
}
74.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)