File indexing completed on 2025-01-19 05:21:21

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /** Zend_Pdf_Filter_Interface */
0024 // require_once 'Zend/Pdf/Filter/Interface.php';
0025 
0026 /**
0027  * RunLength stream filter
0028  *
0029  * @package    Zend_Pdf
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Pdf_Filter_RunLength implements Zend_Pdf_Filter_Interface
0034 {
0035     /**
0036      * Encode data
0037      *
0038      * @param string $data
0039      * @param array $params
0040      * @return string
0041      * @throws Zend_Pdf_Exception
0042      */
0043     public static function encode($data, $params = null)
0044     {
0045         $output = '';
0046 
0047         $chainStartOffset = 0;
0048         $offset = 0;
0049 
0050         while ($offset < strlen($data)) {
0051             // Do not encode 2 char chains since they produce 2 char run sequence,
0052             // but it takes more time to decode such output (because of processing additional run)
0053             if (($repeatedCharChainLength = strspn($data, $data[$offset], $offset + 1, 127) + 1)  >  2) {
0054                 if ($chainStartOffset != $offset) {
0055                     // Drop down previouse (non-repeatable chars) run
0056                     $output .= chr($offset - $chainStartOffset - 1)
0057                              . substr($data, $chainStartOffset, $offset - $chainStartOffset);
0058                 }
0059 
0060                 $output .= chr(257 - $repeatedCharChainLength) . $data[$offset];
0061 
0062                 $offset += $repeatedCharChainLength;
0063                 $chainStartOffset = $offset;
0064             } else {
0065                 $offset++;
0066 
0067                 if ($offset - $chainStartOffset == 128) {
0068                     // Maximum run length is reached
0069                     // Drop down non-repeatable chars run
0070                     $output .= "\x7F" . substr($data, $chainStartOffset, 128);
0071 
0072                     $chainStartOffset = $offset;
0073                 }
0074             }
0075         }
0076 
0077         if ($chainStartOffset != $offset) {
0078             // Drop down non-repeatable chars run
0079             $output .= chr($offset - $chainStartOffset - 1) . substr($data, $chainStartOffset, $offset - $chainStartOffset);
0080         }
0081 
0082         $output .= "\x80";
0083 
0084         return $output;
0085     }
0086 
0087     /**
0088      * Decode data
0089      *
0090      * @param string $data
0091      * @param array $params
0092      * @return string
0093      * @throws Zend_Pdf_Exception
0094      */
0095     public static function decode($data, $params = null)
0096     {
0097         $dataLength = strlen($data);
0098         $output = '';
0099         $offset = 0;
0100 
0101         while($offset < $dataLength) {
0102             $length = ord($data[$offset]);
0103 
0104             $offset++;
0105 
0106             if ($length == 128) {
0107                 // EOD byte
0108                 break;
0109             } else if ($length < 128) {
0110                 $length++;
0111 
0112                 $output .= substr($data, $offset, $length);
0113 
0114                 $offset += $length;
0115             } else if ($length > 128) {
0116                 $output .= str_repeat($data[$offset], 257 - $length);
0117 
0118                 $offset++;
0119             }
0120         }
0121 
0122         return $output;
0123     }
0124 }
0125