PageRenderTime 232ms CodeModel.GetById 91ms app.highlight 6ms RepoModel.GetById 89ms app.codeStats 0ms

/library/Zend/Pdf/Element/String.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 246 lines | 136 code | 32 blank | 78 comment | 12 complexity | 2e722a50e951337974c2d5db8c59c549 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Pdf
 17 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 */
 20
 21
 22/** Zend_Pdf_Element */
 23require_once 'Zend/Pdf/Element.php';
 24
 25
 26/**
 27 * PDF file 'string' element implementation
 28 *
 29 * @category   Zend
 30 * @package    Zend_Pdf
 31 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 33 */
 34class Zend_Pdf_Element_String extends Zend_Pdf_Element
 35{
 36    /**
 37     * Object value
 38     *
 39     * @var string
 40     */
 41    public $value;
 42
 43    /**
 44     * Object constructor
 45     *
 46     * @param string $val
 47     */
 48    public function __construct($val)
 49    {
 50        $this->value   = (string)$val;
 51    }
 52
 53
 54    /**
 55     * Return type of the element.
 56     *
 57     * @return integer
 58     */
 59    public function getType()
 60    {
 61        return Zend_Pdf_Element::TYPE_STRING;
 62    }
 63
 64
 65    /**
 66     * Return object as string
 67     *
 68     * @param Zend_Pdf_Factory $factory
 69     * @return string
 70     */
 71    public function toString($factory = null)
 72    {
 73        return '(' . self::escape((string)$this->value) . ')';
 74    }
 75
 76
 77    /**
 78     * Escape string according to the PDF rules
 79     *
 80     * @param string $inStr
 81     * @return string
 82     */
 83    public static function escape($inStr)
 84    {
 85        $outStr = '';
 86        $lastNL = 0;
 87
 88        for ($count = 0; $count < strlen($inStr); $count++) {
 89            if (strlen($outStr) - $lastNL > 128)  {
 90                $outStr .= "\\\n";
 91                $lastNL = strlen($outStr);
 92            }
 93
 94            $nextCode = ord($inStr[$count]);
 95            switch ($nextCode) {
 96                // "\n" - line feed (LF)
 97                case 10:
 98                    $outStr .= '\\n';
 99                    break;
100
101                // "\r" - carriage return (CR)
102                case 13:
103                    $outStr .= '\\r';
104                    break;
105
106                // "\t" - horizontal tab (HT)
107                case 9:
108                    $outStr .= '\\t';
109                    break;
110
111                // "\b" - backspace (BS)
112                case 8:
113                    $outStr .= '\\b';
114                    break;
115
116                // "\f" - form feed (FF)
117                case 12:
118                    $outStr .= '\\f';
119                    break;
120
121                // '(' - left paranthesis
122                case 40:
123                    $outStr .= '\\(';
124                    break;
125
126                // ')' - right paranthesis
127                case 41:
128                    $outStr .= '\\)';
129                    break;
130
131                // '\' - backslash
132                case 92:
133                    $outStr .= '\\\\';
134                    break;
135
136                default:
137                	// Don't use non-ASCII characters escaping
138                    // if ($nextCode >= 32 && $nextCode <= 126 ) {
139                    //     // Visible ASCII symbol
140                    //     $outStr .= $inStr[$count];
141                    // } else {
142                    //     $outStr .= sprintf('\\%03o', $nextCode);
143                    // }
144                	$outStr .= $inStr[$count];
145
146                    break;
147            }
148        }
149
150        return $outStr;
151    }
152
153
154    /**
155     * Unescape string according to the PDF rules
156     *
157     * @param string $inStr
158     * @return string
159     */
160    public static function unescape($inStr)
161    {
162        $outStr = '';
163
164        for ($count = 0; $count < strlen($inStr); $count++) {
165            if ($inStr[$count] != '\\' || $count == strlen($inStr)-1)  {
166                $outStr .= $inStr[$count];
167            } else { // Escape sequence
168                switch ($inStr{++$count}) {
169                    // '\\n' - line feed (LF)
170                    case 'n':
171                        $outStr .= "\n";
172                        break;
173
174                    // '\\r' - carriage return (CR)
175                    case 'r':
176                        $outStr .= "\r";
177                        break;
178
179                    // '\\t' - horizontal tab (HT)
180                    case 't':
181                        $outStr .= "\t";
182                        break;
183
184                    // '\\b' - backspace (BS)
185                    case 'b':
186                        $outStr .= "\x08";
187                        break;
188
189                    // '\\f' - form feed (FF)
190                    case 'f':
191                        $outStr .= "\x0C";
192                        break;
193
194                    // '\\(' - left paranthesis
195                    case '(':
196                        $outStr .= '(';
197                        break;
198
199                    // '\\)' - right paranthesis
200                    case ')':
201                        $outStr .= ')';
202                        break;
203
204                    // '\\\\' - backslash
205                    case '\\':
206                        $outStr .= '\\';
207                        break;
208
209                    // "\\\n" or "\\\n\r"
210                    case "\n":
211                        // skip new line symbol
212                        if ($inStr[$count+1] == "\r") {
213                            $count++;
214                        }
215                        break;
216
217                    default:
218                        if (ord($inStr[$count]) >= ord('0') &&
219                            ord($inStr[$count]) <= ord('9')) {
220                            // Character in octal representation
221                            // '\\xxx'
222                            $nextCode = '0' . $inStr[$count];
223
224                            if (ord($inStr[$count+1]) >= ord('0') &&
225                                ord($inStr[$count+1]) <= ord('9')) {
226                                $nextCode .= $inStr{++$count};
227
228                                if (ord($inStr[$count+1]) >= ord('0') &&
229                                    ord($inStr[$count+1]) <= ord('9')) {
230                                    $nextCode .= $inStr{++$count};
231                                }
232                            }
233
234                            $outStr .= chr($nextCode);
235                        } else {
236                            $outStr .= $inStr[$count];
237                        }
238                        break;
239                }
240            }
241        }
242
243        return $outStr;
244    }
245
246}