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