stringparser.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 710 行 · 第 1/2 页

PHP
710
字号
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @package    Zend_Pdf * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/new-bsd     New BSD License *//** Zend_Pdf_Element */require_once 'Zend/Pdf/Element.php';/** Zend_Pdf_Element_Array */require_once 'Zend/Pdf/Element/Array.php';/** Zend_Pdf_Element_String_Binary */require_once 'Zend/Pdf/Element/String/Binary.php';/** Zend_Pdf_Element_Boolean */require_once 'Zend/Pdf/Element/Boolean.php';/** Zend_Pdf_Element_Dictionary */require_once 'Zend/Pdf/Element/Dictionary.php';/** Zend_Pdf_Element_Name */require_once 'Zend/Pdf/Element/Name.php';/** Zend_Pdf_Element_Numeric */require_once 'Zend/Pdf/Element/Numeric.php';/** Zend_Pdf_Element_Object */require_once 'Zend/Pdf/Element/Object.php';/** Zend_Pdf_Element_Reference */require_once 'Zend/Pdf/Element/Reference.php';/** Zend_Pdf_Element_Object_Stream */require_once 'Zend/Pdf/Element/Object/Stream.php';/** Zend_Pdf_Element_String */require_once 'Zend/Pdf/Element/String.php';/** Zend_Pdf_Element_Null */require_once 'Zend/Pdf/Element/Null.php';/** Zend_Pdf_Element_Reference_Context */require_once 'Zend/Pdf/Element/Reference/Context.php';/** Zend_Pdf_Element_Reference_Table */require_once 'Zend/Pdf/Element/Reference/Table.php';/** Zend_Pdf_ElementFactory_Interface */require_once 'Zend/Pdf/ElementFactory/Interface.php';/** Zend_Pdf_PhpArray */require_once 'Zend/Pdf/PhpArray.php';/** * PDF string parser * * @package    Zend_Pdf * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/new-bsd     New BSD License */class Zend_Pdf_StringParser{    /**     * Source PDF     *     * @var string     */    public $data = '';    /**     * Current position in a data     *     * @var integer     */    public $offset = 0;    /**     * Current reference context     *     * @var Zend_Pdf_Element_Reference_Context     */    private $_context = null;    /**     * Array of elements of the currently parsed object/trailer     *     * @var array     */    private $_elements = array();    /**     * PDF objects factory.     *     * @var Zend_Pdf_ElementFactory_Interface     */    private $_objFactory = null;    /**     * Clean up resources.     *     * Clear current state to remove cyclic object references     */    public function cleanUp()    {        $this->_context = null;        $this->_elements = array();        $this->_objFactory = null;    }    /**     * Character with code $chCode is white space     *     * @param integer $chCode     * @return boolean     */    public static function isWhiteSpace($chCode)    {        if ($chCode == 0x00 || // null character            $chCode == 0x09 || // Tab            $chCode == 0x0A || // Line feed            $chCode == 0x0C || // Form Feed            $chCode == 0x0D || // Carriage return            $chCode == 0x20    // Space           ) {            return true;        } else {            return false;        }    }    /**     * Character with code $chCode is a delimiter character     *     * @param integer $chCode     * @return boolean     */    public static function isDelimiter($chCode )    {        if ($chCode == 0x28 || // '('            $chCode == 0x29 || // ')'            $chCode == 0x3C || // '<'            $chCode == 0x3E || // '>'            $chCode == 0x5B || // '['            $chCode == 0x5D || // ']'            $chCode == 0x7B || // '{'            $chCode == 0x7D || // '}'            $chCode == 0x2F || // '/'            $chCode == 0x25    // '%'           ) {            return true;        } else {            return false;        }    }    /**     * Skip white space     *     * @param boolean $skipComment     */    public function skipWhiteSpace($skipComment = true)    {        while ($this->offset < strlen($this->data)) {            if (self::isWhiteSpace( ord($this->data[$this->offset]) )) {                $this->offset++;            } else if (ord($this->data[$this->offset]) == 0x25 && $skipComment) { // '%'                $this->skipComment();            } else {                return;            }        }    }    /**     * Skip comment     */    public function skipComment()    {        while ($this->offset < strlen($this->data))        {            if (ord($this->data[$this->offset]) != 0x0A || // Line feed                ord($this->data[$this->offset]) != 0x0d    // Carriage return               ) {                $this->offset++;            } else {                return;            }        }    }    /**     * Read comment line     *     * @return string     */    public function readComment()    {        $this->skipWhiteSpace(false);        /** Check if it's a comment line */        if ($this->data[$this->offset] != '%') {            return '';        }        for ($start = $this->offset;             $this->offset < strlen($this->data);             $this->offset++) {            if (ord($this->data[$this->offset]) == 0x0A || // Line feed                ord($this->data[$this->offset]) == 0x0d    // Carriage return               ) {                break;            }        }        return substr($this->data, $start, $this->offset-$start);    }    /**     * Returns next lexeme from a pdf stream     *     * @return string     */    public function readLexeme()    {        $this->skipWhiteSpace();        if ($this->offset >= strlen($this->data)) {            return '';        }        $start = $this->offset;        if (self::isDelimiter( ord($this->data[$start]) )) {            if ($this->data[$start] == '<' && $this->offset + 1 < strlen($this->data) && $this->data[$start+1] == '<') {                $this->offset += 2;                return '<<';            } else if ($this->data[$start] == '>' && $this->offset + 1 < strlen($this->data) && $this->data[$start+1] == '>') {                $this->offset += 2;                return '>>';            } else {                $this->offset++;                return $this->data[$start];            }        } else {            while ( ($this->offset < strlen($this->data)) &&                    (!self::isDelimiter(  ord($this->data[$this->offset]) )) &&                    (!self::isWhiteSpace( ord($this->data[$this->offset]) ))   ) {                $this->offset++;            }            return substr($this->data, $start, $this->offset - $start);        }    }    /**     * Read elemental object from a PDF stream     *     * @return Zend_Pdf_Element     * @throws Zend_Pdf_Exception     */    public function readElement($nextLexeme = null)    {        if ($nextLexeme === null) {            $nextLexeme = $this->readLexeme();        }        /**         * Note: readElement() method is a public method and could be invoked from other classes.         * If readElement() is used not by Zend_Pdf_StringParser::getObject() method, then we should not care         * about _elements member management.         */        switch ($nextLexeme) {            case '(':                return ($this->_elements[] = $this->_readString());            case '<':                return ($this->_elements[] = $this->_readBinaryString());            case '/':                return ($this->_elements[] = new Zend_Pdf_Element_Name(                                                Zend_Pdf_Element_Name::unescape( $this->readLexeme() )                                                                      ));            case '[':                return ($this->_elements[] = $this->_readArray());            case '<<':                return ($this->_elements[] = $this->_readDictionary());            case ')':                // fall through to next case            case '>':                // fall through to next case            case ']':                // fall through to next case            case '>>':                // fall through to next case            case '{':                // fall through to next case            case '}':                throw new Zend_Pdf_Exception(sprintf('PDF file syntax error. Offset - 0x%X.',                                                $this->offset));            default:                if (strcasecmp($nextLexeme, 'true') == 0) {                    return ($this->_elements[] = new Zend_Pdf_Element_Boolean(true));                } else if (strcasecmp($nextLexeme, 'false') == 0) {                    return ($this->_elements[] = new Zend_Pdf_Element_Boolean(false));                } else if (strcasecmp($nextLexeme, 'null') == 0) {                    return ($this->_elements[] = new Zend_Pdf_Element_Null());                }                $ref = $this->_readReference($nextLexeme);                if ($ref !== null) {                    return ($this->_elements[] = $ref);                }                return ($this->_elements[] = $this->_readNumeric($nextLexeme));        }    }    /**     * Read string PDF object     * Also reads trailing ')' from a pdf stream     *     * @return Zend_Pdf_Element_String     * @throws Zend_Pdf_Exception     */    private function _readString()    {        $start = $this->offset;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?