Skip to content

Commit 111e945

Browse files
committed
Remove rounding, fix unit tests and add new unit test
1 parent 9a9acba commit 111e945

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed
Binary file not shown.

src/Smalot/PdfParser/Page.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,12 @@ public function getDataTm(?array $dataCommands = null): array
818818
case 'Tm':
819819
$Tm = explode(' ', $command['c']);
820820
$TempMatrix = [];
821-
$TempMatrix[0] = round((float) $Tm[0] * (float) $concatTm[0] + (float) $Tm[1] * (float) $concatTm[2], 6);
822-
$TempMatrix[1] = round((float) $Tm[0] * (float) $concatTm[1] + (float) $Tm[1] * (float) $concatTm[3], 6);
823-
$TempMatrix[2] = round((float) $Tm[2] * (float) $concatTm[0] + (float) $Tm[3] * (float) $concatTm[2], 6);
824-
$TempMatrix[3] = round((float) $Tm[2] * (float) $concatTm[1] + (float) $Tm[3] * (float) $concatTm[3], 6);
825-
$TempMatrix[4] = round((float) $Tm[4] * (float) $concatTm[0] + (float) $Tm[5] * (float) $concatTm[2] + (float) $concatTm[4], 2);
826-
$TempMatrix[5] = round((float) $Tm[4] * (float) $concatTm[1] + (float) $Tm[5] * (float) $concatTm[3] + (float) $concatTm[5], 2);
821+
$TempMatrix[0] = (float) $Tm[0] * (float) $concatTm[0] + (float) $Tm[1] * (float) $concatTm[2];
822+
$TempMatrix[1] = (float) $Tm[0] * (float) $concatTm[1] + (float) $Tm[1] * (float) $concatTm[3];
823+
$TempMatrix[2] = (float) $Tm[2] * (float) $concatTm[0] + (float) $Tm[3] * (float) $concatTm[2];
824+
$TempMatrix[3] = (float) $Tm[2] * (float) $concatTm[1] + (float) $Tm[3] * (float) $concatTm[3];
825+
$TempMatrix[4] = (float) $Tm[4] * (float) $concatTm[0] + (float) $Tm[5] * (float) $concatTm[2] + (float) $concatTm[4];
826+
$TempMatrix[5] = (float) $Tm[4] * (float) $concatTm[1] + (float) $Tm[5] * (float) $concatTm[3] + (float) $concatTm[5];
827827
$Tm = $TempMatrix;
828828
$Tx = (float) $Tm[$x];
829829
$Ty = (float) $Tm[$y];

tests/PHPUnit/Integration/PageTest.php

+79-10
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ public function testGetDataTm(): void
307307
'201.96',
308308
'720.68',
309309
],
310-
$item[0]
310+
[
311+
round($item[0][0], 6),
312+
round($item[0][1], 6),
313+
round($item[0][2], 6),
314+
round($item[0][3], 6),
315+
round($item[0][4], 2),
316+
round($item[0][5], 2)
317+
]
311318
);
312319
$this->assertStringContainsString('Document title', $item[1]);
313320

@@ -321,7 +328,14 @@ public function testGetDataTm(): void
321328
'70.8',
322329
'673.64',
323330
],
324-
$item[0]
331+
[
332+
round($item[0][0], 6),
333+
round($item[0][1], 6),
334+
round($item[0][2], 6),
335+
round($item[0][3], 6),
336+
round($item[0][4], 2),
337+
round($item[0][5], 2)
338+
]
325339
);
326340
$this->assertStringContainsString('Calibri : Lorem ipsum dolor sit amet, consectetur a', $item[1]);
327341

@@ -332,10 +346,17 @@ public function testGetDataTm(): void
332346
'0',
333347
'0',
334348
'1',
335-
'342.840222606',
349+
'342.84',
336350
'81.44',
337351
],
338-
$item[0]
352+
[
353+
round($item[0][0], 6),
354+
round($item[0][1], 6),
355+
round($item[0][2], 6),
356+
round($item[0][3], 6),
357+
round($item[0][4], 2),
358+
round($item[0][5], 2)
359+
]
339360
);
340361
$this->assertStringContainsString('nenatis.', $item[1]);
341362

@@ -626,7 +647,7 @@ public function testGetTextXY(): void
626647
$document = $parser->parseFile($filename);
627648
$pages = $document->getPages();
628649
$page = $pages[0];
629-
$result = $page->getTextXY(201.96, 720.68);
650+
$result = $page->getTextXY(201.96, 720.68, 0.01, 0.01);
630651
$this->assertCount(1, $result);
631652
$this->assertCount(2, $result[0]);
632653
$this->assertEquals(
@@ -638,7 +659,14 @@ public function testGetTextXY(): void
638659
'201.96',
639660
'720.68',
640661
],
641-
$result[0][0]
662+
[
663+
round($result[0][0][0], 6),
664+
round($result[0][0][1], 6),
665+
round($result[0][0][2], 6),
666+
round($result[0][0][3], 6),
667+
round($result[0][0][4], 2),
668+
round($result[0][0][5], 2),
669+
]
642670
);
643671
$this->assertStringContainsString('Document title', $result[0][1]);
644672

@@ -657,7 +685,14 @@ public function testGetTextXY(): void
657685
'201.96',
658686
'720.68',
659687
],
660-
$result[0][0]
688+
[
689+
round($result[0][0][0], 6),
690+
round($result[0][0][1], 6),
691+
round($result[0][0][2], 6),
692+
round($result[0][0][3], 6),
693+
round($result[0][0][4], 2),
694+
round($result[0][0][5], 2),
695+
]
661696
);
662697
$this->assertStringContainsString('Document title', $result[0][1]);
663698

@@ -827,10 +862,10 @@ public function testIssue454(): void
827862
$this->assertEquals(2, \count($dataTm[0]));
828863
$this->assertIsArray($dataTm[0][0]);
829864
$this->assertEquals(6, \count($dataTm[0][0]));
830-
$this->assertEquals(201.96, $dataTm[0][0][4]);
831-
$this->assertEquals(720.68, $dataTm[0][0][5]);
865+
$this->assertEquals(201.96, round($dataTm[0][0][4], 2));
866+
$this->assertEquals(720.68, round($dataTm[0][0][5], 2));
832867
$this->assertStringContainsString('Document title', $dataTm[0][1]);
833-
$textData = $page->getTextXY(201.96, 720.68);
868+
$textData = $page->getTextXY(201.96, 720.68, 0.01, 0.01);
834869
$this->assertStringContainsString('Document title', $textData[0][1]);
835870
$page = $pages[2];
836871
$dataTm = $page->getDataTm();
@@ -889,4 +924,38 @@ public function testIssue629WithoutDataTmFontInfo(): void
889924
$this->assertCount(2, $dataTm[0]);
890925
$this->assertFalse(isset($dataTm[0][2]));
891926
}
927+
928+
public function testCmCommandInPdfs() : void
929+
{
930+
$config = new Config();
931+
$parser = $this->getParserInstance($config);
932+
$filename = $this->rootDir.'/samples/Document-Word-Landscape-printedaspdf.pdf';
933+
$document = $parser->parseFile($filename);
934+
$pages = $document->getPages();
935+
$page = $pages[0];
936+
$dataTm = $page->getDataTm();
937+
$item = $dataTm[2];
938+
$this->assertCount(6, $dataTm);
939+
$this->assertCount(2, $item);
940+
$this->assertCount(6, $item[0]);
941+
$this->assertEquals('This is just a test', trim($item[1]));
942+
$this->assertEquals(
943+
[
944+
'0.75',
945+
'0.0',
946+
'0.0',
947+
'0.75',
948+
'59.16',
949+
'500.4'
950+
],
951+
[
952+
round($item[0][0], 6),
953+
round($item[0][1], 6),
954+
round($item[0][2], 6),
955+
round($item[0][3], 6),
956+
round($item[0][4], 2),
957+
round($item[0][5], 2)
958+
]
959+
);
960+
}
892961
}

0 commit comments

Comments
 (0)