Document.php
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
declare (strict_types=1);
namespace SearchWP\Dependencies\RtfHtmlPhp;
class Document
{
private $rtf;
// RTF string being parsed
private $pos;
// Current position in RTF string
private $len;
// Length of RTF string
public $root = null;
// Root group
private $group;
// Current RTF group
public function __construct($rtf)
{
$this->Parse($rtf);
}
// Get the next character from the RTF stream.
// Parsing is aborted when reading beyond end of input string.
protected function GetChar()
{
$this->char = null;
if ($this->pos < \strlen($this->rtf)) {
$this->char = $this->rtf[$this->pos++];
} else {
$err = "Parse error: Tried to read past end of input; RTF is probably truncated.";
\trigger_error($err);
throw new \Exception($err);
}
}
/*
* (Helper method)
* Is the current character a letter?
*/
protected function is_letter() : bool
{
if (\ord($this->char) >= 65 && \ord($this->char) <= 90) {
return \true;
}
if (\ord($this->char) >= 97 && \ord($this->char) <= 122) {
return \true;
}
return \false;
}
/*
* (Helper method)
* Is the current character a digit?
*/
protected function is_digit() : bool
{
return \ord($this->char) >= 48 && \ord($this->char) <= 57;
}
/*
* (Helper method)
* Is the current character end-of-line (EOL)?
*/
protected function is_endofline()
{
if ($this->char == "\r" || $this->char == "\n") {
// Checks for a Windows/Acron type EOL
if ($this->rtf[$this->pos] == "\n" || $this->rtf[$this->pos] == "\r") {
$this->GetChar();
}
return \true;
}
return \false;
}
/*
* (Helper method)
* Is the current character for a space delimiter?
*/
protected function is_space_delimiter()
{
return $this->char == " " || $this->is_endofline();
}
// Store state of document on stack.
protected function ParseStartGroup()
{
$group = new Group();
// Is there a current group? Then make the new group its child:
if ($this->group != null) {
$group->parent = $this->group;
\array_push($this->group->children, $group);
\array_push($this->uc, \end($this->uc));
} else {
$this->root = $group;
// Create uc stack and insert the first default value
$this->uc = array(1);
}
// Set the new group as the current group:
$this->group = $group;
}
// Retrieve state of document from stack.
protected function ParseEndGroup()
{
$this->group = $this->group->parent;
// Retrieve last uc value from stack
\array_pop($this->uc);
}
protected function ParseControlWord()
{
// Read letters until a non-letter is reached.
$word = "";
$this->GetChar();
while ($this->is_letter()) {
$word .= $this->char;
$this->GetChar();
}
// Read parameter (if any) consisting of digits.
// Parameter may be negative, i.e., starting with a '-'
$parameter = null;
$negative = \false;
if ($this->char == '-') {
$this->GetChar();
$negative = \true;
}
while ($this->is_digit()) {
if ($parameter == null) {
$parameter = 0;
}
$parameter = $parameter * 10 + $this->char;
$this->GetChar();
}
// If no parameter present, assume control word's default (usually 1)
// If no default then assign 0 to the parameter
if ($parameter === null) {
$parameter = 1;
}
// Convert parameter to a negative number when applicable
if ($negative) {
$parameter = -$parameter;
}
// Update uc value
if ($word == "uc") {
\array_pop($this->uc);
$this->uc[] = $parameter;
}
// Skip space delimiter
if (!$this->is_space_delimiter()) {
$this->pos--;
}
// If this is \u, then the parameter will be followed
// by {$this->uc} characters.
if ($word == "u") {
// Convert parameter to unsigned decimal unicode
if ($negative) {
$parameter = 65536 + $parameter;
}
// Will ignore replacement characters $uc times
$uc = \end($this->uc);
while ($uc > 0) {
$this->GetChar();
// If the replacement character is encoded as
// hexadecimal value \'hh then jump over it
if ($this->char == '\\' && $this->rtf[$this->pos] == '\'') {
$this->pos = $this->pos + 3;
} elseif ($this->char == '{' || $this->char == '{') {
break;
}
// - To include an RTF delimiter in skippable data, it must be
// represented using the appropriate control symbol (that is,
// escaped with a backslash,) as in plain text.
//
// - Any RTF control word or symbol is considered a single character
// for the purposes of counting skippable characters. For this reason
// it's more appropriate to create a $skip flag and let the Parse()
// function take care of the skippable characters.
$uc--;
}
}
// Add new RTF word as a child to the current group.
$rtfword = new ControlWord();
$rtfword->word = $word;
$rtfword->parameter = $parameter;
\array_push($this->group->children, $rtfword);
}
protected function ParseControlSymbol()
{
// Read symbol (one character only).
$this->GetChar();
$symbol = $this->char;
// Exceptional case:
// Treat EOL symbols as \par control word
if ($this->is_endofline()) {
$rtfword = new ControlWord();
$rtfword->word = 'par';
$rtfword->parameter = 0;
\array_push($this->group->children, $rtfword);
return;
}
// Symbols ordinarily have no parameter. However,
// if this is \' (a single quote), then it is
// followed by a 2-digit hex-code:
$parameter = 0;
if ($symbol == '\'') {
$this->GetChar();
$parameter = $this->char;
$this->GetChar();
$parameter = \hexdec($parameter . $this->char);
}
// Add new control symbol as a child to the current group:
$rtfsymbol = new ControlSymbol();
$rtfsymbol->symbol = $symbol;
$rtfsymbol->parameter = $parameter;
\array_push($this->group->children, $rtfsymbol);
}
protected function ParseControl()
{
// Beginning of an RTF control word or control symbol.
// Look ahead by one character to see if it starts with
// a letter (control world) or another symbol (control symbol):
$this->GetChar();
$this->pos--;
// (go back after look-ahead)
if ($this->is_letter()) {
$this->ParseControlWord();
} else {
$this->ParseControlSymbol();
}
}
protected function ParseText()
{
// Parse plain text up to backslash or brace,
// unless escaped.
$text = "";
$terminate = \false;
do {
// Ignore EOL characters
if ($this->char == "\r" || $this->char == "\n") {
$this->GetChar();
continue;
}
// Is this an escape?
if ($this->char == '\\') {
// Perform lookahead to see if this
// is really an escape sequence.
$this->GetChar();
switch ($this->char) {
case '\\':
break;
case '{':
break;
case '}':
break;
default:
// Not an escape. Roll back.
$this->pos = $this->pos - 2;
$terminate = \true;
break;
}
} elseif ($this->char == '{' || $this->char == '}') {
$this->pos--;
$terminate = \true;
}
if (!$terminate) {
// Save plain text
$text .= $this->char;
$this->GetChar();
}
} while (!$terminate && $this->pos < $this->len);
// Create new Text element:
$text = new Text($text);
// If there is no current group, then this is not a valid RTF file.
// Throw an exception.
if ($this->group == null) {
$err = "Parse error: RTF text outside of group.";
\trigger_error($err);
throw new \Exception($err);
}
// Add text as a child to the current group:
\array_push($this->group->children, $text);
}
/*
* Attempt to parse an RTF string.
*/
protected function Parse(string $rtf)
{
$this->rtf = $rtf;
$this->pos = 0;
$this->len = \strlen($this->rtf);
$this->group = null;
$this->root = null;
while ($this->pos < $this->len - 1) {
// Read next character:
$this->GetChar();
// Ignore \r and \n
if ($this->char == "\n" || $this->char == "\r") {
continue;
}
// What type of character is this?
switch ($this->char) {
case '{':
$this->ParseStartGroup();
break;
case '}':
$this->ParseEndGroup();
break;
case '\\':
$this->ParseControl();
break;
default:
$this->ParseText();
break;
}
}
}
public function __toString()
{
if (!$this->root) {
return "No root group";
}
return $this->root->toString();
}
}