String.php 6.7 KB

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