|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* This file is part of the PdfParser library. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Sébastien MALOT <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @date 2017-01-03 |
|
10
|
|
|
* |
|
11
|
|
|
* @license LGPLv3 |
|
12
|
|
|
* |
|
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 Smalot\PdfParser\Element; |
|
34
|
|
|
|
|
35
|
|
|
use Smalot\PdfParser\Document; |
|
36
|
|
|
use Smalot\PdfParser\Element; |
|
37
|
|
|
use Smalot\PdfParser\Font; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Class ElementString |
|
41
|
|
|
*/ |
|
42
|
|
|
class ElementString extends Element |
|
43
|
|
|
{ |
|
44
|
78 |
|
public function __construct($value) |
|
45
|
|
|
{ |
|
46
|
78 |
|
parent::__construct($value, null); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function equals($value): bool |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $value == $this->value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool|ElementString |
|
56
|
|
|
*/ |
|
57
|
71 |
|
public static function parse(string $content, ?Document $document = null, int &$offset = 0) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
71 |
|
if (preg_match('/^\s*\((?P<name>.*)/s', $content, $match)) { |
|
60
|
68 |
|
$name = $match['name']; |
|
61
|
|
|
|
|
62
|
68 |
|
$delimiterCount = 0; |
|
63
|
68 |
|
$position = 0; |
|
64
|
68 |
|
$processedName = ''; |
|
65
|
|
|
do { |
|
66
|
68 |
|
$char = substr($name, 0, 1); |
|
67
|
68 |
|
$name = substr($name, 1); |
|
68
|
68 |
|
++$position; |
|
69
|
|
|
switch ($char) { |
|
70
|
|
|
// matched delimiters should be treated as part of string |
|
71
|
68 |
|
case '(': |
|
72
|
4 |
|
$processedName .= $char; |
|
73
|
4 |
|
++$delimiterCount; |
|
74
|
4 |
|
break; |
|
75
|
68 |
|
case ')': |
|
76
|
68 |
|
if (0 === $delimiterCount) { |
|
77
|
68 |
|
$name = substr($name, 1); |
|
|
|
|
|
|
78
|
68 |
|
break 2; |
|
79
|
|
|
} |
|
80
|
4 |
|
$processedName .= $char; |
|
81
|
4 |
|
--$delimiterCount; |
|
82
|
4 |
|
break; |
|
83
|
|
|
// escaped chars |
|
84
|
68 |
|
case '\\': |
|
85
|
33 |
|
$nextChar = substr($name, 0, 1); |
|
86
|
|
|
switch ($nextChar) { |
|
87
|
|
|
// end-of-line markers (CR, LF, CRLF) should be ignored |
|
88
|
33 |
|
case "\r": |
|
89
|
31 |
|
case "\n": |
|
90
|
2 |
|
preg_match('/^\\r?\\n?/', $name, $matches); |
|
91
|
2 |
|
$name = substr($name, \strlen($matches[0])); |
|
92
|
2 |
|
$position += \strlen($matches[0]); |
|
93
|
2 |
|
break; |
|
94
|
|
|
// process LF, CR, HT, BS, FF |
|
95
|
31 |
|
case 'n': |
|
96
|
31 |
|
case 't': |
|
97
|
31 |
|
case 'r': |
|
98
|
30 |
|
case 'b': |
|
99
|
30 |
|
case 'f': |
|
100
|
1 |
|
$processedName .= stripcslashes('\\'.$nextChar); |
|
101
|
1 |
|
$name = substr($name, 1); |
|
102
|
1 |
|
++$position; |
|
103
|
1 |
|
break; |
|
104
|
|
|
// decode escaped parentheses and backslash |
|
105
|
30 |
|
case '(': |
|
106
|
30 |
|
case ')': |
|
107
|
20 |
|
case '\\': |
|
108
|
19 |
|
case ' ': // TODO: this should probably be removed - kept for compatibility |
|
109
|
14 |
|
$processedName .= $nextChar; |
|
110
|
14 |
|
$name = substr($name, 1); |
|
111
|
14 |
|
++$position; |
|
112
|
14 |
|
break; |
|
113
|
|
|
// TODO: process octal encoding (but it is also processed later) |
|
114
|
|
|
// keep backslash in other cases |
|
115
|
|
|
default: |
|
116
|
19 |
|
$processedName .= $char; |
|
117
|
|
|
} |
|
118
|
33 |
|
break; |
|
119
|
|
|
default: |
|
120
|
68 |
|
$processedName .= $char; |
|
121
|
|
|
} |
|
122
|
68 |
|
} while (\strlen($name)); |
|
123
|
|
|
|
|
124
|
68 |
|
$offset += strpos($content, '(') + 1 + $position; |
|
125
|
|
|
|
|
126
|
68 |
|
$name = $processedName; |
|
127
|
|
|
|
|
128
|
|
|
// Decode string. |
|
129
|
68 |
|
$name = Font::decodeOctal($name); |
|
130
|
68 |
|
$name = Font::decodeEntities($name); |
|
131
|
68 |
|
$name = Font::decodeHexadecimal($name, false); |
|
132
|
68 |
|
$name = Font::decodeUnicode($name); |
|
133
|
|
|
|
|
134
|
68 |
|
return new self($name); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
21 |
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.