PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/baruffaldi/webapp-urltube
PHP | 246 lines | 136 code | 32 blank | 78 comment | 12 complexity | 2e722a50e951337974c2d5db8c59c549 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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. /** Zend_Pdf_Element */
  21. require_once 'Zend/Pdf/Element.php';
  22. /**
  23. * PDF file 'string' element implementation
  24. *
  25. * @category Zend
  26. * @package Zend_Pdf
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Pdf_Element_String extends Zend_Pdf_Element
  31. {
  32. /**
  33. * Object value
  34. *
  35. * @var string
  36. */
  37. public $value;
  38. /**
  39. * Object constructor
  40. *
  41. * @param string $val
  42. */
  43. public function __construct($val)
  44. {
  45. $this->value = (string)$val;
  46. }
  47. /**
  48. * Return type of the element.
  49. *
  50. * @return integer
  51. */
  52. public function getType()
  53. {
  54. return Zend_Pdf_Element::TYPE_STRING;
  55. }
  56. /**
  57. * Return object as string
  58. *
  59. * @param Zend_Pdf_Factory $factory
  60. * @return string
  61. */
  62. public function toString($factory = null)
  63. {
  64. return '(' . self::escape((string)$this->value) . ')';
  65. }
  66. /**
  67. * Escape string according to the PDF rules
  68. *
  69. * @param string $inStr
  70. * @return string
  71. */
  72. public static function escape($inStr)
  73. {
  74. $outStr = '';
  75. $lastNL = 0;
  76. for ($count = 0; $count < strlen($inStr); $count++) {
  77. if (strlen($outStr) - $lastNL > 128) {
  78. $outStr .= "\\\n";
  79. $lastNL = strlen($outStr);
  80. }
  81. $nextCode = ord($inStr[$count]);
  82. switch ($nextCode) {
  83. // "\n" - line feed (LF)
  84. case 10:
  85. $outStr .= '\\n';
  86. break;
  87. // "\r" - carriage return (CR)
  88. case 13:
  89. $outStr .= '\\r';
  90. break;
  91. // "\t" - horizontal tab (HT)
  92. case 9:
  93. $outStr .= '\\t';
  94. break;
  95. // "\b" - backspace (BS)
  96. case 8:
  97. $outStr .= '\\b';
  98. break;
  99. // "\f" - form feed (FF)
  100. case 12:
  101. $outStr .= '\\f';
  102. break;
  103. // '(' - left paranthesis
  104. case 40:
  105. $outStr .= '\\(';
  106. break;
  107. // ')' - right paranthesis
  108. case 41:
  109. $outStr .= '\\)';
  110. break;
  111. // '\' - backslash
  112. case 92:
  113. $outStr .= '\\\\';
  114. break;
  115. default:
  116. // Don't use non-ASCII characters escaping
  117. // if ($nextCode >= 32 && $nextCode <= 126 ) {
  118. // // Visible ASCII symbol
  119. // $outStr .= $inStr[$count];
  120. // } else {
  121. // $outStr .= sprintf('\\%03o', $nextCode);
  122. // }
  123. $outStr .= $inStr[$count];
  124. break;
  125. }
  126. }
  127. return $outStr;
  128. }
  129. /**
  130. * Unescape string according to the PDF rules
  131. *
  132. * @param string $inStr
  133. * @return string
  134. */
  135. public static function unescape($inStr)
  136. {
  137. $outStr = '';
  138. for ($count = 0; $count < strlen($inStr); $count++) {
  139. if ($inStr[$count] != '\\' || $count == strlen($inStr)-1) {
  140. $outStr .= $inStr[$count];
  141. } else { // Escape sequence
  142. switch ($inStr{++$count}) {
  143. // '\\n' - line feed (LF)
  144. case 'n':
  145. $outStr .= "\n";
  146. break;
  147. // '\\r' - carriage return (CR)
  148. case 'r':
  149. $outStr .= "\r";
  150. break;
  151. // '\\t' - horizontal tab (HT)
  152. case 't':
  153. $outStr .= "\t";
  154. break;
  155. // '\\b' - backspace (BS)
  156. case 'b':
  157. $outStr .= "\x08";
  158. break;
  159. // '\\f' - form feed (FF)
  160. case 'f':
  161. $outStr .= "\x0C";
  162. break;
  163. // '\\(' - left paranthesis
  164. case '(':
  165. $outStr .= '(';
  166. break;
  167. // '\\)' - right paranthesis
  168. case ')':
  169. $outStr .= ')';
  170. break;
  171. // '\\\\' - backslash
  172. case '\\':
  173. $outStr .= '\\';
  174. break;
  175. // "\\\n" or "\\\n\r"
  176. case "\n":
  177. // skip new line symbol
  178. if ($inStr[$count+1] == "\r") {
  179. $count++;
  180. }
  181. break;
  182. default:
  183. if (ord($inStr[$count]) >= ord('0') &&
  184. ord($inStr[$count]) <= ord('9')) {
  185. // Character in octal representation
  186. // '\\xxx'
  187. $nextCode = '0' . $inStr[$count];
  188. if (ord($inStr[$count+1]) >= ord('0') &&
  189. ord($inStr[$count+1]) <= ord('9')) {
  190. $nextCode .= $inStr{++$count};
  191. if (ord($inStr[$count+1]) >= ord('0') &&
  192. ord($inStr[$count+1]) <= ord('9')) {
  193. $nextCode .= $inStr{++$count};
  194. }
  195. }
  196. $outStr .= chr($nextCode);
  197. } else {
  198. $outStr .= $inStr[$count];
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. return $outStr;
  205. }
  206. }