Test Failed
Push — fix/replace-pr-403-getFontSpac... ( 226700 )
by Konrad
03:17
created

PDFObjectTest::testGetFontSpaceLimitOnNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 * @date    2020-06-01
8
 *
9
 * @author  Sébastien MALOT <[email protected]>
10
 * @date    2017-01-03
11
 *
12
 * @license LGPLv3
13
 * @url     <https://github.com/smalot/pdfparser>
14
 *
15
 *  PdfParser is a pdf library written in PHP, extraction oriented.
16
 *  Copyright (C) 2017 - Sébastien MALOT <[email protected]>
17
 *
18
 *  This program is free software: you can redistribute it and/or modify
19
 *  it under the terms of the GNU Lesser General Public License as published by
20
 *  the Free Software Foundation, either version 3 of the License, or
21
 *  (at your option) any later version.
22
 *
23
 *  This program is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU Lesser General Public License for more details.
27
 *
28
 *  You should have received a copy of the GNU Lesser General Public License
29
 *  along with this program.
30
 *  If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
31
 */
32
33
namespace Tests\Smalot\PdfParser\Integration;
34
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Parser;
37
use Smalot\PdfParser\PDFObject;
38
use Tests\Smalot\PdfParser\TestCase;
39
40
class PDFObjectTest extends TestCase
41
{
42
    const TYPE = 't';
43
44
    const OPERATOR = 'o';
45
46
    const COMMAND = 'c';
47
48
    protected function getPdfObjectInstance($document)
49
    {
50
        return new PDFObject($document);
51
    }
52
53
    public function testGetCommandsText()
54
    {
55
        $content = "/R14 30 Tf 0.999016 0 0 1 137.4
56
342.561 Tm
57
[(A)-168.854( BC D)-220.905(\\(E\\))20.905<20>]
58
TJ /R14 17.16 Tf <20> Tj
59
0.999014 0 0 1 336.84 319.161 Tm T* ( \x00m)Tj
60
/R14 20.04 Tf
61
ET Q
62
q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
63
BI";
64
65
        $offset = 0;
66
        $parts = $this->getPdfObjectInstance(new Document())->getCommandsText($content, $offset);
67
        $reference = [
68
            [
69
                self::TYPE => '/',
70
                self::OPERATOR => 'Tf',
71
                self::COMMAND => 'R14 30',
72
            ],
73
            [
74
                self::TYPE => '',
75
                self::OPERATOR => 'Tm',
76
                self::COMMAND => "0.999016 0 0 1 137.4\n342.561",
77
            ],
78
            [
79
                self::TYPE => '[',
80
                self::OPERATOR => 'TJ',
81
                self::COMMAND => [
82
                    [
83
                        self::TYPE => '(',
84
                        self::OPERATOR => '',
85
                        self::COMMAND => 'A',
86
                    ],
87
                    [
88
                        self::TYPE => 'n',
89
                        self::OPERATOR => '',
90
                        self::COMMAND => '-168.854',
91
                    ],
92
                    [
93
                        self::TYPE => '(',
94
                        self::OPERATOR => '',
95
                        self::COMMAND => ' BC D',
96
                    ],
97
                    [
98
                        self::TYPE => 'n',
99
                        self::OPERATOR => '',
100
                        self::COMMAND => '-220.905',
101
                    ],
102
                    [
103
                        self::TYPE => '(',
104
                        self::OPERATOR => '',
105
                        self::COMMAND => '\\(E\\)',
106
                    ],
107
                    [
108
                        self::TYPE => 'n',
109
                        self::OPERATOR => '',
110
                        self::COMMAND => '20.905',
111
                    ],
112
                    [
113
                        self::TYPE => '<',
114
                        self::OPERATOR => '',
115
                        self::COMMAND => '20',
116
                    ],
117
                ],
118
            ],
119
            [
120
                self::TYPE => '/',
121
                self::OPERATOR => 'Tf',
122
                self::COMMAND => 'R14 17.16',
123
            ],
124
            [
125
                self::TYPE => '<',
126
                self::OPERATOR => 'Tj',
127
                self::COMMAND => '20',
128
            ],
129
            [
130
                self::TYPE => '',
131
                self::OPERATOR => 'Tm',
132
                self::COMMAND => '0.999014 0 0 1 336.84 319.161',
133
            ],
134
            [
135
                self::TYPE => '',
136
                self::OPERATOR => 'T*',
137
                self::COMMAND => '',
138
            ],
139
            [
140
                self::TYPE => '(',
141
                self::OPERATOR => 'Tj',
142
                self::COMMAND => " \x00m",
143
            ],
144
            [
145
                self::TYPE => '/',
146
                self::OPERATOR => 'Tf',
147
                self::COMMAND => 'R14 20.04',
148
            ],
149
        ];
150
151
        $this->assertEquals($parts, $reference);
152
        $this->assertEquals(172, $offset);
153
    }
154
155
    public function testCleanContent()
156
    {
157
        $content = '/Shape <</MCID << /Font<8>>> BT >>BDC
158
Q
159
/CS0 cs 1 1 0  scn
160
1 i
161
/GS0 gs
162
BT
163
/TT0 1 Tf
164
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
165
(Modificatio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
166
EMC
167
(ABC) Tj
168
169
[ (a)-4.5(b)6(c)8.8 ( fsdfsdfsdf[]sd) ] TD
170
171
ET
172
/Shape <</MCID 2 >>BDC
173
q
174
0.03 841';
175
176
        $expected = '_____________________________________
177
Q
178
/CS0 cs 1 1 0  scn
179
1 i
180
/GS0 gs
181
BT
182
/TT0 1 Tf
183
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
184
(________________________________________________)Tj
185
___
186
(___) Tj
187
188
[_____________________________________] TD
189
190
ET
191
______________________
192
q
193
0.03 841';
194
195
        $cleaned = $this->getPdfObjectInstance(new Document())->cleanContent($content, '_');
196
197
        $this->assertEquals($cleaned, $expected);
198
    }
199
200
    public function testGetSectionText()
201
    {
202
        $content = '/Shape <</MCID 1 >>BDC
203
Q
204
/CS0 cs 1 1 0  scn
205
1 i
206
/GS0 gs
207
BT
208
/TT0 1 Tf
209
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
210
(Mod BT atio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
211
EMC
212
(ABC) Tj
213
214
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD
215
216
ET
217
/Shape <</MCID [BT] >>BDC BT /TT1 1.5 Tf (BT )Tj ET
218
q
219
0.03 841';
220
221
        $sections = $this->getPdfObjectInstance(new Document())->getSectionsText($content);
222
223
        $this->assertEquals(
224
            ['/TT0 1 Tf
225
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
226
(Mod BT atio[ns] au \(14\) septembre 2009 ET 2010)Tj
227
EMC
228
(ABC) Tj
229
230
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD', '/TT1 1.5 Tf (BT )Tj
231
q'],
232
            $sections
233
        );
234
    }
235
236
    public function testGetFontSpaceLimitOnNull()
237
    {
238
        $parser = new Parser();
239
240
        $filename = __DIR__.'/../../samples/bugs/pr_403.pdf';
241
        $pdf = $parser->parseFile($filename);
242
        $pages = $pdf->getPages();
243
244
        foreach ($pages as $index => $page)
245
        {
246
            $text = $page->getTextXY(181.56, 816.95, 5); //<- throw Call to a member function getFontSpaceLimit() on null
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
247
        }
248
    }
249
}
250