From e766ba6c42def385f5db58e92092aa1bb4e260b7 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 29 Sep 2023 09:27:56 +0200 Subject: [PATCH 1/3] Font.php: Small workaround if Widths-key is not set in $details array --- src/Smalot/PdfParser/Font.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Smalot/PdfParser/Font.php b/src/Smalot/PdfParser/Font.php index b6207bb8..4394c99d 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -283,7 +283,9 @@ public function calculateTextWidth(string $text, array &$missing = null): ?float { $index_map = array_flip($this->table); $details = $this->getDetails(); - $widths = $details['Widths']; + + // TODO: Temporary fix, in the rare case the Widths-key is not set in $details array. + $widths = $details['Widths'] ?? []; // Widths array is zero indexed but table is not. We must map them based on FirstChar and LastChar $width_map = array_flip(range($details['FirstChar'], $details['LastChar'])); From 8dbfcd1f8c696db2052f6e8f95b84e88d7a79a08 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 29 Sep 2023 09:38:28 +0200 Subject: [PATCH 2/3] FontTest.php: add test to check behavior if Widths key is not set in $details array --- tests/PHPUnit/Integration/FontTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/PHPUnit/Integration/FontTest.php b/tests/PHPUnit/Integration/FontTest.php index c1aeabba..e76a051d 100644 --- a/tests/PHPUnit/Integration/FontTest.php +++ b/tests/PHPUnit/Integration/FontTest.php @@ -454,6 +454,29 @@ public function testCalculateTextWidth(): void $this->assertEquals([], $missing); } + /** + * Check behavior if getDetails() does return an array without a Widths-key. + * + * @see https://github.com/smalot/pdfparser/issues/619 + */ + public function testCalculateTextWidthNoWidthsKey(): void + { + $document = $this->createMock(Document::class); + + $header = $this->createMock(Header::class); + $header->method('getDetails')->willReturn([ + 'FirstChar' => '', + 'LastChar' => '', + // 'Widths' key is not set, so without the fix in Font.php a warning would be thrown. + ]); + + $font = new Font($document, $header); + $font->setTable([]); + $width = $font->calculateTextWidth('foo'); + + $this->assertNull($width); + } + /** * Check behavior if iconv function gets input which contains illegal characters. * From 468cb2031837bf880a796a29bb71a03ccb11702b Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Mon, 2 Oct 2023 13:23:24 +0200 Subject: [PATCH 3/3] Font.php: refined wording in comment --- src/Smalot/PdfParser/Font.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Smalot/PdfParser/Font.php b/src/Smalot/PdfParser/Font.php index 4394c99d..2e05cff5 100644 --- a/src/Smalot/PdfParser/Font.php +++ b/src/Smalot/PdfParser/Font.php @@ -284,7 +284,7 @@ public function calculateTextWidth(string $text, array &$missing = null): ?float $index_map = array_flip($this->table); $details = $this->getDetails(); - // TODO: Temporary fix, in the rare case the Widths-key is not set in $details array. + // Usually, Widths key is set in $details array, but if it isn't use an empty array instead. $widths = $details['Widths'] ?? []; // Widths array is zero indexed but table is not. We must map them based on FirstChar and LastChar