Skip to content

Commit e74da2b

Browse files
jakejackson1claude
andcommitted
Show a list box's rows whole in its appearance (#426)
The appearance of an active list box split the height inside its border between the rows by the font's line height, so the last row was cut off. It now takes its rows from the select tag, gives each an equal share of that height, shrinks the font to keep each row whole, and draws only that many rows, starting at the first selection when it would fall below the last. A highlight fills its row. Rebased onto gravitypdf as one commit: the branch's six commits had added a combo box drop-down arrow and then taken it out again, as viewers draw their own, so only the list box change remains. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 21e4759 commit e74da2b

4 files changed

Lines changed: 130 additions & 15 deletions

File tree

‎src/Form.php‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ function print_ob_select($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
416416
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));
417417
}
418418

419-
$this->SetFormChoice($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $flags, $data, $rtlalign, $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), $this->activeBorder($objattr));
419+
$this->SetFormChoice($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $flags, $data, $rtlalign, $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), $this->activeBorder($objattr), (isset($objattr['rows']) ? $objattr['rows'] : 1));
420420
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));
421421

422422
} else {
@@ -1199,7 +1199,7 @@ function SetFormText($w, $h, $name, $value = '', $default = '', $title = '', $fl
11991199
}
12001200
}
12011201

1202-
function SetFormChoice($w, $h, $name, $flags, $array, $align = 'L', $js = '', $background_col = false, $border_col = false, $border = [])
1202+
function SetFormChoice($w, $h, $name, $flags, $array, $align = 'L', $js = '', $background_col = false, $border_col = false, $border = [], $rows = 1)
12031203
{
12041204
$this->formCount++;
12051205
if ($this->mpdf->blk[$this->mpdf->blklvl]['direction'] === 'rtl') {
@@ -1214,7 +1214,7 @@ function SetFormChoice($w, $h, $name, $flags, $array, $align = 'L', $js = '', $b
12141214
if (in_array(self::FLAG_COMBOBOX, $flags, true)) {
12151215
$appearance = $this->appearanceText($w, $h, $border['W'], [$array['SEL'] ? $array['OPT'][$array['SEL'][0]] : ''], $align, 'line');
12161216
} else {
1217-
$appearance = $this->appearanceText($w, $h, $border['W'], $array['OPT'], $align, 'list', $array['SEL']);
1217+
$appearance = $this->appearanceText($w, $h, $border['W'], $array['OPT'], $align, 'list', $array['SEL'], true, $rows);
12181218
}
12191219
if ($this->mpdf->onlyCoreFonts) {
12201220
for ($i = 0; $i < count($array['VAL']); $i++) {
@@ -1689,16 +1689,17 @@ private function fitLine($size, $room, $length, $measure)
16891689
* @param float $border its border width, in points
16901690
* @param string[] $lines the text, a line each, in the document's encoding
16911691
* @param string $align '0', '1' or '2', left, centred or right as /Q has it
1692-
* @param string $flow 'line' centres the first line on the height, 'wrap' runs the lines down from the top
1693-
* wrapped to the width, and 'list' runs them down unwrapped, as a list box shows its options
1692+
* @param string $flow 'line' centres the first line on the height, 'wrap' runs the lines down from the top wrapped
1693+
* to the width, and 'list' shows them unwrapped a row each, as a list box shows its options
16941694
* @param int[] $selected the lines a list box highlights
16951695
* @param bool $fit whether a 'line' is fitted to the widget as fitLine() does. Not for a hidden field, which has no
16961696
* room, or a button that shows an icon instead
1697+
* @param int $rows the rows a list box shows
16971698
*
16981699
* @return mixed[] the font size, each line with where it starts and as the font encodes it, and the highlights, in
16991700
* points from the bottom left
17001701
*/
1701-
private function appearanceText($w, $h, $border, array $lines, $align, $flow, array $selected = [], $fit = true)
1702+
private function appearanceText($w, $h, $border, array $lines, $align, $flow, array $selected = [], $fit = true, $rows = 1)
17021703
{
17031704
if (!$this->mpdf->usingCoreFont && !$this->complexText) {
17041705
foreach ($lines as $line) {
@@ -1730,23 +1731,40 @@ private function appearanceText($w, $h, $border, array $lines, $align, $flow, ar
17301731
});
17311732
$lines = [mb_substr($line, 0, $length, $this->mpdf->mb_enc)];
17321733
}
1734+
1735+
// A list box's rows share the height inside the border. Tag\Select allowed one font size a row, less than the
1736+
// font's line height, so the font shrinks to keep each row whole
1737+
$rowHeight = ($height - 2 * $border) / $rows;
1738+
if ($flow === 'list') {
1739+
$size = min($size, $rowHeight / ($ascent - $descent));
1740+
}
17331741
$leading = ($ascent - $descent) * $size;
17341742

17351743
$top = 0;
17361744
if ($flow === 'wrap') {
17371745
$lines = $this->wrap($lines, $room / $size);
1738-
} elseif ($flow === 'line') {
1746+
} elseif ($flow !== 'list') {
17391747
$lines = array_slice($lines, 0, 1);
1740-
} elseif ($selected && min($selected) >= floor(($height - 2 * $padding) / $leading)) {
1748+
} elseif ($selected && min($selected) >= $rows) {
17411749
// A list box too short to show its first selected option starts at it
17421750
$top = min($selected);
17431751
}
17441752

17451753
$layout = ['size' => $size, 'lines' => [], 'highlights' => []];
1746-
foreach (array_slice($lines, $top, null, true) as $i => $line) {
1747-
$y = $flow === 'line' ? ($height - $leading) / 2 - $descent * $size : $height - $padding - $ascent * $size - ($i - $top) * $leading;
1748-
if ($y + $ascent * $size < 0) {
1749-
break;
1754+
foreach (array_slice($lines, $top, $flow === 'list' ? $rows : null, true) as $i => $line) {
1755+
if ($flow === 'list') {
1756+
$rowBottom = $height - $border - ($i - $top + 1) * $rowHeight;
1757+
$y = $rowBottom + ($rowHeight - $leading) / 2 - $descent * $size;
1758+
if (in_array($i, $selected, true)) {
1759+
$layout['highlights'][] = [$border, $rowBottom, $width - 2 * $border, $rowHeight];
1760+
}
1761+
} elseif ($flow === 'wrap') {
1762+
$y = $height - $padding - $ascent * $size - $i * $leading;
1763+
if ($y + $ascent * $size < 0) {
1764+
break;
1765+
}
1766+
} else {
1767+
$y = ($height - $leading) / 2 - $descent * $size;
17501768
}
17511769

17521770
$lineWidth = $this->emWidth($line) * $size;
@@ -1759,9 +1777,6 @@ private function appearanceText($w, $h, $border, array $lines, $align, $flow, ar
17591777
}
17601778

17611779
$layout['lines'][] = [$x, $y, $this->mpdf->usingCoreFont ? $line : $this->writer->utf8ToUtf16BigEndian($line, false)];
1762-
if (in_array($i, $selected, true)) {
1763-
$layout['highlights'][] = [$border, $y + $descent * $size, $width - 2 * $border, $leading];
1764-
}
17651780
}
17661781

17671782
return $layout;

‎tests/Mpdf/FormAppearanceTest.php‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,106 @@ public function testPdfxCheckboxesNeedNoCoreFont()
226226
$this->assertStringNotContainsString('/BaseFont /ZapfDingbats', $pdf);
227227
}
228228

229+
/**
230+
* A list box draws as many options as its size asks for, each whole and inside its own row, and nothing after
231+
* them
232+
*
233+
* @dataProvider listBoxes
234+
*
235+
* @param string $mode
236+
* @param int $rows the list box's size
237+
* @param string $style
238+
*/
239+
public function testListBoxShowsItsRowsWhole($mode, $rows, $style)
240+
{
241+
$options = '';
242+
foreach (['Apple', 'Banana', 'Cherry', 'Damson', 'Elderberry', 'Fig'] as $i => $option) {
243+
$options .= '<option value="' . $i . '">' . $option . ' gjpqy</option>';
244+
}
245+
246+
$mpdf = $this->mpdf(['mode' => $mode, 'useActiveForms' => true]);
247+
$mpdf->WriteHTML('<form><select name="s" size="' . $rows . '" multiple="multiple" style="' . $style . '">' . $options . '</select></form>');
248+
$desc = $mpdf->CurrentFont['desc'];
249+
$appearance = $this->choiceAppearance($this->output($mpdf));
250+
251+
$this->assertSame(1, preg_match('/([\d.]+) ([\d.]+) ([\d.]+) ([\d.]+) re W n BT \/F\d+ ([\d.]+) Tf/', $appearance, $clip));
252+
list(, , $bottom, , $height, $size) = $clip;
253+
$row = $height / $rows;
254+
255+
preg_match_all('/ 1 0 0 1 -?[\d.]+ (-?[\d.]+) Tm \(/', $appearance, $baselines);
256+
$this->assertCount($rows, $baselines[1]);
257+
foreach ($baselines[1] as $i => $baseline) {
258+
$rowBottom = $bottom + ($rows - 1 - $i) * $row;
259+
$this->assertGreaterThanOrEqual($rowBottom - 0.002, $baseline + $desc['Descent'] / 1000 * $size, 'Row ' . $i . ' reaches below its row');
260+
$this->assertLessThanOrEqual($rowBottom + $row + 0.002, $baseline + $desc['Ascent'] / 1000 * $size, 'Row ' . $i . ' reaches above its row');
261+
}
262+
}
263+
264+
/**
265+
* One, three and six rows, in a core and an embedded font, and in a size larger than the default
266+
*
267+
* @return mixed[][]
268+
*/
269+
public function listBoxes()
270+
{
271+
return [
272+
'one row' => ['utf-8', 1, ''],
273+
'three rows' => ['utf-8', 3, ''],
274+
'six rows, larger text' => ['utf-8', 6, 'font-size: 16pt'],
275+
'three rows, core font' => ['c', 3, ''],
276+
];
277+
}
278+
279+
/**
280+
* No choice field's appearance draws a drop-down arrow, and its text is clipped at the full width inside the
281+
* border. Interactive viewers draw a combo box's button themselves, so an arrow in the appearance shows twice
282+
*
283+
* @dataProvider choiceFields
284+
*
285+
* @param string $select
286+
*/
287+
public function testAChoiceFieldAppearanceHasNoDropDownArrow($select)
288+
{
289+
$appearance = $this->choiceAppearance($this->render('<form>' . $select . '</form>', $this->pdfa(true)));
290+
291+
$this->assertSame(1, preg_match('/ 0 0 ([\d.]+) [\d.]+ re f .*? ([\d.]+) [\d.]+ ([\d.]+) [\d.]+ re W n/', $appearance, $box));
292+
list(, $width, $border, $clipWidth) = $box;
293+
294+
$this->assertDoesNotMatchRegularExpression('/ [\d.]+ [\d.]+ m [\d.]+ [\d.]+ l [\d.]+ [\d.]+ l f /', $appearance);
295+
$this->assertEqualsWithDelta($width - 2 * $border, (float) $clipWidth, 0.002);
296+
}
297+
298+
/**
299+
* A combo box, and list boxes of one and several rows
300+
*
301+
* @return string[][]
302+
*/
303+
public function choiceFields()
304+
{
305+
$options = '<option value="1">One</option><option value="2">Two</option>';
306+
307+
return [
308+
'combo box' => ['<select name="s">' . $options . '</select>'],
309+
'one-row list box' => ['<select name="s" size="1" multiple="multiple">' . $options . '</select>'],
310+
'list box' => ['<select name="s" size="2">' . $options . '</select>'],
311+
];
312+
}
313+
314+
/**
315+
* The content of the appearance of a document's first choice field
316+
*
317+
* @param string $pdf
318+
*
319+
* @return string
320+
*/
321+
private function choiceAppearance($pdf)
322+
{
323+
$this->assertSame(1, preg_match('/\/FT \/Ch.*?\/AP << \/N (\d+) 0 R/s', $pdf, $ref));
324+
$this->assertSame(1, preg_match('/stream\n(.*)\nendstream/s', $this->object($pdf, $ref[1]), $stream));
325+
326+
return $stream[1];
327+
}
328+
229329
/**
230330
* Every kind of widget, with a script, a submit and a reset button, a button hidden from print and a hidden input
231331
*
-4 Bytes
Binary file not shown.
-4 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)