/framework/vendor/zend/Zend/Pdf/Filter/RunLength.php

http://zoop.googlecode.com/ · PHP · 124 lines · 54 code · 21 blank · 49 comment · 15 complexity · 1ba5571d7fe05f4b9e04da8d59187f94 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Pdf_Filter_Interface */
  21. require_once 'Zend/Pdf/Filter/Interface.php';
  22. /**
  23. * RunLength stream filter
  24. *
  25. * @package Zend_Pdf
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Pdf_Filter_RunLength implements Zend_Pdf_Filter_Interface
  30. {
  31. /**
  32. * Encode data
  33. *
  34. * @param string $data
  35. * @param array $params
  36. * @return string
  37. * @throws Zend_Pdf_Exception
  38. */
  39. public static function encode($data, $params = null)
  40. {
  41. $output = '';
  42. $chainStartOffset = 0;
  43. $offset = 0;
  44. while ($offset < strlen($data)) {
  45. // Do not encode 2 char chains since they produce 2 char run sequence,
  46. // but it takes more time to decode such output (because of processing additional run)
  47. if (($repeatedCharChainLength = strspn($data, $data[$offset], $offset + 1, 127) + 1) > 2) {
  48. if ($chainStartOffset != $offset) {
  49. // Drop down previouse (non-repeatable chars) run
  50. $output .= chr($offset - $chainStartOffset - 1)
  51. . substr($data, $chainStartOffset, $offset - $chainStartOffset);
  52. }
  53. $output .= chr(257 - $repeatedCharChainLength) . $data[$offset];
  54. $offset += $repeatedCharChainLength;
  55. $chainStartOffset = $offset;
  56. } else {
  57. $offset++;
  58. if ($offset - $chainStartOffset == 128) {
  59. // Maximum run length is reached
  60. // Drop down non-repeatable chars run
  61. $output .= "\x7F" . substr($data, $chainStartOffset, 128);
  62. $chainStartOffset = $offset;
  63. }
  64. }
  65. }
  66. if ($chainStartOffset != $offset) {
  67. // Drop down non-repeatable chars run
  68. $output .= chr($offset - $chainStartOffset - 1) . substr($data, $chainStartOffset, $offset - $chainStartOffset);
  69. }
  70. $output .= "\x80";
  71. return $output;
  72. }
  73. /**
  74. * Decode data
  75. *
  76. * @param string $data
  77. * @param array $params
  78. * @return string
  79. * @throws Zend_Pdf_Exception
  80. */
  81. public static function decode($data, $params = null)
  82. {
  83. $dataLength = strlen($data);
  84. $output = '';
  85. $offset = 0;
  86. while($offset < $dataLength) {
  87. $length = ord($data[$offset]);
  88. $offset++;
  89. if ($length == 128) {
  90. // EOD byte
  91. break;
  92. } else if ($length < 128) {
  93. $length++;
  94. $output .= substr($data, $offset, $length);
  95. $offset += $length;
  96. } else if ($length > 128) {
  97. $output .= str_repeat($data[$offset], 257 - $length);
  98. $offset++;
  99. }
  100. }
  101. return $output;
  102. }
  103. }