PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Pdf/Filter/RunLength.php

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