Stream.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /** Internally used classes */
  22. require_once 'Zend/Pdf/Element/Stream.php';
  23. require_once 'Zend/Pdf/Element/Dictionary.php';
  24. require_once 'Zend/Pdf/Element/Numeric.php';
  25. /** Zend_Pdf_Element_Object */
  26. require_once 'Zend/Pdf/Element/Object.php';
  27. /**
  28. * PDF file 'stream object' element implementation
  29. *
  30. * @category Zend
  31. * @package Zend_Pdf
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Pdf_Element_Object_Stream extends Zend_Pdf_Element_Object
  36. {
  37. /**
  38. * StreamObject dictionary
  39. * Required enries:
  40. * Length
  41. *
  42. * @var Zend_Pdf_Element_Dictionary
  43. */
  44. private $_dictionary;
  45. /**
  46. * Flag which signals, that stream is decoded
  47. *
  48. * @var boolean
  49. */
  50. private $_streamDecoded;
  51. /**
  52. * Stored original stream object dictionary.
  53. * Used to decode stream during an access time.
  54. *
  55. * The only properties, which affect decoding, are sored here.
  56. *
  57. * @var array|null
  58. */
  59. private $_originalDictionary = null;
  60. /**
  61. * Object constructor
  62. *
  63. * @param mixed $val
  64. * @param integer $objNum
  65. * @param integer $genNum
  66. * @param Zend_Pdf_ElementFactory $factory
  67. * @param Zend_Pdf_Element_Dictionary|null $dictionary
  68. * @throws Zend_Pdf_Exception
  69. */
  70. public function __construct($val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory, $dictionary = null)
  71. {
  72. parent::__construct(new Zend_Pdf_Element_Stream($val), $objNum, $genNum, $factory);
  73. if ($dictionary === null) {
  74. $this->_dictionary = new Zend_Pdf_Element_Dictionary();
  75. $this->_dictionary->Length = new Zend_Pdf_Element_Numeric(strlen( $val ));
  76. $this->_streamDecoded = true;
  77. } else {
  78. $this->_dictionary = $dictionary;
  79. $this->_streamDecoded = false;
  80. }
  81. }
  82. /**
  83. * Store original dictionary information in $_originalDictionary class member.
  84. * Used to store information and to normalize filters information before defiltering.
  85. *
  86. */
  87. private function _storeOriginalDictionary()
  88. {
  89. $this->_originalDictionary = array();
  90. $this->_originalDictionary['Filter'] = array();
  91. $this->_originalDictionary['DecodeParms'] = array();
  92. if ($this->_dictionary->Filter === null) {
  93. // Do nothing.
  94. } else if ($this->_dictionary->Filter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
  95. foreach ($this->_dictionary->Filter->items as $id => $filter) {
  96. $this->_originalDictionary['Filter'][$id] = $filter->value;
  97. $this->_originalDictionary['DecodeParms'][$id] = array();
  98. if ($this->_dictionary->DecodeParms !== null ) {
  99. if ($this->_dictionary->DecodeParms->items[$id] !== null &&
  100. $this->_dictionary->DecodeParms->items[$id]->value !== null ) {
  101. foreach ($this->_dictionary->DecodeParms->items[$id]->getKeys() as $paramKey) {
  102. $this->_originalDictionary['DecodeParms'][$id][$paramKey] =
  103. $this->_dictionary->DecodeParms->items[$id]->$paramKey->value;
  104. }
  105. }
  106. }
  107. }
  108. } else if ($this->_dictionary->Filter->getType() != Zend_Pdf_Element::TYPE_NULL) {
  109. $this->_originalDictionary['Filter'][0] = $this->_dictionary->Filter->value;
  110. $this->_originalDictionary['DecodeParms'][0] = array();
  111. if ($this->_dictionary->DecodeParms !== null ) {
  112. foreach ($this->_dictionary->DecodeParms->getKeys() as $paramKey) {
  113. $this->_originalDictionary['DecodeParms'][0][$paramKey] =
  114. $this->_dictionary->DecodeParms->$paramKey->value;
  115. }
  116. }
  117. }
  118. if ($this->_dictionary->F !== null) {
  119. $this->_originalDictionary['F'] = $this->_dictionary->F->value;
  120. }
  121. $this->_originalDictionary['FFilter'] = array();
  122. $this->_originalDictionary['FDecodeParms'] = array();
  123. if ($this->_dictionary->FFilter === null) {
  124. // Do nothing.
  125. } else if ($this->_dictionary->FFilter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
  126. foreach ($this->_dictionary->FFilter->items as $id => $filter) {
  127. $this->_originalDictionary['FFilter'][$id] = $filter->value;
  128. $this->_originalDictionary['FDecodeParms'][$id] = array();
  129. if ($this->_dictionary->FDecodeParms !== null ) {
  130. if ($this->_dictionary->FDecodeParms->items[$id] !== null &&
  131. $this->_dictionary->FDecodeParms->items[$id]->value !== null) {
  132. foreach ($this->_dictionary->FDecodeParms->items[$id]->getKeys() as $paramKey) {
  133. $this->_originalDictionary['FDecodeParms'][$id][$paramKey] =
  134. $this->_dictionary->FDecodeParms->items[$id]->items[$paramKey]->value;
  135. }
  136. }
  137. }
  138. }
  139. } else {
  140. $this->_originalDictionary['FFilter'][0] = $this->_dictionary->FFilter->value;
  141. $this->_originalDictionary['FDecodeParms'][0] = array();
  142. if ($this->_dictionary->FDecodeParms !== null ) {
  143. foreach ($this->_dictionary->FDecodeParms->getKeys() as $paramKey) {
  144. $this->_originalDictionary['FDecodeParms'][0][$paramKey] =
  145. $this->_dictionary->FDecodeParms->items[$paramKey]->value;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Decode stream
  152. *
  153. * @throws Zend_Pdf_Exception
  154. */
  155. private function _decodeStream()
  156. {
  157. if ($this->_originalDictionary === null) {
  158. $this->_storeOriginalDictionary();
  159. }
  160. /**
  161. * All applied stream filters must be processed to decode stream.
  162. * If we don't recognize any of applied filetrs an exception should be thrown here
  163. */
  164. if (isset($this->_originalDictionary['F'])) {
  165. /** @todo Check, how external files can be processed. */
  166. require_once 'Zend/Pdf/Exception.php';
  167. throw new Zend_Pdf_Exception('External filters are not supported now.');
  168. }
  169. foreach ($this->_originalDictionary['Filter'] as $id => $filterName ) {
  170. $valueRef = &$this->_value->value->getRef();
  171. $this->_value->value->touch();
  172. switch ($filterName) {
  173. case 'ASCIIHexDecode':
  174. require_once 'Zend/Pdf/Filter/AsciiHex.php';
  175. $valueRef = Zend_Pdf_Filter_AsciiHex::decode($valueRef);
  176. break;
  177. case 'ASCII85Decode':
  178. require_once 'Zend/Pdf/Filter/Ascii85.php';
  179. $valueRef = Zend_Pdf_Filter_Ascii85::decode($valueRef);
  180. break;
  181. case 'FlateDecode':
  182. require_once 'Zend/Pdf/Filter/Compression/Flate.php';
  183. $valueRef = Zend_Pdf_Filter_Compression_Flate::decode($valueRef,
  184. $this->_originalDictionary['DecodeParms'][$id]);
  185. break;
  186. case 'LZWDecode':
  187. require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
  188. $valueRef = Zend_Pdf_Filter_Compression_Lzw::decode($valueRef,
  189. $this->_originalDictionary['DecodeParms'][$id]);
  190. break;
  191. default:
  192. require_once 'Zend/Pdf/Exception.php';
  193. throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
  194. }
  195. }
  196. $this->_streamDecoded = true;
  197. }
  198. /**
  199. * Encode stream
  200. *
  201. * @throws Zend_Pdf_Exception
  202. */
  203. private function _encodeStream()
  204. {
  205. /**
  206. * All applied stream filters must be processed to encode stream.
  207. * If we don't recognize any of applied filetrs an exception should be thrown here
  208. */
  209. if (isset($this->_originalDictionary['F'])) {
  210. /** @todo Check, how external files can be processed. */
  211. require_once 'Zend/Pdf/Exception.php';
  212. throw new Zend_Pdf_Exception('External filters are not supported now.');
  213. }
  214. $filters = array_reverse($this->_originalDictionary['Filter'], true);
  215. foreach ($filters as $id => $filterName ) {
  216. $valueRef = &$this->_value->value->getRef();
  217. $this->_value->value->touch();
  218. switch ($filterName) {
  219. case 'ASCIIHexDecode':
  220. require_once 'Zend/Pdf/Filter/AsciiHex.php';
  221. $valueRef = Zend_Pdf_Filter_AsciiHex::encode($valueRef);
  222. break;
  223. case 'ASCII85Decode':
  224. require_once 'Zend/Pdf/Filter/Ascii85.php';
  225. $valueRef = Zend_Pdf_Filter_Ascii85::encode($valueRef);
  226. break;
  227. case 'FlateDecode':
  228. require_once 'Zend/Pdf/Filter/Compression/Flate.php';
  229. $valueRef = Zend_Pdf_Filter_Compression_Flate::encode($valueRef,
  230. $this->_originalDictionary['DecodeParms'][$id]);
  231. break;
  232. case 'LZWDecode':
  233. require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
  234. $valueRef = Zend_Pdf_Filter_Compression_Lzw::encode($valueRef,
  235. $this->_originalDictionary['DecodeParms'][$id]);
  236. break;
  237. default:
  238. require_once 'Zend/Pdf/Exception.php';
  239. throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
  240. }
  241. }
  242. $this->_streamDecoded = false;
  243. }
  244. /**
  245. * Get handler
  246. *
  247. * @param string $property
  248. * @return mixed
  249. * @throws Zend_Pdf_Exception
  250. */
  251. public function __get($property)
  252. {
  253. if ($property == 'dictionary') {
  254. /**
  255. * If stream is note decoded yet, then store original decoding options (do it only once).
  256. */
  257. if (( !$this->_streamDecoded ) && ($this->_originalDictionary === null)) {
  258. $this->_storeOriginalDictionary();
  259. }
  260. return $this->_dictionary;
  261. }
  262. if ($property == 'value') {
  263. if (!$this->_streamDecoded) {
  264. $this->_decodeStream();
  265. }
  266. return $this->_value->value->getRef();
  267. }
  268. require_once 'Zend/Pdf/Exception.php';
  269. throw new Zend_Pdf_Exception('Unknown stream object property requested.');
  270. }
  271. /**
  272. * Set handler
  273. *
  274. * @param string $property
  275. * @param mixed $value
  276. */
  277. public function __set($property, $value)
  278. {
  279. if ($property == 'value') {
  280. $valueRef = &$this->_value->value->getRef();
  281. $valueRef = $value;
  282. $this->_value->value->touch();
  283. $this->_streamDecoded = true;
  284. return;
  285. }
  286. require_once 'Zend/Pdf/Exception.php';
  287. throw new Zend_Pdf_Exception('Unknown stream object property: \'' . $property . '\'.');
  288. }
  289. /**
  290. * Treat stream data as already encoded
  291. */
  292. public function skipFilters()
  293. {
  294. $this->_streamDecoded = false;
  295. }
  296. /**
  297. * Call handler
  298. *
  299. * @param string $method
  300. * @param array $args
  301. * @return mixed
  302. */
  303. public function __call($method, $args)
  304. {
  305. if (!$this->_streamDecoded) {
  306. $this->_decodeStream();
  307. }
  308. switch (count($args)) {
  309. case 0:
  310. return $this->_value->$method();
  311. case 1:
  312. return $this->_value->$method($args[0]);
  313. default:
  314. require_once 'Zend/Pdf/Exception.php';
  315. throw new Zend_Pdf_Exception('Unsupported number of arguments');
  316. }
  317. }
  318. /**
  319. * Dump object to a string to save within PDF file
  320. *
  321. * $factory parameter defines operation context.
  322. *
  323. * @param Zend_Pdf_ElementFactory $factory
  324. * @return string
  325. */
  326. public function dump(Zend_Pdf_ElementFactory $factory)
  327. {
  328. $shift = $factory->getEnumerationShift($this->_factory);
  329. if ($this->_streamDecoded) {
  330. $this->_storeOriginalDictionary();
  331. $this->_encodeStream();
  332. } else if ($this->_originalDictionary != null) {
  333. $startDictionary = $this->_originalDictionary;
  334. $this->_storeOriginalDictionary();
  335. $newDictionary = $this->_originalDictionary;
  336. if ($startDictionary !== $newDictionary) {
  337. $this->_originalDictionary = $startDictionary;
  338. $this->_decodeStream();
  339. $this->_originalDictionary = $newDictionary;
  340. $this->_encodeStream();
  341. }
  342. }
  343. // Update stream length
  344. $this->dictionary->Length->value = $this->_value->length();
  345. return $this->_objNum + $shift . " " . $this->_genNum . " obj \n"
  346. . $this->dictionary->toString($factory) . "\n"
  347. . $this->_value->toString($factory) . "\n"
  348. . "endobj\n";
  349. }
  350. /**
  351. * Clean up resources, used by object
  352. */
  353. public function cleanUp()
  354. {
  355. $this->_dictionary = null;
  356. $this->_value = null;
  357. }
  358. }