Document.php 11.0 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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
<?php 

namespace RtfHtmlPhp;

class Document
{
    /** @var string RTF string being parsed */
    protected $rtf;
    /** @var int Current position in RTF string */
    protected $pos;
    /** @var int Length of RTF string */
    protected $len;
    /** @var Group Current RTF group */
    protected $group;

    /** @var Group Root group */
    public $root = null;

    /**
     * Object contructor
     *
     * @param string $rtf The RTF content
     */
    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.
     *
     * @return 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.";
            throw new \Exception($err);
        }
    }

    /**
     * (Helper method) Is the current character a letter?
     *
     * @return bool
     */
    protected function isLetter()
    {
        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?
     *
     * @return bool
     */
    protected function isDigit()
    {
        return (ord($this->char) >= 48 && ord($this->char) <= 57);
    }

    /**
     * (Helper method) Is the current character end-of-line (EOL)?
     *
     * @return bool
     */
    protected function isEndOfLine()
    {
        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?
     *
     * @return bool
     */
    protected function isSpaceDelimiter()
    {
        return ($this->char == " " || $this->isEndOfLine());
    }

    /**
     * Store state of document on stack.
     *
     * @return void
     */
    protected function parseStartGroup()
    {
        $group = new Group();

        if ($this->group != null) {
            // Make the new group a child of the current group
            $group->parent = $this->group;

            array_push($this->group->children, $group);
            array_push($this->uc, end($this->uc));
        } else {
            // If there is no parent group, then set this group
            // as the root group.
            $this->root = $group;
            // Create uc stack and insert the first default value
            $this->uc = [1];
        }

        // Set the new group as the current group:
        $this->group = $group;
    }

    /**
     * Retrieve state of document from stack.
     *
     * @return void
     */
    protected function parseEndGroup()
    {
        $this->group = $this->group->parent;
        // Retrieve last uc value from stack
        array_pop($this->uc);
    }

    /**
     * Parse ControlWord element
     *
     * @return void
     */
    protected function parseControlWord()
    {
        // Read letters until a non-letter is reached.
        $word = '';
        $this->getChar();

        while ($this->isLetter()) {
            $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->isDigit()) {
            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->isSpaceDelimiter()) {
            $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 if it's an RTF scope delimiter
                    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);
    }

    /**
     * Parse ControlSymbol element
     *
     * @return void
     */
    protected function parseControlSymbol()
    {
        // Read symbol (one character only).
        $this->getChar();
        $symbol = $this->char;

        // Exceptional case:
        // Treat EOL symbols as \par control word
        if ($this->isEndOfLine()) {
            $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);
    }

    /**
     * Parse Control element
     *
     * @return void
     */
    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->isLetter()) {
            $this->parseControlWord();
        } else {
            $this->parseControlSymbol();
        }
    }

    /**
     * Parse Text element
     *
     * @return void
     */
    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) {
            throw new \Exception("Parse error: RTF text outside of group.");
        }

        // Add text as a child to the current group:
        array_push($this->group->children, $text);
    }

    /**
     * Attempt to parse an RTF string.
     *
     * @param string $rtf RTF content
     *
     * @return void
     */
    protected function parse($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;
            }
        }
    }

    /**
     * Returns string representation of the document for debug purposes.
     *
     * @return string
     */
    public function __toString()
    {
        if (!$this->root) {
            return "No root group";
        }

        return $this->root->toString();
    }
}