Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable auto font-size for active forms #1721

Merged
merged 2 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New features
* Service container for internal services
* Set /Lang entry for better accessibility when document language is available (@cuongmits, #1418)
* More verbose helper methods for `Output`: `OutputBinaryData`, `OutputHttpInline`, `OutputHttpDownload`, `OutputFile`
* Set font-size to `auto` in textarea and input in active forms to resize the font-size (@ChrisB9, #1721)

Bugfixes
--------
Expand Down
9 changes: 9 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ function print_ob_text($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
$js[] = ['K', $objattr['onKeystroke']];
}

if (!empty($objattr['use_auto_fontsize']) && $objattr['use_auto_fontsize'] === true) {
$this->mpdf->FontSizePt = 0.0;
}

$this->SetFormText($w, $h, $objattr['fieldname'], $val, $val, $objattr['title'], $flags, $fieldalign, false, (isset($objattr['maxlength']) ? $objattr['maxlength'] : false), $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));

} else {
Expand Down Expand Up @@ -319,6 +323,11 @@ function print_ob_textarea($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
if (!empty($objattr['onKeystroke'])) {
$js[] = ['K', $objattr['onKeystroke']];
}

if (!empty($objattr['use_auto_fontsize']) && $objattr['use_auto_fontsize'] === true) {
$this->mpdf->FontSizePt = 0.0;
}

$this->SetFormText($w, $h, $objattr['fieldname'], $texto, $texto, (isset($objattr['title']) ? $objattr['title'] : ''), $flags, $fieldalign, false, -1, $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));

Expand Down
7 changes: 6 additions & 1 deletion src/Tag/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function open($attr, &$ahtml, &$ihtml)
if (isset($properties['FONT-FAMILY'])) {
$this->mpdf->SetFont($properties['FONT-FAMILY'], $this->mpdf->FontStyle, 0, false);
}
if (isset($properties['FONT-SIZE'])) {
if (isset($properties['FONT-SIZE']) && $properties['FONT-SIZE'] !== 'auto') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, though, whether we should filter out font-size: 0 too, as it breaks the input and text-area as well.

Maybe like this:
0 = default, auto = /F 0

$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
$this->mpdf->SetFontSize($mmsize * Mpdf::SCALE, false);
}
Expand Down Expand Up @@ -356,6 +356,11 @@ public function open($attr, &$ahtml, &$ihtml)
if (strtoupper($attr['TYPE']) === 'PASSWORD') {
$type = 'PASSWORD';
}

if ($properties['FONT-SIZE'] === 'auto' && $this->mpdf->useActiveForms) {
$objattr['use_auto_fontsize'] = true;
}

if (isset($attr['VALUE'])) {
if ($type === 'PASSWORD') {
$num_stars = mb_strlen($attr['VALUE'], $this->mpdf->mb_enc);
Expand Down
6 changes: 5 additions & 1 deletion src/Tag/TextArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function open($attr, &$ahtml, &$ihtml)
if (isset($properties['FONT-FAMILY'])) {
$this->mpdf->SetFont($properties['FONT-FAMILY'], '', 0, false);
}
if (isset($properties['FONT-SIZE'])) {
if (isset($properties['FONT-SIZE']) && $properties['FONT-SIZE'] !== 'auto') {
$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
$this->mpdf->SetFontSize($mmsize * Mpdf::SCALE, false);
}
Expand Down Expand Up @@ -143,6 +143,10 @@ public function open($attr, &$ahtml, &$ihtml)
$objattr['rows'] = $rowsize;
$objattr['cols'] = $colsize;

if ($properties['FONT-SIZE'] === 'auto' && $this->mpdf->useActiveForms) {
$objattr['use_auto_fontsize'] = true;
}

$this->mpdf->specialcontent = serialize($objattr);

if ($this->mpdf->tableLevel) { // *TABLES*
Expand Down
49 changes: 49 additions & 0 deletions tests/Issues/Issue834Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Issues;

use Mpdf\BaseMpdfTest;
use Mpdf\Output\Destination;

final class Issue834Test extends BaseMpdfTest
{
public function testInputFormFontSizeAuto()
{
$this->mpdf->useActiveForms = true;
$this->mpdf->WriteHTML('<form><input type="text" name="test" style="font-size: auto" /></form>');

$output = $this->mpdf->Output('', Destination::STRING_RETURN);
$this->assertStringContainsString('/T (test)', $output);
$this->assertStringContainsString('/DA (/F2 0 Tf 0.000 g)', $output);
}

public function testTextareaFormFontSizeAuto()
{
$this->mpdf->useActiveForms = true;
$this->mpdf->WriteHTML('<form><textarea name="test" style="font-size: auto">&nbsp;</textarea></form>');

$output = $this->mpdf->Output('', Destination::STRING_RETURN);
$this->assertStringContainsString('/T (test)', $output);
$this->assertStringContainsString('/DA (/F2 0 Tf 0.000 g)', $output);
}

public function testInputFormFontSize10pt()
{
$this->mpdf->useActiveForms = true;
$this->mpdf->WriteHTML('<form><input type="text" name="test" style="font-size: 10pt" /></form>');

$output = $this->mpdf->Output('', Destination::STRING_RETURN);
$this->assertStringContainsString('/T (test)', $output);
$this->assertStringContainsString('/DA (/F2 10 Tf 0.000 g)', $output);
}

public function testTextareaFormFontSize10pt()
{
$this->mpdf->useActiveForms = true;
$this->mpdf->WriteHTML('<form><textarea name="test" style="font-size: 10pt">&nbsp;</textarea></form>');

$output = $this->mpdf->Output('', Destination::STRING_RETURN);
$this->assertStringContainsString('/T (test)', $output);
$this->assertStringContainsString('/DA (/F2 10 Tf 0.000 g)', $output);
}
}