Passed
Push — fix/replace-pr-403-getFontSpac... ( 933b8a...86462a )
by Konrad
02:41
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\PDFObject;
37
use Tests\Smalot\PdfParser\TestCase;
38
39
class PDFObjectTest extends TestCase
40
{
41
    const TYPE = 't';
42
43
    const OPERATOR = 'o';
44
45
    const COMMAND = 'c';
46
47
    protected function getPdfObjectInstance($document)
48
    {
49
        return new PDFObject($document);
50
    }
51
52
    public function testGetCommandsText()
53
    {
54
        $content = "/R14 30 Tf 0.999016 0 0 1 137.4
55
342.561 Tm
56
[(A)-168.854( BC D)-220.905(\\(E\\))20.905<20>]
57
TJ /R14 17.16 Tf <20> Tj
58
0.999014 0 0 1 336.84 319.161 Tm T* ( \x00m)Tj
59
/R14 20.04 Tf
60
ET Q
61
q -124.774 124.127 5.64213 5.67154 930.307 4436.95 cm
62
BI";
63
64
        $offset = 0;
65
        $parts = $this->getPdfObjectInstance(new Document())->getCommandsText($content, $offset);
66
        $reference = [
67
            [
68
                self::TYPE => '/',
69
                self::OPERATOR => 'Tf',
70
                self::COMMAND => 'R14 30',
71
            ],
72
            [
73
                self::TYPE => '',
74
                self::OPERATOR => 'Tm',
75
                self::COMMAND => "0.999016 0 0 1 137.4\n342.561",
76
            ],
77
            [
78
                self::TYPE => '[',
79
                self::OPERATOR => 'TJ',
80
                self::COMMAND => [
81
                    [
82
                        self::TYPE => '(',
83
                        self::OPERATOR => '',
84
                        self::COMMAND => 'A',
85
                    ],
86
                    [
87
                        self::TYPE => 'n',
88
                        self::OPERATOR => '',
89
                        self::COMMAND => '-168.854',
90
                    ],
91
                    [
92
                        self::TYPE => '(',
93
                        self::OPERATOR => '',
94
                        self::COMMAND => ' BC D',
95
                    ],
96
                    [
97
                        self::TYPE => 'n',
98
                        self::OPERATOR => '',
99
                        self::COMMAND => '-220.905',
100
                    ],
101
                    [
102
                        self::TYPE => '(',
103
                        self::OPERATOR => '',
104
                        self::COMMAND => '\\(E\\)',
105
                    ],
106
                    [
107
                        self::TYPE => 'n',
108
                        self::OPERATOR => '',
109
                        self::COMMAND => '20.905',
110
                    ],
111
                    [
112
                        self::TYPE => '<',
113
                        self::OPERATOR => '',
114
                        self::COMMAND => '20',
115
                    ],
116
                ],
117
            ],
118
            [
119
                self::TYPE => '/',
120
                self::OPERATOR => 'Tf',
121
                self::COMMAND => 'R14 17.16',
122
            ],
123
            [
124
                self::TYPE => '<',
125
                self::OPERATOR => 'Tj',
126
                self::COMMAND => '20',
127
            ],
128
            [
129
                self::TYPE => '',
130
                self::OPERATOR => 'Tm',
131
                self::COMMAND => '0.999014 0 0 1 336.84 319.161',
132
            ],
133
            [
134
                self::TYPE => '',
135
                self::OPERATOR => 'T*',
136
                self::COMMAND => '',
137
            ],
138
            [
139
                self::TYPE => '(',
140
                self::OPERATOR => 'Tj',
141
                self::COMMAND => " \x00m",
142
            ],
143
            [
144
                self::TYPE => '/',
145
                self::OPERATOR => 'Tf',
146
                self::COMMAND => 'R14 20.04',
147
            ],
148
        ];
149
150
        $this->assertEquals($parts, $reference);
151
        $this->assertEquals(172, $offset);
152
    }
153
154
    public function testCleanContent()
155
    {
156
        $content = '/Shape <</MCID << /Font<8>>> BT >>BDC
157
Q
158
/CS0 cs 1 1 0  scn
159
1 i
160
/GS0 gs
161
BT
162
/TT0 1 Tf
163
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
164
(Modificatio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
165
EMC
166
(ABC) Tj
167
168
[ (a)-4.5(b)6(c)8.8 ( fsdfsdfsdf[]sd) ] TD
169
170
ET
171
/Shape <</MCID 2 >>BDC
172
q
173
0.03 841';
174
175
        $expected = '_____________________________________
176
Q
177
/CS0 cs 1 1 0  scn
178
1 i
179
/GS0 gs
180
BT
181
/TT0 1 Tf
182
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
183
(________________________________________________)Tj
184
___
185
(___) Tj
186
187
[_____________________________________] TD
188
189
ET
190
______________________
191
q
192
0.03 841';
193
194
        $cleaned = $this->getPdfObjectInstance(new Document())->cleanContent($content, '_');
195
196
        $this->assertEquals($cleaned, $expected);
197
    }
198
199
    public function testGetSectionText()
200
    {
201
        $content = '/Shape <</MCID 1 >>BDC
202
Q
203
/CS0 cs 1 1 0  scn
204
1 i
205
/GS0 gs
206
BT
207
/TT0 1 Tf
208
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
209
(Mod BT atio[ns] au \\(14\\) septembre 2009 ET 2010)Tj
210
EMC
211
(ABC) Tj
212
213
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD
214
215
ET
216
/Shape <</MCID [BT] >>BDC BT /TT1 1.5 Tf (BT )Tj ET
217
q
218
0.03 841';
219
220
        $sections = $this->getPdfObjectInstance(new Document())->getSectionsText($content);
221
222
        $this->assertEquals(
223
            ['/TT0 1 Tf
224
0.0007 Tc 0.0018 Tw 0  Ts 100  Tz 0 Tr 24 0 0 24 51.3 639.26025 Tm
225
(Mod BT atio[ns] au \(14\) septembre 2009 ET 2010)Tj
226
EMC
227
(ABC) Tj
228
229
[ (a)-4.5(b) 6(c)8.8 ( fsdfsdfsdf[ sd) ] TD', '/TT1 1.5 Tf (BT )Tj
230
q'],
231
            $sections
232
        );
233
    }
234
}
235