Name.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 'name' 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_Name 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. * @throws Zend_Pdf_Exception
  44. */
  45. public function __construct($val)
  46. {
  47. settype($val, 'string');
  48. if (strpos($val,"\x00") !== false) {
  49. throw new Zend_Pdf_Exception('Null character is not allowed in PDF Names');
  50. }
  51. $this->value = (string)$val;
  52. }
  53. /**
  54. * Return type of the element.
  55. *
  56. * @return integer
  57. */
  58. public function getType()
  59. {
  60. return Zend_Pdf_Element::TYPE_NAME;
  61. }
  62. /**
  63. * Escape string according to the PDF rules
  64. *
  65. * @param string $inStr
  66. * @return string
  67. */
  68. public static function escape($inStr)
  69. {
  70. $outStr = '';
  71. for ($count = 0; $count < strlen($inStr); $count++) {
  72. $nextCode = ord($inStr[$count]);
  73. switch ($inStr[$count]) {
  74. case '(':
  75. // fall through to next case
  76. case ')':
  77. // fall through to next case
  78. case '<':
  79. // fall through to next case
  80. case '>':
  81. // fall through to next case
  82. case '[':
  83. // fall through to next case
  84. case ']':
  85. // fall through to next case
  86. case '{':
  87. // fall through to next case
  88. case '}':
  89. // fall through to next case
  90. case '/':
  91. // fall through to next case
  92. case '%':
  93. // fall through to next case
  94. case '\\':
  95. // fall through to next case
  96. case '#':
  97. $outStr .= sprintf('#%02X', $nextCode);
  98. break;
  99. default:
  100. if ($nextCode >= 33 && $nextCode <= 126 ) {
  101. // Visible ASCII symbol
  102. $outStr .= $inStr[$count];
  103. } else {
  104. $outStr .= sprintf('#%02X', $nextCode);
  105. }
  106. }
  107. }
  108. return $outStr;
  109. }
  110. /**
  111. * Unescape string according to the PDF rules
  112. *
  113. * @param string $inStr
  114. * @return string
  115. */
  116. public static function unescape($inStr)
  117. {
  118. $outStr = '';
  119. for ($count = 0; $count < strlen($inStr); $count++) {
  120. if ($inStr[$count] != '#' ) {
  121. $outStr .= $inStr[$count];
  122. } else {
  123. // Escape sequence
  124. $outStr .= chr(base_convert(substr($inStr, $count+1, 2), 16, 10 ));
  125. $count +=2;
  126. }
  127. }
  128. return $outStr;
  129. }
  130. /**
  131. * Return object as string
  132. *
  133. * @param Zend_Pdf_Factory $factory
  134. * @return string
  135. */
  136. public function toString($factory = null)
  137. {
  138. return '/' . self::escape((string)$this->value);
  139. }
  140. }