Binary.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_String */
  22. require_once 'Zend/Pdf/Element/String.php';
  23. /**
  24. * PDF file 'binary 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_Binary extends Zend_Pdf_Element_String
  32. {
  33. /**
  34. * Object value
  35. *
  36. * @var string
  37. */
  38. public $value;
  39. /**
  40. * Escape string according to the PDF rules
  41. *
  42. * @param string $inStr
  43. * @return string
  44. */
  45. public static function escape($inStr)
  46. {
  47. $outStr = '';
  48. for ($count = 0; $count < strlen($inStr); $count++) {
  49. $outStr .= sprintf('%02X', ord($inStr[$count]));
  50. }
  51. return $outStr;
  52. }
  53. /**
  54. * Unescape string according to the PDF rules
  55. *
  56. * @param string $inStr
  57. * @return string
  58. */
  59. public static function unescape($inStr)
  60. {
  61. $outStr = '';
  62. $nextHexCode = '';
  63. for ($count = 0; $count < strlen($inStr); $count++) {
  64. $nextCharCode = ord($inStr[$count]);
  65. if( ($nextCharCode >= 48 /*'0'*/ &&
  66. $nextCharCode <= 57 /*'9'*/ ) ||
  67. ($nextCharCode >= 97 /*'a'*/ &&
  68. $nextCharCode <= 102 /*'f'*/ ) ||
  69. ($nextCharCode >= 65 /*'A'*/ &&
  70. $nextCharCode <= 70 /*'F'*/ ) ) {
  71. $nextHexCode .= $inStr[$count];
  72. }
  73. if (strlen($nextHexCode) == 2) {
  74. $outStr .= chr(intval($nextHexCode, 16));
  75. $nextHexCode = '';
  76. }
  77. }
  78. if ($nextHexCode != '') {
  79. // We have odd number of digits.
  80. // Final digit is assumed to be '0'
  81. $outStr .= chr(base_convert($nextHexCode . '0', 16, 10));
  82. }
  83. return $outStr;
  84. }
  85. /**
  86. * Return object as string
  87. *
  88. * @param Zend_Pdf_Factory $factory
  89. * @return string
  90. */
  91. public function toString($factory = null)
  92. {
  93. return '<' . self::escape((string)$this->value) . '>';
  94. }
  95. }